@matheusbsbs / matheus-bagliano / commits / aa8f98c1bf
Fork 0

merge-base --octopus to mimic show-branch --merge-base

While show-branch --merge-base does not support more than MAX_REVS revs, git supports more with a different algorithm (v1.6.0-rc0~51^2~13, Introduce get_octopus_merge_bases() in commit.c, 2008-06-27). Expose that functionality. 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 aa8f98c1bfcf162e0bd23d20c34857940f2c2256
3 files changed +46 -10
Documentation/git-merge-base.txt
+12 -7
@@ -8,7 +8,7 @@ git-merge-base - Find as good common ancestors as possible for a merge
8
9 SYNOPSIS
10 --------
11 -'git merge-base' [-a|--all] <commit> <commit>...
11 +'git merge-base' [-a|--all] [--octopus] <commit> <commit>...
12
13 DESCRIPTION
14 -----------
@@ -20,12 +20,12 @@ that does not have any better common ancestor is a 'best common
20 ancestor', i.e. a 'merge base'. Note that there can be more than one
21 merge base for a pair of commits.
22
23 -Among the two commits to compute the merge base from, one is specified by
24 -the first commit argument on the command line; the other commit is a
25 -(possibly hypothetical) commit that is a merge across all the remaining
26 -commits on the command line. As the most common special case, specifying only
27 -two commits on the command line means computing the merge base between
28 -the given two commits.
23 +Unless `--octopus` is given, among the two commits to compute the merge
24 +base from, one is specified by the first commit argument on the command
25 +line; the other commit is a (possibly hypothetical) commit that is a merge
26 +across all the remaining commits on the command line. As the most common
27 +special case, specifying only two commits on the command line means
28 +computing the merge base between the given two commits.
29
30 As a consequence, the 'merge base' is not necessarily contained in each of the
31 commit arguments if more than two commits are specified. This is different
@@ -37,6 +37,11 @@ OPTIONS
37 --all::
38 Output all merge bases for the commits, instead of just one.
39
40 +--octopus::
41 + Compute the best common ancestors of all supplied commits,
42 + in preparation for an n-way merge. This mimics the behavior
43 + of 'git show-branch --merge-base'.
44 +
45 DISCUSSION
46 ----------
47
builtin/merge-base.c
+32 -3
@@ -23,7 +23,7 @@ static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
23 }
24
25 static const char * const merge_base_usage[] = {
26 - "git merge-base [-a|--all] <commit> <commit>...",
26 + "git merge-base [-a|--all] [--octopus] <commit> <commit>...",
27 NULL
28 };
29
@@ -41,21 +41,50 @@ static struct commit *get_commit_reference(const char *arg)
41 return r;
42 }
43
44 +static int show_octopus_merge_bases(int count, const char **args, int show_all)
45 +{
46 + struct commit_list *revs = NULL;
47 + struct commit_list *result;
48 + int i;
49 +
50 + for (i = count - 1; i >= 0; i++)
51 + commit_list_insert(get_commit_reference(args[i]), &revs);
52 + result = get_octopus_merge_bases(revs);
53 +
54 + if (!result)
55 + return 1;
56 +
57 + while (result) {
58 + printf("%s\n", sha1_to_hex(result->item->object.sha1));
59 + if (!show_all)
60 + return 0;
61 + result = result->next;
62 + }
63 +
64 + return 0;
65 +}
66 +
67 int cmd_merge_base(int argc, const char **argv, const char *prefix)
68 {
69 struct commit **rev;
70 int rev_nr = 0;
71 int show_all = 0;
72 + int octopus = 0;
73
74 struct option options[] = {
51 - OPT_BOOLEAN('a', "all", &show_all, "outputs all common ancestors"),
75 + OPT_BOOLEAN('a', "all", &show_all, "output all common ancestors"),
76 + OPT_BOOLEAN(0, "octopus", &octopus, "find ancestors for a single n-way merge"),
77 OPT_END()
78 };
79
80 git_config(git_default_config, NULL);
81 argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
57 - if (argc < 2)
82 + if (!octopus && argc < 2)
83 usage_with_options(merge_base_usage, options);
84 +
85 + if (octopus)
86 + return show_octopus_merge_bases(argc, argv, show_all);
87 +
88 rev = xmalloc(argc * sizeof(*rev));
89 while (argc-- > 0)
90 rev[rev_nr++] = get_commit_reference(*argv++);
t/t6010-merge-base.sh
+2
@@ -164,9 +164,11 @@ test_expect_success 'merge-base A B C' '
164 git rev-parse --verify MMR >expected.sb &&
165
166 git merge-base --all MMA MMB MMC >actual &&
167 + git merge-base --all --octopus MMA MMB MMC >actual.common &&
168 git show-branch --merge-base MMA MMB MMC >actual.sb &&
169
170 test_cmp expected actual &&
171 + test_cmp expected.sb actual.common &&
172 test_cmp expected.sb actual.sb
173 '
174