simplify-merges: drop merge from irrelevant side branch

The merge simplification rule stated in 6546b59 (revision traversal: show full history with merge simplification, 2008-07-31) still treated merge commits too specially. Namely, in a history with this shape: ---o---o---M / x---x---x where three 'x' were on a history completely unrelated to the main history 'o' and do not touch any of the paths we are following, we still said that after simplifying all of the parents of M, 'x' (which is the leftmost 'x' that rightmost 'x simplifies down to) and 'o' (which would be the last commit on the main history that touches the paths we are following) are independent from each other, and both need to be kept. That is incorrect; when the side branch 'x' never touches the paths, it should be removed to allow M to simplify down to the last commit on the main history that touches the paths. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jan 17, 2013 at 14:23 UTC 4b7f53da7618e94985c9d9d5081b4bc44c45de2f
2 files changed +44 -5
revision.c
+23
@@ -1424,6 +1424,22 @@ static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs,
1424 return st;
1425 }
1426
1427 +static void remove_treesame_parents(struct commit *commit)
1428 +{
1429 + struct commit_list **pp, *p;
1430 +
1431 + pp = &commit->parents;
1432 + while ((p = *pp) != NULL) {
1433 + struct commit *parent = p->item;
1434 + if (parent->object.flags & TREESAME) {
1435 + *pp = p->next;
1436 + free(p);
1437 + continue;
1438 + }
1439 + pp = &p->next;
1440 + }
1441 +}
1442 +
1443 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
1444 {
1445 struct commit_list *p;
@@ -1469,6 +1485,13 @@ static struct commit_list **simplify_one(struct rev_info *revs, struct commit *c
1485 pst = locate_simplify_state(revs, p->item);
1486 p->item = pst->simplified;
1487 }
1488 +
1489 + /*
1490 + * A merge with a tree-same parent is useless
1491 + */
1492 + if (commit->parents && commit->parents->next)
1493 + remove_treesame_parents(commit);
1494 +
1495 cnt = remove_duplicate_parents(commit);
1496
1497 /*
t/t6012-rev-list-simplify.sh
+21 -5
@@ -59,7 +59,23 @@ test_expect_success setup '
59
60 echo "Final change" >file &&
61 test_tick && git commit -a -m "Final change" &&
62 - note I
62 + note I &&
63 +
64 + git symbolic-ref HEAD refs/heads/unrelated &&
65 + git rm -f "*" &&
66 + echo "Unrelated branch" >side &&
67 + git add side &&
68 + test_tick && git commit -m "Side root" &&
69 + note J &&
70 +
71 + git checkout master &&
72 + test_tick && git merge -m "Coolest" unrelated &&
73 + note K &&
74 +
75 + echo "Immaterial" >elif &&
76 + git add elif &&
77 + test_tick && git commit -m "Last" &&
78 + note L
79 '
80
81 FMT='tformat:%P %H | %s'
@@ -82,10 +98,10 @@ check_result () {
98 '
99 }
100
85 -check_result 'I H G F E D C B A' --full-history
86 -check_result 'I H E C B A' --full-history -- file
87 -check_result 'I H E C B A' --full-history --topo-order -- file
88 -check_result 'I H E C B A' --full-history --date-order -- file
101 +check_result 'L K J I H G F E D C B A' --full-history
102 +check_result 'K I H E C B A' --full-history -- file
103 +check_result 'K I H E C B A' --full-history --topo-order -- file
104 +check_result 'K I H E C B A' --full-history --date-order -- file
105 check_result 'I E C B A' --simplify-merges -- file
106 check_result 'I B A' -- file
107 check_result 'I B A' --topo-order -- file