7 #include "parse-options.h"
12 #define MAX_TAGS (FLAG_BITS - 1)
14 static const char * const describe_usage[] = {
15 N_("git describe [options] <committish>*"),
16 N_("git describe [options] --dirty"),
20 static int debug; /* Display lots of verbose info */
21 static int all; /* Any valid ref can be used */
22 static int tags; /* Allow lightweight tags */
23 static int longformat;
24 static int abbrev = -1; /* unspecified */
25 static int max_candidates = 10;
26 static struct hash_table names;
28 static const char *pattern;
30 static const char *dirty;
32 /* diff-index command arguments to check if working tree is dirty. */
33 static const char *diff_index_args[] = {
34 "diff-index", "--quiet", "HEAD", "--", NULL
39 struct commit_name *next;
40 unsigned char peeled[20];
42 unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
43 unsigned name_checked:1;
44 unsigned char sha1[20];
47 static const char *prio_names[] = {
48 "head", "lightweight", "annotated",
51 static inline unsigned int hash_sha1(const unsigned char *sha1)
54 memcpy(&hash, sha1, sizeof(hash));
58 static inline struct commit_name *find_commit_name(const unsigned char *peeled)
60 struct commit_name *n = lookup_hash(hash_sha1(peeled), &names);
61 while (n && !!hashcmp(peeled, n->peeled))
66 static int set_util(void *chain, void *data)
68 struct commit_name *n;
69 for (n = chain; n; n = n->next) {
70 struct commit *c = lookup_commit_reference_gently(n->peeled, 1);
77 static int replace_name(struct commit_name *e,
79 const unsigned char *sha1,
82 if (!e || e->prio < prio)
85 if (e->prio == 2 && prio == 2) {
86 /* Multiple annotated tags point to the same commit.
87 * Select one to keep based upon their tagger date.
92 t = lookup_tag(e->sha1);
93 if (!t || parse_tag(t))
99 if (!t || parse_tag(t))
103 if (e->tag->date < t->date)
110 static void add_to_known_names(const char *path,
111 const unsigned char *peeled,
113 const unsigned char *sha1)
115 struct commit_name *e = find_commit_name(peeled);
116 struct tag *tag = NULL;
117 if (replace_name(e, prio, sha1, &tag)) {
120 e = xmalloc(sizeof(struct commit_name));
121 hashcpy(e->peeled, peeled);
122 pos = insert_hash(hash_sha1(peeled), e, &names);
133 hashcpy(e->sha1, sha1);
138 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
140 int is_tag = !prefixcmp(path, "refs/tags/");
141 unsigned char peeled[20];
142 int is_annotated, prio;
144 /* Reject anything outside refs/tags/ unless --all */
148 /* Accept only tags that match the pattern, if given */
149 if (pattern && (!is_tag || fnmatch(pattern, path + 10, 0)))
152 /* Is it annotated? */
153 if (!peel_ref(path, peeled)) {
154 is_annotated = !!hashcmp(sha1, peeled);
156 hashcpy(peeled, sha1);
161 * By default, we only use annotated tags, but with --tags
162 * we fall back to lightweight ones (even without --tags,
163 * we still remember lightweight ones, only to give hints
164 * in an error message). --all allows any refs to be used.
173 add_to_known_names(all ? path + 5 : path + 10, peeled, prio, sha1);
177 struct possible_tag {
178 struct commit_name *name;
181 unsigned flag_within;
184 static int compare_pt(const void *a_, const void *b_)
186 struct possible_tag *a = (struct possible_tag *)a_;
187 struct possible_tag *b = (struct possible_tag *)b_;
188 if (a->depth != b->depth)
189 return a->depth - b->depth;
190 if (a->found_order != b->found_order)
191 return a->found_order - b->found_order;
195 static unsigned long finish_depth_computation(
196 struct commit_list **list,
197 struct possible_tag *best)
199 unsigned long seen_commits = 0;
201 struct commit *c = pop_commit(list);
202 struct commit_list *parents = c->parents;
204 if (c->object.flags & best->flag_within) {
205 struct commit_list *a = *list;
207 struct commit *i = a->item;
208 if (!(i->object.flags & best->flag_within))
217 struct commit *p = parents->item;
219 if (!(p->object.flags & SEEN))
220 commit_list_insert_by_date(p, list);
221 p->object.flags |= c->object.flags;
222 parents = parents->next;
228 static void display_name(struct commit_name *n)
230 if (n->prio == 2 && !n->tag) {
231 n->tag = lookup_tag(n->sha1);
232 if (!n->tag || parse_tag(n->tag))
233 die(_("annotated tag %s not available"), n->path);
235 if (n->tag && !n->name_checked) {
237 die(_("annotated tag %s has no embedded name"), n->path);
238 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
239 warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path);
244 printf("%s", n->tag->tag);
246 printf("%s", n->path);
249 static void show_suffix(int depth, const unsigned char *sha1)
251 printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
254 static void describe(const char *arg, int last_one)
256 unsigned char sha1[20];
257 struct commit *cmit, *gave_up_on = NULL;
258 struct commit_list *list;
259 struct commit_name *n;
260 struct possible_tag all_matches[MAX_TAGS];
261 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
262 unsigned long seen_commits = 0;
263 unsigned int unannotated_cnt = 0;
265 if (get_sha1(arg, sha1))
266 die(_("Not a valid object name %s"), arg);
267 cmit = lookup_commit_reference(sha1);
269 die(_("%s is not a valid '%s' object"), arg, commit_type);
271 n = find_commit_name(cmit->object.sha1);
272 if (n && (tags || all || n->prio == 2)) {
274 * Exact match to an existing ref.
278 show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
286 die(_("no tag exactly matches '%s'"), sha1_to_hex(cmit->object.sha1));
288 fprintf(stderr, _("searching to describe %s\n"), arg);
291 for_each_hash(&names, set_util, NULL);
296 cmit->object.flags = SEEN;
297 commit_list_insert(cmit, &list);
299 struct commit *c = pop_commit(&list);
300 struct commit_list *parents = c->parents;
304 if (!tags && !all && n->prio < 2) {
306 } else if (match_cnt < max_candidates) {
307 struct possible_tag *t = &all_matches[match_cnt++];
309 t->depth = seen_commits - 1;
310 t->flag_within = 1u << match_cnt;
311 t->found_order = match_cnt;
312 c->object.flags |= t->flag_within;
321 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
322 struct possible_tag *t = &all_matches[cur_match];
323 if (!(c->object.flags & t->flag_within))
326 if (annotated_cnt && !list) {
328 fprintf(stderr, _("finished search at %s\n"),
329 sha1_to_hex(c->object.sha1));
333 struct commit *p = parents->item;
335 if (!(p->object.flags & SEEN))
336 commit_list_insert_by_date(p, &list);
337 p->object.flags |= c->object.flags;
338 parents = parents->next;
343 const unsigned char *sha1 = cmit->object.sha1;
345 printf("%s", find_unique_abbrev(sha1, abbrev));
352 die(_("No annotated tags can describe '%s'.\n"
353 "However, there were unannotated tags: try --tags."),
356 die(_("No tags can describe '%s'.\n"
357 "Try --always, or create some tags."),
361 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
364 commit_list_insert_by_date(gave_up_on, &list);
367 seen_commits += finish_depth_computation(&list, &all_matches[0]);
368 free_commit_list(list);
371 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
372 struct possible_tag *t = &all_matches[cur_match];
373 fprintf(stderr, " %-11s %8d %s\n",
374 prio_names[t->name->prio],
375 t->depth, t->name->path);
377 fprintf(stderr, _("traversed %lu commits\n"), seen_commits);
380 _("more than %i tags found; listed %i most recent\n"
381 "gave up search at %s\n"),
382 max_candidates, max_candidates,
383 sha1_to_hex(gave_up_on->object.sha1));
387 display_name(all_matches[0].name);
389 show_suffix(all_matches[0].depth, cmit->object.sha1);
395 clear_commit_marks(cmit, -1);
398 int cmd_describe(int argc, const char **argv, const char *prefix)
401 struct option options[] = {
402 OPT_BOOLEAN(0, "contains", &contains, N_("find the tag that comes after the commit")),
403 OPT_BOOLEAN(0, "debug", &debug, N_("debug search strategy on stderr")),
404 OPT_BOOLEAN(0, "all", &all, N_("use any ref")),
405 OPT_BOOLEAN(0, "tags", &tags, N_("use any tag, even unannotated")),
406 OPT_BOOLEAN(0, "long", &longformat, N_("always use long format")),
407 OPT__ABBREV(&abbrev),
408 OPT_SET_INT(0, "exact-match", &max_candidates,
409 N_("only output exact matches"), 0),
410 OPT_INTEGER(0, "candidates", &max_candidates,
411 N_("consider <n> most recent tags (default: 10)")),
412 OPT_STRING(0, "match", &pattern, N_("pattern"),
413 N_("only consider tags matching <pattern>")),
414 OPT_BOOLEAN(0, "always", &always,
415 N_("show abbreviated commit object as fallback")),
416 {OPTION_STRING, 0, "dirty", &dirty, N_("mark"),
417 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
418 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
422 git_config(git_default_config, NULL);
423 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
425 abbrev = DEFAULT_ABBREV;
427 if (max_candidates < 0)
429 else if (max_candidates > MAX_TAGS)
430 max_candidates = MAX_TAGS;
432 save_commit_buffer = 0;
434 if (longformat && abbrev == 0)
435 die(_("--long is incompatible with --abbrev=0"));
438 const char **args = xmalloc((7 + argc) * sizeof(char *));
440 args[i++] = "name-rev";
441 args[i++] = "--name-only";
442 args[i++] = "--no-undefined";
444 args[i++] = "--always";
446 args[i++] = "--tags";
448 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
449 sprintf(s, "--refs=refs/tags/%s", pattern);
453 memcpy(args + i, argv, argc * sizeof(char *));
454 args[i + argc] = NULL;
455 return cmd_name_rev(i + argc, args, prefix);
459 for_each_rawref(get_name, NULL);
460 if (!names.nr && !always)
461 die(_("No names found, cannot describe anything."));
465 static struct lock_file index_lock;
468 read_cache_preload(NULL);
469 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
471 fd = hold_locked_index(&index_lock, 0);
473 update_index_if_able(&the_index, &index_lock);
475 if (!cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1,
476 diff_index_args, prefix))
481 die(_("--dirty is incompatible with committishes"));
484 describe(*argv++, argc == 0);