Skip to content

Commit 751c597

Browse files
peffgitster
authored andcommitted
merge: fix incorrect merge message for ambiguous tag/branch
If we have both a tag and a branch named "foo", then calling "git merge foo" will warn about the ambiguous ref, but merge the tag. When generating the commit message, though, we simply checked whether "refs/heads/foo" existed, and if it did, assumed it was a branch. This led to the statement "Merge branch 'foo'" in the commit message, which is quite wrong. Instead, we should use dwim_ref to find the actual ref used, and describe it appropriately. In addition to the test in t7608, we must also tweak the expected output of t4202, which was accidentally triggering this bug. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ce06461 commit 751c597

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

builtin-merge.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ static void merge_name(const char *remote, struct strbuf *msg)
358358
struct strbuf buf = STRBUF_INIT;
359359
struct strbuf bname = STRBUF_INIT;
360360
const char *ptr;
361+
char *found_ref;
361362
int len, early;
362363

363364
strbuf_branchname(&bname, remote);
@@ -368,14 +369,12 @@ static void merge_name(const char *remote, struct strbuf *msg)
368369
if (!remote_head)
369370
die("'%s' does not point to a commit", remote);
370371

371-
strbuf_addstr(&buf, "refs/heads/");
372-
strbuf_addstr(&buf, remote);
373-
resolve_ref(buf.buf, branch_head, 0, NULL);
374-
375-
if (!hashcmp(remote_head->sha1, branch_head)) {
376-
strbuf_addf(msg, "%s\t\tbranch '%s' of .\n",
377-
sha1_to_hex(branch_head), remote);
378-
goto cleanup;
372+
if (dwim_ref(remote, strlen(remote), branch_head, &found_ref) > 0) {
373+
if (!prefixcmp(found_ref, "refs/heads/")) {
374+
strbuf_addf(msg, "%s\t\tbranch '%s' of .\n",
375+
sha1_to_hex(branch_head), remote);
376+
goto cleanup;
377+
}
379378
}
380379

381380
/* See if remote matches <name>^^^.. or <name>~<number> */

t/t4202-log.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,11 @@ test_expect_success 'set up more tangled history' '
320320
'
321321

322322
cat > expect <<\EOF
323-
* Merge branch 'reach'
323+
* Merge commit 'reach'
324324
|\
325325
| \
326326
| \
327-
*-. \ Merge branches 'octopus-a' and 'octopus-b'
327+
*-. \ Merge commit 'octopus-a'; commit 'octopus-b'
328328
|\ \ \
329329
* | | | seventh
330330
| | * | octopus-b

t/t7608-merge-messages.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test_expect_success 'merge tag' '
3838
check_oneline "Merge commit Qtag-1Q"
3939
'
4040

41-
test_expect_failure 'ambiguous tag' '
41+
test_expect_success 'ambiguous tag' '
4242
git checkout -b ambiguous master &&
4343
test_commit ambiguous &&
4444
git checkout master &&

0 commit comments

Comments
 (0)