describe.c 11.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2
#include "cache.h"
#include "commit.h"
3
#include "tag.h"
L
Linus Torvalds 已提交
4
#include "refs.h"
S
Shawn O. Pearce 已提交
5
#include "builtin.h"
6
#include "exec_cmd.h"
7
#include "parse-options.h"
J
Jean Privat 已提交
8
#include "diff.h"
9
#include "hash.h"
L
Linus Torvalds 已提交
10

11 12 13
#define SEEN		(1u<<0)
#define MAX_TAGS	(FLAG_BITS - 1)

14
static const char * const describe_usage[] = {
15 16
	N_("git describe [options] <committish>*"),
	N_("git describe [options] --dirty"),
17 18
	NULL
};
L
Linus Torvalds 已提交
19

20
static int debug;	/* Display lots of verbose info */
21 22
static int all;	/* Any valid ref can be used */
static int tags;	/* Allow lightweight tags */
23
static int longformat;
24
static int abbrev = -1; /* unspecified */
25
static int max_candidates = 10;
26
static struct hash_table names;
27
static int have_util;
28
static const char *pattern;
29
static int always;
J
Jean Privat 已提交
30 31 32 33 34 35 36
static const char *dirty;

/* diff-index command arguments to check if working tree is dirty. */
static const char *diff_index_args[] = {
	"diff-index", "--quiet", "HEAD", "--", NULL
};

L
Linus Torvalds 已提交
37

38
struct commit_name {
39 40
	struct commit_name *next;
	unsigned char peeled[20];
41
	struct tag *tag;
42 43
	unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
	unsigned name_checked:1;
44
	unsigned char sha1[20];
45
	const char *path;
46
};
47 48 49
static const char *prio_names[] = {
	"head", "lightweight", "annotated",
};
L
Linus Torvalds 已提交
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
static inline unsigned int hash_sha1(const unsigned char *sha1)
{
	unsigned int hash;
	memcpy(&hash, sha1, sizeof(hash));
	return hash;
}

static inline struct commit_name *find_commit_name(const unsigned char *peeled)
{
	struct commit_name *n = lookup_hash(hash_sha1(peeled), &names);
	while (n && !!hashcmp(peeled, n->peeled))
		n = n->next;
	return n;
}

