diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2018-11-14 19:08:42 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2018-11-14 19:08:42 +1100 |
commit | a5f483fae97fa16b9291761e74acdc35a669df76 (patch) | |
tree | be21b9607051a5ce5bcbd95670c77f8a41b1a73f /pkg | |
parent | 775d910bdc0fb01a24d0a9b8b3e985a80d8e560e (diff) |
refactor obtaining current branch name
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/commands/git.go | 14 | ||||
-rw-r--r-- | pkg/git/branch_list_builder.go | 10 |
2 files changed, 9 insertions, 15 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index bb4f28b75..a92ce3435 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -208,11 +208,6 @@ func includesInt(list []int, a int) bool { return false } -// GetBranchName branch name -func (c *GitCommand) GetBranchName() (string, error) { - return c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD") -} - // ResetHard does the equivalent of `git reset --hard HEAD` func (c *GitCommand) ResetHard() error { return c.Worktree.Reset(&gogit.ResetOptions{Mode: gogit.HardReset}) @@ -268,11 +263,14 @@ func (c *GitCommand) NewBranch(name string) error { } func (c *GitCommand) CurrentBranchName() (string, error) { - output, err := c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD") + branchName, err := c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD") if err != nil { - return "", err + branchName, err = c.OSCommand.RunCommandWithOutput("git rev-parse --short HEAD") + if err != nil { + return "", err + } } - return utils.TrimTrailingNewline(output), nil + return utils.TrimTrailingNewline(branchName), nil } // DeleteBranch delete branch diff --git a/pkg/git/branch_list_builder.go b/pkg/git/branch_list_builder.go index 151e5b0b4..821f7b9b1 100644 --- a/pkg/git/branch_list_builder.go +++ b/pkg/git/branch_list_builder.go @@ -35,15 +35,11 @@ func NewBranchListBuilder(log *logrus.Entry, gitCommand *commands.GitCommand) (* } func (b *BranchListBuilder) obtainCurrentBranch() *commands.Branch { - // I used go-git for this, but that breaks if you've just done a git init, - // even though you're on 'master' - branchName, err := b.GitCommand.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD") + branchName, err := b.GitCommand.CurrentBranchName() if err != nil { - branchName, err = b.GitCommand.OSCommand.RunCommandWithOutput("git rev-parse --short HEAD") - if err != nil { - panic(err.Error()) - } + panic(err.Error()) } + return &commands.Branch{Name: strings.TrimSpace(branchName), Recency: " *"} } |