From d8474d5990dbcb9c748ee030ebafee2b49b8cf13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 10 Jan 2020 14:08:54 +0100 Subject: [PATCH] Read current git branch in a way that is compatible with older git --- git/git.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/git/git.go b/git/git.go index 91664b118..fd3a1d963 100644 --- a/git/git.go +++ b/git/git.go @@ -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