1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
11 #include "parse-options.h"
15 #include "argv-array.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 [<options>] [<commit-ish>...]"),
27 N_("git describe [<options>] --dirty"),
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;
40 static struct string_list patterns = STRING_LIST_INIT_NODUP;
41 static struct string_list exclude_patterns = STRING_LIST_INIT_NODUP;
43 static const char *suffix, *dirty, *broken;
44 static struct commit_names commit_names;
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
52 struct hashmap_entry entry;
53 struct object_id peeled;
55 unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
56 unsigned name_checked:1;
61 static const char *prio_names[] = {
62 N_("head"), N_("lightweight"), N_("annotated"),
65 static int commit_name_neq(const void *unused_cmp_data,
66 const struct hashmap_entry *eptr,
67 const struct hashmap_entry *entry_or_key,
70 const struct commit_name *cn1, *cn2;
72 cn1 = container_of(eptr, const struct commit_name, entry);
73 cn2 = container_of(entry_or_key, const struct commit_name, entry);
75 return !oideq(&cn1->peeled, peeled ? peeled : &cn2->peeled);
78 static inline struct commit_name *find_commit_name(const struct object_id *peeled)
80 return hashmap_get_entry_from_hash(&names, oidhash(peeled), peeled,
81 struct commit_name, entry);
84 static int replace_name(struct commit_name *e,
86 const struct object_id *oid,
89 if (!e || e->prio < prio)
92 if (e->prio == 2 && prio == 2) {
93 /* Multiple annotated tags point to the same commit.
94 * Select one to keep based upon their tagger date.
99 t = lookup_tag(the_repository, &e->oid);
100 if (!t || parse_tag(t))
105 t = lookup_tag(the_repository, oid);
106 if (!t || parse_tag(t))
110 if (e->tag->date < t->date)
117 static void add_to_known_names(const char *path,
118 const struct object_id *peeled,
120 const struct object_id *oid)
122 struct commit_name *e = find_commit_name(peeled);
123 struct tag *tag = NULL;
124 if (replace_name(e, prio, oid, &tag)) {
126 e = xmalloc(sizeof(struct commit_name));
127 oidcpy(&e->peeled, peeled);
128 hashmap_entry_init(&e->entry, oidhash(peeled));
129 hashmap_add(&names, &e->entry);
135 oidcpy(&e->oid, oid);
137 e->path = xstrdup(path);
141 static int get_name(const char *path, const struct object_id *oid, int flag, void *cb_data)
144 struct object_id peeled;
145 int is_annotated, prio;
146 const char *path_to_match = NULL;
148 if (skip_prefix(path, "refs/tags/", &path_to_match)) {
151 if ((exclude_patterns.nr || patterns.nr) &&
152 !skip_prefix(path, "refs/heads/", &path_to_match) &&
153 !skip_prefix(path, "refs/remotes/", &path_to_match)) {
154 /* Only accept reference of known type if there are match/exclude patterns */
158 /* Reject anything outside refs/tags/ unless --all */
163 * If we're given exclude patterns, first exclude any tag which match
164 * any of the exclude pattern.
166 if (exclude_patterns.nr) {
167 struct string_list_item *item;
169 for_each_string_list_item(item, &exclude_patterns) {
170 if (!wildmatch(item->string, path_to_match, 0))
176 * If we're given patterns, accept only tags which match at least one
181 struct string_list_item *item;
183 for_each_string_list_item(item, &patterns) {
184 if (!wildmatch(item->string, path_to_match, 0)) {
194 /* Is it annotated? */
195 if (!peel_ref(path, &peeled)) {
196 is_annotated = !oideq(oid, &peeled);
198 oidcpy(&peeled, oid);
203 * By default, we only use annotated tags, but with --tags
204 * we fall back to lightweight ones (even without --tags,
205 * we still remember lightweight ones, only to give hints
206 * in an error message). --all allows any refs to be used.
215 add_to_known_names(all ? path + 5 : path + 10, &peeled, prio, oid);
219 struct possible_tag {
220 struct commit_name *name;
223 unsigned flag_within;
226 static int compare_pt(const void *a_, const void *b_)
228 struct possible_tag *a = (struct possible_tag *)a_;
229 struct possible_tag *b = (struct possible_tag *)b_;
230 if (a->depth != b->depth)
231 return a->depth - b->depth;
232 if (a->found_order != b->found_order)
233 return a->found_order - b->found_order;
237 static unsigned long finish_depth_computation(
238 struct commit_list **list,
239 struct possible_tag *best)
241 unsigned long seen_commits = 0;
243 struct commit *c = pop_commit(list);
244 struct commit_list *parents = c->parents;
246 if (c->object.flags & best->flag_within) {
247 struct commit_list *a = *list;
249 struct commit *i = a->item;
250 if (!(i->object.flags & best->flag_within))
259 struct commit *p = parents->item;
261 if (!(p->object.flags & SEEN))
262 commit_list_insert_by_date(p, list);
263 p->object.flags |= c->object.flags;
264 parents = parents->next;
270 static void append_name(struct commit_name *n, struct strbuf *dst)
272 if (n->prio == 2 && !n->tag) {
273 n->tag = lookup_tag(the_repository, &n->oid);
274 if (!n->tag || parse_tag(n->tag))
275 die(_("annotated tag %s not available"), n->path);
277 if (n->tag && !n->name_checked) {
279 die(_("annotated tag %s has no embedded name"), n->path);
280 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
281 warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path);
287 strbuf_addstr(dst, "tags/");
288 strbuf_addstr(dst, n->tag->tag);
290 strbuf_addstr(dst, n->path);
294 static void append_suffix(int depth, const struct object_id *oid, struct strbuf *dst)
296 strbuf_addf(dst, "-%d-g%s", depth, find_unique_abbrev(oid, abbrev));
299 static void describe_commit(struct object_id *oid, struct strbuf *dst)
301 struct commit *cmit, *gave_up_on = NULL;
302 struct commit_list *list;
303 struct commit_name *n;
304 struct possible_tag all_matches[MAX_TAGS];
305 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
306 unsigned long seen_commits = 0;
307 unsigned int unannotated_cnt = 0;
309 cmit = lookup_commit_reference(the_repository, oid);
311 n = find_commit_name(&cmit->object.oid);
312 if (n && (tags || all || n->prio == 2)) {
314 * Exact match to an existing ref.
318 append_suffix(0, n->tag ? get_tagged_oid(n->tag) : oid, dst);
320 strbuf_addstr(dst, suffix);
325 die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit->object.oid));
327 fprintf(stderr, _("No exact match on refs or tags, searching to describe\n"));
330 struct hashmap_iter iter;
332 struct commit_name *n;
334 init_commit_names(&commit_names);
335 hashmap_for_each_entry(&names, &iter, n,
336 entry /* member name */) {
337 c = lookup_commit_reference_gently(the_repository,
340 *commit_names_at(&commit_names, c) = n;
346 cmit->object.flags = SEEN;
347 commit_list_insert(cmit, &list);
349 struct commit *c = pop_commit(&list);
350 struct commit_list *parents = c->parents;
351 struct commit_name **slot;
354 slot = commit_names_peek(&commit_names, c);
355 n = slot ? *slot : NULL;
357 if (!tags && !all && n->prio < 2) {
359 } else if (match_cnt < max_candidates) {
360 struct possible_tag *t = &all_matches[match_cnt++];
362 t->depth = seen_commits - 1;
363 t->flag_within = 1u << match_cnt;
364 t->found_order = match_cnt;
365 c->object.flags |= t->flag_within;
374 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
375 struct possible_tag *t = &all_matches[cur_match];
376 if (!(c->object.flags & t->flag_within))
379 if (annotated_cnt && !list) {
381 fprintf(stderr, _("finished search at %s\n"),
382 oid_to_hex(&c->object.oid));
386 struct commit *p = parents->item;
388 if (!(p->object.flags & SEEN))
389 commit_list_insert_by_date(p, &list);
390 p->object.flags |= c->object.flags;
391 parents = parents->next;
399 struct object_id *cmit_oid = &cmit->object.oid;
401 strbuf_add_unique_abbrev(dst, cmit_oid, abbrev);
403 strbuf_addstr(dst, suffix);
407 die(_("No annotated tags can describe '%s'.\n"
408 "However, there were unannotated tags: try --tags."),
409 oid_to_hex(cmit_oid));
411 die(_("No tags can describe '%s'.\n"
412 "Try --always, or create some tags."),
413 oid_to_hex(cmit_oid));
416 QSORT(all_matches, match_cnt, compare_pt);
419 commit_list_insert_by_date(gave_up_on, &list);
422 seen_commits += finish_depth_computation(&list, &all_matches[0]);
423 free_commit_list(list);
426 static int label_width = -1;
427 if (label_width < 0) {
429 for (i = 0; i < ARRAY_SIZE(prio_names); i++) {
430 w = strlen(_(prio_names[i]));
435 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
436 struct possible_tag *t = &all_matches[cur_match];
437 fprintf(stderr, " %-*s %8d %s\n",
438 label_width, _(prio_names[t->name->prio]),
439 t->depth, t->name->path);
441 fprintf(stderr, _("traversed %lu commits\n"), seen_commits);
444 _("more than %i tags found; listed %i most recent\n"
445 "gave up search at %s\n"),
446 max_candidates, max_candidates,
447 oid_to_hex(&gave_up_on->object.oid));
451 append_name(all_matches[0].name, dst);
453 append_suffix(all_matches[0].depth, &cmit->object.oid, dst);
455 strbuf_addstr(dst, suffix);
458 struct process_commit_data {
459 struct object_id current_commit;
460 struct object_id looking_for;
462 struct rev_info *revs;
465 static void process_commit(struct commit *commit, void *data)
467 struct process_commit_data *pcd = data;
468 pcd->current_commit = commit->object.oid;
471 static void process_object(struct object *obj, const char *path, void *data)
473 struct process_commit_data *pcd = data;
475 if (oideq(&pcd->looking_for, &obj->oid) && !pcd->dst->len) {
476 reset_revision_walk();
477 describe_commit(&pcd->current_commit, pcd->dst);
478 strbuf_addf(pcd->dst, ":%s", path);
479 free_commit_list(pcd->revs->commits);
480 pcd->revs->commits = NULL;
484 static void describe_blob(struct object_id oid, struct strbuf *dst)
486 struct rev_info revs;
487 struct argv_array args = ARGV_ARRAY_INIT;
488 struct process_commit_data pcd = { null_oid, oid, dst, &revs};
490 argv_array_pushl(&args, "internal: The first arg is not parsed",
491 "--objects", "--in-commit-order", "--reverse", "HEAD",
494 repo_init_revisions(the_repository, &revs, NULL);
495 if (setup_revisions(args.argc, args.argv, &revs, NULL) > 1)
496 BUG("setup_revisions could not handle all args?");
498 if (prepare_revision_walk(&revs))
499 die("revision walk setup failed");
501 traverse_commit_list(&revs, process_commit, process_object, &pcd);
502 reset_revision_walk();
505 static void describe(const char *arg, int last_one)
507 struct object_id oid;
509 struct strbuf sb = STRBUF_INIT;
512 fprintf(stderr, _("describe %s\n"), arg);
514 if (get_oid(arg, &oid))
515 die(_("Not a valid object name %s"), arg);
516 cmit = lookup_commit_reference_gently(the_repository, &oid, 1);
519 describe_commit(&oid, &sb);
520 else if (oid_object_info(the_repository, &oid, NULL) == OBJ_BLOB)
521 describe_blob(oid, &sb);
523 die(_("%s is neither a commit nor blob"), arg);
528 clear_commit_marks(cmit, -1);
533 int cmd_describe(int argc, const char **argv, const char *prefix)
536 struct option options[] = {
537 OPT_BOOL(0, "contains", &contains, N_("find the tag that comes after the commit")),
538 OPT_BOOL(0, "debug", &debug, N_("debug search strategy on stderr")),
539 OPT_BOOL(0, "all", &all, N_("use any ref")),
540 OPT_BOOL(0, "tags", &tags, N_("use any tag, even unannotated")),
541 OPT_BOOL(0, "long", &longformat, N_("always use long format")),
542 OPT_BOOL(0, "first-parent", &first_parent, N_("only follow first parent")),
543 OPT__ABBREV(&abbrev),
544 OPT_SET_INT(0, "exact-match", &max_candidates,
545 N_("only output exact matches"), 0),
546 OPT_INTEGER(0, "candidates", &max_candidates,
547 N_("consider <n> most recent tags (default: 10)")),
548 OPT_STRING_LIST(0, "match", &patterns, N_("pattern"),
549 N_("only consider tags matching <pattern>")),
550 OPT_STRING_LIST(0, "exclude", &exclude_patterns, N_("pattern"),
551 N_("do not consider tags matching <pattern>")),
552 OPT_BOOL(0, "always", &always,
553 N_("show abbreviated commit object as fallback")),
554 {OPTION_STRING, 0, "dirty", &dirty, N_("mark"),
555 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
556 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
557 {OPTION_STRING, 0, "broken", &broken, N_("mark"),
558 N_("append <mark> on broken working tree (default: \"-broken\")"),
559 PARSE_OPT_OPTARG, NULL, (intptr_t) "-broken"},
563 git_config(git_default_config, NULL);
564 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
566 abbrev = DEFAULT_ABBREV;
568 if (max_candidates < 0)
570 else if (max_candidates > MAX_TAGS)
571 max_candidates = MAX_TAGS;
573 save_commit_buffer = 0;
575 if (longformat && abbrev == 0)
576 die(_("--long is incompatible with --abbrev=0"));
579 struct string_list_item *item;
580 struct argv_array args;
582 argv_array_init(&args);
583 argv_array_pushl(&args, "name-rev",
584 "--peel-tag", "--name-only", "--no-undefined",
587 argv_array_push(&args, "--always");
589 argv_array_push(&args, "--tags");
590 for_each_string_list_item(item, &patterns)
591 argv_array_pushf(&args, "--refs=refs/tags/%s", item->string);
592 for_each_string_list_item(item, &exclude_patterns)
593 argv_array_pushf(&args, "--exclude=refs/tags/%s", item->string);
596 argv_array_pushv(&args, argv);
598 argv_array_push(&args, "HEAD");
599 return cmd_name_rev(args.argc, args.argv, prefix);
602 hashmap_init(&names, commit_name_neq, NULL, 0);
603 for_each_rawref(get_name, NULL);
604 if (!hashmap_get_size(&names) && !always)
605 die(_("No names found, cannot describe anything."));
609 struct child_process cp = CHILD_PROCESS_INIT;
610 argv_array_pushv(&cp.args, diff_index_args);
618 switch (run_command(&cp)) {
626 /* diff-index aborted abnormally */
630 struct lock_file index_lock = LOCK_INIT;
631 struct rev_info revs;
632 struct argv_array args = ARGV_ARRAY_INIT;
637 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
639 fd = hold_locked_index(&index_lock, 0);
641 repo_update_index_if_able(the_repository, &index_lock);
643 repo_init_revisions(the_repository, &revs, prefix);
644 argv_array_pushv(&args, diff_index_args);
645 if (setup_revisions(args.argc, args.argv, &revs, NULL) != 1)
646 BUG("malformed internal diff-index command line");
647 result = run_diff_index(&revs, 0);
649 if (!diff_result_code(&revs.diffopt, result))
656 die(_("--dirty is incompatible with commit-ishes"));
658 die(_("--broken is incompatible with commit-ishes"));
661 describe(*argv++, argc == 0);