Skip to content

Commit 743474c

Browse files
newrengitster
authored andcommitted
merge-recursive: provide a better label for diff3 common ancestor
In commit 7ca56aa ("merge-recursive: add a label for ancestor", 2010-03-20), a label was added for the '||||||' line to make it have the more informative heading '|||||| merged common ancestors', with the statement: It would be nicer to use a more informative label. Perhaps someone will provide one some day. This chosen label was perfectly reasonable when recursiveness kicks in, i.e. when there are multiple merge bases. (I can't think of a better label in such cases.) But it is actually somewhat misleading when there is a unique merge base or no merge base. Change this based on the number of merge bases: >=2: "merged common ancestors" 1: <abbreviated commit hash> 0: "<empty tree>" Tests have also been added to check that we get the right ancestor name for each of the three cases. Also, since merge_recursive() and merge_trees() have polar opposite pre-conditions for opt->ancestor, document merge_recursive()'s pre-condition with an assertion. (An assertion was added to merge_trees() already a few commits ago.) The differences in pre-conditions stem from two factors: (1) merge_trees() does not recurse and thus does not have multiple sub-merges to worry about -- each of which would require a different value for opt->ancestor, (2) merge_trees() is only passed trees rather than commits and thus cannot internally guess as good of a label. Thus, while external callers of merge_trees() are required to provide a non-NULL opt->ancestor, merge_recursive() expects to set this value itself. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 139ef37 commit 743474c

File tree

3 files changed

+209
-4
lines changed

3 files changed

+209
-4
lines changed

merge-recursive.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3507,6 +3507,11 @@ int merge_recursive(struct merge_options *opt,
35073507
struct commit *merged_common_ancestors;
35083508
struct tree *mrtree;
35093509
int clean;
3510+
const char *ancestor_name;
3511+
struct strbuf merge_base_abbrev = STRBUF_INIT;
3512+
3513+
if (!opt->call_depth)
3514+
assert(opt->ancestor == NULL);
35103515

35113516
if (show(opt, 4)) {
35123517
output(opt, 4, _("Merging:"));
@@ -3535,6 +3540,14 @@ int merge_recursive(struct merge_options *opt,
35353540

35363541
tree = lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);
35373542
merged_common_ancestors = make_virtual_commit(opt->repo, tree, "ancestor");
3543+
ancestor_name = "empty tree";
3544+
} else if (ca) {
3545+
ancestor_name = "merged common ancestors";
3546+
} else {
3547+
strbuf_add_unique_abbrev(&merge_base_abbrev,
3548+
&merged_common_ancestors->object.oid,
3549+
DEFAULT_ABBREV);
3550+
ancestor_name = merge_base_abbrev.buf;
35383551
}
35393552

35403553
for (iter = ca; iter; iter = iter->next) {
@@ -3568,10 +3581,11 @@ int merge_recursive(struct merge_options *opt,
35683581
if (!opt->call_depth)
35693582
repo_read_index(opt->repo);
35703583

3571-
opt->ancestor = "merged common ancestors";
3584+
opt->ancestor = ancestor_name;
35723585
clean = merge_trees(opt, get_commit_tree(h1), get_commit_tree(h2),
35733586
get_commit_tree(merged_common_ancestors),
35743587
&mrtree);
3588+
strbuf_release(&merge_base_abbrev);
35753589
if (clean < 0) {
35763590
flush_output(opt);
35773591
return clean;

t/t6036-recursive-corner-cases.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,6 +1562,7 @@ test_expect_success 'check nested conflicts' '
15621562
cd nested_conflicts &&
15631563
15641564
git clean -f &&
1565+
MASTER=$(git rev-parse --short master) &&
15651566
git checkout L2^0 &&
15661567
15671568
# Merge must fail; there is a conflict
@@ -1582,7 +1583,7 @@ test_expect_success 'check nested conflicts' '
15821583
git cat-file -p R1:a >theirs &&
15831584
test_must_fail git merge-file --diff3 \
15841585
-L "Temporary merge branch 1" \
1585-
-L "merged common ancestors" \
1586+
-L "$MASTER" \
15861587
-L "Temporary merge branch 2" \
15871588
ours \
15881589
base \
@@ -1594,7 +1595,7 @@ test_expect_success 'check nested conflicts' '
15941595
git cat-file -p R1:b >theirs &&
15951596
test_must_fail git merge-file --diff3 \
15961597
-L "Temporary merge branch 1" \
1597-
-L "merged common ancestors" \
1598+
-L "$MASTER" \
15981599
-L "Temporary merge branch 2" \
15991600
ours \
16001601
base \
@@ -1732,6 +1733,7 @@ test_expect_success 'check virtual merge base with nested conflicts' '
17321733
(
17331734
cd virtual_merge_base_has_nested_conflicts &&
17341735
1736+
MASTER=$(git rev-parse --short master) &&
17351737
git checkout L3^0 &&
17361738
17371739
# Merge must fail; there is a conflict
@@ -1760,7 +1762,7 @@ test_expect_success 'check virtual merge base with nested conflicts' '
17601762
cp left merged-once &&
17611763
test_must_fail git merge-file --diff3 \
17621764
-L "Temporary merge branch 1" \
1763-
-L "merged common ancestors" \
1765+
-L "$MASTER" \
17641766
-L "Temporary merge branch 2" \
17651767
merged-once \
17661768
base \

t/t6047-diff3-conflict-markers.sh

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
#!/bin/sh
2+
3+
test_description='recursive merge diff3 style conflict markers'
4+
5+
. ./test-lib.sh
6+
7+
# Setup:
8+
# L1
9+
# \
10+
# ?
11+
# /
12+
# R1
13+
#
14+
# Where:
15+
# L1 and R1 both have a file named 'content' but have no common history
16+
#
17+
18+
test_expect_success 'setup no merge base' '
19+
test_create_repo no_merge_base &&
20+
(
21+
cd no_merge_base &&
22+
23+
git checkout -b L &&
24+
test_commit A content A &&
25+
26+
git checkout --orphan R &&
27+
test_commit B content B
28+
)
29+
'
30+
31+
test_expect_success 'check no merge base' '
32+
(
33+
cd no_merge_base &&
34+
35+
git checkout L^0 &&
36+
37+
test_must_fail git -c merge.conflictstyle=diff3 merge --allow-unrelated-histories -s recursive R^0 &&
38+
39+
grep "|||||| empty tree" content
40+
)
41+
'
42+
43+
# Setup:
44+
# L1
45+
# / \
46+
# master ?
47+
# \ /
48+
# R1
49+
#
50+
# Where:
51+
# L1 and R1 have modified the same file ('content') in conflicting ways
52+
#
53+
54+
test_expect_success 'setup unique merge base' '
55+
test_create_repo unique_merge_base &&
56+
(
57+
cd unique_merge_base &&
58+
59+
test_commit base content "1
60+
2
61+
3
62+
4
63+
5
64+
" &&
65+
66+
git branch L &&
67+
git branch R &&
68+
69+
git checkout L &&
70+
test_commit L content "1
71+
2
72+
3
73+
4
74+
5
75+
7" &&
76+
77+
git checkout R &&
78+
git rm content &&
79+
test_commit R renamed "1
80+
2
81+
3
82+
4
83+
5
84+
six"
85+
)
86+
'
87+
88+
test_expect_success 'check unique merge base' '
89+
(
90+
cd unique_merge_base &&
91+
92+
git checkout L^0 &&
93+
MASTER=$(git rev-parse --short master) &&
94+
95+
test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R^0 &&
96+
97+
grep "|||||| $MASTER:content" renamed
98+
)
99+
'
100+
101+
# Setup:
102+
# L1---L2--L3
103+
# / \ / \
104+
# master X1 ?
105+
# \ / \ /
106+
# R1---R2--R3
107+
#
108+
# Where:
109+
# commits L1 and R1 have modified the same file in non-conflicting ways
110+
# X1 is an auto-generated merge-base used when merging L1 and R1
111+
# commits L2 and R2 are merges of R1 and L1 into L1 and R1, respectively
112+
# commits L3 and R3 both modify 'content' in conflicting ways
113+
#
114+
115+
test_expect_success 'setup multiple merge bases' '
116+
test_create_repo multiple_merge_bases &&
117+
(
118+
cd multiple_merge_bases &&
119+
120+
test_commit initial content "1
121+
2
122+
3
123+
4
124+
5" &&
125+
126+
git branch L &&
127+
git branch R &&
128+
129+
# Create L1
130+
git checkout L &&
131+
test_commit L1 content "0
132+
1
133+
2
134+
3
135+
4
136+
5" &&
137+
138+
# Create R1
139+
git checkout R &&
140+
test_commit R1 content "1
141+
2
142+
3
143+
4
144+
5
145+
6" &&
146+
147+
# Create L2
148+
git checkout L &&
149+
git merge R1 &&
150+
151+
# Create R2
152+
git checkout R &&
153+
git merge L1 &&
154+
155+
# Create L3
156+
git checkout L &&
157+
test_commit L3 content "0
158+
1
159+
2
160+
3
161+
4
162+
5
163+
A" &&
164+
165+
# Create R3
166+
git checkout R &&
167+
git rm content &&
168+
test_commit R3 renamed "0
169+
2
170+
3
171+
4
172+
5
173+
six"
174+
)
175+
'
176+
177+
test_expect_success 'check multiple merge bases' '
178+
(
179+
cd multiple_merge_bases &&
180+
181+
git checkout L^0 &&
182+
183+
test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R^0 &&
184+
185+
grep "|||||| merged common ancestors:content" renamed
186+
)
187+
'
188+
189+
test_done

0 commit comments

Comments
 (0)