Merge pull request #213 from github/git-current-branch-compat

Read current git branch in a way that is compatible with older git
This commit is contained in:
Nate Smith 2020-01-10 16:06:27 -06:00 committed by GitHub
commit 83c647366b

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