Read current git branch in a way that is compatible with older git
This commit is contained in:
parent
f0ab533bbc
commit
d8474d5990
1 changed files with 4 additions and 2 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue