1#include "builtin.h"
2#include "cache.h"
3#include "commit.h"
4#include "parse-options.h"
56
static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
7{
8struct commit_list *result;
910
result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0);
1112
if (!result)
13return 1;
1415
while (result) {
16printf("%s\n", sha1_to_hex(result->item->object.sha1));
17if (!show_all)
18return 0;
19result = result->next;
20}
2122
return 0;
23}
2425
static const char * const merge_base_usage[] = {
26N_("git merge-base [-a|--all] <commit> <commit>..."),
27N_("git merge-base [-a|--all] --octopus <commit>..."),
28N_("git merge-base --independent <commit>..."),
29N_("git merge-base --is-ancestor <commit> <commit>"),
30NULL
31};
3233
static struct commit *get_commit_reference(const char *arg)
34{
35unsigned char revkey[20];
36struct commit *r;
3738
if (get_sha1(arg, revkey))
39die("Not a valid object name %s", arg);
40r = lookup_commit_reference(revkey);
41if (!r)
42die("Not a valid commit name %s", arg);
4344
return r;
45}
4647
static int handle_octopus(int count, const char **args, int reduce, int show_all)
48{
49struct commit_list *revs = NULL;
50struct commit_list *result;
51int i;
5253
if (reduce)
54show_all = 1;
5556
for (i = count - 1; i >= 0; i--)
57commit_list_insert(get_commit_reference(args[i]), &revs);
5859
result = reduce ? reduce_heads(revs) : get_octopus_merge_bases(revs);
6061
if (!result)
62return 1;
6364
while (result) {
65printf("%s\n", sha1_to_hex(result->item->object.sha1));
66if (!show_all)
67return 0;
68result = result->next;
69}
7071
return 0;
72}
7374
static int handle_is_ancestor(int argc, const char **argv)
75{
76struct commit *one, *two;
7778
if (argc != 2)
79die("--is-ancestor takes exactly two commits");
80one = get_commit_reference(argv[0]);
81two = get_commit_reference(argv[1]);
82if (in_merge_bases(one, two))
83return 0;
84else
85return 1;
86}
8788
int cmd_merge_base(int argc, const char **argv, const char *prefix)
89{
90struct commit **rev;
91int rev_nr = 0;
92int show_all = 0;
93int octopus = 0;
94int reduce = 0;
95int is_ancestor = 0;
9697
struct option options[] = {
98OPT_BOOLEAN('a', "all", &show_all, N_("output all common ancestors")),
99OPT_BOOLEAN(0, "octopus", &octopus, N_("find ancestors for a single n-way merge")),
100OPT_BOOLEAN(0, "independent", &reduce, N_("list revs not reachable from others")),
101OPT_BOOLEAN(0, "is-ancestor", &is_ancestor,
102N_("is the first one ancestor of the other?")),
103OPT_END()
104};
105106
git_config(git_default_config, NULL);
107argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
108if (!octopus && !reduce && argc < 2)
109usage_with_options(merge_base_usage, options);
110if (is_ancestor && (show_all || octopus || reduce))
111die("--is-ancestor cannot be used with other options");
112if (is_ancestor)
113return handle_is_ancestor(argc, argv);
114if (reduce && (show_all || octopus))
115die("--independent cannot be used with other options");
116117
if (octopus || reduce)
118return handle_octopus(argc, argv, reduce, show_all);
119120
rev = xmalloc(argc * sizeof(*rev));
121while (argc-- > 0)
122rev[rev_nr++] = get_commit_reference(*argv++);
123return show_merge_base(rev, rev_nr, show_all);
124}