Skip to content

Commit df6e25a

Browse files
authored
Embed GIT_COMMIT_HASH and output it from version command. (#264)
1 parent febabf4 commit df6e25a

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/yb-voyager/src/utils/version.go export-subst

yb-voyager/cmd/version.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ var versionCmd = &cobra.Command{
1414

1515
Run: func(cmd *cobra.Command, args []string) {
1616
fmt.Printf("VERSION=%s\n", utils.YB_VOYAGER_VERSION)
17-
17+
h := utils.GitCommitHash()
18+
if h != "" {
19+
fmt.Printf("GIT_COMMIT_HASH=%s\n", h)
20+
}
1821
info, ok := debug.ReadBuildInfo()
1922
if !ok {
2023
return
2124
}
2225
for _, setting := range info.Settings {
2326
if setting.Key == "vcs.revision" {
24-
fmt.Printf("GIT_HASH=%s\n", setting.Value)
27+
fmt.Printf("GIT_COMMIT_HASH=%s\n", setting.Value)
2528
}
2629
if setting.Key == "vcs.time" {
2730
fmt.Printf("LAST_COMMIT_DATE=%s\n", setting.Value)

yb-voyager/src/utils/version.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,16 @@ package utils
22

33
const (
44
// This constant must be updated on every release.
5-
YB_VOYAGER_VERSION = "v1.0.0-beta.0"
5+
YB_VOYAGER_VERSION = "main"
6+
7+
// @Refer: https://icinga.com/blog/2022/05/25/embedding-git-commit-information-in-go-binaries/
8+
GIT_COMMIT_HASH = "$Format:%H$"
69
)
10+
11+
func GitCommitHash() string {
12+
if len(GIT_COMMIT_HASH) == 40 {
13+
// Substitution has happened.
14+
return GIT_COMMIT_HASH
15+
}
16+
return ""
17+
}

0 commit comments

Comments
 (0)