GIT 1.6.3.1
[git/mingw/4msysgit/kblees.git] / builtin-merge-base.c
blob03fc1c211453f1ed09ee2c6b71d438b0bfbf474f
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
4 #include "parse-options.h"
6 static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
8 struct commit_list *result;
10 result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0);
12 if (!result)
13 return 1;
15 while (result) {
16 printf("%s\n", sha1_to_hex(result->item->object.sha1));
17 if (!show_all)
18 return 0;
19 result = result->next;
22 return 0;
25 static const char * const merge_base_usage[] = {
26 "git merge-base [--all] <commit-id> <commit-id>...",
27 NULL
30 static struct commit *get_commit_reference(const char *arg)
32 unsigned char revkey[20];
33 struct commit *r;
35 if (get_sha1(arg, revkey))
36 die("Not a valid object name %s", arg);
37 r = lookup_commit_reference(revkey);
38 if (!r)
39 die("Not a valid commit name %s", arg);
41 return r;
44 int cmd_merge_base(int argc, const char **argv, const char *prefix)
46 struct commit **rev;
47 int rev_nr = 0;
48 int show_all = 0;
50 struct option options[] = {
51 OPT_BOOLEAN('a', "all", &show_all, "outputs all common ancestors"),
52 OPT_END()
55 git_config(git_default_config, NULL);
56 argc = parse_options(argc, argv, options, merge_base_usage, 0);
57 if (argc < 2)
58 usage_with_options(merge_base_usage, options);
59 rev = xmalloc(argc * sizeof(*rev));
60 while (argc-- > 0)
61 rev[rev_nr++] = get_commit_reference(*argv++);
62 return show_merge_base(rev, rev_nr, show_all);