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