diff --git a/pkg/commands/branch_list_builder.go b/pkg/commands/branch_list_builder.go index 43ba2cf9d..421ffecc7 100644 --- a/pkg/commands/branch_list_builder.go +++ b/pkg/commands/branch_list_builder.go @@ -43,7 +43,7 @@ func (b *BranchListBuilder) obtainCurrentBranchName() string { } func (b *BranchListBuilder) obtainBranches() []*Branch { - cmdStr := `git branch --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)"` + cmdStr := `git for-each-ref --sort=-committerdate --format="%(HEAD)|%(refname:short)|%(upstream:short)|%(upstream:track)" refs/heads` output, err := b.GitCommand.OSCommand.RunCommandWithOutput(cmdStr) if err != nil { panic(err) @@ -51,40 +51,48 @@ func (b *BranchListBuilder) obtainBranches() []*Branch { trimmedOutput := strings.TrimSpace(output) outputLines := strings.Split(trimmedOutput, "\n") - branches := make([]*Branch, len(outputLines)) - for i, line := range outputLines { + branches := make([]*Branch, 0, len(outputLines)) + for _, line := range outputLines { + if line == "" { + continue + } + split := strings.Split(line, SEPARATION_CHAR) name := split[1] - branches[i] = &Branch{ + branch := &Branch{ Name: name, Pullables: "?", Pushables: "?", Head: split[0] == "*", } + upstreamName := split[2] if upstreamName == "" { + branches = append(branches, branch) continue } - branches[i].UpstreamName = upstreamName + branch.UpstreamName = upstreamName track := split[3] re := regexp.MustCompile(`ahead (\d+)`) match := re.FindStringSubmatch(track) if len(match) > 1 { - branches[i].Pushables = match[1] + branch.Pushables = match[1] } else { - branches[i].Pushables = "0" + branch.Pushables = "0" } re = regexp.MustCompile(`behind (\d+)`) match = re.FindStringSubmatch(track) if len(match) > 1 { - branches[i].Pullables = match[1] + branch.Pullables = match[1] } else { - branches[i].Pullables = "0" + branch.Pullables = "0" } + + branches = append(branches, branch) } return branches @@ -116,12 +124,10 @@ outer: branches = append(branchesWithRecency, branches...) - if len(branches) == 0 { - branches = append([]*Branch{{Name: currentBranchName}}, branches...) - } - + foundHead := false for i, branch := range branches { if branch.Head { + foundHead = true branch.Name = currentBranchName branch.Recency = " *" branches = append(branches[0:i], branches[i+1:]...) @@ -129,6 +135,9 @@ outer: break } } + if !foundHead { + branches = append([]*Branch{{Name: currentBranchName, Head: true, Recency: " *"}}, branches...) + } return branches } diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 2bd852de8..6fabfe273 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -866,7 +866,7 @@ func (c *GitCommand) CherryPickCommits(commits []*Commit) error { // GetCommitFiles get the specified commit files func (c *GitCommand) GetCommitFiles(commitSha string, patchManager *PatchManager) ([]*CommitFile, error) { - files, err := c.OSCommand.RunCommandWithOutput("git show --pretty= --name-only --no-renames %s", commitSha) + files, err := c.OSCommand.RunCommandWithOutput("git diff-tree --no-commit-id --name-only -r --no-renames %s", commitSha) if err != nil { return nil, err } @@ -1108,23 +1108,26 @@ func (c *GitCommand) FetchRemote(remoteName string) error { func (c *GitCommand) GetReflogCommits() ([]*Commit, error) { output, err := c.OSCommand.RunCommandWithOutput("git reflog --abbrev=20") if err != nil { - return nil, err + // assume error means we have no reflog + return []*Commit{}, nil } lines := strings.Split(strings.TrimSpace(output), "\n") - commits := make([]*Commit, len(lines)) + commits := make([]*Commit, 0) re := regexp.MustCompile(`(\w+).*HEAD@\{\d+\}: (.*)`) - for i, line := range lines { + for _, line := range lines { match := re.FindStringSubmatch(line) if len(match) <= 1 { continue } - commits[i] = &Commit{ + commit := &Commit{ Sha: match[1], Name: match[2], Status: "reflog", } + + commits = append(commits, commit) } return commits, nil diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 99ab351fa..8070d9610 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -1907,7 +1907,7 @@ func TestGitCommandGetCommitFiles(t *testing.T) { "123456", test.CreateMockCommand(t, []*test.CommandSwapper{ { - Expect: "git show --pretty= --name-only --no-renames 123456", + Expect: "git diff-tree --no-commit-id --name-only -r --no-renames 123456", Replace: "echo 'hello\nworld'", }, }),