From f3fc98a3d03756ec94bcad955eda268fddb3613a Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 25 Sep 2018 20:11:33 +1000 Subject: support git flow when colouring commits --- pkg/commands/git.go | 40 ++++++++++++++++++++++++++++------------ pkg/commands/git_test.go | 22 +++++++++++++++++++--- pkg/gui/commits_panel.go | 9 +++++++-- 3 files changed, 54 insertions(+), 17 deletions(-) (limited to 'pkg') diff --git a/pkg/commands/git.go b/pkg/commands/git.go index b394753ca..0bfbc36a7 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -267,6 +267,10 @@ func (c *GitCommand) NewBranch(name string) error { return c.OSCommand.RunCommand(fmt.Sprintf("git checkout -b %s", name)) } +func (c *GitCommand) CurrentBranchName() (string, error) { + return c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD") +} + // DeleteBranch delete branch func (c *GitCommand) DeleteBranch(branch string, force bool) error { command := "git branch -d" @@ -460,17 +464,27 @@ func (c *GitCommand) GetBranchGraph(branchName string) (string, error) { return c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git log --graph --color --abbrev-commit --decorate --date=relative --pretty=medium -100 %s", branchName)) } -func (c *GitCommand) getMergeBase() string { - output, err := c.OSCommand.RunCommandWithOutput("git merge-base HEAD master") // TODO: support develop as well +func (c *GitCommand) getMergeBase() (string, error) { + currentBranch, err := c.CurrentBranchName() if err != nil { - c.Log.Error("Could not get merge base") - return "" + return "", err + } + + baseBranch := "master" + if strings.HasPrefix(currentBranch, "feature/") { + baseBranch = "develop" } - return output + + output, err := c.OSCommand.RunCommandWithOutput(fmt.Sprintf("git merge-base HEAD %s", baseBranch)) + if err != nil { + // swallowing error because it's not a big deal; probably because there are no commits yet + c.Log.Error(err) + } + return output, nil } // GetCommits obtains the commits of the current branch -func (c *GitCommand) GetCommits() []*Commit { +func (c *GitCommand) GetCommits() ([]*Commit, error) { pushables := c.GetCommitsToPush() log := c.GetLog() @@ -488,14 +502,16 @@ func (c *GitCommand) GetCommits() []*Commit { DisplayString: strings.Join(splitLine, " "), } } - commits = c.setCommitMergedStatuses(commits) - return commits + return c.setCommitMergedStatuses(commits) } -func (c *GitCommand) setCommitMergedStatuses(commits []*Commit) []*Commit { - ancestor := c.getMergeBase() +func (c *GitCommand) setCommitMergedStatuses(commits []*Commit) ([]*Commit, error) { + ancestor, err := c.getMergeBase() + if err != nil { + return nil, err + } if ancestor == "" { - return commits + return commits, nil } passedAncestor := false for i, commit := range commits { @@ -504,7 +520,7 @@ func (c *GitCommand) setCommitMergedStatuses(commits []*Commit) []*Commit { } commits[i].Merged = passedAncestor } - return commits + return commits, nil } // GetLog gets the git log (currently limited to 30 commits for performance diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 790a34690..e5faa707b 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -1484,7 +1484,7 @@ func TestGitCommandGetCommits(t *testing.T) { type scenario struct { testName string command func(string, ...string) *exec.Cmd - test func([]*Commit) + test func([]*Commit, error) } scenarios := []scenario{ @@ -1500,11 +1500,18 @@ func TestGitCommandGetCommits(t *testing.T) { case "log": assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args) return exec.Command("echo") + case "merge-base": + assert.EqualValues(t, []string{"merge-base", "HEAD", "master"}, args) + return exec.Command("test") + case "symbolic-ref": + assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args) + return exec.Command("echo", "master") } return nil }, - func(commits []*Commit) { + func(commits []*Commit, err error) { + assert.NoError(t, err) assert.Len(t, commits, 0) }, }, @@ -1520,23 +1527,32 @@ func TestGitCommandGetCommits(t *testing.T) { case "log": assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args) return exec.Command("echo", "8a2bb0e commit 1\n78976bc commit 2") + case "merge-base": + assert.EqualValues(t, []string{"merge-base", "HEAD", "master"}, args) + return exec.Command("echo", "78976bc") + case "symbolic-ref": + assert.EqualValues(t, []string{"symbolic-ref", "--short", "HEAD"}, args) + return exec.Command("echo", "master") } return nil }, - func(commits []*Commit) { + func(commits []*Commit, err error) { + assert.NoError(t, err) assert.Len(t, commits, 2) assert.EqualValues(t, []*Commit{ { Sha: "8a2bb0e", Name: "commit 1", Pushed: true, + Merged: false, DisplayString: "8a2bb0e commit 1", }, { Sha: "78976bc", Name: "commit 2", Pushed: false, + Merged: true, DisplayString: "78976bc commit 2", }, }, commits) diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index e3f4c84ac..a86401ebf 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -11,10 +11,15 @@ import ( func (gui *Gui) refreshCommits(g *gocui.Gui) error { g.Update(func(*gocui.Gui) error { - gui.State.Commits = gui.GitCommand.GetCommits() + commits, err := gui.GitCommand.GetCommits() + if err != nil { + return err + } + + gui.State.Commits = commits v, err := g.View("commits") if err != nil { - panic(err) + return err } v.Clear() -- cgit v1.2.3