vectordotdev / vector

A high-performance observability data pipeline.
https://vector.dev
Mozilla Public License 2.0
17.69k stars 1.57k forks source link

Allow passing the git short hash via environment variables #21473

Open darix opened 1 week ago

darix commented 1 week ago

A note for the community

Problem

Packagers not building from a git working copy can not finish the build as build.rs can not successfully run git.

other rust tools handle this by first checking the for environment variables before calling git. (e.g. the influxdb rust version)

would it be possible to have similar code for vector?

Configuration

No response

Version

0.41.1

Debug Output

No response

Example Data

No response

Additional Context

No response

References

No response

jszwedko commented 1 week ago

Hi @darix ! Sure, we'd be happy to see a PR for this. You'd want to update build.rs.

darix commented 1 week ago
diff --git a/build.rs b/build.rs
index 5419a99b..f860c401 100644
--- a/build.rs
+++ b/build.rs
@@ -93,17 +93,20 @@ impl BuildConstants {
     }
 }

-fn git_short_hash() -> std::io::Result<String> {
-    let output_result = Command::new("git")
-        .args(["rev-parse", "--short", "HEAD"])
-        .output();
-
-    output_result.map(|output| {
-        let mut hash = String::from_utf8(output.stdout).expect("valid UTF-8");
-        hash.retain(|c| !c.is_ascii_whitespace());
+fn git_short_hash() -> String {
+    let out = match std::env::var("GIT_HASH_SHORT") {
+        Ok(v) => v,
+        Err(_) => {
+            let output = Command::new("git")
+                .args(["rev-parse", "--short", "HEAD"])
+                .output()
+                .expect("failed to execute git rev-parse to read the current git hash");
+            String::from_utf8(output.stdout).expect("non-utf8 found in git hash")
+        }
+    };

-        hash
-    })
+    assert!(!out.is_empty(), "attempting to embed empty git hash");
+    out
 }

 fn main() {
@@ -200,17 +203,7 @@ fn main() {
     // In CI build workflows this will have been pre-configured by running the command
     // "git config --global --add safe.directory /git/vectordotdev/vector", from the vdev package
     // subcommands.
-    let git_short_hash = git_short_hash()
-        .map_err(|e| {
-            #[allow(clippy::print_stderr)]
-            {
-                eprintln!(
-                    "Unable to determine git short hash from rev-parse command: {}",
-                    e
-                );
-            }
-        })
-        .expect("git hash detection failed");
+    let git_short_hash = git_short_hash();

     // Gather up the constants and write them out to our build constants file.
     let mut constants = BuildConstants::new();

would this be acceptable?

jszwedko commented 1 week ago

Something like that looks reasonable to me 👍