From 3ecdffd86ce02f03f5dcbed309f0058c20165816 Mon Sep 17 00:00:00 2001 From: Aaron Sherber Date: Mon, 13 Sep 2021 18:00:59 -0400 Subject: [PATCH] fix: recognize tag and commit merges --- src/segment_git.go | 16 +++++++++++++--- src/segment_git_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/segment_git.go b/src/segment_git.go index 2383f7d4..516fe3e6 100644 --- a/src/segment_git.go +++ b/src/segment_git.go @@ -350,10 +350,20 @@ func (g *git) getGitHEADContext(ref string) string { if g.hasGitFile("MERGE_MSG") && g.hasGitFile("MERGE_HEAD") { icon := g.props.getString(MergeIcon, "\uE727 ") mergeContext := g.getGitFileContents(g.repo.gitWorkingFolder, "MERGE_MSG") - matches := findNamedRegexMatch(`Merge (remote-tracking )?branch '(?P.*)' into`, mergeContext) + matches := findNamedRegexMatch(`Merge (?P(remote-tracking )?branch|commit|tag) '(?P.*)' into`, mergeContext) + if matches != nil && matches["head"] != "" { - branch := g.truncateBranch(matches["head"]) - return fmt.Sprintf("%s%s%s into %s", icon, branchIcon, branch, ref) + var headIcon string + switch matches["type"] { + case "tag": + headIcon = g.props.getString(TagIcon, "\uF412") + case "commit": + headIcon = g.props.getString(CommitIcon, "\uF417") + default: + headIcon = branchIcon + } + head := g.truncateBranch(matches["head"]) + return fmt.Sprintf("%s%s%s into %s", icon, headIcon, head, ref) } } // sequencer status diff --git a/src/segment_git_test.go b/src/segment_git_test.go index bee296b2..aa0a8c86 100644 --- a/src/segment_git_test.go +++ b/src/segment_git_test.go @@ -325,6 +325,30 @@ func TestGetGitHEADContextMergeRemote(t *testing.T) { } func TestGetGitHEADContextMergeTag(t *testing.T) { + want := "\ue727 \uf412v7.8.9 into \ue0a0main" + context := &detachedContext{ + merge: true, + mergeHEAD: "v7.8.9", + mergeMsgStart: "Merge tag", + } + g := setupHEADContextEnv(context) + got := g.getGitHEADContext("main") + assert.Equal(t, want, got) +} + +func TestGetGitHEADContextMergeCommit(t *testing.T) { + want := "\ue727 \uf4178d7e869 into \ue0a0main" + context := &detachedContext{ + merge: true, + mergeHEAD: "8d7e869", + mergeMsgStart: "Merge commit", + } + g := setupHEADContextEnv(context) + got := g.getGitHEADContext("main") + assert.Equal(t, want, got) +} + +func TestGetGitHEADContextMergeIntoTag(t *testing.T) { want := "\ue727 \ue0a0feat into \uf412v3.4.6" context := &detachedContext{ tagName: "v3.4.6",