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

9 10
#define CUTOFF_DATE_SLOP 86400 /* one day */

J
Johannes Schindelin 已提交
11 12
typedef struct rev_name {
	const char *tip_name;
13
	unsigned long taggerdate;
J
Johannes Schindelin 已提交
14
	int generation;
15
	int distance;
J
Johannes Schindelin 已提交
16 17 18 19
} rev_name;

static long cutoff = LONG_MAX;

20 21 22
/* How many generations are maximally preferred over _one_ merge traversal? */
#define MERGE_TRAVERSAL_WEIGHT 65535

23 24 25 26 27 28 29 30 31 32 33
static int is_better_name(struct rev_name *name,
			  const char *tip_name,
			  unsigned long taggerdate,
			  int generation,
			  int distance)
{
	return (name->taggerdate > taggerdate ||
		(name->taggerdate == taggerdate &&
		 name->distance > distance));
}

J
Johannes Schindelin 已提交
34
static void name_rev(struct commit *commit,
35 36
		const char *tip_name, unsigned long taggerdate,
		int generation, int distance,
J
Johannes Schindelin 已提交
37 38
		int deref)
{
39
	struct rev_name *name = (struct rev_name *)commit->util;
J
Johannes Schindelin 已提交
40
	struct commit_list *parents;
J
Junio C Hamano 已提交
41
	int parent_number = 1;
J
Johannes Schindelin 已提交
42

43
	parse_commit(commit);
J
Johannes Schindelin 已提交
44 45 46 47 48

	if (commit->date < cutoff)
		return;

	if (deref) {
49
		tip_name = xstrfmt("%s^0", tip_name);
J
Johannes Schindelin 已提交
50 51 52 53 54 55 56

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

	if (name == NULL) {
		name = xmalloc(sizeof(rev_name));
57
		commit->util = name;
J
Johannes Schindelin 已提交
58
		goto copy_data;
59 60
	} else if (is_better_name(name, tip_name, taggerdate,
				  generation, distance)) {
J
Johannes Schindelin 已提交
61 62
copy_data:
		name->tip_name = tip_name;
63
		name->taggerdate = taggerdate;
J
Johannes Schindelin 已提交
64
		name->generation = generation;
65
		name->distance = distance;
J
Johannes Schindelin 已提交
66 67 68 69 70 71
	} else
		return;

	for (parents = commit->parents;
			parents;
			parents = parents->next, parent_number++) {
J
Junio C Hamano 已提交
72
		if (parent_number > 1) {
73
			size_t len;
74
			char *new_name;
J
Johannes Schindelin 已提交
75

76
			strip_suffix(tip_name, "^0", &len);
J
Johannes Schindelin 已提交
77
			if (generation > 0)
78
				new_name = xstrfmt("%.*s~%d^%d", (int)len, tip_name,
79
						   generation, parent_number);
J
Johannes Schindelin 已提交
80
			else
81
				new_name = xstrfmt("%.*s^%d", (int)len, tip_name,
82
						   parent_number);
J
Johannes Schindelin 已提交
83

84
			name_rev(parents->item, new_name, taggerdate, 0,
85
				distance + MERGE_TRAVERSAL_WEIGHT, 0);
J
Johannes Schindelin 已提交
86
		} else {
87 88
			name_rev(parents->item, tip_name, taggerdate,
				generation + 1, distance + 1, 0);
J
Johannes Schindelin 已提交
89 90 91 92
		}
	}
}

93 94 95 96 97
static int subpath_matches(const char *path, const char *filter)
{
	const char *subpath = path;

	while (subpath) {
98
		if (!wildmatch(filter, subpath, 0, NULL))
99 100 101 102 103 104 105 106
			return subpath - path;
		subpath = strchr(subpath, '/');
		if (subpath)
			subpath++;
	}
	return -1;
}

107 108 109 110
static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
{
	if (shorten_unambiguous)
		refname = shorten_unambiguous_ref(refname, 0);
111
	else if (starts_with(refname, "refs/heads/"))
112
		refname = refname + 11;
113
	else if (starts_with(refname, "refs/"))
114 115 116 117
		refname = refname + 5;
	return refname;
}

118 119
struct name_ref_data {
	int tags_only;
120
	int name_only;
121
	struct string_list ref_filters;
122
	struct string_list exclude_filters;
123 124
};

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
static struct tip_table {
	struct tip_table_entry {
		unsigned char sha1[20];
		const char *refname;
	} *table;
	int nr;
	int alloc;
	int sorted;
} tip_table;

static void add_to_tip_table(const unsigned char *sha1, const char *refname,
			     int shorten_unambiguous)
{
	refname = name_ref_abbrev(refname, shorten_unambiguous);

	ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
	hashcpy(tip_table.table[tip_table.nr].sha1, sha1);
	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_;
	return hashcmp(a->sha1, b->sha1);
}

153
static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
J
Johannes Schindelin 已提交
154
{
155
	struct object *o = parse_object(oid->hash);
156
	struct name_ref_data *data = cb_data;
157
	int can_abbreviate_output = data->tags_only && data->name_only;
J
Johannes Schindelin 已提交
158
	int deref = 0;
159
	unsigned long taggerdate = ULONG_MAX;
J
Johannes Schindelin 已提交
160

161
	if (data->tags_only && !starts_with(path, "refs/tags/"))
162 163
		return 0;

164 165 166 167 168 169 170 171 172
	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;
		}
	}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	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;
			}
