merge-recursive: fix verbose output for multiple base trees

One of the indirect callers of make_virtual_commit() passes the result of oid_to_hex() as the name, i.e. a pointer to a static buffer. Since the function uses that string pointer directly in building a struct merge_remote_desc, multiple entries can end up sharing the same name inadvertently. Fix that by calling set_merge_remote_desc(), which creates a copy of the string, instead of building the struct by hand. Signed-off-by: Rene Scharfe <l.s.r@web.de> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Aug 13, 2016 at 14:16 UTC a25716535ba7ba85322a1e9f20168f31c61dae81
2 files changed +19 -4
merge-recursive.c
+1 -4
@@ -42,12 +42,9 @@ static struct tree *shift_tree_object(struct tree *one, struct tree *two,
42 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
43 {
44 struct commit *commit = alloc_commit_node();
45 - struct merge_remote_desc *desc = xmalloc(sizeof(*desc));
45
47 - desc->name = comment;
48 - desc->obj = (struct object *)commit;
46 + set_merge_remote_desc(commit, comment, (struct object *)commit);
47 commit->tree = tree;
50 - commit->util = desc;
48 commit->object.parsed = 1;
49 return commit;
50 }
t/t3030-merge-recursive.sh
+18
@@ -660,4 +660,22 @@ test_expect_success 'merging with triple rename across D/F conflict' '
660 git merge other
661 '
662
663 +test_expect_success 'merge-recursive remembers the names of all base trees' '
664 + git reset --hard HEAD &&
665 +
666 + # more trees than static slots used by oid_to_hex()
667 + for commit in $c0 $c2 $c4 $c5 $c6 $c7
668 + do
669 + git rev-parse "$commit^{tree}"
670 + done >trees &&
671 +
672 + # ignore the return code -- it only fails because the input is weird
673 + test_must_fail git -c merge.verbosity=5 merge-recursive $(cat trees) -- $c1 $c3 >out &&
674 +
675 + # merge-recursive prints in reverse order, but we do not care
676 + sort <trees >expect &&
677 + sed -n "s/^virtual //p" out | sort >actual &&
678 + test_cmp expect actual
679 +'
680 +
681 test_done