1#include "builtin.h"2#include "cache.h"3#include "commit.h"4#include "parse-options.h"56static int show_merge_base(struct commit **rev, int rev_nr, int show_all)7{8struct commit_list *result;910result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0);1112if (!result)13return 1;1415while (result) {16printf("%s\n", sha1_to_hex(result->item->object.sha1));17if (!show_all)18return 0;19result = result->next;20}2122return 0;23}2425static 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>"),30NULL31};3233static struct commit *get_commit_reference(const char *arg)34{35unsigned char revkey[20];36struct commit *r;3738if (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);4344return r;45}4647static 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;5253if (reduce)54show_all = 1;5556for (i = count - 1; i >= 0; i--)57commit_list_insert(get_commit_reference(args[i]), &revs);5859result = reduce ? reduce_heads(revs) : get_octopus_merge_bases(revs);6061if (!result)62return 1;6364while (result) {65printf("%s\n", sha1_to_hex(result->item->object.sha1));66if (!show_all)67return 0;68result = result->next;69}7071return 0;72}7374static int handle_is_ancestor(int argc, const char **argv)75{76struct commit *one, *two;7778if (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;84else85return 1;86}8788int 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;9697struct option options[] = {98OPT_BOOL('a', "all", &show_all, N_("output all common ancestors")),99OPT_BOOL(0, "octopus", &octopus, N_("find ancestors for a single n-way merge")),100OPT_BOOL(0, "independent", &reduce, N_("list revs not reachable from others")),101OPT_BOOL(0, "is-ancestor", &is_ancestor,102N_("is the first one ancestor of the other?")),103OPT_END()104};105106git_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");116117if (octopus || reduce)118return handle_octopus(argc, argv, reduce, show_all);119120rev = xmalloc(argc * sizeof(*rev));121while (argc-- > 0)122rev[rev_nr++] = get_commit_reference(*argv++);123return show_merge_base(rev, rev_nr, show_all);124}