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