mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-03 13:21:56 +02:00
support git flow when colouring commits
This commit is contained in:
parent
a2b3cd0823
commit
f3fc98a3d0
@ -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
|
||||
}
|
||||
return output
|
||||
|
||||
baseBranch := "master"
|
||||
if strings.HasPrefix(currentBranch, "feature/") {
|
||||
baseBranch = "develop"
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -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)
|
||||
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user