Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 9c129ea

Browse files
kjbracey2gitster
authored andcommitted
simplify-merges: never remove all TREESAME parents
When simplifying an odd merge, such as one that used "-s ours", we may find ourselves TREESAME to apparently redundant parents. Prevent simplify_merges() from removing every TREESAME parent; if this would happen reinstate the first TREESAME parent - the one that the default log would have followed. This avoids producing a totally disjoint history from the default log when the default log is a better explanation of the end result, and aids visualisation of odd merges. Signed-off-by: Kevin Bracey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d5d2fc8 commit 9c129ea

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

Documentation/rev-list-options.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,8 @@ history according to the following rules:
471471
+
472472
* Replace each parent `P` of `C'` with its simplification `P'`. In
473473
the process, drop parents that are ancestors of other parents, and
474-
remove duplicates.
474+
remove duplicates, but take care to never drop all parents that
475+
we are TREESAME to.
475476
+
476477
* If after this parent rewriting, `C'` is a root or merge commit (has
477478
zero or >1 parents), a boundary commit, or !TREESAME, it remains.

revision.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,73 @@ static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
21362136
return marked;
21372137
}
21382138

2139+
/*
2140+
* Awkward naming - this means one parent we are TREESAME to.
2141+
* cf mark_treesame_root_parents: root parents that are TREESAME (to an
2142+
* empty tree). Better name suggestions?
2143+
*/
2144+
static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2145+
{
2146+
struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2147+
struct commit *unmarked = NULL, *marked = NULL;
2148+
struct commit_list *p;
2149+
unsigned n;
2150+
2151+
for (p = commit->parents, n = 0; p; p = p->next, n++) {
2152+
if (ts->treesame[n]) {
2153+
if (p->item->object.flags & TMP_MARK) {
2154+
if (!marked)
2155+
marked = p->item;
2156+
} else {
2157+
if (!unmarked) {
2158+
unmarked = p->item;
2159+
break;
2160+
}
2161+
}
2162+
}
2163+
}
2164+
2165+
/*
2166+
* If we are TREESAME to a marked-for-deletion parent, but not to any
2167+
* unmarked parents, unmark the first TREESAME parent. This is the
2168+
* parent that the default simplify_history==1 scan would have followed,
2169+
* and it doesn't make sense to omit that path when asking for a
2170+
* simplified full history. Retaining it improves the chances of
2171+
* understanding odd missed merges that took an old version of a file.
2172+
*
2173+
* Example:
2174+
*
2175+
* I--------*X A modified the file, but mainline merge X used
2176+
* \ / "-s ours", so took the version from I. X is
2177+
* `-*A--' TREESAME to I and !TREESAME to A.
2178+
*
2179+
* Default log from X would produce "I". Without this check,
2180+
* --full-history --simplify-merges would produce "I-A-X", showing
2181+
* the merge commit X and that it changed A, but not making clear that
2182+
* it had just taken the I version. With this check, the topology above
2183+
* is retained.
2184+
*
2185+
* Note that it is possible that the simplification chooses a different
2186+
* TREESAME parent from the default, in which case this test doesn't
2187+
* activate, and we _do_ drop the default parent. Example:
2188+
*
2189+
* I------X A modified the file, but it was reverted in B,
2190+
* \ / meaning mainline merge X is TREESAME to both
2191+
* *A-*B parents.
2192+
*
2193+
* Default log would produce "I" by following the first parent;
2194+
* --full-history --simplify-merges will produce "I-A-B". But this is a
2195+
* reasonable result - it presents a logical full history leading from
2196+
* I to X, and X is not an important merge.
2197+
*/
2198+
if (!unmarked && marked) {
2199+
marked->object.flags &= ~TMP_MARK;
2200+
return 1;
2201+
}
2202+
2203+
return 0;
2204+
}
2205+
21392206
static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
21402207
{
21412208
struct commit_list **pp, *p;
@@ -2238,6 +2305,8 @@ static struct commit_list **simplify_one(struct rev_info *revs, struct commit *c
22382305
*/
22392306
if (1 < cnt) {
22402307
int marked = mark_redundant_parents(revs, commit);
2308+
if (marked)
2309+
marked -= leave_one_treesame_to_parent(revs, commit);
22412310
if (marked)
22422311
cnt = remove_marked_parents(revs, commit);
22432312
}

t/t6111-rev-list-treesame.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ check_result 'M L H B A' -- file
116116
check_result '(LH)M (B)L (B)H (A)B A' --parents -- file
117117
check_result 'M L J I H G F D B A' --full-history -- file
118118
check_result '(LH)M (K)L (GJ)K (I)J (G)I (G)H (FB)G (D)F (BA)D (A)B A' --full-history --parents -- file
119-
check_outcome failure '(LH)M (G)H (J)L (I)J (G)I (FB)G (B)F (A)B A' --simplify-merges -- file # drops parent B from G
119+
check_result '(LH)M (G)H (J)L (I)J (G)I (FB)G (B)F (A)B A' --simplify-merges -- file
120120
check_result 'M L K G F D B A' --first-parent
121121
check_result 'M L G F B A' --first-parent -- file
122122

@@ -127,7 +127,7 @@ check_result 'M L H' F..M -- file
127127
check_result '(LH)M (B)L (B)H' --parents F..M -- file
128128
check_result 'M L J I H G' F..M --full-history -- file
129129
check_result '(LH)M (K)L (GJ)K (I)J (G)I (G)H (FB)G' F..M --full-history --parents -- file
130-
check_outcome failure '(LH)M (G)H (J)L (I)J (G)I (FB)G' F..M --simplify-merges -- file # drops parent B from G
130+
check_result '(LH)M (G)H (J)L (I)J (G)I (FB)G' F..M --simplify-merges -- file
131131
check_result 'M L K J I H G' F..M --ancestry-path
132132
check_result 'M L J I H G' F..M --ancestry-path -- file
133133
check_result '(LH)M (K)L (GJ)K (I)J (G)I (G)H (FE)G' F..M --ancestry-path --parents -- file

0 commit comments

Comments
 (0)