1#include "builtin.h"
2#include "cache.h"
3#include "config.h"
4#include "commit.h"
5#include "refs.h"
6#include "diff.h"
7#include "revision.h"
8#include "parse-options.h"
9#include "repository.h"
10#include "commit-reach.h"
11
12static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
13{
14 struct commit_list *result, *r;
15
16 result = get_merge_bases_many_dirty(rev[0], rev_nr - 1, rev + 1);
17
18 if (!result)
19 return 1;
20
21 for (r = result; r; r = r->next) {
22 printf("%s\n", oid_to_hex(&r->item->object.oid));
23 if (!show_all)
24 break;
25 }
26
27 free_commit_list(result);
28 return 0;
29}
30
31static const char * const merge_base_usage[] = {
32 N_("git merge-base [-a | --all] <commit> <commit>..."),
33 N_("git merge-base [-a | --all] --octopus <commit>..."),
34 N_("git merge-base --independent <commit>..."),
35 N_("git merge-base --is-ancestor <commit> <commit>"),
36 N_("git merge-base --fork-point <ref> [<commit>]"),
37 NULL
38};
39
40static struct commit *get_commit_reference(const char *arg)
41{
42 struct object_id revkey;
43 struct commit *r;
44
45 if (get_oid(arg, &revkey))
46 die("Not a valid object name %s", arg);
47 r = lookup_commit_reference(the_repository, &revkey);
48 if (!r)
49 die("Not a valid commit name %s", arg);
50
51 return r;
52}
53
54static int handle_independent(int count, const char **args)
55{
56 struct commit_list *revs = NULL, *rev;
57 int i;
58
59 for (i = count - 1; i >= 0; i--)
60 commit_list_insert(get_commit_reference(args[i]), &revs);
61
62 reduce_heads_replace(&revs);
63
64 if (!revs)
65 return 1;
66
67 for (rev = revs; rev; rev = rev->next)
68 printf("%s\n", oid_to_hex(&rev->item->object.oid));
69
70 free_commit_list(revs);
71 return 0;
72}
73
74static int handle_octopus(int count, const char **args, int show_all)
75{
76 struct commit_list *revs = NULL;
77 struct commit_list *result, *rev;
78 int i;
79
80 for (i = count - 1; i >= 0; i--)
81 commit_list_insert(get_commit_reference(args[i]), &revs);
82
83 result = get_octopus_merge_bases(revs);
84 free_commit_list(revs);
85 reduce_heads_replace(&result);
86
87 if (!result)
88 return 1;
89
90 for (rev = result; rev; rev = rev->next) {
91 printf("%s\n", oid_to_hex(&rev->item->object.oid));
92 if (!show_all)
93 break;
94 }
95
96 free_commit_list(result);
97 return 0;
98}
99
100static int handle_is_ancestor(int argc, const char **argv)
101{
102 struct commit *one, *two;
103
104 if (argc != 2)
105 die("--is-ancestor takes exactly two commits");
106 one = get_commit_reference(argv[0]);
107 two = get_commit_reference(argv[1]);
108 if (in_merge_bases(one, two))
109 return 0;
110 else
111 return 1;
112}
113
114struct rev_collect {
115 struct commit **commit;
116 int nr;
117 int alloc;
118 unsigned int initial : 1;
119};
120
121static void add_one_commit(struct object_id *oid, struct rev_collect *revs)
122{
123 struct commit *commit;
124
125 if (is_null_oid(oid))
126 return;
127
128 commit = lookup_commit(the_repository, oid);
129 if (!commit ||
130 (commit->object.flags & TMP_MARK) ||
131 parse_commit(commit))
132 return;
133
134 ALLOC_GROW(revs->commit, revs->nr + 1, revs->alloc);
135 revs->commit[revs->nr++] = commit;
136 commit->object.flags |= TMP_MARK;
137}
138
139static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
140 const char *ident, timestamp_t timestamp,
141 int tz, const char *message, void *cbdata)
142{
143 struct rev_collect *revs = cbdata;
144
145 if (revs->initial) {
146 revs->initial = 0;
147 add_one_commit(ooid, revs);
148 }
149 add_one_commit(noid, revs);
150 return 0;
151}
152
153static int handle_fork_point(int argc, const char **argv)
154{
155 struct object_id oid;
156 char *refname;
157 const char *commitname;
158 struct rev_collect revs;
159 struct commit *derived;
160 struct commit_list *bases;
161 int i, ret = 0;
162
163 switch (dwim_ref(argv[0], strlen(argv[0]), &oid, &refname)) {
164 case 0:
165 die("No such ref: '%s'", argv[0]);
166 case 1:
167 break; /* good */
168 default:
169 die("Ambiguous refname: '%s'", argv[0]);
170 }
171
172 commitname = (argc == 2) ? argv[1] : "HEAD";
173 if (get_oid(commitname, &oid))
174 die("Not a valid object name: '%s'", commitname);
175
176 derived = lookup_commit_reference(the_repository, &oid);
177 memset(&revs, 0, sizeof(revs));
178 revs.initial = 1;
179 for_each_reflog_ent(refname, collect_one_reflog_ent, &revs);
180
181 if (!revs.nr && !get_oid(refname, &oid))
182 add_one_commit(&oid, &revs);
183
184 for (i = 0; i < revs.nr; i++)
185 revs.commit[i]->object.flags &= ~TMP_MARK;
186
187 bases = get_merge_bases_many_dirty(derived, revs.nr, revs.commit);
188
189 /*
190 * There should be one and only one merge base, when we found
191 * a common ancestor among reflog entries.
192 */
193 if (!bases || bases->next) {
194 ret = 1;
195 goto cleanup_return;
196 }
197
198 /* And the found one must be one of the reflog entries */
199 for (i = 0; i < revs.nr; i++)
200 if (&bases->item->object == &revs.commit[i]->object)
201 break; /* found */
202 if (revs.nr <= i) {
203 ret = 1; /* not found */
204 goto cleanup_return;
205 }
206
207 printf("%s\n", oid_to_hex(&bases->item->object.oid));
208
209cleanup_return:
210 free_commit_list(bases);
211 return ret;
212}
213
214int cmd_merge_base(int argc, const char **argv, const char *prefix)
215{
216 struct commit **rev;
217 int rev_nr = 0;
218 int show_all = 0;
219 int cmdmode = 0;
220
221 struct option options[] = {
222 OPT_BOOL('a', "all", &show_all, N_("output all common ancestors")),
223 OPT_CMDMODE(0, "octopus", &cmdmode,
224 N_("find ancestors for a single n-way merge"), 'o'),
225 OPT_CMDMODE(0, "independent", &cmdmode,
226 N_("list revs not reachable from others"), 'r'),
227 OPT_CMDMODE(0, "is-ancestor", &cmdmode,
228 N_("is the first one ancestor of the other?"), 'a'),
229 OPT_CMDMODE(0, "fork-point", &cmdmode,
230 N_("find where <commit> forked from reflog of <ref>"), 'f'),
231 OPT_END()
232 };
233
234 git_config(git_default_config, NULL);
235 argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
236
237 if (cmdmode == 'a') {
238 if (argc < 2)
239 usage_with_options(merge_base_usage, options);
240 if (show_all)
241 die("--is-ancestor cannot be used with --all");
242 return handle_is_ancestor(argc, argv);
243 }
244
245 if (cmdmode == 'r' && show_all)
246 die("--independent cannot be used with --all");
247
248 if (cmdmode == 'o')
249 return handle_octopus(argc, argv, show_all);
250
251 if (cmdmode == 'r')
252 return handle_independent(argc, argv);
253
254 if (cmdmode == 'f') {
255 if (argc < 1 || 2 < argc)
256 usage_with_options(merge_base_usage, options);
257 return handle_fork_point(argc, argv);
258 }
259
260 if (argc < 2)
261 usage_with_options(merge_base_usage, options);
262
263 ALLOC_ARRAY(rev, argc);
264 while (argc-- > 0)
265 rev[rev_nr++] = get_commit_reference(*argv++);
266 return show_merge_base(rev, rev_nr, show_all);
267}