1414package main
1515
1616import (
17- "io/ioutil "
17+ "fmt "
1818 "log"
1919 "os"
2020 "os/exec"
@@ -72,17 +72,44 @@ func gitHash() string {
7272 cmd := exec .Command ("git" , "rev-parse" , "--short=8" , "HEAD" )
7373 hash , err := cmd .Output ()
7474 if err != nil {
75+ log .Printf ("failed to execute git rev-parse --short=8 HEAD, err: %v" , err )
7576 return "UNKNOWN"
7677 }
77- return strings .TrimSpace (string (hash ))
78+ hashStr := strings .TrimSpace (string (hash ))
79+ log .Printf ("Successfully retrieved short hash value from git rev-parse --short=8 HEAD, hashStr: %s" ,
80+ hashStr )
81+ return hashStr
82+ }
83+
84+ func releaseCommitGitHash () (string , error ) {
85+ fullHash , err := os .ReadFile (filepath .Join (".." , ".." , "RELEASE_COMMIT" ))
86+ if err != nil {
87+ return "" , fmt .Errorf ("unable to read RELEASE_COMMIT file, err: %v" , err )
88+ }
89+ fullHashStr := strings .TrimSpace (string (fullHash ))
90+ if fullHashStr == "" || len (fullHashStr ) < 8 {
91+ log .Fatalf ("Invalid hash value obtained from RELEASE_COMMIT file (empty or length <8), fullHashStr: %s" ,
92+ fullHashStr )
93+ }
94+ log .Printf ("Successfully retrieved full hash value from RELEASE_COMMIT file, fullHashStr: %s" , fullHashStr )
95+ // return short hash (first 8 characters) instead of full hash
96+ return fullHashStr [0 :8 ], nil
97+ }
98+
99+ func selectGitHash () string {
100+ hash , err := releaseCommitGitHash ()
101+ if err != nil {
102+ return gitHash ()
103+ }
104+ return hash
78105}
79106
80107// version-gen is a simple program that generates the agent's version file,
81108// containing information about the agent's version, commit hash, and repository
82109// cleanliness.
83110func main () {
84111
85- versionStr , _ := ioutil .ReadFile (filepath .Join (".." , ".." , "VERSION" ))
112+ versionStr , _ := os .ReadFile (filepath .Join (".." , ".." , "VERSION" ))
86113
87114 // default values
88115 info := versionInfo {
@@ -101,7 +128,7 @@ func main() {
101128 // have a commit hash so that it does not churn with every commit. This
102129 // env var should not be set when building, and go generate should be
103130 // run before any build, such that the commithash will be set correctly.
104- info .Hash = gitHash ()
131+ info .Hash = selectGitHash ()
105132 }
106133
107134 outFile , err := os .Create ("version.go" )
0 commit comments