reduce_heads(): protect from duplicate input
authorJunio C Hamano <gitster@pobox.com>
Mon, 14 Jul 2008 07:09:41 +0000 (00:09 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 14 Jul 2008 07:09:41 +0000 (00:09 -0700)
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>
commit.c
t/t7600-merge.sh

index 03e73f323ae082fac9f127900eede7aa3952dc44..5148ec552708bb6c98cc64a84647ec7715ad1c29 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -745,15 +745,22 @@ struct commit_list *reduce_heads(struct commit_list *heads)
        for (p = heads; p; p = p->next) {
                struct commit_list *q, *base;
 
+               /* Do we already have this in the result? */
+               for (q = result; q; q = q->next)
+                       if (p->item == q->item)
+                               break;
+               if (q)
+                       continue;
+
                num_other = 0;
                for (q = heads; q; q = q->next) {
                        if (p->item == q->item)
                                continue;
                        other[num_other++] = q->item;
                }
-               if (num_other) {
+               if (num_other)
                        base = get_merge_bases_many(p->item, num_other, other, 1);
-               else
+               else
                        base = NULL;
                /*
                 * If p->item does not have anything common with other
index f035ea376e4f0203df3d890bab49f65129e30540..d4cf6289a4c06ec9436235b7325379f26a388e06 100755 (executable)
@@ -490,4 +490,26 @@ test_expect_success 'merge c1 with c0, c2, c0, and c1' '
 
 test_debug 'gitk --all'
 
+test_expect_success 'merge c1 with c0, c2, c0, and c1' '
+       git reset --hard c1 &&
+       git config branch.master.mergeoptions "" &&
+       test_tick &&
+       git merge c0 c2 c0 c1 &&
+       verify_merge file result.1-5 &&
+       verify_parents $c1 $c2
+'
+
+test_debug 'gitk --all'
+
+test_expect_success 'merge c1 with c1 and c2' '
+       git reset --hard c1 &&
+       git config branch.master.mergeoptions "" &&
+       test_tick &&
+       git merge c1 c2 &&
+       verify_merge file result.1-5 &&
+       verify_parents $c1 $c2
+'
+
+test_debug 'gitk --all'
+
 test_done