Skip to content

Commit

Permalink
DRY the logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rhoboat committed Mar 18, 2021
1 parent 4503be4 commit d2a5f73
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions modules/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,7 @@ 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
return GetCurrentBranchNameOldE(t)
}

name := strings.TrimSpace(string(bytes))
Expand All @@ -48,7 +38,8 @@ func GetCurrentBranchNameE(t testing.TestingT) (string, error) {
}

// GetCurrentBranchNameOldE retrieves the current branch name or
// empty string in case of detached state.
// empty string in case of detached state. This uses the older pattern
// of `git rev-parse` rather than `git branch --show-current`.
func GetCurrentBranchNameOldE(t testing.TestingT) (string, error) {
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
bytes, err := cmd.Output()
Expand Down

0 comments on commit d2a5f73

Please sign in to comment.