Skip to content

Commit

Permalink
get the CurrentBranch from git instead of parsing text
Browse files Browse the repository at this point in the history
  • Loading branch information
1ace committed Feb 20, 2019
1 parent b2da778 commit 5a30108
Showing 1 changed file with 4 additions and 19 deletions.
23 changes: 4 additions & 19 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,14 @@ func Log(sha1, sha2 string) (string, error) {
return string(outputs), nil
}

// CurrentBranch returns the currently checked out branch and strips away all
// but the branchname itself.
// CurrentBranch returns the currently checked out branch
func CurrentBranch() (string, error) {
cmd := New("branch")
cmd.Stdout = nil
gBranches, err := cmd.Output()
cmd := New("rev-parse", "--abbrev-ref", "HEAD")
branch, err := cmd.Output()
if err != nil {
return "", err
}
branches := strings.Split(string(gBranches), "\n")
var branch string
for _, b := range branches {
if strings.HasPrefix(b, "* ") {
branch = b
break
}
}
if branch == "" {
return "", errors.New("current branch could not be determined")
}
branch = strings.TrimPrefix(branch, "* ")
branch = strings.TrimSpace(branch)
return branch, nil
return string(branch), nil
}

// PathWithNameSpace returns the owner/repository for the current repo
Expand Down

0 comments on commit 5a30108

Please sign in to comment.