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