merge-base.c 6.1 KB
Newer Older
1
#include "builtin.h"
2
#include "cache.h"
3
#include "config.h"
4
#include "commit.h"
5 6 7
#include "refs.h"
#include "diff.h"
#include "revision.h"
8
#include "parse-options.h"
9
#include "repository.h"
10

11
static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
12
{
13
	struct commit_list *result, *r;
14

15
	result = get_merge_bases_many_dirty(rev[0], rev_nr - 1, rev + 1);
16 17 18 19

	if (!result)
		return 1;

20 21
	for (r = result; r; r = r->next) {
		printf("%s\n", oid_to_hex(&r->item->object.oid));
22
		if (!show_all)
23
			break;
24
	}
25

26
	free_commit_list(result);
27
	return 0;
28 29
}

30
static const char * const merge_base_usage[] = {
31 32
	N_("git merge-base [-a | --all] <commit> <commit>..."),
	N_("git merge-base [-a | --all] --octopus <commit>..."),
33
	N_("git merge-base --independent <commit>..."),
J
Junio C Hamano 已提交
34
	N_("git merge-base --is-ancestor <commit> <commit>"),
35
	N_("git merge-base --fork-point <ref> [<commit>]"),
36 37
	NULL
};
38

39 40
static struct commit *get_commit_reference(const char *arg)
{
41
	struct object_id revkey;
42 43
	struct commit *r;

44
	if (get_oid(arg, &revkey))
45
		die("Not a valid object name %s", arg);
46
	r = lookup_commit_reference(the_repository, &revkey);
47 48 49 50 51 52
	if (!r)
		die("Not a valid commit name %s", arg);

	return r;
}

53
static int handle_independent(int count, const char **args)
54
{
55
	struct commit_list *revs = NULL, *rev;
56 57
	int i;

58 59 60
	for (i = count - 1; i >= 0; i--)
		commit_list_insert(get_commit_reference(args[i]), &revs);

M
Martin Ågren 已提交
61
	reduce_heads_replace(&revs);
62 63

	if (!revs)
64 65
		return 1;

66 67 68 69
	for (rev = revs; rev; rev = rev->next)
		printf("%s\n", oid_to_hex(&rev->item->object.oid));

	free_commit_list(revs);
70 71 72 73 74 75
	return 0;
}

static int handle_octopus(int count, const char **args, int show_all)
{
	struct commit_list *revs = NULL;
76
	struct commit_list *result, *rev;
77
	int i;
78 79

	for (i = count - 1; i >= 0; i--)
80
		commit_list_insert(get_commit_reference(args[i]), &revs);
81

M
Martin Ågren 已提交
82 83 84
	result = get_octopus_merge_bases(revs);
	free_commit_list(revs);
	reduce_heads_replace(&result);
85 86 87 88

	if (!result)
		return 1;

89 90
	for (rev = result; rev; rev = rev->next) {
		printf("%s\n", oid_to_hex(&rev->item->object.oid));
91
		if (!show_all)
92
			break;
93 94
	}

95
	free_commit_list(result);
96 97 98
	return 0;
}

J
Junio C Hamano 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112
static int handle_is_ancestor(int argc, const char **argv)
{
	struct commit *one, *two;

	if (argc != 2)
		die("--is-ancestor takes exactly two commits");
	one = get_commit_reference(argv[0]);
	two = get_commit_reference(argv[1]);
	if (in_merge_bases(one, two))
		return 0;
	else
		return 1;
}

113 114 115 116 117 118 119
struct rev_collect {
	struct commit **commit;
	int nr;
	int alloc;
	unsigned int initial : 1;
};

120
static void add_one_commit(struct object_id *oid, struct rev_collect *revs)
121 122 123
{
	struct commit *commit;

124
	if (is_null_oid(oid))
125 126
		return;

127
	commit = lookup_commit(the_repository, oid);
128 129 130 131 132 133 134 135 136 137
	if (!commit ||
	    (commit->object.flags & TMP_MARK) ||
	    parse_commit(commit))
		return;

	ALLOC_GROW(revs->commit, revs->nr + 1, revs->alloc);
	revs->commit[revs->nr++] = commit;
	commit->object.flags |= TMP_MARK;
}

138
static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
139
				  const char *ident, timestamp_t timestamp,
140 141 142 143 144 145
				  int tz, const char *message, void *cbdata)
{
	struct rev_collect *revs = cbdata;

	if (revs->initial) {
		revs->initial = 0;
146
		add_one_commit(ooid, revs);
147
	}
148
	add_one_commit(noid, revs);
149 150 151 152 153
	return 0;
}

