]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/describe.c
describe: don't abort too early when searching tags
[thirdparty/git.git] / builtin / describe.c
1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
2 #include "cache.h"
3 #include "config.h"
4 #include "lockfile.h"
5 #include "commit.h"
6 #include "tag.h"
7 #include "blob.h"
8 #include "refs.h"
9 #include "builtin.h"
10 #include "exec-cmd.h"
11 #include "parse-options.h"
12 #include "revision.h"
13 #include "diff.h"
14 #include "hashmap.h"
15 #include "argv-array.h"
16 #include "run-command.h"
17 #include "object-store.h"
18 #include "list-objects.h"
19 #include "commit-slab.h"
20
21 #define MAX_TAGS (FLAG_BITS - 1)
22
23 define_commit_slab(commit_names, struct commit_name *);
24
25 static const char * const describe_usage[] = {
26 N_("git describe [<options>] [<commit-ish>...]"),
27 N_("git describe [<options>] --dirty"),
28 NULL
29 };
30
31 static int debug; /* Display lots of verbose info */
32 static int all; /* Any valid ref can be used */
33 static int tags; /* Allow lightweight tags */
34 static int longformat;
35 static int first_parent;
36 static int abbrev = -1; /* unspecified */
37 static int max_candidates = 10;
38 static struct hashmap names;
39 static int have_util;
40 static struct string_list patterns = STRING_LIST_INIT_NODUP;
41 static struct string_list exclude_patterns = STRING_LIST_INIT_NODUP;
42 static int always;
43 static const char *suffix, *dirty, *broken;
44 static struct commit_names commit_names;
45
46 /* diff-index command arguments to check if working tree is dirty. */
47 static const char *diff_index_args[] = {
48 "diff-index", "--quiet", "HEAD", "--", NULL
49 };
50
51 struct commit_name {
52 struct hashmap_entry entry;
53 struct object_id peeled;
54 struct tag *tag;
55 unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
56 unsigned name_checked:1;
57 struct object_id oid;
58 char *path;
59 };
60
61 static const char *prio_names[] = {
62 N_("head"), N_("lightweight"), N_("annotated"),
63 };
64
65 static int commit_name_neq(const void *unused_cmp_data,
66 const struct hashmap_entry *eptr,
67 const struct hashmap_entry *entry_or_key,
68 const void *peeled)
69 {
70 const struct commit_name *cn1, *cn2;
71
72 cn1 = container_of(eptr, const struct commit_name, entry);
73 cn2 = container_of(entry_or_key, const struct commit_name, entry);
74
75 return !oideq(&cn1->peeled, peeled ? peeled : &cn2->peeled);
76 }
77
78 static inline struct commit_name *find_commit_name(const struct object_id *peeled)
79 {
80 return hashmap_get_entry_from_hash(&names, oidhash(peeled), peeled,
81 struct commit_name, entry);
82 }
83
84 static int replace_name(struct commit_name *e,
85 int prio,
86 const struct object_id *oid,
87 struct tag **tag)
88 {
89 if (!e || e->prio < prio)
90 return 1;
91
92 if (e->prio == 2 && prio == 2) {
93 /* Multiple annotated tags point to the same commit.
94 * Select one to keep based upon their tagger date.
95 */
96 struct tag *t;
97
98 if (!e->tag) {
99 t = lookup_tag(the_repository, &e->oid);
100 if (!t || parse_tag(t))
101 return 1;
102 e->tag = t;
103 }
104
105 t = lookup_tag(the_repository, oid);
106 if (!t || parse_tag(t))
107 return 0;
108 *tag = t;
109
110 if (e->tag->date < t->date)
111 return 1;
112 }
113
114 return 0;
115 }
116
117 static void add_to_known_names(const char *path,
118 const struct object_id *peeled,
119 int prio,
120 const struct object_id *oid)
121 {
122 struct commit_name *e = find_commit_name(peeled);
123 struct tag *tag = NULL;
124 if (replace_name(e, prio, oid, &tag)) {
125 if (!e) {
126 e = xmalloc(sizeof(struct commit_name));
127 oidcpy(&e->peeled, peeled);
128 hashmap_entry_init(&e->entry, oidhash(peeled));
129 hashmap_add(&names, &e->entry);
130 e->path = NULL;
131 }
132 e->tag = tag;
133 e->prio = prio;
134 e->name_checked = 0;
135 oidcpy(&e->oid, oid);
136 free(e->path);
137 e->path = xstrdup(path);
138 }
139 }
140
141 static int get_name(const char *path, const struct object_id *oid, int flag, void *cb_data)
142 {
143 int is_tag = 0;
144 struct object_id peeled;
145 int is_annotated, prio;
146 const char *path_to_match = NULL;
147
148 if (skip_prefix(path, "refs/tags/", &path_to_match)) {
149 is_tag = 1;
150 } else if (all) {
151 if ((exclude_patterns.nr || patterns.nr) &&
152 !skip_prefix(path, "refs/heads/", &path_to_match) &&
153 !skip_prefix(path, "refs/remotes/", &path_to_match)) {
154 /* Only accept reference of known type if there are match/exclude patterns */
155 return 0;
156 }
157 } else {
158 /* Reject anything outside refs/tags/ unless --all */
159 return 0;
160 }
161
162 /*
163 * If we're given exclude patterns, first exclude any tag which match
164 * any of the exclude pattern.
165 */
166 if (exclude_patterns.nr) {
167 struct string_list_item *item;
168
169 for_each_string_list_item(item, &exclude_patterns) {
170 if (!wildmatch(item->string, path_to_match, 0))
171 return 0;
172 }
173 }
174
175 /*
176 * If we're given patterns, accept only tags which match at least one
177 * pattern.
178 */
179 if (patterns.nr) {
180 int found = 0;
181 struct string_list_item *item;
182
183 for_each_string_list_item(item, &patterns) {
184 if (!wildmatch(item->string, path_to_match, 0)) {
185 found = 1;
186 break;
187 }
188 }
189
190 if (!found)
191 return 0;
192 }
193
194 /* Is it annotated? */
195 if (!peel_ref(path, &peeled)) {
196 is_annotated = !oideq(oid, &peeled);
197 } else {
198 oidcpy(&peeled, oid);
199 is_annotated = 0;
200 }
201
202 /*
203 * By default, we only use annotated tags, but with --tags
204 * we fall back to lightweight ones (even without --tags,
205 * we still remember lightweight ones, only to give hints
206 * in an error message). --all allows any refs to be used.
207 */
208 if (is_annotated)
209 prio = 2;
210 else if (is_tag)
211 prio = 1;
212 else
213 prio = 0;
214
215 add_to_known_names(all ? path + 5 : path + 10, &peeled, prio, oid);
216 return 0;
217 }
218
219 struct possible_tag {
220 struct commit_name *name;
221 int depth;
222 int found_order;
223 unsigned flag_within;
224 };
225
226 static int compare_pt(const void *a_, const void *b_)
227 {
228 struct possible_tag *a = (struct possible_tag *)a_;
229 struct possible_tag *b = (struct possible_tag *)b_;
230 if (a->depth != b->depth)
231 return a->depth - b->depth;
232 if (a->found_order != b->found_order)
233 return a->found_order - b->found_order;
234 return 0;
235 }
236
237 static unsigned long finish_depth_computation(
238 struct commit_list **list,
239 struct possible_tag *best)
240 {
241 unsigned long seen_commits = 0;
242 while (*list) {
243 struct commit *c = pop_commit(list);
244 struct commit_list *parents = c->parents;
245 seen_commits++;
246 if (c->object.flags & best->flag_within) {
247 struct commit_list *a = *list;
248 while (a) {
249 struct commit *i = a->item;
250 if (!(i->object.flags & best->flag_within))
251 break;
252 a = a->next;
253 }
254 if (!a)
255 break;
256 } else
257 best->depth++;
258 while (parents) {
259 struct commit *p = parents->item;
260 parse_commit(p);
261 if (!(p->object.flags & SEEN))
262 commit_list_insert_by_date(p, list);
263 p->object.flags |= c->object.flags;
264 parents = parents->next;
265 }
266 }
267 return seen_commits;
268 }
269
270 static void append_name(struct commit_name *n, struct strbuf *dst)
271 {
272 if (n->prio == 2 && !n->tag) {
273 n->tag = lookup_tag(the_repository, &n->oid);
274 if (!n->tag || parse_tag(n->tag))
275 die(_("annotated tag %s not available"), n->path);
276 }
277 if (n->tag && !n->name_checked) {
278 if (!n->tag->tag)
279 die(_("annotated tag %s has no embedded name"), n->path);
280 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
281 warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path);
282 n->name_checked = 1;
283 }
284
285 if (n->tag) {
286 if (all)
287 strbuf_addstr(dst, "tags/");
288 strbuf_addstr(dst, n->tag->tag);
289 } else {
290 strbuf_addstr(dst, n->path);
291 }
292 }
293
294 static void append_suffix(int depth, const struct object_id *oid, struct strbuf *dst)
295 {
296 strbuf_addf(dst, "-%d-g%s", depth, find_unique_abbrev(oid, abbrev));
297 }
298
299 static void describe_commit(struct object_id *oid, struct strbuf *dst)
300 {
301 struct commit *cmit, *gave_up_on = NULL;
302 struct commit_list *list;
303 struct commit_name *n;
304 struct possible_tag all_matches[MAX_TAGS];
305 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
306 unsigned long seen_commits = 0;
307 unsigned int unannotated_cnt = 0;
308
309 cmit = lookup_commit_reference(the_repository, oid);
310
311 n = find_commit_name(&cmit->object.oid);
312 if (n && (tags || all || n->prio == 2)) {
313 /*
314 * Exact match to an existing ref.
315 */
316 append_name(n, dst);
317 if (longformat)
318 append_suffix(0, n->tag ? get_tagged_oid(n->tag) : oid, dst);
319 if (suffix)
320 strbuf_addstr(dst, suffix);
321 return;
322 }
323
324 if (!max_candidates)
325 die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit->object.oid));
326 if (debug)
327 fprintf(stderr, _("No exact match on refs or tags, searching to describe\n"));
328
329 if (!have_util) {
330 struct hashmap_iter iter;
331 struct commit *c;
332 struct commit_name *n;
333
334 init_commit_names(&commit_names);
335 hashmap_for_each_entry(&names, &iter, n,
336 entry /* member name */) {
337 c = lookup_commit_reference_gently(the_repository,
338 &n->peeled, 1);
339 if (c)
340 *commit_names_at(&commit_names, c) = n;
341 }
342 have_util = 1;
343 }
344
345 list = NULL;
346 cmit->object.flags = SEEN;
347 commit_list_insert(cmit, &list);
348 while (list) {
349 struct commit *c = pop_commit(&list);
350 struct commit_list *parents = c->parents;
351 struct commit_name **slot;
352
353 seen_commits++;
354 slot = commit_names_peek(&commit_names, c);
355 n = slot ? *slot : NULL;
356 if (n) {
357 if (!tags && !all && n->prio < 2) {
358 unannotated_cnt++;
359 } else if (match_cnt < max_candidates) {
360 struct possible_tag *t = &all_matches[match_cnt++];
361 t->name = n;
362 t->depth = seen_commits - 1;
363 t->flag_within = 1u << match_cnt;
364 t->found_order = match_cnt;
365 c->object.flags |= t->flag_within;
366 if (n->prio == 2)
367 annotated_cnt++;
368 }
369 else {
370 gave_up_on = c;
371 break;
372 }
373 }
374 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
375 struct possible_tag *t = &all_matches[cur_match];
376 if (!(c->object.flags & t->flag_within))
377 t->depth++;
378 }
379 /* Stop if last remaining path already covered by best candidate(s) */
380 if (annotated_cnt && !list) {
381 int best_depth = INT_MAX;
382 unsigned best_within = 0;
383 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
384 struct possible_tag *t = &all_matches[cur_match];
385 if (t->depth < best_depth) {
386 best_depth = t->depth;
387 best_within = t->flag_within;
388 } else if (t->depth == best_depth) {
389 best_within |= t->flag_within;
390 }
391 }
392 if ((c->object.flags & best_within) == best_within) {
393 if (debug)
394 fprintf(stderr, _("finished search at %s\n"),
395 oid_to_hex(&c->object.oid));
396 break;
397 }
398 }
399 while (parents) {
400 struct commit *p = parents->item;
401 parse_commit(p);
402 if (!(p->object.flags & SEEN))
403 commit_list_insert_by_date(p, &list);
404 p->object.flags |= c->object.flags;
405 parents = parents->next;
406
407 if (first_parent)
408 break;
409 }
410 }
411
412 if (!match_cnt) {
413 struct object_id *cmit_oid = &cmit->object.oid;
414 if (always) {
415 strbuf_add_unique_abbrev(dst, cmit_oid, abbrev);
416 if (suffix)
417 strbuf_addstr(dst, suffix);
418 return;
419 }
420 if (unannotated_cnt)
421 die(_("No annotated tags can describe '%s'.\n"
422 "However, there were unannotated tags: try --tags."),
423 oid_to_hex(cmit_oid));
424 else
425 die(_("No tags can describe '%s'.\n"
426 "Try --always, or create some tags."),
427 oid_to_hex(cmit_oid));
428 }
429
430 QSORT(all_matches, match_cnt, compare_pt);
431
432 if (gave_up_on) {
433 commit_list_insert_by_date(gave_up_on, &list);
434 seen_commits--;
435 }
436 seen_commits += finish_depth_computation(&list, &all_matches[0]);
437 free_commit_list(list);
438
439 if (debug) {
440 static int label_width = -1;
441 if (label_width < 0) {
442 int i, w;
443 for (i = 0; i < ARRAY_SIZE(prio_names); i++) {
444 w = strlen(_(prio_names[i]));
445 if (label_width < w)
446 label_width = w;
447 }
448 }
449 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
450 struct possible_tag *t = &all_matches[cur_match];
451 fprintf(stderr, " %-*s %8d %s\n",
452 label_width, _(prio_names[t->name->prio]),
453 t->depth, t->name->path);
454 }
455 fprintf(stderr, _("traversed %lu commits\n"), seen_commits);
456 if (gave_up_on) {
457 fprintf(stderr,
458 _("more than %i tags found; listed %i most recent\n"
459 "gave up search at %s\n"),
460 max_candidates, max_candidates,
461 oid_to_hex(&gave_up_on->object.oid));
462 }
463 }
464
465 append_name(all_matches[0].name, dst);
466 if (abbrev)
467 append_suffix(all_matches[0].depth, &cmit->object.oid, dst);
468 if (suffix)
469 strbuf_addstr(dst, suffix);
470 }
471
472 struct process_commit_data {
473 struct object_id current_commit;
474 struct object_id looking_for;
475 struct strbuf *dst;
476 struct rev_info *revs;
477 };
478
479 static void process_commit(struct commit *commit, void *data)
480 {
481 struct process_commit_data *pcd = data;
482 pcd->current_commit = commit->object.oid;
483 }
484
485 static void process_object(struct object *obj, const char *path, void *data)
486 {
487 struct process_commit_data *pcd = data;
488
489 if (oideq(&pcd->looking_for, &obj->oid) && !pcd->dst->len) {
490 reset_revision_walk();
491 describe_commit(&pcd->current_commit, pcd->dst);
492 strbuf_addf(pcd->dst, ":%s", path);
493 free_commit_list(pcd->revs->commits);
494 pcd->revs->commits = NULL;
495 }
496 }
497
498 static void describe_blob(struct object_id oid, struct strbuf *dst)
499 {
500 struct rev_info revs;
501 struct argv_array args = ARGV_ARRAY_INIT;
502 struct process_commit_data pcd = { null_oid, oid, dst, &revs};
503
504 argv_array_pushl(&args, "internal: The first arg is not parsed",
505 "--objects", "--in-commit-order", "--reverse", "HEAD",
506 NULL);
507
508 repo_init_revisions(the_repository, &revs, NULL);
509 if (setup_revisions(args.argc, args.argv, &revs, NULL) > 1)
510 BUG("setup_revisions could not handle all args?");
511
512 if (prepare_revision_walk(&revs))
513 die("revision walk setup failed");
514
515 traverse_commit_list(&revs, process_commit, process_object, &pcd);
516 reset_revision_walk();
517 }
518
519 static void describe(const char *arg, int last_one)
520 {
521 struct object_id oid;
522 struct commit *cmit;
523 struct strbuf sb = STRBUF_INIT;
524
525 if (debug)
526 fprintf(stderr, _("describe %s\n"), arg);
527
528 if (get_oid(arg, &oid))
529 die(_("Not a valid object name %s"), arg);
530 cmit = lookup_commit_reference_gently(the_repository, &oid, 1);
531
532 if (cmit)
533 describe_commit(&oid, &sb);
534 else if (oid_object_info(the_repository, &oid, NULL) == OBJ_BLOB)
535 describe_blob(oid, &sb);
536 else
537 die(_("%s is neither a commit nor blob"), arg);
538
539 puts(sb.buf);
540
541 if (!last_one)
542 clear_commit_marks(cmit, -1);
543
544 strbuf_release(&sb);
545 }
546
547 int cmd_describe(int argc, const char **argv, const char *prefix)
548 {
549 int contains = 0;
550 struct option options[] = {
551 OPT_BOOL(0, "contains", &contains, N_("find the tag that comes after the commit")),
552 OPT_BOOL(0, "debug", &debug, N_("debug search strategy on stderr")),
553 OPT_BOOL(0, "all", &all, N_("use any ref")),
554 OPT_BOOL(0, "tags", &tags, N_("use any tag, even unannotated")),
555 OPT_BOOL(0, "long", &longformat, N_("always use long format")),
556 OPT_BOOL(0, "first-parent", &first_parent, N_("only follow first parent")),
557 OPT__ABBREV(&abbrev),
558 OPT_SET_INT(0, "exact-match", &max_candidates,
559 N_("only output exact matches"), 0),
560 OPT_INTEGER(0, "candidates", &max_candidates,
561 N_("consider <n> most recent tags (default: 10)")),
562 OPT_STRING_LIST(0, "match", &patterns, N_("pattern"),
563 N_("only consider tags matching <pattern>")),
564 OPT_STRING_LIST(0, "exclude", &exclude_patterns, N_("pattern"),
565 N_("do not consider tags matching <pattern>")),
566 OPT_BOOL(0, "always", &always,
567 N_("show abbreviated commit object as fallback")),
568 {OPTION_STRING, 0, "dirty", &dirty, N_("mark"),
569 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
570 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
571 {OPTION_STRING, 0, "broken", &broken, N_("mark"),
572 N_("append <mark> on broken working tree (default: \"-broken\")"),
573 PARSE_OPT_OPTARG, NULL, (intptr_t) "-broken"},
574 OPT_END(),
575 };
576
577 git_config(git_default_config, NULL);
578 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
579 if (abbrev < 0)
580 abbrev = DEFAULT_ABBREV;
581
582 if (max_candidates < 0)
583 max_candidates = 0;
584 else if (max_candidates > MAX_TAGS)
585 max_candidates = MAX_TAGS;
586
587 save_commit_buffer = 0;
588
589 if (longformat && abbrev == 0)
590 die(_("--long is incompatible with --abbrev=0"));
591
592 if (contains) {
593 struct string_list_item *item;
594 struct argv_array args;
595
596 argv_array_init(&args);
597 argv_array_pushl(&args, "name-rev",
598 "--peel-tag", "--name-only", "--no-undefined",
599 NULL);
600 if (always)
601 argv_array_push(&args, "--always");
602 if (!all) {
603 argv_array_push(&args, "--tags");
604 for_each_string_list_item(item, &patterns)
605 argv_array_pushf(&args, "--refs=refs/tags/%s", item->string);
606 for_each_string_list_item(item, &exclude_patterns)
607 argv_array_pushf(&args, "--exclude=refs/tags/%s", item->string);
608 }
609 if (argc)
610 argv_array_pushv(&args, argv);
611 else
612 argv_array_push(&args, "HEAD");
613 return cmd_name_rev(args.argc, args.argv, prefix);
614 }
615
616 hashmap_init(&names, commit_name_neq, NULL, 0);
617 for_each_rawref(get_name, NULL);
618 if (!hashmap_get_size(&names) && !always)
619 die(_("No names found, cannot describe anything."));
620
621 if (argc == 0) {
622 if (broken) {
623 struct child_process cp = CHILD_PROCESS_INIT;
624 argv_array_pushv(&cp.args, diff_index_args);
625 cp.git_cmd = 1;
626 cp.no_stdin = 1;
627 cp.no_stdout = 1;
628
629 if (!dirty)
630 dirty = "-dirty";
631
632 switch (run_command(&cp)) {
633 case 0:
634 suffix = NULL;
635 break;
636 case 1:
637 suffix = dirty;
638 break;
639 default:
640 /* diff-index aborted abnormally */
641 suffix = broken;
642 }
643 } else if (dirty) {
644 struct lock_file index_lock = LOCK_INIT;
645 struct rev_info revs;
646 struct argv_array args = ARGV_ARRAY_INIT;
647 int fd, result;
648
649 setup_work_tree();
650 read_cache();
651 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
652 NULL, NULL, NULL);
653 fd = hold_locked_index(&index_lock, 0);
654 if (0 <= fd)
655 repo_update_index_if_able(the_repository, &index_lock);
656
657 repo_init_revisions(the_repository, &revs, prefix);
658 argv_array_pushv(&args, diff_index_args);
659 if (setup_revisions(args.argc, args.argv, &revs, NULL) != 1)
660 BUG("malformed internal diff-index command line");
661 result = run_diff_index(&revs, 0);
662
663 if (!diff_result_code(&revs.diffopt, result))
664 suffix = NULL;
665 else
666 suffix = dirty;
667 }
668 describe("HEAD", 1);
669 } else if (dirty) {
670 die(_("--dirty is incompatible with commit-ishes"));
671 } else if (broken) {
672 die(_("--broken is incompatible with commit-ishes"));
673 } else {
674 while (argc-- > 0)
675 describe(*argv++, argc == 0);
676 }
677 return 0;
678 }