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