Read current git branch in a way that is compatible with older git

This commit is contained in:
Mislav Marohnić 2020-01-10 14:08:54 +01:00
parent f0ab533bbc
commit d8474d5990

View file

@ -18,11 +18,13 @@ func VerifyRef(ref string) bool {
return err == nil
}
// CurrentBranch reads the checked-out branch for the git repository
func CurrentBranch() (string, error) {
branchCmd := exec.Command("git", "branch", "--show-current")
// we avoid using `git branch --show-current` for compatibility with git < 2.22
branchCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
output, err := utils.PrepareCmd(branchCmd).Output()
branchName := firstLine(output)
if err == nil && branchName == "" {
if err == nil && branchName == "HEAD" {
return "", errors.New("git: not on any branch")
}
return branchName, err