Skip to content

Commit 711f6b2

Browse files
committed
reduce_heads(): protect from duplicate input
Because we do not try computing merge base with itself for obvious reasons, the code was not prepared for an arguably insane case of the caller feeding the same commit twice to it. Noticed and test written by Sverre Hvammen Johansen Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 3d1dd47 commit 711f6b2

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

commit.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,15 +745,22 @@ struct commit_list *reduce_heads(struct commit_list *heads)
745745
for (p = heads; p; p = p->next) {
746746
struct commit_list *q, *base;
747747

748+
/* Do we already have this in the result? */
749+
for (q = result; q; q = q->next)
750+
if (p->item == q->item)
751+
break;
752+
if (q)
753+
continue;
754+
748755
num_other = 0;
749756
for (q = heads; q; q = q->next) {
750757
if (p->item == q->item)
751758
continue;
752759
other[num_other++] = q->item;
753760
}
754-
if (num_other) {
761+
if (num_other)
755762
base = get_merge_bases_many(p->item, num_other, other, 1);
756-
} else
763+
else
757764
base = NULL;
758765
/*
759766
* If p->item does not have anything common with other

t/t7600-merge.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,4 +490,26 @@ test_expect_success 'merge c1 with c0, c2, c0, and c1' '
490490

491491
test_debug 'gitk --all'
492492

493+
test_expect_success 'merge c1 with c0, c2, c0, and c1' '
494+
git reset --hard c1 &&
495+
git config branch.master.mergeoptions "" &&
496+
test_tick &&
497+
git merge c0 c2 c0 c1 &&
498+
verify_merge file result.1-5 &&
499+
verify_parents $c1 $c2
500+
'
501+
502+
test_debug 'gitk --all'
503+
504+
test_expect_success 'merge c1 with c1 and c2' '
505+
git reset --hard c1 &&
506+
git config branch.master.mergeoptions "" &&
507+
test_tick &&
508+
git merge c1 c2 &&
509+
verify_merge file result.1-5 &&
510+
verify_parents $c1 $c2
511+
'
512+
513+
test_debug 'gitk --all'
514+
493515
test_done

0 commit comments

Comments
 (0)