Skip to content

Commit f3478f2

Browse files
soarkaralabe
authored andcommitted
internal/build: fix commit extraction for detached head repo (#18315)
* Fix commit extraction * Comments for commit extraction Requested in #18315
1 parent f891fd9 commit f3478f2

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

internal/build/env.go

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"flag"
2121
"fmt"
2222
"os"
23+
"regexp"
2324
"strconv"
2425
"strings"
2526
"time"
@@ -99,6 +100,13 @@ func LocalEnv() Environment {
99100
if fields := strings.Fields(head); len(fields) == 2 {
100101
head = fields[1]
101102
} else {
103+
// In this case we are in "detached head" state
104+
// see: https://git-scm.com/docs/git-checkout#_detached_head
105+
// Additional check required to verify, that file contains commit hash
106+
commitRe, _ := regexp.Compile("^([0-9a-f]{40})$")
107+
if commit := commitRe.FindString(head); commit != "" && env.Commit == "" {
108+
env.Commit = commit
109+
}
102110
return env
103111
}
104112
if env.Commit == "" {

0 commit comments

Comments
 (0)