summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Kopenga <mkopenga@gmail.com>2018-11-14 13:40:17 +0100
committerGitHub <noreply@github.com>2018-11-14 13:40:17 +0100
commitb0eaf507a516cbc840542b8f26cc507f89c5ff63 (patch)
treeb8456c77295ba2c619113769ad275ffc45892c86
parent448d9caf1bca747a9d828456ac3e8e7dde0a3df2 (diff)
parentb9ecb82cb7ea5be027ebfcfa33ad45287e08c5f7 (diff)
Merge branch 'master' into https-ask-for-username-password
-rw-r--r--pkg/commands/git.go14
-rw-r--r--pkg/commands/git_test.go27
-rw-r--r--pkg/git/branch_list_builder.go25
3 files changed, 43 insertions, 23 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go
index 1b4ec57f5..d4f4ffd03 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/commands/git_test.go b/pkg/commands/git_test.go
index 7f111997c..0410ceb2c 100644
--- a/pkg/commands/git_test.go
+++ b/pkg/commands/git_test.go
@@ -1661,6 +1661,10 @@ func TestGitCommandGetCommits(t *testing.T) {
assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args)
// here's where we are returning the error
return exec.Command("test")
+ case "rev-parse":
+ assert.EqualValues(t, []string{"rev-parse", "--short", "HEAD"}, args)
+ // here too
+ return exec.Command("test")
}
return nil
@@ -1886,7 +1890,6 @@ func TestGitCommandCurrentBranchName(t *testing.T) {
"says we are on the master branch if we are",
func(cmd string, args ...string) *exec.Cmd {
assert.Equal(t, "git", cmd)
- assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args)
return exec.Command("echo", "master")
},
func(output string, err error) {
@@ -1895,10 +1898,30 @@ func TestGitCommandCurrentBranchName(t *testing.T) {
},
},
{
+ "falls back to git rev-parse if symbolic-ref fails",
+ func(cmd string, args ...string) *exec.Cmd {
+ assert.EqualValues(t, "git", cmd)
+
+ switch args[0] {
+ case "symbolic-ref":
+ assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args)
+ return exec.Command("test")
+ case "rev-parse":
+ assert.EqualValues(t, []string{"rev-parse", "--short", "HEAD"}, args)
+ return exec.Command("echo", "master")
+ }
+
+ return nil
+ },
+ func(output string, err error) {
+ assert.NoError(t, err)
+ assert.EqualValues(t, "master", output)
+ },
+ },
+ {
"bubbles up error if there is one",
func(cmd string, args ...string) *exec.Cmd {
assert.Equal(t, "git", cmd)
- assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args)
return exec.Command("test")
},
func(output string, err error) {
diff --git a/pkg/git/branch_list_builder.go b/pkg/git/branch_list_builder.go
index 151e5b0b4..60ec4e060 100644
--- a/pkg/git/branch_list_builder.go
+++ b/pkg/git/branch_list_builder.go
@@ -35,16 +35,12 @@ 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: " *"}
+
+ return &commands.Branch{Name: strings.TrimSpace(branchName)}
}
func (b *BranchListBuilder) obtainReflogBranches() []*commands.Branch {
@@ -61,7 +57,7 @@ func (b *BranchListBuilder) obtainReflogBranches() []*commands.Branch {
branch := &commands.Branch{Name: branchName, Recency: timeNumber + timeUnit}
branches = append(branches, branch)
}
- return branches
+ return uniqueByName(branches)
}
func (b *BranchListBuilder) obtainSafeBranches() []*commands.Branch {
@@ -103,11 +99,8 @@ func (b *BranchListBuilder) Build() []*commands.Branch {
branches := make([]*commands.Branch, 0)
head := b.obtainCurrentBranch()
safeBranches := b.obtainSafeBranches()
- if len(safeBranches) == 0 {
- return append(branches, head)
- }
+
reflogBranches := b.obtainReflogBranches()
- reflogBranches = uniqueByName(append([]*commands.Branch{head}, reflogBranches...))
for i, reflogBranch := range reflogBranches {
reflogBranches[i].Name = sanitisedReflogName(reflogBranch, safeBranches)
}
@@ -115,6 +108,12 @@ func (b *BranchListBuilder) Build() []*commands.Branch {
branches = b.appendNewBranches(branches, reflogBranches, safeBranches, true)
branches = b.appendNewBranches(branches, safeBranches, branches, false)
+ if len(branches) == 0 || branches[0].Name != head.Name {
+ branches = append([]*commands.Branch{head}, branches...)
+ }
+
+ branches[0].Recency = " *"
+
return branches
}