Skip to content

Commit 2cd8616

Browse files
committed
Merge branch 'bm/merge-base-octopus-dedup' into maint
"git merge-base --octopus" used to leave cleaning up suboptimal result to the caller, but now it does the clean-up itself. * bm/merge-base-octopus-dedup: merge-base --octopus: reduce the result from get_octopus_merge_bases() merge-base: separate "--independent" codepath into its own helper
2 parents 5032098 + 8f29299 commit 2cd8616

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

builtin/merge-base.c

+25-6
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,36 @@ static struct commit *get_commit_reference(const char *arg)
4444
return r;
4545
}
4646

47-
static int handle_octopus(int count, const char **args, int reduce, int show_all)
47+
static int handle_independent(int count, const char **args)
4848
{
4949
struct commit_list *revs = NULL;
5050
struct commit_list *result;
5151
int i;
5252

53-
if (reduce)
54-
show_all = 1;
53+
for (i = count - 1; i >= 0; i--)
54+
commit_list_insert(get_commit_reference(args[i]), &revs);
55+
56+
result = reduce_heads(revs);
57+
if (!result)
58+
return 1;
59+
60+
while (result) {
61+
printf("%s\n", sha1_to_hex(result->item->object.sha1));
62+
result = result->next;
63+
}
64+
return 0;
65+
}
66+
67+
static int handle_octopus(int count, const char **args, int show_all)
68+
{
69+
struct commit_list *revs = NULL;
70+
struct commit_list *result;
71+
int i;
5572

5673
for (i = count - 1; i >= 0; i--)
5774
commit_list_insert(get_commit_reference(args[i]), &revs);
5875

59-
result = reduce ? reduce_heads(revs) : get_octopus_merge_bases(revs);
76+
result = reduce_heads(get_octopus_merge_bases(revs));
6077

6178
if (!result)
6279
return 1;
@@ -114,8 +131,10 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
114131
if (reduce && (show_all || octopus))
115132
die("--independent cannot be used with other options");
116133

117-
if (octopus || reduce)
118-
return handle_octopus(argc, argv, reduce, show_all);
134+
if (octopus)
135+
return handle_octopus(argc, argv, show_all);
136+
else if (reduce)
137+
return handle_independent(argc, argv);
119138

120139
rev = xmalloc(argc * sizeof(*rev));
121140
while (argc-- > 0)

t/t6010-merge-base.sh

+39
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,43 @@ test_expect_success 'criss-cross merge-base for octopus-step' '
230230
test_cmp expected.sorted actual.sorted
231231
'
232232

233+
test_expect_success 'merge-base --octopus --all for complex tree' '
234+
# Best common ancestor for JE, JAA and JDD is JC
235+
# JE
236+
# / |
237+
# / |
238+
# / |
239+
# JAA / |
240+
# |\ / |
241+
# | \ | JDD |
242+
# | \ |/ | |
243+
# | JC JD |
244+
# | | /| |
245+
# | |/ | |
246+
# JA | | |
247+
# |\ /| | |
248+
# X JB | X X
249+
# \ \ | / /
250+
# \__\|/___/
251+
# J
252+
test_commit J &&
253+
test_commit JB &&
254+
git reset --hard J &&
255+
test_commit JC &&
256+
git reset --hard J &&
257+
test_commit JTEMP1 &&
258+
test_merge JA JB &&
259+
test_merge JAA JC &&
260+
git reset --hard J &&
261+
test_commit JTEMP2 &&
262+
test_merge JD JB &&
263+
test_merge JDD JC &&
264+
git reset --hard J &&
265+
test_commit JTEMP3 &&
266+
test_merge JE JC &&
267+
git rev-parse JC >expected &&
268+
git merge-base --all --octopus JAA JDD JE >actual &&
269+
test_cmp expected actual
270+
'
271+
233272
test_done

0 commit comments

Comments
 (0)