From 4f2e7ee7a33ed27376146472670dd08cffb1304f Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sun, 11 Oct 2020 11:56:33 +0200 Subject: [PATCH] refactor(git): show merge context --- docs/docs/segment-git.md | 1 + segment_git.go | 8 ++++++++ segment_git_test.go | 34 ++++++++++++++++++++++++++++++++++ themes/jandedobbeleer.json | 5 ++++- 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/docs/docs/segment-git.md b/docs/docs/segment-git.md index 0884e33d08ad..d1e47cb2541b 100644 --- a/docs/docs/segment-git.md +++ b/docs/docs/segment-git.md @@ -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 diff --git a/segment_git.go b/segment_git.go index ab70ba69ee15..a54efc16f707 100755 --- a/segment_git.go +++ b/segment_git.go @@ -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 { @@ -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") diff --git a/segment_git_test.go b/segment_git_test.go index 6319d85b9bb0..dc58a2578566 100755 --- a/segment_git_test.go +++ b/segment_git_test.go @@ -51,6 +51,8 @@ type detachedContext struct { tagName string cherryPick bool cherryPickSHA string + merge bool + mergeHEAD string } func setupHEADContextEnv(context *detachedContext) environmentInfo { @@ -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 } @@ -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) diff --git a/themes/jandedobbeleer.json b/themes/jandedobbeleer.json index 6ea2f0bde048..b116794ddabb 100644 --- a/themes/jandedobbeleer.json +++ b/themes/jandedobbeleer.json @@ -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 " } }, {