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 cmdmode = 0;
9495
struct option options[] = {
96OPT_BOOL('a', "all", &show_all, N_("output all common ancestors")),
97OPT_CMDMODE(0, "octopus", &cmdmode,
98N_("find ancestors for a single n-way merge"), 'o'),
99OPT_CMDMODE(0, "independent", &cmdmode,
100N_("list revs not reachable from others"), 'r'),
101OPT_CMDMODE(0, "is-ancestor", &cmdmode,
102N_("is the first one ancestor of the other?"), 'a'),
103OPT_END()
104};
105106
git_config(git_default_config, NULL);
107argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
108109
if (cmdmode == 'a') {
110if (argc < 2)
111usage_with_options(merge_base_usage, options);
112if (show_all)
113die("--is-ancestor cannot be used with --all");
114return handle_is_ancestor(argc, argv);
115}
116117
if (cmdmode == 'r' && show_all)
118die("--independent cannot be used with --all");
119120
if (cmdmode == 'r' || cmdmode == 'o')
121return handle_octopus(argc, argv, cmdmode == 'r', show_all);
122123
if (argc < 2)
124usage_with_options(merge_base_usage, options);
125126
rev = xmalloc(argc * sizeof(*rev));
127while (argc-- > 0)
128rev[rev_nr++] = get_commit_reference(*argv++);
129return show_merge_base(rev, rev_nr, show_all);
130}