Make merge-base a built-in.
[git/git.git] / builtin-merge-base.c
CommitLineData
6683463e 1#include "cache.h"
b5039db6 2#include "commit.h"
6683463e 3
71dfbf22 4static int show_merge_base(struct commit *rev1, struct commit *rev2, int show_all)
52cab8a0 5{
c0fa8255 6 struct commit_list *result = get_merge_bases(rev1, rev2, 0);
52cab8a0
JS
7
8 if (!result)
9 return 1;
10
9585e406 11 while (result) {
52cab8a0 12 printf("%s\n", sha1_to_hex(result->item->object.sha1));
9585e406
JH
13 if (!show_all)
14 return 0;
52cab8a0 15 result = result->next;
9585e406 16 }
52cab8a0 17
9585e406 18 return 0;
6683463e
LT
19}
20
9585e406
JH
21static const char merge_base_usage[] =
22"git-merge-base [--all] <commit-id> <commit-id>";
23
71dfbf22 24int cmd_merge_base(int argc, const char **argv, const char *prefix)
6683463e 25{
9585e406 26 struct commit *rev1, *rev2;
b5039db6 27 unsigned char rev1key[20], rev2key[20];
71dfbf22 28 int show_all = 0;
6683463e 29
84a9b58c 30 git_config(git_default_config);
53228a5f 31
9585e406 32 while (1 < argc && argv[1][0] == '-') {
71dfbf22 33 const char *arg = argv[1];
9585e406
JH
34 if (!strcmp(arg, "-a") || !strcmp(arg, "--all"))
35 show_all = 1;
36 else
37 usage(merge_base_usage);
38 argc--; argv++;
39 }
31fff305 40 if (argc != 3)
9585e406 41 usage(merge_base_usage);
31fff305
DL
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]);
9b632be3
LT
46 rev1 = lookup_commit_reference(rev1key);
47 rev2 = lookup_commit_reference(rev2key);
4f7eb2e5
LT
48 if (!rev1 || !rev2)
49 return 1;
71dfbf22 50 return show_merge_base(rev1, rev2, show_all);
6683463e 51}