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