consolidate into one symbolic-ref call
This commit is contained in:
parent
dd877b6a34
commit
0af232444c
1 changed files with 11 additions and 15 deletions
26
git/git.go
26
git/git.go
|
|
@ -21,27 +21,23 @@ func VerifyRef(ref string) bool {
|
|||
|
||||
// CurrentBranch reads the checked-out branch for the git repository
|
||||
func CurrentBranch() (string, error) {
|
||||
noBranchError := errors.New("git: not on any branch")
|
||||
|
||||
// we avoid using `git branch --show-current` for compatibility with git < 2.22
|
||||
output, err := run.PrepareCmd(GitCommand("rev-parse", "--abbrev-ref", "HEAD")).Output()
|
||||
branchName := firstLine(output)
|
||||
refCmd := GitCommand("symbolic-ref", "--quiet", "--short", "HEAD")
|
||||
|
||||
output, err := run.PrepareCmd(refCmd).Output()
|
||||
if err == nil {
|
||||
if branchName == "HEAD" {
|
||||
return "", noBranchError
|
||||
} else {
|
||||
return branchName, nil
|
||||
}
|
||||
// Found the branch name
|
||||
return firstLine(output), nil
|
||||
}
|
||||
|
||||
// Fall back to symbolic-ref in case we're in a repository with no commits
|
||||
output, err = run.PrepareCmd(GitCommand("symbolic-ref", "--short", "HEAD")).Output()
|
||||
if err != nil {
|
||||
return "", noBranchError
|
||||
ce := err.(*run.CmdError)
|
||||
if ce.Stderr.Len() == 0 {
|
||||
// Detached head
|
||||
return "", errors.New("git: not on any branch")
|
||||
}
|
||||
branchName = firstLine(output)
|
||||
|
||||
return branchName, nil
|
||||
// Unknown error
|
||||
return "", err
|
||||
}
|
||||
|
||||
func listRemotes() ([]string, error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue