Skip to content

Commit 2912285

Browse files
elboulangerostapelberg
authored andcommitted
Allow passing a commit to -git_revision (#95)
Signed-off-by: Arnaud Rebillout <[email protected]>
1 parent db00c37 commit 2912285

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

version.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,28 @@ func pkgVersionFromGit(gitdir string) (string, error) {
5151
// This results in an output like 4.10.2-232-g9f107c8
5252
cmd = exec.Command("git", "describe", "--long", "--tags")
5353
cmd.Dir = gitdir
54+
lastCommitSha := ""
5455
describeBytes, err := cmd.Output()
5556
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")
6059
cmd.Dir = gitdir
6160
cmd.Stderr = os.Stderr
62-
describeBytes, err = cmd.Output()
61+
revparseBytes, err := cmd.Output()
6362
if err != nil {
6463
return "", err
6564
}
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])
7072
}
7173
version := fmt.Sprintf("%sgit%s.%s",
7274
lastTag,
7375
time.Unix(lastCommitUnix, 0).UTC().Format("20060102"),
74-
string(submatches[1]))
76+
lastCommitSha)
7577
return version, nil
7678
}

0 commit comments

Comments
 (0)