Code

fast-import: don't allow 'ls' of path with empty components
[git.git] / builtin / merge-base.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
4 #include "parse-options.h"
6 static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
7 {
8         struct commit_list *result;
10         result = get_merge_bases_many(rev[0], rev_nr - 1, rev + 1, 0);
12         if (!result)
13                 return 1;
15         while (result) {
16                 printf("%s\n", sha1_to_hex(result->item->object.sha1));
17                 if (!show_all)
18                         return 0;
19                 result = result->next;
20         }
22         return 0;
23 }
25 static const char * const merge_base_usage[] = {
26         "git merge-base [-a|--all] [--octopus] <commit> <commit>...",
27         "git merge-base --independent <commit>...",
28         NULL
29 };
31 static struct commit *get_commit_reference(const char *arg)
32 {
33         unsigned char revkey[20];
34         struct commit *r;
36         if (get_sha1(arg, revkey))
37                 die("Not a valid object name %s", arg);
38         r = lookup_commit_reference(revkey);
39         if (!r)
40                 die("Not a valid commit name %s", arg);
42         return r;
43 }
45 static int handle_octopus(int count, const char **args, int reduce, int show_all)
46 {
47         struct commit_list *revs = NULL;
48         struct commit_list *result;
49         int i;
51         if (reduce)
52                 show_all = 1;
54         for (i = count - 1; i >= 0; i--)
55                 commit_list_insert(get_commit_reference(args[i]), &revs);
57         result = reduce ? reduce_heads(revs) : get_octopus_merge_bases(revs);
59         if (!result)
60                 return 1;
62         while (result) {
63                 printf("%s\n", sha1_to_hex(result->item->object.sha1));
64                 if (!show_all)
65                         return 0;
66                 result = result->next;
67         }
69         return 0;
70 }
72 int cmd_merge_base(int argc, const char **argv, const char *prefix)
73 {
74         struct commit **rev;
75         int rev_nr = 0;
76         int show_all = 0;
77         int octopus = 0;
78         int reduce = 0;
80         struct option options[] = {
81                 OPT_BOOLEAN('a', "all", &show_all, "output all common ancestors"),
82                 OPT_BOOLEAN(0, "octopus", &octopus, "find ancestors for a single n-way merge"),
83                 OPT_BOOLEAN(0, "independent", &reduce, "list revs not reachable from others"),
84                 OPT_END()
85         };
87         git_config(git_default_config, NULL);
88         argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
89         if (!octopus && !reduce && argc < 2)
90                 usage_with_options(merge_base_usage, options);
91         if (reduce && (show_all || octopus))
92                 die("--independent cannot be used with other options");
94         if (octopus || reduce)
95                 return handle_octopus(argc, argv, reduce, show_all);
97         rev = xmalloc(argc * sizeof(*rev));
98         while (argc-- > 0)
99                 rev[rev_nr++] = get_commit_reference(*argv++);
100         return show_merge_base(rev, rev_nr, show_all);