7 #include "parse-options.h"
11 #define MAX_TAGS (FLAG_BITS - 1)
13 static const char * const describe_usage[] = {
14 "git describe [options] <committish>*",
15 "git describe [options] --dirty",
19 static int debug; /* Display lots of verbose info */
20 static int all; /* Any valid ref can be used */
21 static int tags; /* Allow lightweight tags */
22 static int longformat;
23 static int abbrev = DEFAULT_ABBREV;
24 static int max_candidates = 10;
25 static int found_names;
26 static const char *pattern;
28 static const char *dirty;
30 /* diff-index command arguments to check if working tree is dirty. */
31 static const char *diff_index_args[] = {
32 "diff-index", "--quiet", "HEAD", "--", NULL
38 int prio; /* annotated tag = 2, tag = 1, head = 0 */
39 unsigned char sha1[20];
40 char path[FLEX_ARRAY]; /* more */
42 static const char *prio_names[] = {
43 "head", "lightweight", "annotated",
46 static void add_to_known_names(const char *path,
47 struct commit *commit,
49 const unsigned char *sha1)
51 struct commit_name *e = commit->util;
52 if (!e || e->prio < prio) {
53 size_t len = strlen(path)+1;
55 e = xmalloc(sizeof(struct commit_name) + len);
58 hashcpy(e->sha1, sha1);
59 memcpy(e->path, path, len);
65 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
67 int might_be_tag = !prefixcmp(path, "refs/tags/");
68 struct commit *commit;
69 struct object *object;
70 unsigned char peeled[20];
73 if (!all && !might_be_tag)
76 if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
77 commit = lookup_commit_reference_gently(peeled, 1);
80 is_tag = !!hashcmp(sha1, commit->object.sha1);
82 commit = lookup_commit_reference_gently(sha1, 1);
83 object = parse_object(sha1);
84 if (!commit || !object)
86 is_tag = object->type == OBJ_TAG;
89 /* If --all, then any refs are used.
90 * If --tags, then any tags are used.
91 * Otherwise only annotated tags are used.
99 if (pattern && fnmatch(pattern, path + 10, 0))
108 if (!tags && prio < 2)
111 add_to_known_names(all ? path + 5 : path + 10, commit, prio, sha1);
115 struct possible_tag {
116 struct commit_name *name;
119 unsigned flag_within;
122 static int compare_pt(const void *a_, const void *b_)
124 struct possible_tag *a = (struct possible_tag *)a_;
125 struct possible_tag *b = (struct possible_tag *)b_;
126 if (a->depth != b->depth)
127 return a->depth - b->depth;
128 if (a->found_order != b->found_order)
129 return a->found_order - b->found_order;
133 static unsigned long finish_depth_computation(
134 struct commit_list **list,
135 struct possible_tag *best)
137 unsigned long seen_commits = 0;
139 struct commit *c = pop_commit(list);
140 struct commit_list *parents = c->parents;
142 if (c->object.flags & best->flag_within) {
143 struct commit_list *a = *list;
145 struct commit *i = a->item;
146 if (!(i->object.flags & best->flag_within))
155 struct commit *p = parents->item;
157 if (!(p->object.flags & SEEN))
158 insert_by_date(p, list);
159 p->object.flags |= c->object.flags;
160 parents = parents->next;
166 static void display_name(struct commit_name *n)
168 if (n->prio == 2 && !n->tag) {
169 n->tag = lookup_tag(n->sha1);
170 if (!n->tag || parse_tag(n->tag) || !n->tag->tag)
171 die("annotated tag %s not available", n->path);
172 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
173 warning("tag '%s' is really '%s' here", n->tag->tag, n->path);
177 printf("%s", n->tag->tag);
179 printf("%s", n->path);
182 static void show_suffix(int depth, const unsigned char *sha1)
184 printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
187 static void describe(const char *arg, int last_one)
189 unsigned char sha1[20];
190 struct commit *cmit, *gave_up_on = NULL;
191 struct commit_list *list;
192 struct commit_name *n;
193 struct possible_tag all_matches[MAX_TAGS];
194 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
195 unsigned long seen_commits = 0;
197 if (get_sha1(arg, sha1))
198 die("Not a valid object name %s", arg);
199 cmit = lookup_commit_reference(sha1);
201 die("%s is not a valid '%s' object", arg, commit_type);
206 * Exact match to an existing ref.
210 show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
218 die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
220 fprintf(stderr, "searching to describe %s\n", arg);
223 cmit->object.flags = SEEN;
224 commit_list_insert(cmit, &list);
226 struct commit *c = pop_commit(&list);
227 struct commit_list *parents = c->parents;
231 if (match_cnt < max_candidates) {
232 struct possible_tag *t = &all_matches[match_cnt++];
234 t->depth = seen_commits - 1;
235 t->flag_within = 1u << match_cnt;
236 t->found_order = match_cnt;
237 c->object.flags |= t->flag_within;
246 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
247 struct possible_tag *t = &all_matches[cur_match];
248 if (!(c->object.flags & t->flag_within))
251 if (annotated_cnt && !list) {
253 fprintf(stderr, "finished search at %s\n",
254 sha1_to_hex(c->object.sha1));
258 struct commit *p = parents->item;
260 if (!(p->object.flags & SEEN))
261 insert_by_date(p, &list);
262 p->object.flags |= c->object.flags;
263 parents = parents->next;
268 const unsigned char *sha1 = cmit->object.sha1;
270 printf("%s", find_unique_abbrev(sha1, abbrev));
276 die("cannot describe '%s'", sha1_to_hex(sha1));
279 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
282 insert_by_date(gave_up_on, &list);
285 seen_commits += finish_depth_computation(&list, &all_matches[0]);
286 free_commit_list(list);
289 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
290 struct possible_tag *t = &all_matches[cur_match];
291 fprintf(stderr, " %-11s %8d %s\n",
292 prio_names[t->name->prio],
293 t->depth, t->name->path);
295 fprintf(stderr, "traversed %lu commits\n", seen_commits);
298 "more than %i tags found; listed %i most recent\n"
299 "gave up search at %s\n",
300 max_candidates, max_candidates,
301 sha1_to_hex(gave_up_on->object.sha1));
305 display_name(all_matches[0].name);
307 show_suffix(all_matches[0].depth, cmit->object.sha1);
313 clear_commit_marks(cmit, -1);
316 int cmd_describe(int argc, const char **argv, const char *prefix)
319 struct option options[] = {
320 OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
321 OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
322 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
323 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
324 OPT_BOOLEAN(0, "long", &longformat, "always use long format"),
325 OPT__ABBREV(&abbrev),
326 OPT_SET_INT(0, "exact-match", &max_candidates,
327 "only output exact matches", 0),
328 OPT_INTEGER(0, "candidates", &max_candidates,
329 "consider <n> most recent tags (default: 10)"),
330 OPT_STRING(0, "match", &pattern, "pattern",
331 "only consider tags matching <pattern>"),
332 OPT_BOOLEAN(0, "always", &always,
333 "show abbreviated commit object as fallback"),
334 {OPTION_STRING, 0, "dirty", &dirty, "mark",
335 "append <mark> on dirty working tree (default: \"-dirty\")",
336 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
340 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
341 if (max_candidates < 0)
343 else if (max_candidates > MAX_TAGS)
344 max_candidates = MAX_TAGS;
346 save_commit_buffer = 0;
348 if (longformat && abbrev == 0)
349 die("--long is incompatible with --abbrev=0");
352 const char **args = xmalloc((7 + argc) * sizeof(char *));
354 args[i++] = "name-rev";
355 args[i++] = "--name-only";
356 args[i++] = "--no-undefined";
358 args[i++] = "--always";
360 args[i++] = "--tags";
362 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
363 sprintf(s, "--refs=refs/tags/%s", pattern);
367 memcpy(args + i, argv, argc * sizeof(char *));
368 args[i + argc] = NULL;
369 return cmd_name_rev(i + argc, args, prefix);
372 for_each_ref(get_name, NULL);
373 if (!found_names && !always)
374 die("No names found, cannot describe anything.");
377 if (dirty && !cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1, diff_index_args, prefix))
381 die("--dirty is incompatible with committishes");
384 describe(*argv++, argc == 0);