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