diff-ni: allow running from a subdirectory.
[git.git] / builtin-merge-base.c
blobe35d362f2697ebc7e627f230283aedb8ed92857f
1 #include "cache.h"
2 #include "commit.h"
4 static int show_merge_base(struct commit *rev1, struct commit *rev2, int show_all)
6 struct commit_list *result = get_merge_bases(rev1, rev2, 0);
8 if (!result)
9 return 1;
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;
18 return 0;
21 static const char merge_base_usage[] =
22 "git-merge-base [--all] <commit-id> <commit-id>";
24 int cmd_merge_base(int argc, const char **argv, const char *prefix)
26 struct commit *rev1, *rev2;
27 unsigned char rev1key[20], rev2key[20];
28 int show_all = 0;
30 git_config(git_default_config);
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++;
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);