7 #include "parse-options.h"
10 #define MAX_TAGS (FLAG_BITS - 1)
12 static const char * const describe_usage[] = {
13 "git-describe [options] <committish>*",
17 static int debug; /* Display lots of verbose info */
18 static int all; /* Default to annotated tags only */
19 static int tags; /* But allow any tags if --tags is specified */
20 static int abbrev = DEFAULT_ABBREV;
21 static int max_candidates = 10;
22 const char *pattern = NULL;
25 int prio; /* annotated tag = 2, tag = 1, head = 0 */
26 char path[FLEX_ARRAY]; /* more */
28 static const char *prio_names[] = {
29 "head", "lightweight", "annotated",
32 static void add_to_known_names(const char *path,
33 struct commit *commit,
36 struct commit_name *e = commit->util;
37 if (!e || e->prio < prio) {
38 size_t len = strlen(path)+1;
40 e = xmalloc(sizeof(struct commit_name) + len);
42 memcpy(e->path, path, len);
47 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
49 struct commit *commit;
50 struct object *object;
51 unsigned char peeled[20];
54 if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
55 commit = lookup_commit_reference_gently(peeled, 1);
58 is_tag = !!hashcmp(sha1, commit->object.sha1);
60 commit = lookup_commit_reference_gently(sha1, 1);
61 object = parse_object(sha1);
62 if (!commit || !object)
64 is_tag = object->type == OBJ_TAG;
67 /* If --all, then any refs are used.
68 * If --tags, then any tags are used.
69 * Otherwise only annotated tags are used.
71 if (!prefixcmp(path, "refs/tags/")) {
74 if (pattern && fnmatch(pattern, path + 10, 0))
85 if (!tags && prio < 2)
88 add_to_known_names(all ? path + 5 : path + 10, commit, prio);
93 struct commit_name *name;
99 static int compare_pt(const void *a_, const void *b_)
101 struct possible_tag *a = (struct possible_tag *)a_;
102 struct possible_tag *b = (struct possible_tag *)b_;
103 if (a->name->prio != b->name->prio)
104 return b->name->prio - a->name->prio;
105 if (a->depth != b->depth)
106 return a->depth - b->depth;
107 if (a->found_order != b->found_order)
108 return a->found_order - b->found_order;
112 static unsigned long finish_depth_computation(
113 struct commit_list **list,
114 struct possible_tag *best)
116 unsigned long seen_commits = 0;
118 struct commit *c = pop_commit(list);
119 struct commit_list *parents = c->parents;
121 if (c->object.flags & best->flag_within) {
122 struct commit_list *a = *list;
124 struct commit *i = a->item;
125 if (!(i->object.flags & best->flag_within))
134 struct commit *p = parents->item;
136 if (!(p->object.flags & SEEN))
137 insert_by_date(p, list);
138 p->object.flags |= c->object.flags;
139 parents = parents->next;
145 static void describe(const char *arg, int last_one)
147 unsigned char sha1[20];
148 struct commit *cmit, *gave_up_on = NULL;
149 struct commit_list *list;
150 static int initialized = 0;
151 struct commit_name *n;
152 struct possible_tag all_matches[MAX_TAGS];
153 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
154 unsigned long seen_commits = 0;
156 if (get_sha1(arg, sha1))
157 die("Not a valid object name %s", arg);
158 cmit = lookup_commit_reference(sha1);
160 die("%s is not a valid '%s' object", arg, commit_type);
164 for_each_ref(get_name, NULL);
169 printf("%s\n", n->path);
174 fprintf(stderr, "searching to describe %s\n", arg);
177 cmit->object.flags = SEEN;
178 commit_list_insert(cmit, &list);
180 struct commit *c = pop_commit(&list);
181 struct commit_list *parents = c->parents;
185 if (match_cnt < max_candidates) {
186 struct possible_tag *t = &all_matches[match_cnt++];
188 t->depth = seen_commits - 1;
189 t->flag_within = 1u << match_cnt;
190 t->found_order = match_cnt;
191 c->object.flags |= t->flag_within;
200 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
201 struct possible_tag *t = &all_matches[cur_match];
202 if (!(c->object.flags & t->flag_within))
205 if (annotated_cnt && !list) {
207 fprintf(stderr, "finished search at %s\n",
208 sha1_to_hex(c->object.sha1));
212 struct commit *p = parents->item;
214 if (!(p->object.flags & SEEN))
215 insert_by_date(p, &list);
216 p->object.flags |= c->object.flags;
217 parents = parents->next;
222 die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
224 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
227 insert_by_date(gave_up_on, &list);
230 seen_commits += finish_depth_computation(&list, &all_matches[0]);
231 free_commit_list(list);
234 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
235 struct possible_tag *t = &all_matches[cur_match];
236 fprintf(stderr, " %-11s %8d %s\n",
237 prio_names[t->name->prio],
238 t->depth, t->name->path);
240 fprintf(stderr, "traversed %lu commits\n", seen_commits);
243 "more than %i tags found; listed %i most recent\n"
244 "gave up search at %s\n",
245 max_candidates, max_candidates,
246 sha1_to_hex(gave_up_on->object.sha1));
250 printf("%s\n", all_matches[0].name->path );
252 printf("%s-%d-g%s\n", all_matches[0].name->path,
253 all_matches[0].depth,
254 find_unique_abbrev(cmit->object.sha1, abbrev));
257 clear_commit_marks(cmit, -1);
260 int cmd_describe(int argc, const char **argv, const char *prefix)
263 struct option options[] = {
264 OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
265 OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
266 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
267 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
268 OPT__ABBREV(&abbrev),
269 OPT_INTEGER(0, "candidates", &max_candidates,
270 "consider <n> most recent tags (default: 10)"),
271 OPT_STRING(0, "match", &pattern, "pattern",
272 "only consider tags matching <pattern>"),
276 argc = parse_options(argc, argv, options, describe_usage, 0);
277 if (max_candidates < 1)
279 else if (max_candidates > MAX_TAGS)
280 max_candidates = MAX_TAGS;
282 save_commit_buffer = 0;
285 const char **args = xmalloc((6 + argc) * sizeof(char*));
287 args[i++] = "name-rev";
288 args[i++] = "--name-only";
289 args[i++] = "--no-undefined";
291 args[i++] = "--tags";
293 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
294 sprintf(s, "--refs=refs/tags/%s", pattern);
298 memcpy(args + i, argv, argc * sizeof(char*));
299 args[i + argc] = NULL;
300 return cmd_name_rev(i + argc, args, prefix);
307 describe(*argv++, argc == 0);