@@ -51,26 +51,28 @@ func pkgVersionFromGit(gitdir string) (string, error) {
51
51
// This results in an output like 4.10.2-232-g9f107c8
52
52
cmd = exec .Command ("git" , "describe" , "--long" , "--tags" )
53
53
cmd .Dir = gitdir
54
+ lastCommitSha := ""
54
55
describeBytes , err := cmd .Output ()
55
56
if err != nil {
56
- // In case there are no tags at all, we need to pass --all, but we
57
- // cannot use --all unconditionally because then git will describe
58
- // e.g. heads/master instead of tags.
59
- cmd = exec .Command ("git" , "describe" , "--long" , "--all" )
57
+ // In case there are no tags at all, we just use the sha of the current commit
58
+ cmd = exec .Command ("git" , "rev-parse" , "--short" , "HEAD" )
60
59
cmd .Dir = gitdir
61
60
cmd .Stderr = os .Stderr
62
- describeBytes , err = cmd .Output ()
61
+ revparseBytes , err : = cmd .Output ()
63
62
if err != nil {
64
63
return "" , err
65
64
}
66
- }
67
- submatches := describeRegexp .FindSubmatch (describeBytes )
68
- if submatches == nil {
69
- return "" , fmt .Errorf ("git describe output %q does not match expected format" , string (describeBytes ))
65
+ lastCommitSha = strings .TrimSpace (string (revparseBytes ))
66
+ } else {
67
+ submatches := describeRegexp .FindSubmatch (describeBytes )
68
+ if submatches == nil {
69
+ return "" , fmt .Errorf ("git describe output %q does not match expected format" , string (describeBytes ))
70
+ }
71
+ lastCommitSha = string (submatches [1 ])
70
72
}
71
73
version := fmt .Sprintf ("%sgit%s.%s" ,
72
74
lastTag ,
73
75
time .Unix (lastCommitUnix , 0 ).UTC ().Format ("20060102" ),
74
- string ( submatches [ 1 ]) )
76
+ lastCommitSha )
75
77
return version , nil
76
78
}
0 commit comments