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