vector icon indicating copy to clipboard operation
vector copied to clipboard

Allow passing the git short hash via environment variables

Open darix opened this issue 1 year ago • 3 comments

A note for the community

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

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

darix avatar Oct 10 '24 16:10 darix

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

jszwedko avatar Oct 10 '24 17:10 jszwedko

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?

darix avatar Oct 11 '24 16:10 darix

Something like that looks reasonable to me 👍

jszwedko avatar Oct 11 '24 17:10 jszwedko