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