Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing the git short hash via environment variables #21473

Open
darix opened this issue Oct 10, 2024 · 3 comments · May be fixed by #21490
Open

Allow passing the git short hash via environment variables #21473

darix opened this issue Oct 10, 2024 · 3 comments · May be fixed by #21490
Labels
domain: dev Anything related to Vector's development environment type: enhancement A value-adding code change that enhances its existing functionality.

Comments

@darix
Copy link

darix commented Oct 10, 2024

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 darix added the type: bug A code related bug. label Oct 10, 2024
@jszwedko jszwedko added type: enhancement A value-adding code change that enhances its existing functionality. domain: dev Anything related to Vector's development environment and removed type: bug A code related bug. labels Oct 10, 2024
@jszwedko
Copy link
Member

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

@darix
Copy link
Author

darix commented Oct 11, 2024

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
Copy link
Member

Something like that looks reasonable to me 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain: dev Anything related to Vector's development environment type: enhancement A value-adding code change that enhances its existing functionality.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants