name-rev.c 12.6 KB
Newer Older
1
#include "builtin.h"
J
Johannes Schindelin 已提交
2
#include "cache.h"
3
#include "config.h"
J
Johannes Schindelin 已提交
4 5 6
#include "commit.h"
#include "tag.h"
#include "refs.h"
7
#include "parse-options.h"
8
#include "sha1-lookup.h"
9
#include "commit-slab.h"
J
Johannes Schindelin 已提交
10

11 12
#define CUTOFF_DATE_SLOP 86400 /* one day */

J
Johannes Schindelin 已提交
13 14
typedef struct rev_name {
	const char *tip_name;
15
	timestamp_t taggerdate;
J
Johannes Schindelin 已提交
16
	int generation;
17
	int distance;
18
	int from_tag;
J
Johannes Schindelin 已提交
19 20
} rev_name;

21 22
define_commit_slab(commit_rev_name, struct rev_name *);

23
static timestamp_t cutoff = TIME_MAX;
24
static struct commit_rev_name rev_names;
J
Johannes Schindelin 已提交
25

26 27 28
/* How many generations are maximally preferred over _one_ merge traversal? */
#define MERGE_TRAVERSAL_WEIGHT 65535

29 30 31 32 33 34 35 36 37 38 39 40
static struct rev_name *get_commit_rev_name(struct commit *commit)
{
	struct rev_name **slot = commit_rev_name_peek(&rev_names, commit);

	return slot ? *slot : NULL;
}

static void set_commit_rev_name(struct commit *commit, struct rev_name *name)
{
	*commit_rev_name_at(&rev_names, commit) = name;
}

41 42
static int is_better_name(struct rev_name *name,
			  const char *tip_name,
43
			  timestamp_t taggerdate,
44
			  int generation,
45 46
			  int distance,
			  int from_tag)
47
{
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	/*
	 * When comparing names based on tags, prefer names
	 * based on the older tag, even if it is farther away.
	 */
	if (from_tag && name->from_tag)
		return (name->taggerdate > taggerdate ||
			(name->taggerdate == taggerdate &&
			 name->distance > distance));

	/*
	 * We know that at least one of them is a non-tag at this point.
	 * favor a tag over a non-tag.
	 */
	if (name->from_tag != from_tag)
		return from_tag;

	/*
	 * We are now looking at two non-tags.  Tiebreak to favor
	 * shorter hops.
	 */
	if (name->distance != distance)
		return name->distance > distance;

	/* ... or tiebreak to favor older date */
	if (name->taggerdate != taggerdate)
		return name->taggerdate > taggerdate;

	/* keep the current one if we cannot decide */
	return 0;
77 78
}

J
Johannes Schindelin 已提交
79
static void name_rev(struct commit *commit,
80
		const char *tip_name, timestamp_t taggerdate,
81
		int generation, int distance, int from_tag,
J
Johannes Schindelin 已提交
82 83
		int deref)
{
84
	struct rev_name *name = get_commit_rev_name(commit);
J
Johannes Schindelin 已提交
85
	struct commit_list *parents;
J
Junio C Hamano 已提交
86
	int parent_number = 1;
87
	char *to_free = NULL;
J
Johannes Schindelin 已提交
88

89
	parse_commit(commit);
J
Johannes Schindelin 已提交
90 91 92 93 94

	if (commit->date < cutoff)
		return;

	if (deref) {
95
		tip_name = to_free = xstrfmt("%s^0", tip_name);
J
Johannes Schindelin 已提交
96 97 98 99 100 101 102

		if (generation)
			die("generation: %d, but deref?", generation);
	}

	if (name == NULL) {
		name = xmalloc(sizeof(rev_name));
103
		set_commit_rev_name(commit, name);
J
Johannes Schindelin 已提交
104
		goto copy_data;
105
	} else if (is_better_name(name, tip_name, taggerdate,
106
				  generation, distance, from_tag)) {
J
Johannes Schindelin 已提交
107 108
copy_data:
		name->tip_name = tip_name;
109
		name->taggerdate = taggerdate;
J
Johannes Schindelin 已提交
110
		name->generation = generation;
111
		name->distance = distance;
112
		name->from_tag = from_tag;
113 114
	} else {
		free(to_free);
J
Johannes Schindelin 已提交
115
		return;
116
	}
J
Johannes Schindelin 已提交
117 118 119 120

	for (parents = commit->parents;
			parents;
			parents = parents->next, parent_number++) {
J
Junio C Hamano 已提交
121
		if (parent_number > 1) {
122
			size_t len;
123
			char *new_name;
J
Johannes Schindelin 已提交
124

125
			strip_suffix(tip_name, "^0", &len);
J
Johannes Schindelin 已提交
126
			if (generation > 0)
127
				new_name = xstrfmt("%.*s~%d^%d", (int)len, tip_name,
128
						   generation, parent_number);
J
Johannes Schindelin 已提交
129
			else
130
				new_name = xstrfmt("%.*s^%d", (int)len, tip_name,
131
						   parent_number);
J
Johannes Schindelin 已提交
132

133
			name_rev(parents->item, new_name, taggerdate, 0,
134 135
				 distance + MERGE_TRAVERSAL_WEIGHT,
				 from_tag, 0);
J
Johannes Schindelin 已提交
136
		} else {
137
			name_rev(parents->item, tip_name, taggerdate,
138 139
				 generation + 1, distance + 1,
				 from_tag, 0);
J
Johannes Schindelin 已提交
140 141 142 143
		}
	}
}

144 145 146 147 148
static int subpath_matches(const char *path, const char *filter)
{
	const char *subpath = path;

	while (subpath) {
149
		if (!wildmatch(filter, subpath, 0))
150 151 152 153 154 155 156 157
			return subpath - path;
		subpath = strchr(subpath, '/');
		if (subpath)
			subpath++;
	}
	return -1;
}

158 159 160 161
static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
{
	if (shorten_unambiguous)
		refname = shorten_unambiguous_ref(refname, 0);
162
	else if (starts_with(refname, "refs/heads/"))
163
		refname = refname + 11;
164
	else if (starts_with(refname, "refs/"))
165 166 167 168
		refname = refname + 5;
	return refname;
}

169 170
struct name_ref_data {
	int tags_only;
171
	int name_only;
172
	struct string_list ref_filters;
173
	struct string_list exclude_filters;
174 175
};

176 177
static struct tip_table {
	struct tip_table_entry {
178
		struct object_id oid;
179 180 181 182 183 184 185
		const char *refname;
	} *table;
	int nr;
	int alloc;
	int sorted;
} tip_table;

186
static void add_to_tip_table(const struct object_id *oid, const char *refname,
187 188 189 190 191
			     int shorten_unambiguous)
{
	refname = name_ref_abbrev(refname, shorten_unambiguous);

	ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
192
	oidcpy(&tip_table.table[tip_table.nr].oid, oid);
193 194 195 196 197 198 199 200
	tip_table.table[tip_table.nr].refname = xstrdup(refname);
	tip_table.nr++;
	tip_table.sorted = 0;
}

static int tipcmp(const void *a_, const void *b_)
{
	const struct tip_table_entry *a = a_, *b = b_;
201
	return oidcmp(&a->oid, &b->oid);
202 203
}

204
static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
J
Johannes Schindelin 已提交
205
{
206
	struct object *o = parse_object(oid);
207
	struct name_ref_data *data = cb_data;
208
	int can_abbreviate_output = data->tags_only && data->name_only;
J
Johannes Schindelin 已提交
209
	int deref = 0;
210
	timestamp_t taggerdate = TIME_MAX;
J
Johannes Schindelin 已提交
211

212
	if (data->tags_only && !starts_with(path, "refs/tags/"))
213 214
		return 0;

215 216 217 218 219 220 221 222 223
	if (data->exclude_filters.nr) {
		struct string_list_item *item;

		for_each_string_list_item(item, &data->exclude_filters) {
			if (subpath_matches(path, item->string) >= 0)
				return 0;
		}
	}

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	if (data->ref_filters.nr) {
		struct string_list_item *item;
		int matched = 0;

		/* See if any of the patterns match. */
		for_each_string_list_item(item, &data->ref_filters) {
			/*
			 * Check all patterns even after finding a match, so
			 * that we can see if a match with a subpath exists.
			 * When a user asked for 'refs/tags/v*' and 'v1.*',
			 * both of which match, the user is showing her
			 * willingness to accept a shortened output by having
			 * the 'v1.*' in the acceptable refnames, so we
			 * shouldn't stop when seeing 'refs/tags/v1.4' matches
			 * 'refs/tags/v*'.  We should show it as 'v1.4'.
			 */
			switch (subpath_matches(path, item->string)) {
			case -1: /* did not match */
				break;
			case 0: /* matched fully */
				matched = 1;
				break;
			default: /* matched subpath */
				matched = 1;
				can_abbreviate_output = 1;
				break;
			}
251
		}
252 253 254 255

		/* If none of the patterns matched, stop now */
		if (!matched)
			return 0;
256
	}
J
Johannes Schindelin 已提交
257

258
	add_to_tip_table(oid, path, can_abbreviate_output);
259

260
	while (o && o->type == OBJ_TAG) {
J
Johannes Schindelin 已提交
261 262 263
		struct tag *t = (struct tag *) o;
		if (!t->tagged)
			break; /* broken repository */
264
		o = parse_object(&t->tagged->oid);
J
Johannes Schindelin 已提交
265
		deref = 1;
266
		taggerdate = t->date;
J
Johannes Schindelin 已提交
267
	}
268
	if (o && o->type == OBJ_COMMIT) {
J
Johannes Schindelin 已提交
269
		struct commit *commit = (struct commit *)o;
270
		int from_tag = starts_with(path, "refs/tags/");
J
Johannes Schindelin 已提交
271

272
		if (taggerdate == TIME_MAX)
273
			taggerdate = ((struct commit *)o)->date;
274
		path = name_ref_abbrev(path, can_abbreviate_output);
275 276
		name_rev(commit, xstrdup(path), taggerdate, 0, 0,
			 from_tag, deref);
J
Johannes Schindelin 已提交
277 278 279 280
	}
	return 0;
}

281 282 283
static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
{
	struct tip_table_entry *table = table_;
284
	return table[ix].oid.hash;
285 286 287 288 289 290 291 292 293 294
}

static const char *get_exact_ref_match(const struct object *o)
{
	int found;

	if (!tip_table.table || !tip_table.nr)
		return NULL;

	if (!tip_table.sorted) {
R
René Scharfe 已提交
295
		QSORT(tip_table.table, tip_table.nr, tipcmp);
296 297 298
		tip_table.sorted = 1;
	}

B
brian m. carlson 已提交
299
	found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr,
300 301 302 303 304 305
			 nth_tip_table_ent);
	if (0 <= found)
		return tip_table.table[found].refname;
	return NULL;
}

306 307
/* may return a constant string or use "buf" as scratch space */
static const char *get_rev_name(const struct object *o, struct strbuf *buf)
J
Johannes Schindelin 已提交
308
{
309 310 311
	struct rev_name *n;
	struct commit *c;

312
	if (o->type != OBJ_COMMIT)
313
		return get_exact_ref_match(o);
314
	c = (struct commit *) o;
315
	n = get_commit_rev_name(c);
J
Johannes Schindelin 已提交
316
	if (!n)
317
		return NULL;
J
Johannes Schindelin 已提交
318 319 320

	if (!n->generation)
		return n->tip_name;
321 322 323 324
	else {
		int len = strlen(n->tip_name);
		if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
			len -= 2;
325 326 327
		strbuf_reset(buf);
		strbuf_addf(buf, "%.*s~%d", len, n->tip_name, n->generation);
		return buf->buf;
328
	}
J
Johannes Schindelin 已提交
329
}
330

331 332 333 334 335
static void show_name(const struct object *obj,
		      const char *caller_name,
		      int always, int allow_undefined, int name_only)
{
	const char *name;
336
	const struct object_id *oid = &obj->oid;
337
	struct strbuf buf = STRBUF_INIT;
338 339

	if (!name_only)
340
		printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
341
	name = get_rev_name(obj, &buf);
342 343 344 345 346
	if (name)
		printf("%s\n", name);
	else if (allow_undefined)
		printf("undefined\n");
	else if (always)
347
		printf("%s\n", find_unique_abbrev(oid, DEFAULT_ABBREV));
348
	else
349
		die("cannot describe '%s'", oid_to_hex(oid));
350
	strbuf_release(&buf);
351 352
}

353
static char const * const name_rev_usage[] = {
354 355 356
	N_("git name-rev [<options>] <commit>..."),
	N_("git name-rev [<options>] --all"),
	N_("git name-rev [<options>] --stdin"),
357 358 359
	NULL
};

360 361
static void name_rev_line(char *p, struct name_ref_data *data)
{
362
	struct strbuf buf = STRBUF_INIT;
363 364 365 366 367 368
	int forty = 0;
	char *p_start;
	for (p_start = p; *p; p++) {
#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
		if (!ishex(*p))
			forty = 0;
369
		else if (++forty == GIT_SHA1_HEXSZ &&
370
			 !ishex(*(p+1))) {
371
			struct object_id oid;
372 373
			const char *name = NULL;
			char c = *(p+1);
374
			int p_len = p - p_start + 1;
375 376 377 378

			forty = 0;

			*(p+1) = 0;
379
			if (!get_oid(p - (GIT_SHA1_HEXSZ - 1), &oid)) {
380
				struct object *o =
381
					lookup_object(oid.hash);
382
				if (o)
383
					name = get_rev_name(o, &buf);
384 385 386 387 388 389
			}
			*(p+1) = c;

			if (!name)
				continue;

390
			if (data->name_only)
391
				printf("%.*s%s", p_len - GIT_SHA1_HEXSZ, p_start, name);
392 393
			else
				printf("%.*s (%s)", p_len, p_start, name);
394 395 396 397 398 399 400
			p_start = p + 1;
		}
	}

	/* flush */
	if (p_start != p)
		fwrite(p_start, p - p_start, 1, stdout);
401 402

	strbuf_release(&buf);
403 404
}

405
int cmd_name_rev(int argc, const char **argv, const char *prefix)
J
Johannes Schindelin 已提交
406
{
407
	struct object_array revs = OBJECT_ARRAY_INIT;
408
	int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
409
	struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
410
	struct option opts[] = {
411 412
		OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
		OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
413
		OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
414
				   N_("only use refs matching <pattern>")),
415 416
		OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
				   N_("ignore refs matching <pattern>")),
417
		OPT_GROUP(""),
418 419 420 421
		OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
		OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
		OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
		OPT_BOOL(0, "always",     &always,
422
			   N_("show abbreviated commit object as fallback")),
423 424 425 426 427 428
		{
			/* A Hidden OPT_BOOL */
			OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
			N_("dereference tags in the input (internal use)"),
			PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
		},
429 430
		OPT_END(),
	};
J
Johannes Schindelin 已提交
431

432
	init_commit_rev_name(&rev_names);
433
	git_config(git_default_config, NULL);
434
	argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
435
	if (all + transform_stdin + !!argc > 1) {
436 437 438 439 440
		error("Specify either a list, or --all, not both!");
		usage_with_options(name_rev_usage, opts);
	}
	if (all || transform_stdin)
		cutoff = 0;
J
Johannes Schindelin 已提交
441

442
	for (; argc; argc--, argv++) {
443
		struct object_id oid;
444
		struct object *object;
J
Johannes Schindelin 已提交
445 446
		struct commit *commit;

447
		if (get_oid(*argv, &oid)) {
J
Johannes Schindelin 已提交
448 449 450 451 452
			fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
					*argv);
			continue;
		}

453
		commit = NULL;
454
		object = parse_object(&oid);
455 456 457 458 459 460 461 462
		if (object) {
			struct object *peeled = deref_tag(object, *argv, 0);
			if (peeled && peeled->type == OBJ_COMMIT)
				commit = (struct commit *)peeled;
		}

		if (!object) {
			fprintf(stderr, "Could not get object for %s. Skipping.\n",
J
Johannes Schindelin 已提交
463 464 465 466
					*argv);
			continue;
		}

467 468 469 470
		if (commit) {
			if (cutoff > commit->date)
				cutoff = commit->date;
		}
471 472 473 474 475 476 477 478 479

		if (peel_tag) {
			if (!commit) {
				fprintf(stderr, "Could not get commit for %s. Skipping.\n",
					*argv);
				continue;
			}
			object = (struct object *)commit;
		}
480
		add_object_array(object, *argv, &revs);
J
Johannes Schindelin 已提交
481 482
	}

483 484
	if (cutoff)
		cutoff = cutoff - CUTOFF_DATE_SLOP;
485
	for_each_ref(name_ref, &data);
J
Johannes Schindelin 已提交
486 487 488 489 490

	if (transform_stdin) {
		char buffer[2048];

		while (!feof(stdin)) {
491
			char *p = fgets(buffer, sizeof(buffer), stdin);
J
Johannes Schindelin 已提交
492 493
			if (!p)
				break;
494
			name_rev_line(p, &data);
J
Johannes Schindelin 已提交
495 496
		}
	} else if (all) {
497
		int i, max;
J
Johannes Schindelin 已提交
498

499
		max = get_max_object_index();
500 501
		for (i = 0; i < max; i++) {
			struct object *obj = get_indexed_object(i);
502
			if (!obj || obj->type != OBJ_COMMIT)
503 504
				continue;
			show_name(obj, NULL,
505
				  always, allow_undefined, data.name_only);
506
		}
507 508
	} else {
		int i;
509 510 511
		for (i = 0; i < revs.nr; i++)
			show_name(revs.objects[i].item, revs.objects[i].name,
				  always, allow_undefined, data.name_only);
512
	}
J
Johannes Schindelin 已提交
513

M
Martin Ågren 已提交
514
	UNLEAK(revs);
J
Johannes Schindelin 已提交
515 516
	return 0;
}