7 #include "parse-options.h"
9 static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
11 struct commit_list *result;
13 result = get_merge_bases_many_dirty(rev[0], rev_nr - 1, rev + 1);
19 printf("%s\n", oid_to_hex(&result->item->object.oid));
22 result = result->next;
28 static const char * const merge_base_usage[] = {
29 N_("git merge-base [-a | --all] <commit> <commit>..."),
30 N_("git merge-base [-a | --all] --octopus <commit>..."),
31 N_("git merge-base --independent <commit>..."),
32 N_("git merge-base --is-ancestor <commit> <commit>"),
33 N_("git merge-base --fork-point <ref> [<commit>]"),
37 static struct commit *get_commit_reference(const char *arg)
39 unsigned char revkey[20];
42 if (get_sha1(arg, revkey))
43 die("Not a valid object name %s", arg);
44 r = lookup_commit_reference(revkey);
46 die("Not a valid commit name %s", arg);
51 static int handle_independent(int count, const char **args)
53 struct commit_list *revs = NULL;
54 struct commit_list *result;
57 for (i = count - 1; i >= 0; i--)
58 commit_list_insert(get_commit_reference(args[i]), &revs);
60 result = reduce_heads(revs);
65 printf("%s\n", oid_to_hex(&result->item->object.oid));
66 result = result->next;
71 static int handle_octopus(int count, const char **args, int show_all)
73 struct commit_list *revs = NULL;
74 struct commit_list *result;
77 for (i = count - 1; i >= 0; i--)
78 commit_list_insert(get_commit_reference(args[i]), &revs);
80 result = reduce_heads(get_octopus_merge_bases(revs));
86 printf("%s\n", oid_to_hex(&result->item->object.oid));
89 result = result->next;
95 static int handle_is_ancestor(int argc, const char **argv)
97 struct commit *one, *two;
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))
110 struct commit **commit;
113 unsigned int initial : 1;
116 static void add_one_commit(unsigned char *sha1, struct rev_collect *revs)
118 struct commit *commit;
120 if (is_null_sha1(sha1))
123 commit = lookup_commit(sha1);
125 (commit->object.flags & TMP_MARK) ||
126 parse_commit(commit))
129 ALLOC_GROW(revs->commit, revs->nr + 1, revs->alloc);
130 revs->commit[revs->nr++] = commit;
131 commit->object.flags |= TMP_MARK;
134 static int collect_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
135 const char *ident, unsigned long timestamp,
136 int tz, const char *message, void *cbdata)
138 struct rev_collect *revs = cbdata;
142 add_one_commit(osha1, revs);
144 add_one_commit(nsha1, revs);
148 static int handle_fork_point(int argc, const char **argv)
150 unsigned char sha1[20];
152 const char *commitname;
153 struct rev_collect revs;
154 struct commit *derived;
155 struct commit_list *bases;
158 switch (dwim_ref(argv[0], strlen(argv[0]), sha1, &refname)) {
160 die("No such ref: '%s'", argv[0]);
164 die("Ambiguous refname: '%s'", argv[0]);
167 commitname = (argc == 2) ? argv[1] : "HEAD";
168 if (get_sha1(commitname, sha1))
169 die("Not a valid object name: '%s'", commitname);
171 derived = lookup_commit_reference(sha1);
172 memset(&revs, 0, sizeof(revs));
174 for_each_reflog_ent(refname, collect_one_reflog_ent, &revs);
176 if (!revs.nr && !get_sha1(refname, sha1))
177 add_one_commit(sha1, &revs);
179 for (i = 0; i < revs.nr; i++)
180 revs.commit[i]->object.flags &= ~TMP_MARK;
182 bases = get_merge_bases_many_dirty(derived, revs.nr, revs.commit);
185 * There should be one and only one merge base, when we found
186 * a common ancestor among reflog entries.
188 if (!bases || bases->next) {
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)
198 ret = 1; /* not found */
202 printf("%s\n", oid_to_hex(&bases->item->object.oid));
205 free_commit_list(bases);
209 int cmd_merge_base(int argc, const char **argv, const char *prefix)
216 struct option options[] = {
217 OPT_BOOL('a', "all", &show_all, N_("output all common ancestors")),
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'),
224 OPT_CMDMODE(0, "fork-point", &cmdmode,
225 N_("find where <commit> forked from reflog of <ref>"), 'f'),
229 git_config(git_default_config, NULL);
230 argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
232 if (cmdmode == 'a') {
234 usage_with_options(merge_base_usage, options);
236 die("--is-ancestor cannot be used with --all");
237 return handle_is_ancestor(argc, argv);
240 if (cmdmode == 'r' && show_all)
241 die("--independent cannot be used with --all");
244 return handle_octopus(argc, argv, show_all);
247 return handle_independent(argc, argv);
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);
256 usage_with_options(merge_base_usage, options);
258 ALLOC_ARRAY(rev, argc);
260 rev[rev_nr++] = get_commit_reference(*argv++);
261 return show_merge_base(rev, rev_nr, show_all);