200
		}
201 202 203 204

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

207
	add_to_tip_table(oid->hash, path, can_abbreviate_output);
208

209
	while (o && o->type == OBJ_TAG) {
J
Johannes Schindelin 已提交
210 211 212
		struct tag *t = (struct tag *) o;
		if (!t->tagged)
			break; /* broken repository */
B
brian m. carlson 已提交
213
		o = parse_object(t->tagged->oid.hash);
J
Johannes Schindelin 已提交
214
		deref = 1;
215
		taggerdate = t->date;
J
Johannes Schindelin 已提交
216
	}
217
	if (o && o->type == OBJ_COMMIT) {
J
Johannes Schindelin 已提交
218 219
		struct commit *commit = (struct commit *)o;

220
		path = name_ref_abbrev(path, can_abbreviate_output);
221
		name_rev(commit, xstrdup(path), taggerdate, 0, 0, deref);
J
Johannes Schindelin 已提交
222 223 224 225
	}
	return 0;
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239
static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
{
	struct tip_table_entry *table = table_;
	return table[ix].sha1;
}

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 已提交
240
		QSORT(tip_table.table, tip_table.nr, tipcmp);
241 242 243
		tip_table.sorted = 1;
	}

B
brian m. carlson 已提交
244
	found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr,
245 246 247 248 249 250
			 nth_tip_table_ent);
	if (0 <= found)
		return tip_table.table[found].refname;
	return NULL;
}

J
Johannes Schindelin 已提交
251
/* returns a static buffer */
252
static const char *get_rev_name(const struct object *o)
J
Johannes Schindelin 已提交
253 254
{
	static char buffer[1024];
255 256 257
	struct rev_name *n;
	struct commit *c;

258
	if (o->type != OBJ_COMMIT)
259
		return get_exact_ref_match(o);
260 261
	c = (struct commit *) o;
	n = c->util;
J
Johannes Schindelin 已提交
262
	if (!n)
263
		return NULL;
J
Johannes Schindelin 已提交
264 265 266

	if (!n->generation)
		return n->tip_name;
267 268 269 270 271 272 273 274 275
	else {
		int len = strlen(n->tip_name);
		if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
			len -= 2;
		snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
				n->generation);

		return buffer;
	}
J
Johannes Schindelin 已提交
276
}
277

