describe.c 12.1 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
	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
			e->path = NULL;
130
		}
131
		e->tag = tag;
132
		e->prio = prio;
133
		e->name_checked = 0;
134
		hashcpy(e->sha1, sha1);
135 136
		free(e->path);
		e->path = xstrdup(path);
L
Linus Torvalds 已提交
137 138 139
	}
}

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

146 147
	/* Reject anything outside refs/tags/ unless --all */
	if (!all && !is_tag)
148 149
		return 0;

150 151 152 153 154
	/* Accept only tags that match the pattern, if given */
	if (pattern && (!is_tag || fnmatch(pattern, path + 10, 0)))
		return 0;

	/* Is it annotated? */
J
Jeff King 已提交
155
	if (!peel_ref(path, peeled)) {
156
		is_annotated = !!hashcmp(sha1, peeled);
157
	} else {
158
		hashcpy(peeled, sha1);
159
		is_annotated = 0;
160
	}
161

162 163 164 165 166
	/*
	 * By default, we only use annotated tags, but with --tags
	 * we fall back to lightweight ones (even without --tags,
	 * we still remember lightweight ones, only to give hints
	 * in an error message).  --all allows any refs to be used.
J
Junio C Hamano 已提交
167
	 */
168 169 170 171
	if (is_annotated)
		prio = 2;
	else if (is_tag)
		prio = 1;
172 173 174
	else
		prio = 0;

175
	add_to_known_names(all ? path + 5 : path + 10, peeled, prio, sha1);
L
Linus Torvalds 已提交
176 177 178
	return 0;
}

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

186 187 188 189 190 191 192 193 194 195 196
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;
}

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
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))
222
				commit_list_insert_by_date(p, list);
223 224 225 226 227 228 229
			p->object.flags |= c->object.flags;
			parents = parents->next;
		}
	}
	return seen_commits;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

S
Shawn O. Pearce 已提交
400
int cmd_describe(int argc, const char **argv, const char *prefix)
L
Linus Torvalds 已提交
401
{
402
	int contains = 0;
403
	struct option options[] = {
404 405
		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")),
G
Greg Price 已提交
406 407
		OPT_BOOLEAN(0, "all",        &all, N_("use any ref")),
		OPT_BOOLEAN(0, "tags",       &tags, N_("use any tag, even unannotated")),
408
		OPT_BOOLEAN(0, "long",       &longformat, N_("always use long format")),
409
		OPT__ABBREV(&abbrev),
410
		OPT_SET_INT(0, "exact-match", &max_candidates,
411
			    N_("only output exact matches"), 0),
412
		OPT_INTEGER(0, "candidates", &max_candidates,
413 414 415
			    N_("consider <n> most recent tags (default: 10)")),
		OPT_STRING(0, "match",       &pattern, N_("pattern"),
			   N_("only consider tags matching <pattern>")),
416
		OPT_BOOLEAN(0, "always",     &always,
417 418 419
			   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 已提交
420
		 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
421 422
		OPT_END(),
	};
L
Linus Torvalds 已提交
423

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

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

434
	save_commit_buffer = 0;
435

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

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

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

465
	if (argc == 0) {
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
		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 已提交
481
		describe("HEAD", 1);
J
Jean Privat 已提交
482
	} else if (dirty) {
483
		die(_("--dirty is incompatible with committishes"));
484 485 486
	} else {
		while (argc-- > 0) {
			describe(*argv++, argc == 0);
J
Junio C Hamano 已提交
487
		}
488
	}
L
Linus Torvalds 已提交
489 490
	return 0;
}