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 = lookup_commit_reference_gently(sha1, 1);
50 struct object *object;
55 object = parse_object(sha1);
56 /* If --all, then any refs are used.
57 * If --tags, then any tags are used.
58 * Otherwise only annotated tags are used.
60 if (!prefixcmp(path, "refs/tags/")) {
61 if (object->type == OBJ_TAG) {
63 if (pattern && fnmatch(pattern, path + 10, 0))
74 if (!tags && prio < 2)
77 add_to_known_names(all ? path + 5 : path + 10, commit, prio);
82 struct commit_name *name;
88 static int compare_pt(const void *a_, const void *b_)
90 struct possible_tag *a = (struct possible_tag *)a_;
91 struct possible_tag *b = (struct possible_tag *)b_;
92 if (a->name->prio != b->name->prio)
93 return b->name->prio - a->name->prio;
94 if (a->depth != b->depth)
95 return a->depth - b->depth;
96 if (a->found_order != b->found_order)
97 return a->found_order - b->found_order;
101 static unsigned long finish_depth_computation(
102 struct commit_list **list,
103 struct possible_tag *best)
105 unsigned long seen_commits = 0;
107 struct commit *c = pop_commit(list);
108 struct commit_list *parents = c->parents;
110 if (c->object.flags & best->flag_within) {
111 struct commit_list *a = *list;
113 struct commit *i = a->item;
114 if (!(i->object.flags & best->flag_within))
123 struct commit *p = parents->item;
125 if (!(p->object.flags & SEEN))
126 insert_by_date(p, list);
127 p->object.flags |= c->object.flags;
128 parents = parents->next;
134 static void describe(const char *arg, int last_one)
136 unsigned char sha1[20];
137 struct commit *cmit, *gave_up_on = NULL;
138 struct commit_list *list;
139 static int initialized = 0;
140 struct commit_name *n;
141 struct possible_tag all_matches[MAX_TAGS];
142 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
143 unsigned long seen_commits = 0;
145 if (get_sha1(arg, sha1))
146 die("Not a valid object name %s", arg);
147 cmit = lookup_commit_reference(sha1);
149 die("%s is not a valid '%s' object", arg, commit_type);
153 for_each_ref(get_name, NULL);
158 printf("%s\n", n->path);
163 fprintf(stderr, "searching to describe %s\n", arg);
166 cmit->object.flags = SEEN;
167 commit_list_insert(cmit, &list);
169 struct commit *c = pop_commit(&list);
170 struct commit_list *parents = c->parents;
174 if (match_cnt < max_candidates) {
175 struct possible_tag *t = &all_matches[match_cnt++];
177 t->depth = seen_commits - 1;
178 t->flag_within = 1u << match_cnt;
179 t->found_order = match_cnt;
180 c->object.flags |= t->flag_within;
189 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
190 struct possible_tag *t = &all_matches[cur_match];
191 if (!(c->object.flags & t->flag_within))
194 if (annotated_cnt && !list) {
196 fprintf(stderr, "finished search at %s\n",
197 sha1_to_hex(c->object.sha1));
201 struct commit *p = parents->item;
203 if (!(p->object.flags & SEEN))
204 insert_by_date(p, &list);
205 p->object.flags |= c->object.flags;
206 parents = parents->next;
211 die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
213 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
216 insert_by_date(gave_up_on, &list);
219 seen_commits += finish_depth_computation(&list, &all_matches[0]);
220 free_commit_list(list);
223 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
224 struct possible_tag *t = &all_matches[cur_match];
225 fprintf(stderr, " %-11s %8d %s\n",
226 prio_names[t->name->prio],
227 t->depth, t->name->path);
229 fprintf(stderr, "traversed %lu commits\n", seen_commits);
232 "more than %i tags found; listed %i most recent\n"
233 "gave up search at %s\n",
234 max_candidates, max_candidates,
235 sha1_to_hex(gave_up_on->object.sha1));
239 printf("%s\n", all_matches[0].name->path );
241 printf("%s-%d-g%s\n", all_matches[0].name->path,
242 all_matches[0].depth,
243 find_unique_abbrev(cmit->object.sha1, abbrev));
246 clear_commit_marks(cmit, -1);
249 int cmd_describe(int argc, const char **argv, const char *prefix)
252 struct option options[] = {
253 OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
254 OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
255 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
256 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
257 OPT__ABBREV(&abbrev),
258 OPT_INTEGER(0, "candidates", &max_candidates,
259 "consider <n> most recent tags (default: 10)"),
260 OPT_STRING(0, "match", &pattern, "pattern",
261 "only consider tags matching <pattern>"),
265 argc = parse_options(argc, argv, options, describe_usage, 0);
266 if (max_candidates < 1)
268 else if (max_candidates > MAX_TAGS)
269 max_candidates = MAX_TAGS;
271 save_commit_buffer = 0;
274 const char **args = xmalloc((6 + argc) * sizeof(char*));
276 args[i++] = "name-rev";
277 args[i++] = "--name-only";
278 args[i++] = "--no-undefined";
280 args[i++] = "--tags";
282 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
283 sprintf(s, "--refs=refs/tags/%s", pattern);
287 memcpy(args + i, argv, argc * sizeof(char*));
288 args[i + argc] = NULL;
289 return cmd_name_rev(i + argc, args, prefix);
296 describe(*argv++, argc == 0);