278 279 280 281 282
static void show_name(const struct object *obj,
		      const char *caller_name,
		      int always, int allow_undefined, int name_only)
{
	const char *name;
283
	const struct object_id *oid = &obj->oid;
284 285

	if (!name_only)
286
		printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
287 288 289 290 291 292
	name = get_rev_name(obj);
	if (name)
		printf("%s\n", name);
	else if (allow_undefined)
		printf("undefined\n");
	else if (always)
293
		printf("%s\n", find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
294
	else
295
		die("cannot describe '%s'", oid_to_hex(oid));
296 297
}

298
static char const * const name_rev_usage[] = {
299 300 301
	N_("git name-rev [<options>] <commit>..."),
	N_("git name-rev [<options>] --all"),
	N_("git name-rev [<options>] --stdin"),
302 303 304
	NULL
};

305 306 307 308 309 310 311 312 313 314 315 316 317
static void name_rev_line(char *p, struct name_ref_data *data)
{
	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;
		else if (++forty == 40 &&
			 !ishex(*(p+1))) {
			unsigned char sha1[40];
			const char *name = NULL;
			char c = *(p+1);
318
			int p_len = p - p_start + 1;
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

			forty = 0;

			*(p+1) = 0;
			if (!get_sha1(p - 39, sha1)) {
				struct object *o =
					lookup_object(sha1);
				if (o)
					name = get_rev_name(o);
			}
			*(p+1) = c;

			if (!name)
				continue;

334 335 336 337
			if (data->name_only)
				printf("%.*s%s", p_len - 40, p_start, name);
			else
				printf("%.*s (%s)", p_len, p_start, name);
338 339 340 341 342 343 344 345 346
			p_start = p + 1;
		}
	}

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

347
int cmd_name_rev(int argc, const char **argv, const char *prefix)
J
Johannes Schindelin 已提交
348
{
349
	struct object_array revs = OBJECT_ARRAY_INIT;
350
	int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
351
	struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
352
	struct option opts[] = {
353 354
		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")),
355
		OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
356
				   N_("only use refs matching <pattern>")),
357 358
		OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
				   N_("ignore refs matching <pattern>")),
359
		OPT_GROUP(""),
360 361 362 363
		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,
364
			   N_("show abbreviated commit object as fallback")),
365 366 367 368 369 370
		{
			/* 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,
		},
371 372
		OPT_END(),
	};
J
Johannes Schindelin 已提交
373

374
	git_config(git_default_config, NULL);
375
	argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
376
	if (all + transform_stdin + !!argc > 1) {
377 378 379 380 381
		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 已提交
382

383
	for (; argc; argc--, argv++) {
J
Johannes Schindelin 已提交
384
		unsigned char sha1[20];
385
		struct object *object;
J
Johannes Schindelin 已提交
386 387 388 389 390 391 392 393
		struct commit *commit;

		if (get_sha1(*argv, sha1)) {
			fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
					*argv);
			continue;
		}

394 395 396 397 398 399 400 401 402 403
		commit = NULL;
		object = parse_object(sha1);
		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 已提交
404 405 406 407
					*argv);
			continue;
		}

408 409 410 411
		if (commit) {
			if (cutoff > commit->date)
				cutoff = commit->date;
		}
412 413 414 415 416 417 418 419 420

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

424 425
	if (cutoff)
		cutoff = cutoff - CUTOFF_DATE_SLOP;
426
	for_each_ref(name_ref, &data);
J
Johannes Schindelin 已提交
427 428 429 430 431

	if (transform_stdin) {
		char buffer[2048];

		while (!feof(stdin)) {
432
			char *p = fgets(buffer, sizeof(buffer), stdin);
J
Johannes Schindelin 已提交
433 434
			if (!p)
				break;
435
			name_rev_line(p, &data);
J
Johannes Schindelin 已提交
436 437
		}
	} else if (all) {
438
		int i, max;
J
Johannes Schindelin 已提交
439

440
		max = get_max_object_index();
441 442
		for (i = 0; i < max; i++) {
			struct object *obj = get_indexed_object(i);
443
			if (!obj || obj->type != OBJ_COMMIT)
444 445
				continue;
			show_name(obj, NULL,
446
				  always, allow_undefined, data.name_only);
447
		}
448 449
	} else {
		int i;
450 451 452
		for (i = 0; i < revs.nr; i++)
			show_name(revs.objects[i].item, revs.objects[i].name,
				  always, allow_undefined, data.name_only);
453
	}
J
Johannes Schindelin 已提交
454 455 456

	return 0;
}