Skip to content

Commit

Permalink
refactor(git): show merge context
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Oct 11, 2020
1 parent 2d3939f commit 4f2e7ee
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/docs/segment-git.md
Expand Up @@ -53,3 +53,4 @@ Local changes can also shown by default using the following syntax for both the
- display_stash_count: `boolean` show stash count or not
- stash_count_icon: `string` icon/text to display before the stash context
- status_separator_icon: `string` icon/text to display between staging and working area changes
- merge_icon: `string` icon/text to display before the merge context
8 changes: 8 additions & 0 deletions segment_git.go
Expand Up @@ -82,6 +82,8 @@ const (
StashCountIcon Property = "stash_count_icon"
//StatusSeparatorIcon shows between staging and working area
StatusSeparatorIcon Property = "status_separator_icon"
//MergeIcon shows before the merge context
MergeIcon Property = "merge_icon"
)

func (g *git) enabled() bool {
Expand Down Expand Up @@ -177,6 +179,12 @@ func (g *git) getGitHEADContext(ref string) string {
icon := g.props.getString(RebaseIcon, "REBASING:")
return fmt.Sprintf("%s%s%s (%s/%s) at %s", icon, branchIcon, origin, step, total, ref)
}
// merge
if g.env.hasFiles(".git/MERGE_HEAD") {
mergeHEAD := g.getGitRefFileSymbolicName("MERGE_HEAD")
icon := g.props.getString(MergeIcon, "MERGING:")
return fmt.Sprintf("%s%s%s into %s", icon, branchIcon, mergeHEAD, ref)
}
// cherry-pick
if g.env.hasFiles(".git/CHERRY_PICK_HEAD") {
sha := g.getGitRefFileSymbolicName("CHERRY_PICK_HEAD")
Expand Down
34 changes: 34 additions & 0 deletions segment_git_test.go
Expand Up @@ -51,6 +51,8 @@ type detachedContext struct {
tagName string
cherryPick bool
cherryPickSHA string
merge bool
mergeHEAD string
}

func setupHEADContextEnv(context *detachedContext) environmentInfo {
Expand All @@ -65,12 +67,15 @@ func setupHEADContextEnv(context *detachedContext) environmentInfo {
env.On("getFileContent", ".git/rebase-apply/last").Return(context.total)
env.On("getFileContent", ".git/rebase-apply/head-name").Return(context.origin)
env.On("getFileContent", ".git/CHERRY_PICK_HEAD").Return(context.cherryPickSHA)
env.On("getFileContent", ".git/MERGE_HEAD").Return(context.mergeHEAD)
env.On("hasFiles", ".git/CHERRY_PICK_HEAD").Return(context.cherryPick)
env.On("hasFiles", ".git/MERGE_HEAD").Return(context.merge)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "rev-parse", "--short", "HEAD"}).Return(context.currentCommit)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "describe", "--tags", "--exact-match"}).Return(context.tagName)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "name-rev", "--name-only", "--exclude=tags/*", context.origin}).Return(context.origin)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "name-rev", "--name-only", "--exclude=tags/*", context.onto}).Return(context.onto)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "name-rev", "--name-only", "--exclude=tags/*", context.cherryPickSHA}).Return(context.cherryPickSHA)
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "name-rev", "--name-only", "--exclude=tags/*", context.mergeHEAD}).Return(context.mergeHEAD)
return env
}

Expand Down Expand Up @@ -184,6 +189,35 @@ func TestGetGitHEADContextCherryPickOnTag(t *testing.T) {
assert.Equal(t, want, got)
}

func TestGetGitHEADContextMerge(t *testing.T) {
want := "MERGING:BRANCH:feat into BRANCH:main"
context := &detachedContext{
merge: true,
mergeHEAD: "feat",
}
env := setupHEADContextEnv(context)
g := &git{
env: env,
}
got := g.getGitHEADContext("main")
assert.Equal(t, want, got)
}

func TestGetGitHEADContextMergeTag(t *testing.T) {
want := "MERGING:BRANCH:feat into TAG:v3.4.6"
context := &detachedContext{
tagName: "v3.4.6",
merge: true,
mergeHEAD: "feat",
}
env := setupHEADContextEnv(context)
g := &git{
env: env,
}
got := g.getGitHEADContext("")
assert.Equal(t, want, got)
}

func TestGetStashContextZeroEntries(t *testing.T) {
want := ""
env := new(MockedEnvironment)
Expand Down
5 changes: 4 additions & 1 deletion themes/jandedobbeleer.json
Expand Up @@ -56,7 +56,10 @@
"rebase_icon": "",
"cherry_pick_icon": "",
"detached_icon": "",
"tag_icon": ""
"tag_icon": "",
"display_stash_count": true,
"stash_count_icon": "\uF692 ",
"merge_icon": "\uE726 "
}
},
{
Expand Down

0 comments on commit 4f2e7ee

Please sign in to comment.