diff --git a/modules/git/git.go b/modules/git/git.go index df0b97e94..d5c444a7c 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -20,9 +20,38 @@ func GetCurrentBranchName(t testing.TestingT) string { // GetCurrentBranchNameE retrieves the current branch name or // empty string in case of detached state. +// Uses branch --show-current, which was introduced in git v2.22. +// Falls back to rev-parse for users of the older version, like Ubuntu 18.04. func GetCurrentBranchNameE(t testing.TestingT) (string, error) { cmd := exec.Command("git", "branch", "--show-current") bytes, err := cmd.Output() + if err != nil { + cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") + bytes, err := cmd.Output() + if err != nil { + return "", err + } + name := strings.TrimSpace(string(bytes)) + if name == "HEAD" { + return "", nil + } + + return name, nil + } + + name := strings.TrimSpace(string(bytes)) + if name == "HEAD" { + return "", nil + } + + return name, nil +} + +// GetCurrentBranchNameOldE retrieves the current branch name or +// empty string in case of detached state. +func GetCurrentBranchNameOldE(t testing.TestingT) (string, error) { + cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD") + bytes, err := cmd.Output() if err != nil { return "", err }