influxdb icon indicating copy to clipboard operation
influxdb copied to clipboard

inconsistencies build.rs and git hash handling

Open darix opened this issue 1 year ago • 7 comments

  1. the main function checks for different environment variables than the get_git_hash* functions
  2. get_git_hash checks for the wrong variable compared to main
  3. get_git_hash_short doesnt even check for the environment variable before trying to run git.
diff --git a/influxdb3_process/build.rs b/influxdb3_process/build.rs
index e4b4f992ff..33343f989b 100644
--- a/influxdb3_process/build.rs
+++ b/influxdb3_process/build.rs
@@ -15,7 +15,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
 }
 
 fn get_git_hash() -> String {
-    let out = match std::env::var("VERSION_HASH") {
+    let out = match std::env::var("GIT_HASH") {
         Ok(v) => v,
         Err(_) => {
             let output = Command::new("git")
@@ -32,9 +32,17 @@ fn get_git_hash() -> String {
 }
 
 fn get_git_hash_short() -> String {
-    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")
+    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")
+        }
+    };
+
+    assert!(!out.is_empty(), "attempting to embed empty git hash");
+    out
 }

darix avatar Aug 25 '24 03:08 darix

Hey @darix - thanks for raising the issue. It appears that VERSION_HASH is an artifact of an older iteration of our build process, and that it is no longer needed.

I think the solution here would be to remove reference to VERSION_HASH, and rely strictly on the git commands to produce values for GIT_HASH and GIT_HASH_SHORT; there is no need to check for their existence first using the match statement. I can open a PR.

hiltontj avatar Aug 26 '24 15:08 hiltontj

please do not rely on git command to get that information packagers usually do NOT work from git working copies so that would just fail. but we can provide the environment variables during the build with the information.

darix avatar Aug 26 '24 15:08 darix

FWIW: i am using the patch mentioned in the original comment with my test rpm package.

darix avatar Aug 26 '24 15:08 darix

@darix - is it possible to link to the source for your package for additional context?

hiltontj avatar Aug 26 '24 15:08 hiltontj

https://build.opensuse.org/package/show/home:darix:apps/influxdb3

also while we are talking about sources ... where can one find the source for influxctl that is referenced in the tutorials? all i can find is the binary downloads.

darix avatar Aug 26 '24 16:08 darix

https://build.opensuse.org/package/show/home:darix:apps/influxdb3

Thanks for sharing.

where can one find the source for influxctl

influxctl is not currently open-source.

hiltontj avatar Aug 26 '24 16:08 hiltontj

updated patch:

diff --git a/influxdb3_process/build.rs b/influxdb3_process/build.rs
index 518c04878a..33343f989b 100644
--- a/influxdb3_process/build.rs
+++ b/influxdb3_process/build.rs
@@ -15,23 +15,34 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
 }
 
 fn get_git_hash() -> String {
-    let git_hash = {
-        let output = Command::new("git")
-            .args(["describe", "--always", "--dirty", "--abbrev=64"])
-            .output()
-            .expect("failed to execute git rev-parse to read the current git hash");
+    let out = match std::env::var("GIT_HASH") {
+        Ok(v) => v,
+        Err(_) => {
+            let output = Command::new("git")
+                .args(["describe", "--always", "--dirty", "--abbrev=64"])
+                .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")
+            String::from_utf8(output.stdout).expect("non-utf8 found in git hash")
+        }
     };
 
-    assert!(!git_hash.is_empty(), "attempting to embed empty git hash");
-    git_hash
+    assert!(!out.is_empty(), "attempting to embed empty git hash");
+    out
 }
 
 fn get_git_hash_short() -> String {
-    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")
+    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")
+        }
+    };
+
+    assert!(!out.is_empty(), "attempting to embed empty git hash");
+    out
 }

darix avatar Aug 26 '24 17:08 darix

Adjusted the patch against v3.0.3:

diff '--color=auto' -Naur influxdb-3.0.3~/influxdb3_process/build.rs influxdb-3.0.3/influxdb3_process/build.rs
--- influxdb-3.0.3~/influxdb3_process/build.rs	2025-05-15 20:15:39.000000000 +0200
+++ influxdb-3.0.3/influxdb3_process/build.rs	2025-07-16 21:54:40.830761223 +0200
@@ -15,29 +15,40 @@
 }
 
 fn get_git_hash() -> String {
-    let git_hash = {
-        let output = Command::new("git")
-            // We used `git describe`, but when you tag a build the commit hash goes missing when
-            // using describe. So, switching it to use `rev-parse` which is consistent with the
-            // `get_git_hash_short` below as well.
-            //
-            // And we already have cargo version appearing as a separate string so using `git
-            // describe` looks redundant on tagged release builds
-            .args(["rev-parse", "HEAD"])
-            .output()
-            .expect("failed to execute git rev-parse to read the current git hash");
+    let out = match std::env::var("GIT_HASH") {
+        Ok(v) => v,
+        Err(_) => {
+            let output = Command::new("git")
+                // We used `git describe`, but when you tag a build the commit hash goes missing when
+                // using describe. So, switching it to use `rev-parse` which is consistent with the
+                // `get_git_hash_short` below as well.
+                //
+                // And we already have cargo version appearing as a separate string so using `git
+                // describe` looks redundant on tagged release builds
+                .args(["rev-parse", "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")
+            String::from_utf8(output.stdout).expect("non-utf8 found in git hash")
+        }
     };
 
-    assert!(!git_hash.is_empty(), "attempting to embed empty git hash");
-    git_hash
+    assert!(!out.is_empty(), "attempting to embed empty git hash");
+    out
 }
 
 fn get_git_hash_short() -> String {
-    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")
+    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")
+        }
+    };
+
+    assert!(!out.is_empty(), "attempting to embed empty git hash");
+    out
 }

Can this be incorporated, or another approach exists?

pfactum avatar Jul 16 '25 20:07 pfactum

@pfactum - this has sat idle for some time. I think the best thing to do would be to open a PR with the requested changes so that we can have it reviewed. I believe the change is justified.

hiltontj avatar Jul 17 '25 13:07 hiltontj