1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::process::Command;
fn main() {
// Re-run if HEAD moves (new commit, branch switch). Best-effort: missing
// .git (e.g. crates.io tarball build) just leaves LLMENV_GIT_HASH unset
// and the version string falls back to the bare Cargo.toml version.
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/refs/heads");
let hash = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.ok()
.filter(|o| o.status.success())
.and_then(|o| String::from_utf8(o.stdout).ok())
.map(|s| s.trim().to_owned())
.unwrap_or_default();
let dirty = Command::new("git")
.args(["status", "--porcelain"])
.output()
.ok()
.filter(|o| o.status.success())
.map(|o| !o.stdout.is_empty())
.unwrap_or(false);
let pkg_version = std::env::var("CARGO_PKG_VERSION").unwrap_or_default();
// Human-readable, surfaced via `llmenv --version`. Includes -dirty so
// dev builds advertise themselves clearly.
let version = if hash.is_empty() {
pkg_version.clone()
} else if dirty {
format!("{pkg_version} ({hash}-dirty)")
} else {
format!("{pkg_version} ({hash})")
};
// Filesystem-safe prefix for cache buckets. Omits -dirty so all dev
// iterations at a given HEAD share a bucket (avoids cache fragmentation
// while editing). Format: `{pkg_version}-{hash}` or `{pkg_version}` when
// no .git is present (e.g. crates.io tarball builds).
let version_tag = if hash.is_empty() {
pkg_version.clone()
} else {
format!("{pkg_version}-{hash}")
};
println!("cargo:rustc-env=LLMENV_VERSION={version}");
println!("cargo:rustc-env=LLMENV_VERSION_TAG={version_tag}");
// Components surfaced separately so the cache layer can compose
// version-fidelity folder names (#196) without re-parsing the combined
// tag — parsing `{pkg}-{hash}` is ambiguous when `pkg` is a semver
// prerelease that itself contains dashes (e.g. `1.2.3-rc.1`).
println!("cargo:rustc-env=LLMENV_PKG_VERSION={pkg_version}");
println!("cargo:rustc-env=LLMENV_GIT_HASH={hash}");
}