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