From 9a2aa96530e137c15365b1d6041560e76c8a2d85 Mon Sep 17 00:00:00 2001 From: Ammar Bandukwala Date: Wed, 10 Sep 2025 11:13:38 -0500 Subject: [PATCH 1/2] Include commit hash in version output --- README.md | 2 +- build.rs | 23 +++++++++++++++++++++++ src/main.rs | 2 +- tests/smoke_test.rs | 8 ++++---- 4 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 build.rs diff --git a/README.md b/README.md index ad537437..6fde69e9 100644 --- a/README.md +++ b/README.md @@ -256,7 +256,7 @@ OPTIONS: --timeout Timeout for command execution -v, --verbose Increase verbosity (-vvv for max) -h, --help Print help - -V, --version Print version + -V, --version Print version with commit hash RULE FORMAT: Rules are specified with -r/--rule and use the format: diff --git a/build.rs b/build.rs new file mode 100644 index 00000000..944d8c7e --- /dev/null +++ b/build.rs @@ -0,0 +1,23 @@ +use std::process::Command; +use std::env; + +fn main() { + // Retrieve crate version provided by Cargo + let version = env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "unknown".to_string()); + + // Try to get short commit hash + let git_hash = Command::new("git") + .args(["rev-parse", "--short", "HEAD"]) + .output() + .ok() + .and_then(|o| if o.status.success() { + Some(String::from_utf8_lossy(&o.stdout).trim().to_string()) + } else { + None + }) + .unwrap_or_else(|| "unknown".to_string()); + + // Export as environment variables for use in the code and tests + println!("cargo:rustc-env=GIT_HASH={}", git_hash); + println!("cargo:rustc-env=VERSION_WITH_GIT_HASH={} ({})", version, git_hash); +} diff --git a/src/main.rs b/src/main.rs index ced0a4f5..e5cdab07 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use tracing::{debug, info, warn}; #[derive(Parser, Debug)] #[command(name = "httpjail")] -#[command(version, about, long_about = None)] +#[command(version = env!("VERSION_WITH_GIT_HASH"), about, long_about = None)] #[command(about = "Monitor and restrict HTTP/HTTPS requests from processes")] struct Args { /// Rules for filtering requests (can be specified multiple times) diff --git a/tests/smoke_test.rs b/tests/smoke_test.rs index 2a2d5ca9..84c368e9 100644 --- a/tests/smoke_test.rs +++ b/tests/smoke_test.rs @@ -15,10 +15,10 @@ fn test_httpjail_help() { fn test_httpjail_version() { let mut cmd = Command::cargo_bin("httpjail").unwrap(); cmd.arg("--version"); - - cmd.assert() - .success() - .stdout(predicate::str::contains("httpjail")); + let hash = env!("GIT_HASH"); + cmd.assert().success().stdout( + predicate::str::contains("httpjail").and(predicate::str::contains(hash)), + ); } #[test] From 6f8a988099b6c0764d8d63c65a0502f90245ec0a Mon Sep 17 00:00:00 2001 From: Ammar Bandukwala Date: Wed, 10 Sep 2025 11:33:49 -0500 Subject: [PATCH 2/2] Format code and document running cargo fmt --- CLAUDE.md | 4 ++++ build.rs | 17 +++++++++++------ tests/smoke_test.rs | 6 +++--- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 9b52a4f0..b7f0304d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -47,3 +47,7 @@ cargo clippy --all-targets -- -D warnings When the user asks to run clippy and provides the ability to run on both targets, try to run it on both targets. + +## Formatting + +After modifying code, run `cargo fmt` to ensure consistent formatting before committing changes. diff --git a/build.rs b/build.rs index 944d8c7e..dcd7eb46 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,5 @@ -use std::process::Command; use std::env; +use std::process::Command; fn main() { // Retrieve crate version provided by Cargo @@ -10,14 +10,19 @@ fn main() { .args(["rev-parse", "--short", "HEAD"]) .output() .ok() - .and_then(|o| if o.status.success() { - Some(String::from_utf8_lossy(&o.stdout).trim().to_string()) - } else { - None + .and_then(|o| { + if o.status.success() { + Some(String::from_utf8_lossy(&o.stdout).trim().to_string()) + } else { + None + } }) .unwrap_or_else(|| "unknown".to_string()); // Export as environment variables for use in the code and tests println!("cargo:rustc-env=GIT_HASH={}", git_hash); - println!("cargo:rustc-env=VERSION_WITH_GIT_HASH={} ({})", version, git_hash); + println!( + "cargo:rustc-env=VERSION_WITH_GIT_HASH={} ({})", + version, git_hash + ); } diff --git a/tests/smoke_test.rs b/tests/smoke_test.rs index 84c368e9..2c3d8a69 100644 --- a/tests/smoke_test.rs +++ b/tests/smoke_test.rs @@ -16,9 +16,9 @@ fn test_httpjail_version() { let mut cmd = Command::cargo_bin("httpjail").unwrap(); cmd.arg("--version"); let hash = env!("GIT_HASH"); - cmd.assert().success().stdout( - predicate::str::contains("httpjail").and(predicate::str::contains(hash)), - ); + cmd.assert() + .success() + .stdout(predicate::str::contains("httpjail").and(predicate::str::contains(hash))); } #[test]