git-svn: match SVN 1.5 behaviour of info' on unknown item
[alt-git.git] / builtin-merge-base.c
blobb08da516e491e7089b1bb178e9a4b05c2ab36539
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
5 static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
7 struct commit_list *result;
9 result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0);
11 if (!result)
12 return 1;
14 while (result) {
15 printf("%s\n", sha1_to_hex(result->item->object.sha1));
16 if (!show_all)
17 return 0;
18 result = result->next;
21 return 0;
24 static const char merge_base_usage[] =
25 "git merge-base [--all] <commit-id> <commit-id>...";
27 static struct commit *get_commit_reference(const char *arg)
29 unsigned char revkey[20];
30 struct commit *r;
32 if (get_sha1(arg, revkey))
33 die("Not a valid object name %s", arg);
34 r = lookup_commit_reference(revkey);
35 if (!r)
36 die("Not a valid commit name %s", arg);
38 return r;
41 int cmd_merge_base(int argc, const char **argv, const char *prefix)
43 struct commit **rev;
44 int rev_nr = 0;
45 int show_all = 0;
47 git_config(git_default_config, NULL);
49 while (1 < argc && argv[1][0] == '-') {
50 const char *arg = argv[1];
51 if (!strcmp(arg, "-a") || !strcmp(arg, "--all"))
52 show_all = 1;
53 else
54 usage(merge_base_usage);
55 argc--; argv++;
57 if (argc < 3)
58 usage(merge_base_usage);
60 rev = xmalloc((argc - 1) * sizeof(*rev));
62 do {
63 rev[rev_nr++] = get_commit_reference(argv[1]);
64 argc--; argv++;
65 } while (argc > 1);
67 return show_merge_base(rev, rev_nr, show_all);