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