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