Skip to content

Commit 27b4c58

Browse files
authored
Add --version flag using git tags (#124)
Use build.rs to capture the version from `git describe --tags` at compile time, falling back to CARGO_PKG_VERSION if git is unavailable. This enables `agentfs --version` to show the git tag, commit count, and hash.
2 parents 874d953 + 971c755 commit 27b4c58

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

cli/build.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::process::Command;
2+
13
fn main() {
24
// Sandbox uses libunwind-ptrace which depends on liblzma and gcc_s.
35
// Only available on Linux x86_64.
@@ -15,4 +17,25 @@ fn main() {
1517
println!("cargo:rustc-link-search=/usr/local/lib");
1618
println!("cargo:rustc-link-search=/Library/Frameworks/macFUSE.framework/Versions/A");
1719
}
20+
21+
// Capture git version from tags for --version flag
22+
// Rerun if git HEAD changes (new commits or tags)
23+
println!("cargo:rerun-if-changed=../.git/HEAD");
24+
println!("cargo:rerun-if-changed=../.git/refs/tags");
25+
26+
let version = Command::new("git")
27+
.args(["describe", "--tags", "--always", "--dirty"])
28+
.output()
29+
.ok()
30+
.and_then(|output| {
31+
if output.status.success() {
32+
String::from_utf8(output.stdout).ok()
33+
} else {
34+
None
35+
}
36+
})
37+
.map(|v| v.trim().to_string())
38+
.unwrap_or_else(|| env!("CARGO_PKG_VERSION").to_string());
39+
40+
println!("cargo:rustc-env=AGENTFS_VERSION={}", version);
1841
}

cli/src/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::path::{Path, PathBuf};
88

99
#[derive(Parser, Debug)]
1010
#[command(name = "agentfs")]
11+
#[command(version = env!("AGENTFS_VERSION"))]
1112
#[command(about = "The filesystem for agents", long_about = None)]
1213
pub struct Args {
1314
#[command(subcommand)]

0 commit comments

Comments
 (0)