git p4: test sync/clone --branch behavior
[git/mjg.git] / builtin / merge-base.c
blob1bc79910481e6ee04b470620da5609c1c6fbcac4
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)
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;
22 return 0;
25 static const char * const merge_base_usage[] = {
26 N_("git merge-base [-a|--all] <commit> <commit>..."),
27 N_("git merge-base [-a|--all] --octopus <commit>..."),
28 N_("git merge-base --independent <commit>..."),
29 N_("git merge-base --is-ancestor <commit> <commit>"),
30 NULL
33 static struct commit *get_commit_reference(const char *arg)
35 unsigned char revkey[20];
36 struct commit *r;
38 if (get_sha1(arg, revkey))
39 die("Not a valid object name %s", arg);
40 r = lookup_commit_reference(revkey);
41 if (!r)
42 die("Not a valid commit name %s", arg);
44 return r;
47 static int handle_octopus(int count, const char **args, int reduce, int show_all)
49 struct commit_list *revs = NULL;
50 struct commit_list *result;
51 int i;
53 if (reduce)
54 show_all = 1;
56 for (i = count - 1; i >= 0; i--)
57 commit_list_insert(get_commit_reference(args[i]), &revs);
59 result = reduce ? reduce_heads(revs) : get_octopus_merge_bases(revs);
61 if (!result)
62 return 1;
64 while (result) {
65 printf("%s\n", sha1_to_hex(result->item->object.sha1));
66 if (!show_all)
67 return 0;
68 result = result->next;
71 return 0;
74 static int handle_is_ancestor(int argc, const char **argv)
76 struct commit *one, *two;
78 if (argc != 2)
79 die("--is-ancestor takes exactly two commits");
80 one = get_commit_reference(argv[0]);
81 two = get_commit_reference(argv[1]);
82 if (in_merge_bases(one, two))
83 return 0;
84 else
85 return 1;
88 int cmd_merge_base(int argc, const char **argv, const char *prefix)
90 struct commit **rev;
91 int rev_nr = 0;
92 int show_all = 0;
93 int octopus = 0;
94 int reduce = 0;
95 int is_ancestor = 0;
97 struct option options[] = {
98 OPT_BOOLEAN('a', "all", &show_all, N_("output all common ancestors")),
99 OPT_BOOLEAN(0, "octopus", &octopus, N_("find ancestors for a single n-way merge")),
100 OPT_BOOLEAN(0, "independent", &reduce, N_("list revs not reachable from others")),
101 OPT_BOOLEAN(0, "is-ancestor", &is_ancestor,
102 N_("is the first one ancestor of the other?")),
103 OPT_END()
106 git_config(git_default_config, NULL);
107 argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
108 if (!octopus && !reduce && argc < 2)
109 usage_with_options(merge_base_usage, options);
110 if (is_ancestor && (show_all | octopus | reduce))
111 die("--is-ancestor cannot be used with other options");
112 if (is_ancestor)
113 return handle_is_ancestor(argc, argv);
114 if (reduce && (show_all || octopus))
115 die("--independent cannot be used with other options");
117 if (octopus || reduce)
118 return handle_octopus(argc, argv, reduce, show_all);
120 rev = xmalloc(argc * sizeof(*rev));
121 while (argc-- > 0)
122 rev[rev_nr++] = get_commit_reference(*argv++);
123 return show_merge_base(rev, rev_nr, show_all);