]>
Commit | Line | Data |
---|---|---|
1 | #include "cache.h" | |
2 | #include "lockfile.h" | |
3 | #include "commit.h" | |
4 | #include "tag.h" | |
5 | #include "refs.h" | |
6 | #include "builtin.h" | |
7 | #include "exec_cmd.h" | |
8 | #include "parse-options.h" | |
9 | #include "diff.h" | |
10 | #include "hashmap.h" | |
11 | #include "argv-array.h" | |
12 | ||
13 | #define SEEN (1u << 0) | |
14 | #define MAX_TAGS (FLAG_BITS - 1) | |
15 | ||
16 | static const char * const describe_usage[] = { | |
17 | N_("git describe [<options>] [<commit-ish>...]"), | |
18 | N_("git describe [<options>] --dirty"), | |
19 | NULL | |
20 | }; | |
21 | ||
22 | static int debug; /* Display lots of verbose info */ | |
23 | static int all; /* Any valid ref can be used */ | |
24 | static int tags; /* Allow lightweight tags */ | |
25 | static int longformat; | |
26 | static int first_parent; | |
27 | static int abbrev = -1; /* unspecified */ | |
28 | static int max_candidates = 10; | |
29 | static struct hashmap names; | |
30 | static int have_util; | |
31 | static struct string_list patterns = STRING_LIST_INIT_NODUP; | |
32 | static struct string_list exclude_patterns = STRING_LIST_INIT_NODUP; | |
33 | static int always; | |
34 | static const char *dirty; | |
35 | ||
36 | /* diff-index command arguments to check if working tree is dirty. */ | |
37 | static const char *diff_index_args[] = { | |
38 | "diff-index", "--quiet", "HEAD", "--", NULL | |
39 | }; | |
40 | ||
41 | struct commit_name { | |
42 | struct hashmap_entry entry; | |
43 | struct object_id peeled; | |
44 | struct tag *tag; | |
45 | unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */ | |
46 | unsigned name_checked:1; | |
47 | struct object_id oid; | |
48 | char *path; | |
49 | }; | |
50 | ||
51 | static const char *prio_names[] = { | |
52 | "head", "lightweight", "annotated", | |
53 | }; | |
54 | ||
55 | static int commit_name_cmp(const struct commit_name *cn1, | |
56 | const struct commit_name *cn2, const void *peeled) | |
57 | { | |
58 | return oidcmp(&cn1->peeled, peeled ? peeled : &cn2->peeled); | |
59 | } | |
60 | ||
61 | static inline struct commit_name *find_commit_name(const struct object_id *peeled) | |
62 | { | |
63 | return hashmap_get_from_hash(&names, sha1hash(peeled->hash), peeled->hash); | |
64 | } | |
65 | ||
66 | static int replace_name(struct commit_name *e, | |
67 | int prio, | |
68 | const struct object_id *oid, | |
69 | struct tag **tag) | |
70 | { | |
71 | if (!e || e->prio < prio) | |
72 | return 1; | |
73 | ||
74 | if (e->prio == 2 && prio == 2) { | |
75 | /* Multiple annotated tags point to the same commit. | |
76 | * Select one to keep based upon their tagger date. | |
77 | */ | |
78 | struct tag *t; | |
79 | ||
80 | if (!e->tag) { | |
81 | t = lookup_tag(e->oid.hash); | |
82 | if (!t || parse_tag(t)) | |
83 | return 1; | |
84 | e->tag = t; | |
85 | } | |
86 | ||
87 | t = lookup_tag(oid->hash); | |
88 | if (!t || parse_tag(t)) | |
89 | return 0; | |
90 | *tag = t; | |
91 | ||
92 | if (e->tag->date < t->date) | |
93 | return 1; | |
94 | } | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
99 | static void add_to_known_names(const char *path, | |
100 | const struct object_id *peeled, | |
101 | int prio, | |
102 | const struct object_id *oid) | |
103 | { | |
104 | struct commit_name *e = find_commit_name(peeled); | |
105 | struct tag *tag = NULL; | |
106 | if (replace_name(e, prio, oid, &tag)) { | |
107 | if (!e) { | |
108 | e = xmalloc(sizeof(struct commit_name)); | |
109 | oidcpy(&e->peeled, peeled); | |
110 | hashmap_entry_init(e, sha1hash(peeled->hash)); | |
111 | hashmap_add(&names, e); | |
112 | e->path = NULL; | |
113 | } | |
114 | e->tag = tag; | |
115 | e->prio = prio; | |
116 | e->name_checked = 0; | |
117 | oidcpy(&e->oid, oid); | |
118 | free(e->path); | |
119 | e->path = xstrdup(path); | |
120 | } | |
121 | } | |
122 | ||
123 | static int get_name(const char *path, const struct object_id *oid, int flag, void *cb_data) | |
124 | { | |
125 | int is_tag = starts_with(path, "refs/tags/"); | |
126 | struct object_id peeled; | |
127 | int is_annotated, prio; | |
128 | ||
129 | /* Reject anything outside refs/tags/ unless --all */ | |
130 | if (!all && !is_tag) | |
131 | return 0; | |
132 | ||
133 | /* | |
134 | * If we're given exclude patterns, first exclude any tag which match | |
135 | * any of the exclude pattern. | |
136 | */ | |
137 | if (exclude_patterns.nr) { | |
138 | struct string_list_item *item; | |
139 | ||
140 | if (!is_tag) | |
141 | return 0; | |
142 | ||
143 | for_each_string_list_item(item, &exclude_patterns) { | |
144 | if (!wildmatch(item->string, path + 10, 0, NULL)) | |
145 | return 0; | |
146 | } | |
147 | } | |
148 | ||
149 | /* | |
150 | * If we're given patterns, accept only tags which match at least one | |
151 | * pattern. | |
152 | */ | |
153 | if (patterns.nr) { | |
154 | struct string_list_item *item; | |
155 | ||
156 | if (!is_tag) | |
157 | return 0; | |
158 | ||
159 | for_each_string_list_item(item, &patterns) { | |
160 | if (!wildmatch(item->string, path + 10, 0, NULL)) | |
161 | break; | |
162 | ||
163 | /* If we get here, no pattern matched. */ | |
164 | return 0; | |
165 | } | |
166 | } | |
167 | ||
168 | /* Is it annotated? */ | |
169 | if (!peel_ref(path, peeled.hash)) { | |
170 | is_annotated = !!oidcmp(oid, &peeled); | |
171 | } else { | |
172 | oidcpy(&peeled, oid); | |
173 | is_annotated = 0; | |
174 | } | |
175 | ||
176 | /* | |
177 | * By default, we only use annotated tags, but with --tags | |
178 | * we fall back to lightweight ones (even without --tags, | |
179 | * we still remember lightweight ones, only to give hints | |
180 | * in an error message). --all allows any refs to be used. | |
181 | */ | |
182 | if (is_annotated) | |
183 | prio = 2; | |
184 | else if (is_tag) | |
185 | prio = 1; | |
186 | else | |
187 | prio = 0; | |
188 | ||
189 | add_to_known_names(all ? path + 5 : path + 10, &peeled, prio, oid); | |
190 | return 0; | |
191 | } | |
192 | ||
193 | struct possible_tag { | |
194 | struct commit_name *name; | |
195 | int depth; | |
196 | int found_order; | |
197 | unsigned flag_within; | |
198 | }; | |
199 | ||
200 | static int compare_pt(const void *a_, const void *b_) | |
201 | { | |
202 | struct possible_tag *a = (struct possible_tag *)a_; | |
203 | struct possible_tag *b = (struct possible_tag *)b_; | |
204 | if (a->depth != b->depth) | |
205 | return a->depth - b->depth; | |
206 | if (a->found_order != b->found_order) | |
207 | return a->found_order - b->found_order; | |
208 | return 0; | |
209 | } | |
210 | ||
211 | static unsigned long finish_depth_computation( | |
212 | struct commit_list **list, | |
213 | struct possible_tag *best) | |
214 | { | |
215 | unsigned long seen_commits = 0; | |
216 | while (*list) { | |
217 | struct commit *c = pop_commit(list); | |
218 | struct commit_list *parents = c->parents; | |
219 | seen_commits++; | |
220 | if (c->object.flags & best->flag_within) { | |
221 | struct commit_list *a = *list; | |
222 | while (a) { | |
223 | struct commit *i = a->item; | |
224 | if (!(i->object.flags & best->flag_within)) | |
225 | break; | |
226 | a = a->next; | |
227 | } | |
228 | if (!a) | |
229 | break; | |
230 | } else | |
231 | best->depth++; | |
232 | while (parents) { | |
233 | struct commit *p = parents->item; | |
234 | parse_commit(p); | |
235 | if (!(p->object.flags & SEEN)) | |
236 | commit_list_insert_by_date(p, list); | |
237 | p->object.flags |= c->object.flags; | |
238 | parents = parents->next; | |
239 | } | |
240 | } | |
241 | return seen_commits; | |
242 | } | |
243 | ||
244 | static void display_name(struct commit_name *n) | |
245 | { | |
246 | if (n->prio == 2 && !n->tag) { | |
247 | n->tag = lookup_tag(n->oid.hash); | |
248 | if (!n->tag || parse_tag(n->tag)) | |
249 | die(_("annotated tag %s not available"), n->path); | |
250 | } | |
251 | if (n->tag && !n->name_checked) { | |
252 | if (!n->tag->tag) | |
253 | die(_("annotated tag %s has no embedded name"), n->path); | |
254 | if (strcmp(n->tag->tag, all ? n->path + 5 : n->path)) | |
255 | warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path); | |
256 | n->name_checked = 1; | |
257 | } | |
258 | ||
259 | if (n->tag) | |
260 | printf("%s", n->tag->tag); | |
261 | else | |
262 | printf("%s", n->path); | |
263 | } | |
264 | ||
265 | static void show_suffix(int depth, const struct object_id *oid) | |
266 | { | |
267 | printf("-%d-g%s", depth, find_unique_abbrev(oid->hash, abbrev)); | |
268 | } | |
269 | ||
270 | static void describe(const char *arg, int last_one) | |
271 | { | |
272 | struct object_id oid; | |
273 | struct commit *cmit, *gave_up_on = NULL; | |
274 | struct commit_list *list; | |
275 | struct commit_name *n; | |
276 | struct possible_tag all_matches[MAX_TAGS]; | |
277 | unsigned int match_cnt = 0, annotated_cnt = 0, cur_match; | |
278 | unsigned long seen_commits = 0; | |
279 | unsigned int unannotated_cnt = 0; | |
280 | ||
281 | if (get_oid(arg, &oid)) | |
282 | die(_("Not a valid object name %s"), arg); | |
283 | cmit = lookup_commit_reference(oid.hash); | |
284 | if (!cmit) | |
285 | die(_("%s is not a valid '%s' object"), arg, commit_type); | |
286 | ||
287 | n = find_commit_name(&cmit->object.oid); | |
288 | if (n && (tags || all || n->prio == 2)) { | |
289 | /* | |
290 | * Exact match to an existing ref. | |
291 | */ | |
292 | display_name(n); | |
293 | if (longformat) | |
294 | show_suffix(0, n->tag ? &n->tag->tagged->oid : &oid); | |
295 | if (dirty) | |
296 | printf("%s", dirty); | |
297 | printf("\n"); | |
298 | return; | |
299 | } | |
300 | ||
301 | if (!max_candidates) | |
302 | die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit->object.oid)); | |
303 | if (debug) | |
304 | fprintf(stderr, _("searching to describe %s\n"), arg); | |
305 | ||
306 | if (!have_util) { | |
307 | struct hashmap_iter iter; | |
308 | struct commit *c; | |
309 | struct commit_name *n = hashmap_iter_first(&names, &iter); | |
310 | for (; n; n = hashmap_iter_next(&iter)) { | |
311 | c = lookup_commit_reference_gently(n->peeled.hash, 1); | |
312 | if (c) | |
313 | c->util = n; | |
314 | } | |
315 | have_util = 1; | |
316 | } | |
317 | ||
318 | list = NULL; | |
319 | cmit->object.flags = SEEN; | |
320 | commit_list_insert(cmit, &list); | |
321 | while (list) { | |
322 | struct commit *c = pop_commit(&list); | |
323 | struct commit_list *parents = c->parents; | |
324 | seen_commits++; | |
325 | n = c->util; | |
326 | if (n) { | |
327 | if (!tags && !all && n->prio < 2) { | |
328 | unannotated_cnt++; | |
329 | } else if (match_cnt < max_candidates) { | |
330 | struct possible_tag *t = &all_matches[match_cnt++]; | |
331 | t->name = n; | |
332 | t->depth = seen_commits - 1; | |
333 | t->flag_within = 1u << match_cnt; | |
334 | t->found_order = match_cnt; | |
335 | c->object.flags |= t->flag_within; | |
336 | if (n->prio == 2) | |
337 | annotated_cnt++; | |
338 | } | |
339 | else { | |
340 | gave_up_on = c; | |
341 | break; | |
342 | } | |
343 | } | |
344 | for (cur_match = 0; cur_match < match_cnt; cur_match++) { | |
345 | struct possible_tag *t = &all_matches[cur_match]; | |
346 | if (!(c->object.flags & t->flag_within)) | |
347 | t->depth++; | |
348 | } | |
349 | if (annotated_cnt && !list) { | |
350 | if (debug) | |
351 | fprintf(stderr, _("finished search at %s\n"), | |
352 | oid_to_hex(&c->object.oid)); | |
353 | break; | |
354 | } | |
355 | while (parents) { | |
356 | struct commit *p = parents->item; | |
357 | parse_commit(p); | |
358 | if (!(p->object.flags & SEEN)) | |
359 | commit_list_insert_by_date(p, &list); | |
360 | p->object.flags |= c->object.flags; | |
361 | parents = parents->next; | |
362 | ||
363 | if (first_parent) | |
364 | break; | |
365 | } | |
366 | } | |
367 | ||
368 | if (!match_cnt) { | |
369 | struct object_id *oid = &cmit->object.oid; | |
370 | if (always) { | |
371 | printf("%s", find_unique_abbrev(oid->hash, abbrev)); | |
372 | if (dirty) | |
373 | printf("%s", dirty); | |
374 | printf("\n"); | |
375 | return; | |
376 | } | |
377 | if (unannotated_cnt) | |
378 | die(_("No annotated tags can describe '%s'.\n" | |
379 | "However, there were unannotated tags: try --tags."), | |
380 | oid_to_hex(oid)); | |
381 | else | |
382 | die(_("No tags can describe '%s'.\n" | |
383 | "Try --always, or create some tags."), | |
384 | oid_to_hex(oid)); | |
385 | } | |
386 | ||
387 | QSORT(all_matches, match_cnt, compare_pt); | |
388 | ||
389 | if (gave_up_on) { | |
390 | commit_list_insert_by_date(gave_up_on, &list); | |
391 | seen_commits--; | |
392 | } | |
393 | seen_commits += finish_depth_computation(&list, &all_matches[0]); | |
394 | free_commit_list(list); | |
395 | ||
396 | if (debug) { | |
397 | for (cur_match = 0; cur_match < match_cnt; cur_match++) { | |
398 | struct possible_tag *t = &all_matches[cur_match]; | |
399 | fprintf(stderr, " %-11s %8d %s\n", | |
400 | prio_names[t->name->prio], | |
401 | t->depth, t->name->path); | |
402 | } | |
403 | fprintf(stderr, _("traversed %lu commits\n"), seen_commits); | |
404 | if (gave_up_on) { | |
405 | fprintf(stderr, | |
406 | _("more than %i tags found; listed %i most recent\n" | |
407 | "gave up search at %s\n"), | |
408 | max_candidates, max_candidates, | |
409 | oid_to_hex(&gave_up_on->object.oid)); | |
410 | } | |
411 | } | |
412 | ||
413 | display_name(all_matches[0].name); | |
414 | if (abbrev) | |
415 | show_suffix(all_matches[0].depth, &cmit->object.oid); | |
416 | if (dirty) | |
417 | printf("%s", dirty); | |
418 | printf("\n"); | |
419 | ||
420 | if (!last_one) | |
421 | clear_commit_marks(cmit, -1); | |
422 | } | |
423 | ||
424 | int cmd_describe(int argc, const char **argv, const char *prefix) | |
425 | { | |
426 | int contains = 0; | |
427 | struct option options[] = { | |
428 | OPT_BOOL(0, "contains", &contains, N_("find the tag that comes after the commit")), | |
429 | OPT_BOOL(0, "debug", &debug, N_("debug search strategy on stderr")), | |
430 | OPT_BOOL(0, "all", &all, N_("use any ref")), | |
431 | OPT_BOOL(0, "tags", &tags, N_("use any tag, even unannotated")), | |
432 | OPT_BOOL(0, "long", &longformat, N_("always use long format")), | |
433 | OPT_BOOL(0, "first-parent", &first_parent, N_("only follow first parent")), | |
434 | OPT__ABBREV(&abbrev), | |
435 | OPT_SET_INT(0, "exact-match", &max_candidates, | |
436 | N_("only output exact matches"), 0), | |
437 | OPT_INTEGER(0, "candidates", &max_candidates, | |
438 | N_("consider <n> most recent tags (default: 10)")), | |
439 | OPT_STRING_LIST(0, "match", &patterns, N_("pattern"), | |
440 | N_("only consider tags matching <pattern>")), | |
441 | OPT_STRING_LIST(0, "exclude", &exclude_patterns, N_("pattern"), | |
442 | N_("do not consider tags matching <pattern>")), | |
443 | OPT_BOOL(0, "always", &always, | |
444 | N_("show abbreviated commit object as fallback")), | |
445 | {OPTION_STRING, 0, "dirty", &dirty, N_("mark"), | |
446 | N_("append <mark> on dirty working tree (default: \"-dirty\")"), | |
447 | PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"}, | |
448 | OPT_END(), | |
449 | }; | |
450 | ||
451 | git_config(git_default_config, NULL); | |
452 | argc = parse_options(argc, argv, prefix, options, describe_usage, 0); | |
453 | if (abbrev < 0) | |
454 | abbrev = DEFAULT_ABBREV; | |
455 | ||
456 | if (max_candidates < 0) | |
457 | max_candidates = 0; | |
458 | else if (max_candidates > MAX_TAGS) | |
459 | max_candidates = MAX_TAGS; | |
460 | ||
461 | save_commit_buffer = 0; | |
462 | ||
463 | if (longformat && abbrev == 0) | |
464 | die(_("--long is incompatible with --abbrev=0")); | |
465 | ||
466 | if (contains) { | |
467 | struct string_list_item *item; | |
468 | struct argv_array args; | |
469 | ||
470 | argv_array_init(&args); | |
471 | argv_array_pushl(&args, "name-rev", | |
472 | "--peel-tag", "--name-only", "--no-undefined", | |
473 | NULL); | |
474 | if (always) | |
475 | argv_array_push(&args, "--always"); | |
476 | if (!all) { | |
477 | argv_array_push(&args, "--tags"); | |
478 | for_each_string_list_item(item, &patterns) | |
479 | argv_array_pushf(&args, "--refs=refs/tags/%s", item->string); | |
480 | for_each_string_list_item(item, &exclude_patterns) | |
481 | argv_array_pushf(&args, "--exclude=refs/tags/%s", item->string); | |
482 | } | |
483 | if (argc) | |
484 | argv_array_pushv(&args, argv); | |
485 | else | |
486 | argv_array_push(&args, "HEAD"); | |
487 | return cmd_name_rev(args.argc, args.argv, prefix); | |
488 | } | |
489 | ||
490 | hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, 0); | |
491 | for_each_rawref(get_name, NULL); | |
492 | if (!names.size && !always) | |
493 | die(_("No names found, cannot describe anything.")); | |
494 | ||
495 | if (argc == 0) { | |
496 | if (dirty) { | |
497 | static struct lock_file index_lock; | |
498 | int fd; | |
499 | ||
500 | read_cache_preload(NULL); | |
501 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, | |
502 | NULL, NULL, NULL); | |
503 | fd = hold_locked_index(&index_lock, 0); | |
504 | if (0 <= fd) | |
505 | update_index_if_able(&the_index, &index_lock); | |
506 | ||
507 | if (!cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1, | |
508 | diff_index_args, prefix)) | |
509 | dirty = NULL; | |
510 | } | |
511 | describe("HEAD", 1); | |
512 | } else if (dirty) { | |
513 | die(_("--dirty is incompatible with commit-ishes")); | |
514 | } else { | |
515 | while (argc-- > 0) | |
516 | describe(*argv++, argc == 0); | |
517 | } | |
518 | return 0; | |
519 | } |