Skip to content

Commit d8474d5

Browse files
committed
Read current git branch in a way that is compatible with older git
1 parent f0ab533 commit d8474d5

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

git/git.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ func VerifyRef(ref string) bool {
1818
return err == nil
1919
}
2020

21+
// CurrentBranch reads the checked-out branch for the git repository
2122
func CurrentBranch() (string, error) {
22-
branchCmd := exec.Command("git", "branch", "--show-current")
23+
// we avoid using `git branch --show-current` for compatibility with git < 2.22
24+
branchCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
2325
output, err := utils.PrepareCmd(branchCmd).Output()
2426
branchName := firstLine(output)
25-
if err == nil && branchName == "" {
27+
if err == nil && branchName == "HEAD" {
2628
return "", errors.New("git: not on any branch")
2729
}
2830
return branchName, err

0 commit comments

Comments
 (0)