builtin/bundle: have unbundle check for repo before opening its bundle
[git/gitster.git] / builtin / describe.c
blobcf8edc422262c25d46c701e64d6cbc885a977a3f
1 #include "builtin.h"
2 #include "config.h"
3 #include "environment.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "lockfile.h"
7 #include "commit.h"
8 #include "tag.h"
9 #include "refs.h"
10 #include "object-name.h"
11 #include "parse-options.h"
12 #include "read-cache-ll.h"
13 #include "revision.h"
14 #include "diff.h"
15 #include "hashmap.h"
16 #include "setup.h"
17 #include "strvec.h"
18 #include "run-command.h"
19 #include "object-store-ll.h"
20 #include "list-objects.h"
21 #include "commit-slab.h"
22 #include "wildmatch.h"
24 #define MAX_TAGS (FLAG_BITS - 1)
25 #define DEFAULT_CANDIDATES 10
27 define_commit_slab(commit_names, struct commit_name *);
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
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 = DEFAULT_CANDIDATES;
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;
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
56 static const char *update_index_args[] = {
57 "update-index", "--unmerged", "-q", "--refresh", NULL
60 struct commit_name {
61 struct hashmap_entry entry;
62 struct object_id peeled;
63 struct tag *tag;
64 unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
65 unsigned name_checked:1;
66 unsigned misnamed:1;
67 struct object_id oid;
68 char *path;
71 static const char *prio_names[] = {
72 N_("head"), N_("lightweight"), N_("annotated"),
75 static int commit_name_neq(const void *cmp_data UNUSED,
76 const struct hashmap_entry *eptr,
77 const struct hashmap_entry *entry_or_key,
78 const void *peeled)
80 const struct commit_name *cn1, *cn2;
82 cn1 = container_of(eptr, const struct commit_name, entry);
83 cn2 = container_of(entry_or_key, const struct commit_name, entry);
85 return !oideq(&cn1->peeled, peeled ? peeled : &cn2->peeled);
88 static inline struct commit_name *find_commit_name(const struct object_id *peeled)
90 return hashmap_get_entry_from_hash(&names, oidhash(peeled), peeled,
91 struct commit_name, entry);
94 static int replace_name(struct commit_name *e,
95 int prio,
96 const struct object_id *oid,
97 struct tag **tag)
99 if (!e || e->prio < prio)
100 return 1;
102 if (e->prio == 2 && prio == 2) {
103 /* Multiple annotated tags point to the same commit.
104 * Select one to keep based upon their tagger date.
106 struct tag *t;
108 if (!e->tag) {
109 t = lookup_tag(the_repository, &e->oid);
110 if (!t || parse_tag(t))
111 return 1;
112 e->tag = t;
115 t = lookup_tag(the_repository, oid);
116 if (!t || parse_tag(t))
117 return 0;
118 *tag = t;
120 if (e->tag->date < t->date)
121 return 1;
124 return 0;
127 static void add_to_known_names(const char *path,
128 const struct object_id *peeled,
129 int prio,
130 const struct object_id *oid)
132 struct commit_name *e = find_commit_name(peeled);
133 struct tag *tag = NULL;
134 if (replace_name(e, prio, oid, &tag)) {
135 if (!e) {
136 e = xmalloc(sizeof(struct commit_name));
137 oidcpy(&e->peeled, peeled);
138 hashmap_entry_init(&e->entry, oidhash(peeled));
139 hashmap_add(&names, &e->entry);
140 e->path = NULL;
142 e->tag = tag;
143 e->prio = prio;
144 e->name_checked = 0;
145 e->misnamed = 0;
146 oidcpy(&e->oid, oid);
147 free(e->path);
148 e->path = xstrdup(path);
152 static int get_name(const char *path, const struct object_id *oid,
153 int flag UNUSED, void *cb_data UNUSED)
155 int is_tag = 0;
156 struct object_id peeled;
157 int is_annotated, prio;
158 const char *path_to_match = NULL;
160 if (skip_prefix(path, "refs/tags/", &path_to_match)) {
161 is_tag = 1;
162 } else if (all) {
163 if ((exclude_patterns.nr || patterns.nr) &&
164 !skip_prefix(path, "refs/heads/", &path_to_match) &&
165 !skip_prefix(path, "refs/remotes/", &path_to_match)) {
166 /* Only accept reference of known type if there are match/exclude patterns */
167 return 0;
169 } else {
170 /* Reject anything outside refs/tags/ unless --all */
171 return 0;
175 * If we're given exclude patterns, first exclude any tag which match
176 * any of the exclude pattern.
178 if (exclude_patterns.nr) {
179 struct string_list_item *item;
181 for_each_string_list_item(item, &exclude_patterns) {
182 if (!wildmatch(item->string, path_to_match, 0))
183 return 0;
188 * If we're given patterns, accept only tags which match at least one
189 * pattern.
191 if (patterns.nr) {
192 int found = 0;
193 struct string_list_item *item;
195 for_each_string_list_item(item, &patterns) {
196 if (!wildmatch(item->string, path_to_match, 0)) {
197 found = 1;
198 break;
202 if (!found)
203 return 0;
206 /* Is it annotated? */
207 if (!peel_iterated_oid(the_repository, oid, &peeled)) {
208 is_annotated = !oideq(oid, &peeled);
209 } else {
210 oidcpy(&peeled, oid);
211 is_annotated = 0;
215 * By default, we only use annotated tags, but with --tags
216 * we fall back to lightweight ones (even without --tags,
217 * we still remember lightweight ones, only to give hints
218 * in an error message). --all allows any refs to be used.
220 if (is_annotated)
221 prio = 2;
222 else if (is_tag)
223 prio = 1;
224 else
225 prio = 0;
227 add_to_known_names(all ? path + 5 : path + 10, &peeled, prio, oid);
228 return 0;
231 struct possible_tag {
232 struct commit_name *name;
233 int depth;
234 int found_order;
235 unsigned flag_within;
238 static int compare_pt(const void *a_, const void *b_)
240 struct possible_tag *a = (struct possible_tag *)a_;
241 struct possible_tag *b = (struct possible_tag *)b_;
242 if (a->depth != b->depth)
243 return a->depth - b->depth;
244 if (a->found_order != b->found_order)
245 return a->found_order - b->found_order;
246 return 0;
249 static unsigned long finish_depth_computation(
250 struct commit_list **list,
251 struct possible_tag *best)
253 unsigned long seen_commits = 0;
254 while (*list) {
255 struct commit *c = pop_commit(list);
256 struct commit_list *parents = c->parents;
257 seen_commits++;
258 if (c->object.flags & best->flag_within) {
259 struct commit_list *a = *list;
260 while (a) {
261 struct commit *i = a->item;
262 if (!(i->object.flags & best->flag_within))
263 break;
264 a = a->next;
266 if (!a)
267 break;
268 } else
269 best->depth++;
270 while (parents) {
271 struct commit *p = parents->item;
272 repo_parse_commit(the_repository, p);
273 if (!(p->object.flags & SEEN))
274 commit_list_insert_by_date(p, list);
275 p->object.flags |= c->object.flags;
276 parents = parents->next;
279 return seen_commits;
282 static void append_name(struct commit_name *n, struct strbuf *dst)
284 if (n->prio == 2 && !n->tag) {
285 n->tag = lookup_tag(the_repository, &n->oid);
286 if (!n->tag || parse_tag(n->tag))
287 die(_("annotated tag %s not available"), n->path);
289 if (n->tag && !n->name_checked) {
290 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path)) {
291 warning(_("tag '%s' is externally known as '%s'"),
292 n->path, n->tag->tag);
293 n->misnamed = 1;
295 n->name_checked = 1;
298 if (n->tag) {
299 if (all)
300 strbuf_addstr(dst, "tags/");
301 strbuf_addstr(dst, n->tag->tag);
302 } else {
303 strbuf_addstr(dst, n->path);
307 static void append_suffix(int depth, const struct object_id *oid, struct strbuf *dst)
309 strbuf_addf(dst, "-%d-g%s", depth,
310 repo_find_unique_abbrev(the_repository, oid, abbrev));
313 static void describe_commit(struct object_id *oid, struct strbuf *dst)
315 struct commit *cmit, *gave_up_on = NULL;
316 struct commit_list *list;
317 struct commit_name *n;
318 struct possible_tag all_matches[MAX_TAGS];
319 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
320 unsigned long seen_commits = 0;
321 unsigned int unannotated_cnt = 0;
323 cmit = lookup_commit_reference(the_repository, oid);
325 n = find_commit_name(&cmit->object.oid);
326 if (n && (tags || all || n->prio == 2)) {
328 * Exact match to an existing ref.
330 append_name(n, dst);
331 if (n->misnamed || longformat)
332 append_suffix(0, n->tag ? get_tagged_oid(n->tag) : oid, dst);
333 if (suffix)
334 strbuf_addstr(dst, suffix);
335 return;
338 if (!max_candidates)
339 die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit->object.oid));
340 if (debug)
341 fprintf(stderr, _("No exact match on refs or tags, searching to describe\n"));
343 if (!have_util) {
344 struct hashmap_iter iter;
345 struct commit *c;
346 struct commit_name *n;
348 init_commit_names(&commit_names);
349 hashmap_for_each_entry(&names, &iter, n,
350 entry /* member name */) {
351 c = lookup_commit_reference_gently(the_repository,
352 &n->peeled, 1);
353 if (c)
354 *commit_names_at(&commit_names, c) = n;
356 have_util = 1;
359 list = NULL;
360 cmit->object.flags = SEEN;
361 commit_list_insert(cmit, &list);
362 while (list) {
363 struct commit *c = pop_commit(&list);
364 struct commit_list *parents = c->parents;
365 struct commit_name **slot;
367 seen_commits++;
368 slot = commit_names_peek(&commit_names, c);
369 n = slot ? *slot : NULL;
370 if (n) {
371 if (!tags && !all && n->prio < 2) {
372 unannotated_cnt++;
373 } else if (match_cnt < max_candidates) {
374 struct possible_tag *t = &all_matches[match_cnt++];
375 t->name = n;
376 t->depth = seen_commits - 1;
377 t->flag_within = 1u << match_cnt;
378 t->found_order = match_cnt;
379 c->object.flags |= t->flag_within;
380 if (n->prio == 2)
381 annotated_cnt++;
383 else {
384 gave_up_on = c;
385 break;
388 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
389 struct possible_tag *t = &all_matches[cur_match];
390 if (!(c->object.flags & t->flag_within))
391 t->depth++;
393 /* Stop if last remaining path already covered by best candidate(s) */
394 if (annotated_cnt && !list) {
395 int best_depth = INT_MAX;
396 unsigned best_within = 0;
397 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
398 struct possible_tag *t = &all_matches[cur_match];
399 if (t->depth < best_depth) {
400 best_depth = t->depth;
401 best_within = t->flag_within;
402 } else if (t->depth == best_depth) {
403 best_within |= t->flag_within;
406 if ((c->object.flags & best_within) == best_within) {
407 if (debug)
408 fprintf(stderr, _("finished search at %s\n"),
409 oid_to_hex(&c->object.oid));
410 break;
413 while (parents) {
414 struct commit *p = parents->item;
415 repo_parse_commit(the_repository, p);
416 if (!(p->object.flags & SEEN))
417 commit_list_insert_by_date(p, &list);
418 p->object.flags |= c->object.flags;
419 parents = parents->next;
421 if (first_parent)
422 break;
426 if (!match_cnt) {
427 struct object_id *cmit_oid = &cmit->object.oid;
428 if (always) {
429 strbuf_add_unique_abbrev(dst, cmit_oid, abbrev);
430 if (suffix)
431 strbuf_addstr(dst, suffix);
432 return;
434 if (unannotated_cnt)
435 die(_("No annotated tags can describe '%s'.\n"
436 "However, there were unannotated tags: try --tags."),
437 oid_to_hex(cmit_oid));
438 else
439 die(_("No tags can describe '%s'.\n"
440 "Try --always, or create some tags."),
441 oid_to_hex(cmit_oid));
444 QSORT(all_matches, match_cnt, compare_pt);
446 if (gave_up_on) {
447 commit_list_insert_by_date(gave_up_on, &list);
448 seen_commits--;
450 seen_commits += finish_depth_computation(&list, &all_matches[0]);
451 free_commit_list(list);
453 if (debug) {
454 static int label_width = -1;
455 if (label_width < 0) {
456 int i, w;
457 for (i = 0; i < ARRAY_SIZE(prio_names); i++) {
458 w = strlen(_(prio_names[i]));
459 if (label_width < w)
460 label_width = w;
463 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
464 struct possible_tag *t = &all_matches[cur_match];
465 fprintf(stderr, " %-*s %8d %s\n",
466 label_width, _(prio_names[t->name->prio]),
467 t->depth, t->name->path);
469 fprintf(stderr, _("traversed %lu commits\n"), seen_commits);
470 if (gave_up_on) {
471 fprintf(stderr,
472 _("more than %i tags found; listed %i most recent\n"
473 "gave up search at %s\n"),
474 max_candidates, max_candidates,
475 oid_to_hex(&gave_up_on->object.oid));
479 append_name(all_matches[0].name, dst);
480 if (all_matches[0].name->misnamed || abbrev)
481 append_suffix(all_matches[0].depth, &cmit->object.oid, dst);
482 if (suffix)
483 strbuf_addstr(dst, suffix);
486 struct process_commit_data {
487 struct object_id current_commit;
488 struct object_id looking_for;
489 struct strbuf *dst;
490 struct rev_info *revs;
493 static void process_commit(struct commit *commit, void *data)
495 struct process_commit_data *pcd = data;
496 pcd->current_commit = commit->object.oid;
499 static void process_object(struct object *obj, const char *path, void *data)
501 struct process_commit_data *pcd = data;
503 if (oideq(&pcd->looking_for, &obj->oid) && !pcd->dst->len) {
504 reset_revision_walk();
505 describe_commit(&pcd->current_commit, pcd->dst);
506 strbuf_addf(pcd->dst, ":%s", path);
507 free_commit_list(pcd->revs->commits);
508 pcd->revs->commits = NULL;
512 static void describe_blob(struct object_id oid, struct strbuf *dst)
514 struct rev_info revs;
515 struct strvec args = STRVEC_INIT;
516 struct process_commit_data pcd = { *null_oid(), oid, dst, &revs};
518 strvec_pushl(&args, "internal: The first arg is not parsed",
519 "--objects", "--in-commit-order", "--reverse", "HEAD",
520 NULL);
522 repo_init_revisions(the_repository, &revs, NULL);
523 if (setup_revisions(args.nr, args.v, &revs, NULL) > 1)
524 BUG("setup_revisions could not handle all args?");
526 if (prepare_revision_walk(&revs))
527 die("revision walk setup failed");
529 traverse_commit_list(&revs, process_commit, process_object, &pcd);
530 reset_revision_walk();
531 release_revisions(&revs);
534 static void describe(const char *arg, int last_one)
536 struct object_id oid;
537 struct commit *cmit;
538 struct strbuf sb = STRBUF_INIT;
540 if (debug)
541 fprintf(stderr, _("describe %s\n"), arg);
543 if (repo_get_oid(the_repository, arg, &oid))
544 die(_("Not a valid object name %s"), arg);
545 cmit = lookup_commit_reference_gently(the_repository, &oid, 1);
547 if (cmit)
548 describe_commit(&oid, &sb);
549 else if (oid_object_info(the_repository, &oid, NULL) == OBJ_BLOB)
550 describe_blob(oid, &sb);
551 else
552 die(_("%s is neither a commit nor blob"), arg);
554 puts(sb.buf);
556 if (!last_one)
557 clear_commit_marks(cmit, -1);
559 strbuf_release(&sb);
562 static int option_parse_exact_match(const struct option *opt, const char *arg,
563 int unset)
565 int *val = opt->value;
567 BUG_ON_OPT_ARG(arg);
569 *val = unset ? DEFAULT_CANDIDATES : 0;
570 return 0;
573 int cmd_describe(int argc, const char **argv, const char *prefix)
575 int contains = 0;
576 struct option options[] = {
577 OPT_BOOL(0, "contains", &contains, N_("find the tag that comes after the commit")),
578 OPT_BOOL(0, "debug", &debug, N_("debug search strategy on stderr")),
579 OPT_BOOL(0, "all", &all, N_("use any ref")),
580 OPT_BOOL(0, "tags", &tags, N_("use any tag, even unannotated")),
581 OPT_BOOL(0, "long", &longformat, N_("always use long format")),
582 OPT_BOOL(0, "first-parent", &first_parent, N_("only follow first parent")),
583 OPT__ABBREV(&abbrev),
584 OPT_CALLBACK_F(0, "exact-match", &max_candidates, NULL,
585 N_("only output exact matches"),
586 PARSE_OPT_NOARG, option_parse_exact_match),
587 OPT_INTEGER(0, "candidates", &max_candidates,
588 N_("consider <n> most recent tags (default: 10)")),
589 OPT_STRING_LIST(0, "match", &patterns, N_("pattern"),
590 N_("only consider tags matching <pattern>")),
591 OPT_STRING_LIST(0, "exclude", &exclude_patterns, N_("pattern"),
592 N_("do not consider tags matching <pattern>")),
593 OPT_BOOL(0, "always", &always,
594 N_("show abbreviated commit object as fallback")),
595 {OPTION_STRING, 0, "dirty", &dirty, N_("mark"),
596 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
597 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
598 {OPTION_STRING, 0, "broken", &broken, N_("mark"),
599 N_("append <mark> on broken working tree (default: \"-broken\")"),
600 PARSE_OPT_OPTARG, NULL, (intptr_t) "-broken"},
601 OPT_END(),
604 git_config(git_default_config, NULL);
605 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
606 if (abbrev < 0)
607 abbrev = DEFAULT_ABBREV;
609 if (max_candidates < 0)
610 max_candidates = 0;
611 else if (max_candidates > MAX_TAGS)
612 max_candidates = MAX_TAGS;
614 save_commit_buffer = 0;
616 if (longformat && abbrev == 0)
617 die(_("options '%s' and '%s' cannot be used together"), "--long", "--abbrev=0");
619 if (contains) {
620 struct string_list_item *item;
621 struct strvec args;
623 strvec_init(&args);
624 strvec_pushl(&args, "name-rev",
625 "--peel-tag", "--name-only", "--no-undefined",
626 NULL);
627 if (always)
628 strvec_push(&args, "--always");
629 if (!all) {
630 strvec_push(&args, "--tags");
631 for_each_string_list_item(item, &patterns)
632 strvec_pushf(&args, "--refs=refs/tags/%s", item->string);
633 for_each_string_list_item(item, &exclude_patterns)
634 strvec_pushf(&args, "--exclude=refs/tags/%s", item->string);
636 if (argc)
637 strvec_pushv(&args, argv);
638 else
639 strvec_push(&args, "HEAD");
640 return cmd_name_rev(args.nr, args.v, prefix);
643 hashmap_init(&names, commit_name_neq, NULL, 0);
644 refs_for_each_rawref(get_main_ref_store(the_repository), get_name,
645 NULL);
646 if (!hashmap_get_size(&names) && !always)
647 die(_("No names found, cannot describe anything."));
649 if (argc == 0) {
650 if (broken) {
651 struct child_process cp = CHILD_PROCESS_INIT;
653 strvec_pushv(&cp.args, update_index_args);
654 cp.git_cmd = 1;
655 cp.no_stdin = 1;
656 cp.no_stdout = 1;
657 run_command(&cp);
659 child_process_init(&cp);
660 strvec_pushv(&cp.args, diff_index_args);
661 cp.git_cmd = 1;
662 cp.no_stdin = 1;
663 cp.no_stdout = 1;
665 if (!dirty)
666 dirty = "-dirty";
668 switch (run_command(&cp)) {
669 case 0:
670 suffix = NULL;
671 break;
672 case 1:
673 suffix = dirty;
674 break;
675 default:
676 /* diff-index aborted abnormally */
677 suffix = broken;
679 } else if (dirty) {
680 struct lock_file index_lock = LOCK_INIT;
681 struct rev_info revs;
682 struct strvec args = STRVEC_INIT;
683 int fd;
685 setup_work_tree();
686 prepare_repo_settings(the_repository);
687 the_repository->settings.command_requires_full_index = 0;
688 repo_read_index(the_repository);
689 refresh_index(the_repository->index, REFRESH_QUIET|REFRESH_UNMERGED,
690 NULL, NULL, NULL);
691 fd = repo_hold_locked_index(the_repository,
692 &index_lock, 0);
693 if (0 <= fd)
694 repo_update_index_if_able(the_repository, &index_lock);
696 repo_init_revisions(the_repository, &revs, prefix);
697 strvec_pushv(&args, diff_index_args);
698 if (setup_revisions(args.nr, args.v, &revs, NULL) != 1)
699 BUG("malformed internal diff-index command line");
700 run_diff_index(&revs, 0);
702 if (!diff_result_code(&revs.diffopt))
703 suffix = NULL;
704 else
705 suffix = dirty;
706 release_revisions(&revs);
708 describe("HEAD", 1);
709 } else if (dirty) {
710 die(_("option '%s' and commit-ishes cannot be used together"), "--dirty");
711 } else if (broken) {
712 die(_("option '%s' and commit-ishes cannot be used together"), "--broken");
713 } else {
714 while (argc-- > 0)
715 describe(*argv++, argc == 0);
717 return 0;