Make merge-base a built-in.
[git/git.git] / builtin-merge-base.c
1 #include "cache.h"
2 #include "commit.h"
3
4 static int show_merge_base(struct commit *rev1, struct commit *rev2, int show_all)
5 {
6 struct commit_list *result = get_merge_bases(rev1, rev2, 0);
7
8 if (!result)
9 return 1;
10
11 while (result) {
12 printf("%s\n", sha1_to_hex(result->item->object.sha1));
13 if (!show_all)
14 return 0;
15 result = result->next;
16 }
17
18 return 0;
19 }
20
21 static const char merge_base_usage[] =
22 "git-merge-base [--all] <commit-id> <commit-id>";
23
24 int cmd_merge_base(int argc, const char **argv, const char *prefix)
25 {
26 struct commit *rev1, *rev2;
27 unsigned char rev1key[20], rev2key[20];
28 int show_all = 0;
29
30 git_config(git_default_config);
31
32 while (1 < argc && argv[1][0] == '-') {
33 const char *arg = argv[1];
34 if (!strcmp(arg, "-a") || !strcmp(arg, "--all"))
35 show_all = 1;
36 else
37 usage(merge_base_usage);
38 argc--; argv++;
39 }
40 if (argc != 3)
41 usage(merge_base_usage);
42 if (get_sha1(argv[1], rev1key))
43 die("Not a valid object name %s", argv[1]);
44 if (get_sha1(argv[2], rev2key))
45 die("Not a valid object name %s", argv[2]);
46 rev1 = lookup_commit_reference(rev1key);
47 rev2 = lookup_commit_reference(rev2key);
48 if (!rev1 || !rev2)
49 return 1;
50 return show_merge_base(rev1, rev2, show_all);
51 }