66
static int set_util(void *chain, void *data)
67 68 69 70 71 72 73 74 75 76
{
	struct commit_name *n;
	for (n = chain; n; n = n->next) {
		struct commit *c = lookup_commit_reference_gently(n->peeled, 1);
		if (c)
			c->util = n;
	}
	return 0;
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
static int replace_name(struct commit_name *e,
			       int prio,
			       const unsigned char *sha1,
			       struct tag **tag)
{
	if (!e || e->prio < prio)
		return 1;

	if (e->prio == 2 && prio == 2) {
		/* Multiple annotated tags point to the same commit.
		 * Select one to keep based upon their tagger date.
		 */
		struct tag *t;

		if (!e->tag) {
			t = lookup_tag(e->sha1);
			if (!t || parse_tag(t))
				return 1;
			e->tag = t;
		}

		t = lookup_tag(sha1);
		if (!t || parse_tag(t))
			return 0;
		*tag = t;

		if (e->tag->date < t->date)
			return 1;
	}

	return 0;
}

110
static void add_to_known_names(const char *path,
111
			       const unsigned char *peeled,
112 113
			       int prio,
			       const unsigned char *sha1)
L
Linus Torvalds 已提交
114
{
115
	struct commit_name *e = find_commit_name(peeled);
116 117
	struct tag *tag = NULL;
	if (replace_name(e, prio, sha1, &tag)) {
118
		if (!e) {
119
			void **pos;
120
			e = xmalloc(sizeof(struct commit_name));
121 122 123 124 125 126 127 128
			hashcpy(e->peeled, peeled);
			pos = insert_hash(hash_sha1(peeled), e, &names);
			if (pos) {
				e->next = *pos;
				*pos = e;
			} else {
				e->next = NULL;
			}
129
		}
130
		e->tag = tag;
131
		e->prio = prio;
132
		e->name_checked = 0;
133
		hashcpy(e->sha1, sha1);
134
		e->path = path;
L
Linus Torvalds 已提交
135 136 137
	}
}

138
static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
L
Linus Torvalds 已提交
139
{
140
	int might_be_tag = !prefixcmp(path, "refs/tags/");
141 142 143
	unsigned char peeled[20];
	int is_tag, prio;

144 145 146
	if (!all && !might_be_tag)
		return 0;

J
Jeff King 已提交
147
	if (!peel_ref(path, peeled)) {
148
		is_tag = !!hashcmp(sha1, peeled);
149
	} else {
150 151
		hashcpy(peeled, sha1);
		is_tag = 0;
152
	}
153

J
Junio C Hamano 已提交
154 155 156 157
	/* If --all, then any refs are used.
	 * If --tags, then any tags are used.
	 * Otherwise only annotated tags are used.
	 */
158
	if (might_be_tag) {
159
		if (is_tag)
160
			prio = 2;
161
		else
162
			prio = 1;
163 164 165

		if (pattern && fnmatch(pattern, path + 10, 0))
			prio = 0;
166 167 168 169
	}
	else
		prio = 0;

170
	if (!all) {
171 172
		if (!prio)
			return 0;
173
	}
174
	add_to_known_names(all ? path + 5 : path + 10, peeled, prio, sha1);
L
Linus Torvalds 已提交
175 176 177
	return 0;
}

178 179
struct possible_tag {
	struct commit_name *name;
180 181
	int depth;
	int found_order;
182
	unsigned flag_within;
183 184
};

185 186 187 188 189 190 191 192 193 194 195
static int compare_pt(const void *a_, const void *b_)
{
	struct possible_tag *a = (struct possible_tag *)a_;
	struct possible_tag *b = (struct possible_tag *)b_;
	if (a->depth != b->depth)
		return a->depth - b->depth;
	if (a->found_order != b->found_order)
		return a->found_order - b->found_order;
	return 0;
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
static unsigned long finish_depth_computation(
	struct commit_list **list,
	struct possible_tag *best)
{
	unsigned long seen_commits = 0;
	while (*list) {
		struct commit *c = pop_commit(list);
		struct commit_list *parents = c->parents;
		seen_commits++;
		if (c->object.flags & best->flag_within) {
			struct commit_list *a = *list;
			while (a) {
				struct commit *i = a->item;
				if (!(i->object.flags & best->flag_within))
					break;
				a = a->next;
			}
			if (!a)
				break;
		} else
			best->depth++;
		while (parents) {
			struct commit *p = parents->item;
			parse_commit(p);
			if (!(p->object.flags & SEEN))
221
				commit_list_insert_by_date(p, list);
222 223 224 225 226 227 228
			p->object.flags |= c->object.flags;
			parents = parents->next;
		}
	}
	return seen_commits;
}

229 230 231 232
static void display_name(struct commit_name *n)
{
	if (n->prio == 2 && !n->tag) {
		n->tag = lookup_tag(n->sha1);
233
		if (!n->tag || parse_tag(n->tag))
234
			die(_("annotated tag %s not available"), n->path);
235 236 237
	}
	if (n->tag && !n->name_checked) {
		if (!n->tag->tag)
238
			die(_("annotated tag %s has no embedded name"), n->path);
239
		if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
240
			warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path);
241
		n->name_checked = 1;
242 243 244 245 246 247
	}

	if (n->tag)
		printf("%s", n->tag->tag);
	else
		printf("%s", n->path);
J
Junio C Hamano 已提交
248 249 250 251 252
}

static void show_suffix(int depth, const unsigned char *sha1)
{
	printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
253 254
}

T
Timo Hirvonen 已提交
255
static void describe(const char *arg, int last_one)
L
Linus Torvalds 已提交
256
{
J
Junio C Hamano 已提交
257
	unsigned char sha1[20];
258
	struct commit *cmit, *gave_up_on = NULL;
L
Linus Torvalds 已提交
259 260
	struct commit_list *list;
	struct commit_name *n;
261
	struct possible_tag all_matches[MAX_TAGS];
262 263
	unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
	unsigned long seen_commits = 0;
264
	unsigned int unannotated_cnt = 0;
L
Linus Torvalds 已提交
265

266
	if (get_sha1(arg, sha1))
267
		die(_("Not a valid object name %s"), arg);
J
Junio C Hamano 已提交
268 269
	cmit = lookup_commit_reference(sha1);
	if (!cmit)
270
		die(_("%s is not a valid '%s' object"), arg, commit_type);
J
Junio C Hamano 已提交
271

272
	n = find_commit_name(cmit->object.sha1);
273
	if (n && (tags || all || n->prio == 2)) {
J
Junio C Hamano 已提交
274 275 276
		/*
		 * Exact match to an existing ref.
		 */
277
		display_name(n);
J
Junio C Hamano 已提交
278
		if (longformat)
279
			show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
J
Jean Privat 已提交
280 281
		if (dirty)
			printf("%s", dirty);
282
		printf("\n");
L
Linus Torvalds 已提交
283 284 285
		return;
	}

286
	if (!max_candidates)
287
		die(_("no tag exactly matches '%s'"), sha1_to_hex(cmit->object.sha1));
288
	if (debug)
289
		fprintf(stderr, _("searching to describe %s\n"), arg);
290

291
	if (!have_util) {
292
		for_each_hash(&names, set_util, NULL);
293 294 295
		have_util = 1;
	}

L
Linus Torvalds 已提交
296
	list = NULL;
297
	cmit->object.flags = SEEN;
L
Linus Torvalds 已提交
298 299
	commit_list_insert(cmit, &list);
	while (list) {
300
		struct commit *c = pop_commit(&list);
301
		struct commit_list *parents = c->parents;
302
		seen_commits++;
303
		n = c->util;
L
Linus Torvalds 已提交
304
		if (n) {
305 306 307
			if (!tags && !all && n->prio < 2) {
				unannotated_cnt++;
			} else if (match_cnt < max_candidates) {
308 309 310 311
				struct possible_tag *t = &all_matches[match_cnt++];
				t->name = n;
				t->depth = seen_commits - 1;
				t->flag_within = 1u << match_cnt;
312
				t->found_order = match_cnt;
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
				c->object.flags |= t->flag_within;
				if (n->prio == 2)
					annotated_cnt++;
			}
			else {
				gave_up_on = c;
				break;
			}
		}
		for (cur_match = 0; cur_match < match_cnt; cur_match++) {
			struct possible_tag *t = &all_matches[cur_match];
			if (!(c->object.flags & t->flag_within))
				t->depth++;
		}
		if (annotated_cnt && !list) {
			if (debug)
329
				fprintf(stderr, _("finished search at %s\n"),
330 331
					sha1_to_hex(c->object.sha1));
			break;
332 333 334 335
		}
		while (parents) {
			struct commit *p = parents->item;
			parse_commit(p);
336
			if (!(p->object.flags & SEEN))
337
				commit_list_insert_by_date(p, &list);
338
			p->object.flags |= c->object.flags;
339
			parents = parents->next;
340 341 342
		}
	}

343 344 345
	if (!match_cnt) {
		const unsigned char *sha1 = cmit->object.sha1;
		if (always) {
J
Jean Privat 已提交
346 347 348 349
			printf("%s", find_unique_abbrev(sha1, abbrev));
			if (dirty)
				printf("%s", dirty);
			printf("\n");
350 351
			return;
		}
352
		if (unannotated_cnt)
353 354
			die(_("No annotated tags can describe '%s'.\n"
			    "However, there were unannotated tags: try --tags."),
355 356
			    sha1_to_hex(sha1));
		else
357 358
			die(_("No tags can describe '%s'.\n"
			    "Try --always, or create some tags."),
359
			    sha1_to_hex(sha1));
360
	}
361

362
	qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
363 364

	if (gave_up_on) {
365
		commit_list_insert_by_date(gave_up_on, &list);
366 367 368 369 370
		seen_commits--;
	}
	seen_commits += finish_depth_computation(&list, &all_matches[0]);
	free_commit_list(list);

371 372 373
	if (debug) {
		for (cur_match = 0; cur_match < match_cnt; cur_match++) {
			struct possible_tag *t = &all_matches[cur_match];
374 375
			fprintf(stderr, " %-11s %8d %s\n",
				prio_names[t->name->prio],
376 377
				t->depth, t->name->path);
		}
378
		fprintf(stderr, _("traversed %lu commits\n"), seen_commits);
379 380
		if (gave_up_on) {
			fprintf(stderr,
381 382
				_("more than %i tags found; listed %i most recent\n"
				"gave up search at %s\n"),
383 384 385
				max_candidates, max_candidates,
				sha1_to_hex(gave_up_on->object.sha1));
		}
386
	}
387 388 389

	display_name(all_matches[0].name);
	if (abbrev)
J
Junio C Hamano 已提交
390
		show_suffix(all_matches[0].depth, cmit->object.sha1);
J
Jean Privat 已提交
391 392
	if (dirty)
		printf("%s", dirty);
393
	printf("\n");
394

395 396
	if (!last_one)
		clear_commit_marks(cmit, -1);
L
Linus Torvalds 已提交
397 398
}

S
Shawn O. Pearce 已提交
399
int cmd_describe(int argc, const char **argv, const char *prefix)
L
Linus Torvalds 已提交
400
{
401
	int contains = 0;
402
	struct option options[] = {
403 404 405 406 407
		OPT_BOOLEAN(0, "contains",   &contains, N_("find the tag that comes after the commit")),
		OPT_BOOLEAN(0, "debug",      &debug, N_("debug search strategy on stderr")),
		OPT_BOOLEAN(0, "all",        &all, N_("use any ref in .git/refs")),
		OPT_BOOLEAN(0, "tags",       &tags, N_("use any tag in .git/refs/tags")),
		OPT_BOOLEAN(0, "long",       &longformat, N_("always use long format")),
408
		OPT__ABBREV(&abbrev),
409
		OPT_SET_INT(0, "exact-match", &max_candidates,
410
			    N_("only output exact matches"), 0),
411
		OPT_INTEGER(0, "candidates", &max_candidates,
412 413 414
			    N_("consider <n> most recent tags (default: 10)")),
		OPT_STRING(0, "match",       &pattern, N_("pattern"),
			   N_("only consider tags matching <pattern>")),
415
		OPT_BOOLEAN(0, "always",     &always,
416 417 418
			   N_("show abbreviated commit object as fallback")),
		{OPTION_STRING, 0, "dirty",  &dirty, N_("mark"),
			   N_("append <mark> on dirty working tree (default: \"-dirty\")"),
J
Jean Privat 已提交
419
		 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
420 421
		OPT_END(),
	};
L
Linus Torvalds 已提交
422

423
	git_config(git_default_config, NULL);
424
	argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
425 426 427
	if (abbrev < 0)
		abbrev = DEFAULT_ABBREV;

428 429
	if (max_candidates < 0)
		max_candidates = 0;
430 431
	else if (max_candidates > MAX_TAGS)
		max_candidates = MAX_TAGS;
J
Junio C Hamano 已提交
432

433
	save_commit_buffer = 0;
434

435
	if (longformat && abbrev == 0)
436
		die(_("--long is incompatible with --abbrev=0"));
437

438
	if (contains) {
439
		const char **args = xmalloc((7 + argc) * sizeof(char *));
440 441 442
		int i = 0;
		args[i++] = "name-rev";
		args[i++] = "--name-only";
443
		args[i++] = "--no-undefined";
444 445
		if (always)
			args[i++] = "--always";
446
		if (!all) {
447
			args[i++] = "--tags";
448 449 450 451 452 453
			if (pattern) {
				char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
				sprintf(s, "--refs=refs/tags/%s", pattern);
				args[i++] = s;
			}
		}
454
		memcpy(args + i, argv, argc * sizeof(char *));
455 456
		args[i + argc] = NULL;
		return cmd_name_rev(i + argc, args, prefix);
457 458
	}

459
	init_hash(&names);
A
Anders Kaseorg 已提交
460
	for_each_rawref(get_name, NULL);
461
	if (!names.nr && !always)
462
		die(_("No names found, cannot describe anything."));
463

464
	if (argc == 0) {
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
		if (dirty) {
			static struct lock_file index_lock;
			int fd;

			read_cache_preload(NULL);
			refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
				      NULL, NULL, NULL);
			fd = hold_locked_index(&index_lock, 0);
			if (0 <= fd)
				update_index_if_able(&the_index, &index_lock);

			if (!cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1,
					    diff_index_args, prefix))
				dirty = NULL;
		}
J
Junio C Hamano 已提交
480
		describe("HEAD", 1);
J
Jean Privat 已提交
481
	} else if (dirty) {
482
		die(_("--dirty is incompatible with committishes"));
483 484 485
	} else {
		while (argc-- > 0) {
			describe(*argv++, argc == 0);
J
Junio C Hamano 已提交
486
		}
487
	}
L
Linus Torvalds 已提交
488 489
	return 0;
}