Code

fast-import: Cleanup mode setting.
[git.git] / builtin-merge-base.c
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)
6 {
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;
17         }
19         return 0;
20 }
22 static const char merge_base_usage[] =
23 "git merge-base [--all] <commit-id> <commit-id>";
25 static struct commit *get_commit_reference(const char *arg)
26 {
27         unsigned char revkey[20];
28         struct commit *r;
30         if (get_sha1(arg, revkey))
31                 die("Not a valid object name %s", arg);
32         r = lookup_commit_reference(revkey);
33         if (!r)
34                 die("Not a valid commit name %s", arg);
36         return r;
37 }
39 int cmd_merge_base(int argc, const char **argv, const char *prefix)
40 {
41         struct commit *rev1, *rev2;
42         int show_all = 0;
44         git_config(git_default_config, NULL);
46         while (1 < argc && argv[1][0] == '-') {
47                 const char *arg = argv[1];
48                 if (!strcmp(arg, "-a") || !strcmp(arg, "--all"))
49                         show_all = 1;
50                 else
51                         usage(merge_base_usage);
52                 argc--; argv++;
53         }
54         if (argc != 3)
55                 usage(merge_base_usage);
56         rev1 = get_commit_reference(argv[1]);
57         rev2 = get_commit_reference(argv[2]);
59         return show_merge_base(rev1, rev2, show_all);
60 }