Skip to content

Commit 4f2e7ee

Browse files
refactor(git): show merge context
1 parent 2d3939f commit 4f2e7ee

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

docs/docs/segment-git.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ Local changes can also shown by default using the following syntax for both the
5353
- display_stash_count: `boolean` show stash count or not
5454
- stash_count_icon: `string` icon/text to display before the stash context
5555
- status_separator_icon: `string` icon/text to display between staging and working area changes
56+
- merge_icon: `string` icon/text to display before the merge context

segment_git.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ const (
8282
StashCountIcon Property = "stash_count_icon"
8383
//StatusSeparatorIcon shows between staging and working area
8484
StatusSeparatorIcon Property = "status_separator_icon"
85+
//MergeIcon shows before the merge context
86+
MergeIcon Property = "merge_icon"
8587
)
8688

8789
func (g *git) enabled() bool {
@@ -177,6 +179,12 @@ func (g *git) getGitHEADContext(ref string) string {
177179
icon := g.props.getString(RebaseIcon, "REBASING:")
178180
return fmt.Sprintf("%s%s%s (%s/%s) at %s", icon, branchIcon, origin, step, total, ref)
179181
}
182+
// merge
183+
if g.env.hasFiles(".git/MERGE_HEAD") {
184+
mergeHEAD := g.getGitRefFileSymbolicName("MERGE_HEAD")
185+
icon := g.props.getString(MergeIcon, "MERGING:")
186+
return fmt.Sprintf("%s%s%s into %s", icon, branchIcon, mergeHEAD, ref)
187+
}
180188
// cherry-pick
181189
if g.env.hasFiles(".git/CHERRY_PICK_HEAD") {
182190
sha := g.getGitRefFileSymbolicName("CHERRY_PICK_HEAD")

segment_git_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ type detachedContext struct {
5151
tagName string
5252
cherryPick bool
5353
cherryPickSHA string
54+
merge bool
55+
mergeHEAD string
5456
}
5557

5658
func setupHEADContextEnv(context *detachedContext) environmentInfo {
@@ -65,12 +67,15 @@ func setupHEADContextEnv(context *detachedContext) environmentInfo {
6567
env.On("getFileContent", ".git/rebase-apply/last").Return(context.total)
6668
env.On("getFileContent", ".git/rebase-apply/head-name").Return(context.origin)
6769
env.On("getFileContent", ".git/CHERRY_PICK_HEAD").Return(context.cherryPickSHA)
70+
env.On("getFileContent", ".git/MERGE_HEAD").Return(context.mergeHEAD)
6871
env.On("hasFiles", ".git/CHERRY_PICK_HEAD").Return(context.cherryPick)
72+
env.On("hasFiles", ".git/MERGE_HEAD").Return(context.merge)
6973
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "rev-parse", "--short", "HEAD"}).Return(context.currentCommit)
7074
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "describe", "--tags", "--exact-match"}).Return(context.tagName)
7175
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "name-rev", "--name-only", "--exclude=tags/*", context.origin}).Return(context.origin)
7276
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "name-rev", "--name-only", "--exclude=tags/*", context.onto}).Return(context.onto)
7377
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "name-rev", "--name-only", "--exclude=tags/*", context.cherryPickSHA}).Return(context.cherryPickSHA)
78+
env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "name-rev", "--name-only", "--exclude=tags/*", context.mergeHEAD}).Return(context.mergeHEAD)
7479
return env
7580
}
7681

@@ -184,6 +189,35 @@ func TestGetGitHEADContextCherryPickOnTag(t *testing.T) {
184189
assert.Equal(t, want, got)
185190
}
186191

192+
func TestGetGitHEADContextMerge(t *testing.T) {
193+
want := "MERGING:BRANCH:feat into BRANCH:main"
194+
context := &detachedContext{
195+
merge: true,
196+
mergeHEAD: "feat",
197+
}
198+
env := setupHEADContextEnv(context)
199+
g := &git{
200+
env: env,
201+
}
202+
got := g.getGitHEADContext("main")
203+
assert.Equal(t, want, got)
204+
}
205+
206+
func TestGetGitHEADContextMergeTag(t *testing.T) {
207+
want := "MERGING:BRANCH:feat into TAG:v3.4.6"
208+
context := &detachedContext{
209+
tagName: "v3.4.6",
210+
merge: true,
211+
mergeHEAD: "feat",
212+
}
213+
env := setupHEADContextEnv(context)
214+
g := &git{
215+
env: env,
216+
}
217+
got := g.getGitHEADContext("")
218+
assert.Equal(t, want, got)
219+
}
220+
187221
func TestGetStashContextZeroEntries(t *testing.T) {
188222
want := ""
189223
env := new(MockedEnvironment)

themes/jandedobbeleer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@
5656
"rebase_icon": "",
5757
"cherry_pick_icon": "",
5858
"detached_icon": "",
59-
"tag_icon": ""
59+
"tag_icon": "",
60+
"display_stash_count": true,
61+
"stash_count_icon": "\uF692 ",
62+
"merge_icon": "\uE726 "
6063
}
6164
},
6265
{

0 commit comments

Comments
 (0)