Skip to content

Commit

Permalink
more compatible commands
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Mar 23, 2020
1 parent c06c0b7 commit 3d3e0be
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
35 changes: 22 additions & 13 deletions pkg/commands/branch_list_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,48 +43,56 @@ 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)
}

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
Expand Down Expand Up @@ -116,19 +124,20 @@ 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:]...)
branches = append([]*Branch{branch}, branches...)
break
}
}
if !foundHead {
branches = append([]*Branch{{Name: currentBranchName, Head: true, Recency: " *"}}, branches...)
}

return branches
}
Expand Down
13 changes: 8 additions & 5 deletions pkg/commands/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
},
}),
Expand Down

0 comments on commit 3d3e0be

Please sign in to comment.