Skip to content

Commit

Permalink
Implement a fallback.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhoboat committed Feb 19, 2021
1 parent d28b779 commit 4f933d2
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions modules/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,38 @@ func GetCurrentBranchName(t testing.TestingT) string {

// GetCurrentBranchNameE retrieves the current branch name or
// empty string in case of detached state.
// Uses branch --show-current, which was introduced in git v2.22.
// Falls back to rev-parse for users of the older version, like Ubuntu 18.04.
func GetCurrentBranchNameE(t testing.TestingT) (string, error) {
cmd := exec.Command("git", "branch", "--show-current")
bytes, err := cmd.Output()
if err != nil {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
bytes, err := cmd.Output()
if err != nil {
return "", err
}
name := strings.TrimSpace(string(bytes))
if name == "HEAD" {
return "", nil
}

return name, nil
}

name := strings.TrimSpace(string(bytes))
if name == "HEAD" {
return "", nil
}

return name, nil
}

// GetCurrentBranchNameOldE retrieves the current branch name or
// empty string in case of detached state.
func GetCurrentBranchNameOldE(t testing.TestingT) (string, error) {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
bytes, err := cmd.Output()
if err != nil {
return "", err
}
Expand Down

0 comments on commit 4f933d2

Please sign in to comment.