mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-15 00:15:32 +02:00
support git flow when colouring commits
This commit is contained in:
@ -267,6 +267,10 @@ func (c *GitCommand) NewBranch(name string) error {
|
|||||||
return c.OSCommand.RunCommand(fmt.Sprintf("git checkout -b %s", name))
|
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
|
// DeleteBranch delete branch
|
||||||
func (c *GitCommand) DeleteBranch(branch string, force bool) error {
|
func (c *GitCommand) DeleteBranch(branch string, force bool) error {
|
||||||
command := "git branch -d"
|
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))
|
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 {
|
func (c *GitCommand) getMergeBase() (string, error) {
|
||||||
output, err := c.OSCommand.RunCommandWithOutput("git merge-base HEAD master") // TODO: support develop as well
|
currentBranch, err := c.CurrentBranchName()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Log.Error("Could not get merge base")
|
return "", err
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
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
|
// GetCommits obtains the commits of the current branch
|
||||||
func (c *GitCommand) GetCommits() []*Commit {
|
func (c *GitCommand) GetCommits() ([]*Commit, error) {
|
||||||
pushables := c.GetCommitsToPush()
|
pushables := c.GetCommitsToPush()
|
||||||
log := c.GetLog()
|
log := c.GetLog()
|
||||||
|
|
||||||
@ -488,14 +502,16 @@ func (c *GitCommand) GetCommits() []*Commit {
|
|||||||
DisplayString: strings.Join(splitLine, " "),
|
DisplayString: strings.Join(splitLine, " "),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
commits = c.setCommitMergedStatuses(commits)
|
return c.setCommitMergedStatuses(commits)
|
||||||
return commits
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *GitCommand) setCommitMergedStatuses(commits []*Commit) []*Commit {
|
func (c *GitCommand) setCommitMergedStatuses(commits []*Commit) ([]*Commit, error) {
|
||||||
ancestor := c.getMergeBase()
|
ancestor, err := c.getMergeBase()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
if ancestor == "" {
|
if ancestor == "" {
|
||||||
return commits
|
return commits, nil
|
||||||
}
|
}
|
||||||
passedAncestor := false
|
passedAncestor := false
|
||||||
for i, commit := range commits {
|
for i, commit := range commits {
|
||||||
@ -504,7 +520,7 @@ func (c *GitCommand) setCommitMergedStatuses(commits []*Commit) []*Commit {
|
|||||||
}
|
}
|
||||||
commits[i].Merged = passedAncestor
|
commits[i].Merged = passedAncestor
|
||||||
}
|
}
|
||||||
return commits
|
return commits, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLog gets the git log (currently limited to 30 commits for performance
|
// GetLog gets the git log (currently limited to 30 commits for performance
|
||||||
|
@ -1484,7 +1484,7 @@ func TestGitCommandGetCommits(t *testing.T) {
|
|||||||
type scenario struct {
|
type scenario struct {
|
||||||
testName string
|
testName string
|
||||||
command func(string, ...string) *exec.Cmd
|
command func(string, ...string) *exec.Cmd
|
||||||
test func([]*Commit)
|
test func([]*Commit, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
scenarios := []scenario{
|
scenarios := []scenario{
|
||||||
@ -1500,11 +1500,18 @@ func TestGitCommandGetCommits(t *testing.T) {
|
|||||||
case "log":
|
case "log":
|
||||||
assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)
|
assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)
|
||||||
return exec.Command("echo")
|
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
|
return nil
|
||||||
},
|
},
|
||||||
func(commits []*Commit) {
|
func(commits []*Commit, err error) {
|
||||||
|
assert.NoError(t, err)
|
||||||
assert.Len(t, commits, 0)
|
assert.Len(t, commits, 0)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -1520,23 +1527,32 @@ func TestGitCommandGetCommits(t *testing.T) {
|
|||||||
case "log":
|
case "log":
|
||||||
assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)
|
assert.EqualValues(t, []string{"log", "--oneline", "-30"}, args)
|
||||||
return exec.Command("echo", "8a2bb0e commit 1\n78976bc commit 2")
|
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
|
return nil
|
||||||
},
|
},
|
||||||
func(commits []*Commit) {
|
func(commits []*Commit, err error) {
|
||||||
|
assert.NoError(t, err)
|
||||||
assert.Len(t, commits, 2)
|
assert.Len(t, commits, 2)
|
||||||
assert.EqualValues(t, []*Commit{
|
assert.EqualValues(t, []*Commit{
|
||||||
{
|
{
|
||||||
Sha: "8a2bb0e",
|
Sha: "8a2bb0e",
|
||||||
Name: "commit 1",
|
Name: "commit 1",
|
||||||
Pushed: true,
|
Pushed: true,
|
||||||
|
Merged: false,
|
||||||
DisplayString: "8a2bb0e commit 1",
|
DisplayString: "8a2bb0e commit 1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Sha: "78976bc",
|
Sha: "78976bc",
|
||||||
Name: "commit 2",
|
Name: "commit 2",
|
||||||
Pushed: false,
|
Pushed: false,
|
||||||
|
Merged: true,
|
||||||
DisplayString: "78976bc commit 2",
|
DisplayString: "78976bc commit 2",
|
||||||
},
|
},
|
||||||
}, commits)
|
}, commits)
|
||||||
|
@ -11,10 +11,15 @@ import (
|
|||||||
|
|
||||||
func (gui *Gui) refreshCommits(g *gocui.Gui) error {
|
func (gui *Gui) refreshCommits(g *gocui.Gui) error {
|
||||||
g.Update(func(*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")
|
v, err := g.View("commits")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
v.Clear()
|
v.Clear()
|
||||||
|
Reference in New Issue
Block a user