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