merge-base --independent to print reduced parent list in a merge
While show-branch --independent does not support more than MAX_REVS revs, git internally supports more with a different algorithm. Expose that functionality as "git merge-base --independent". This should help scripts to catch up with builtin merge in supporting dodecapus. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Aug 17, 2010 at 02:01 UTC
a1e0ad78b784fd7c47c7bc2847f4813aca4cebaf
3 files changed
+62
-7
Documentation/git-merge-base.txt
+9
@@ -8,7 +8,9 @@ git-merge-base - Find as good common ancestors as possible for a merge
8
9
SYNOPSIS
10
--------
11
+[verse]
12
'git merge-base' [-a|--all] [--octopus] <commit> <commit>...
13
+'git merge-base' --independent <commit>...
14
15
DESCRIPTION
16
-----------
@@ -42,6 +44,13 @@ OPTIONS
44
in preparation for an n-way merge. This mimics the behavior
45
of 'git show-branch --merge-base'.
46
47
+--independent::
48
+ Instead of printing merge bases, print a minimal subset of
49
+ the supplied commits with the same ancestors. In other words,
50
+ among the commits given, list those which cannot be reached
51
+ from any other. This mimics the behavior of 'git show-branch
52
+ --independent'.
53
+
54
DISCUSSION
55
----------
56
builtin/merge-base.c
+15
-6
@@ -24,6 +24,7 @@ static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
24
25
static const char * const merge_base_usage[] = {
26
"git merge-base [-a|--all] [--octopus] <commit> <commit>...",
27
+ "git merge-base --independent <commit>...",
28
NULL
29
};
30
@@ -41,15 +42,19 @@ static struct commit *get_commit_reference(const char *arg)
42
return r;
43
}
44
44
-static int show_octopus_merge_bases(int count, const char **args, int show_all)
45
+static int handle_octopus(int count, const char **args, int reduce, int show_all)
46
{
47
struct commit_list *revs = NULL;
48
struct commit_list *result;
49
int i;
50
50
- for (i = count - 1; i >= 0; i++)
51
+ if (reduce)
52
+ show_all = 1;
53
+
54
+ for (i = count - 1; i >= 0; i--)
55
commit_list_insert(get_commit_reference(args[i]), &revs);
52
- result = get_octopus_merge_bases(revs);
56
+
57
+ result = reduce ? reduce_heads(revs) : get_octopus_merge_bases(revs);
58
59
if (!result)
60
return 1;
@@ -70,20 +75,24 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
75
int rev_nr = 0;
76
int show_all = 0;
77
int octopus = 0;
78
+ int reduce = 0;
79
80
struct option options[] = {
81
OPT_BOOLEAN('a', "all", &show_all, "output all common ancestors"),
82
OPT_BOOLEAN(0, "octopus", &octopus, "find ancestors for a single n-way merge"),
83
+ OPT_BOOLEAN(0, "independent", &reduce, "list revs not reachable from others"),
84
OPT_END()
85
};
86
87
git_config(git_default_config, NULL);
88
argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
82
- if (!octopus && argc < 2)
89
+ if (!octopus && !reduce && argc < 2)
90
usage_with_options(merge_base_usage, options);
91
+ if (reduce && (show_all || octopus))
92
+ die("--independent cannot be used with other options");
93
85
- if (octopus)
86
- return show_octopus_merge_bases(argc, argv, show_all);
94
+ if (octopus || reduce)
95
+ return handle_octopus(argc, argv, reduce, show_all);
96
97
rev = xmalloc(argc * sizeof(*rev));
98
while (argc-- > 0)
t/t6010-merge-base.sh
+38
-1
@@ -3,7 +3,7 @@
3
# Copyright (c) 2005 Junio C Hamano
4
#
5
6
-test_description='Merge base computation.
6
+test_description='Merge base and parent list computation.
7
'
8
9
. ./test-lib.sh
@@ -75,6 +75,26 @@ test_expect_success 'merge-base G H' '
75
test_cmp expected actual.sb
76
'
77
78
+test_expect_success 'merge-base/show-branch --independent' '
79
+ git name-rev "$H" >expected1 &&
80
+ git name-rev "$H" "$G" >expected2 &&
81
+
82
+ parents=$(git merge-base --independent H) &&
83
+ git name-rev $parents >actual1.mb &&
84
+ parents=$(git merge-base --independent A H G) &&
85
+ git name-rev $parents >actual2.mb &&
86
+
87
+ parents=$(git show-branch --independent H) &&
88
+ git name-rev $parents >actual1.sb &&
89
+ parents=$(git show-branch --independent A H G) &&
90
+ git name-rev $parents >actual2.sb &&
91
+
92
+ test_cmp expected1 actual1.mb &&
93
+ test_cmp expected2 actual2.mb &&
94
+ test_cmp expected1 actual1.sb &&
95
+ test_cmp expected2 actual2.sb
96
+'
97
+
98
test_expect_success 'unsynchronized clocks' '
99
# This test is to demonstrate that relying on timestamps in a distributed
100
# SCM to provide a _consistent_ partial ordering of commits leads to
@@ -125,6 +145,23 @@ test_expect_success 'unsynchronized clocks' '
145
test_cmp expected actual.all
146
'
147
148
+test_expect_success '--independent with unsynchronized clocks' '
149
+ IB=$(doit 0 IB) &&
150
+ I1=$(doit -10 I1 $IB) &&
151
+ I2=$(doit -9 I2 $I1) &&
152
+ I3=$(doit -8 I3 $I2) &&
153
+ I4=$(doit -7 I4 $I3) &&
154
+ I5=$(doit -6 I5 $I4) &&
155
+ I6=$(doit -5 I6 $I5) &&
156
+ I7=$(doit -4 I7 $I6) &&
157
+ I8=$(doit -3 I8 $I7) &&
158
+ IH=$(doit -2 IH $I8) &&
159
+
160
+ echo $IH >expected &&
161
+ git merge-base --independent IB IH >actual &&
162
+ test_cmp expected actual
163
+'
164
+
165
test_expect_success 'merge-base for octopus-step (setup)' '
166
# Another set to demonstrate base between one commit and a merge
167
# in the documentation.