static int handle_fork_point(int argc, const char **argv)
{
154
	struct object_id oid;
155 156 157 158 159 160 161
	char *refname;
	const char *commitname;
	struct rev_collect revs;
	struct commit *derived;
	struct commit_list *bases;
	int i, ret = 0;

162
	switch (dwim_ref(argv[0], strlen(argv[0]), &oid, &refname)) {
163 164 165 166 167 168 169 170 171
	case 0:
		die("No such ref: '%s'", argv[0]);
	case 1:
		break; /* good */
	default:
		die("Ambiguous refname: '%s'", argv[0]);
	}

	commitname = (argc == 2) ? argv[1] : "HEAD";
172
	if (get_oid(commitname, &oid))
173 174
		die("Not a valid object name: '%s'", commitname);

175
	derived = lookup_commit_reference(the_repository, &oid);
176 177 178 179
	memset(&revs, 0, sizeof(revs));
	revs.initial = 1;
	for_each_reflog_ent(refname, collect_one_reflog_ent, &revs);

180 181
	if (!revs.nr && !get_oid(refname, &oid))
		add_one_commit(&oid, &revs);
182

183 184 185
	for (i = 0; i < revs.nr; i++)
		revs.commit[i]->object.flags &= ~TMP_MARK;

186
	bases = get_merge_bases_many_dirty(derived, revs.nr, revs.commit);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

	/*
	 * There should be one and only one merge base, when we found
	 * a common ancestor among reflog entries.
	 */
	if (!bases || bases->next) {
		ret = 1;
		goto cleanup_return;
	}

	/* And the found one must be one of the reflog entries */
	for (i = 0; i < revs.nr; i++)
		if (&bases->item->object == &revs.commit[i]->object)
			break; /* found */
	if (revs.nr <= i) {
		ret = 1; /* not found */
		goto cleanup_return;
	}

206
	printf("%s\n", oid_to_hex(&bases->item->object.oid));
207 208 209 210 211 212

cleanup_return:
	free_commit_list(bases);
	return ret;
}

J
Junio C Hamano 已提交
213
int cmd_merge_base(int argc, const char **argv, const char *prefix)
214
{
215 216
	struct commit **rev;
	int rev_nr = 0;
J
Junio C Hamano 已提交
217
	int show_all = 0;
218
	int cmdmode = 0;
219

220
	struct option options[] = {
221
		OPT_BOOL('a', "all", &show_all, N_("output all common ancestors")),
222 223 224 225 226 227
		OPT_CMDMODE(0, "octopus", &cmdmode,
			    N_("find ancestors for a single n-way merge"), 'o'),
		OPT_CMDMODE(0, "independent", &cmdmode,
			    N_("list revs not reachable from others"), 'r'),
		OPT_CMDMODE(0, "is-ancestor", &cmdmode,
			    N_("is the first one ancestor of the other?"), 'a'),
228 229
		OPT_CMDMODE(0, "fork-point", &cmdmode,
			    N_("find where <commit> forked from reflog of <ref>"), 'f'),
230 231
		OPT_END()
	};
232

233
	git_config(git_default_config, NULL);
234
	argc = parse_options(argc, argv, prefix, options, merge_base_usage, 0);
235 236 237 238 239 240

	if (cmdmode == 'a') {
		if (argc < 2)
			usage_with_options(merge_base_usage, options);
		if (show_all)
			die("--is-ancestor cannot be used with --all");
J
Junio C Hamano 已提交
241
		return handle_is_ancestor(argc, argv);
242
	}
243

244 245 246
	if (cmdmode == 'r' && show_all)
		die("--independent cannot be used with --all");

247
	if (cmdmode == 'o')
248
		return handle_octopus(argc, argv, show_all);
249 250

	if (cmdmode == 'r')
251
		return handle_independent(argc, argv);
252

253 254 255 256 257 258
	if (cmdmode == 'f') {
		if (argc < 1 || 2 < argc)
			usage_with_options(merge_base_usage, options);
		return handle_fork_point(argc, argv);
	}

259 260
	if (argc < 2)
		usage_with_options(merge_base_usage, options);
261

J
Jeff King 已提交
262
	ALLOC_ARRAY(rev, argc);
263 264
	while (argc-- > 0)
		rev[rev_nr++] = get_commit_reference(*argv++);
265
	return show_merge_base(rev, rev_nr, show_all);
266
}