diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2018-11-14 19:17:05 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2018-11-14 21:19:12 +1100 |
commit | 0eb1e4a86bee71158b8a5a2f73368cdf6d330283 (patch) | |
tree | 493b35eebc90b6692a5782f695bd226744f5123e /pkg | |
parent | a5f483fae97fa16b9291761e74acdc35a669df76 (diff) |
change how we build our list of branches to support detached heads
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/commands/git_test.go | 27 | ||||
-rw-r--r-- | pkg/git/branch_list_builder.go | 15 |
2 files changed, 34 insertions, 8 deletions
diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index ebb9de136..9bfdaf519 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -1658,6 +1658,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 @@ -1883,7 +1887,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) { @@ -1892,10 +1895,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 821f7b9b1..60ec4e060 100644 --- a/pkg/git/branch_list_builder.go +++ b/pkg/git/branch_list_builder.go @@ -40,7 +40,7 @@ func (b *BranchListBuilder) obtainCurrentBranch() *commands.Branch { panic(err.Error()) } - return &commands.Branch{Name: strings.TrimSpace(branchName), Recency: " *"} + return &commands.Branch{Name: strings.TrimSpace(branchName)} } func (b *BranchListBuilder) obtainReflogBranches() []*commands.Branch { @@ -57,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 { @@ -99,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) } @@ -111,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 } |