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;
24 int prio; /* annotated tag = 2, tag = 1, head = 0 */
25 char path[FLEX_ARRAY]; /* more */
27 static const char *prio_names[] = {
28 "head", "lightweight", "annotated",
31 static void add_to_known_names(const char *path,
32 struct commit *commit,
35 struct commit_name *e = commit->util;
36 if (!e || e->prio < prio) {
37 size_t len = strlen(path)+1;
39 e = xmalloc(sizeof(struct commit_name) + len);
41 memcpy(e->path, path, len);
46 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
48 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
49 struct object *object;
54 object = parse_object(sha1);
55 /* If --all, then any refs are used.
56 * If --tags, then any tags are used.
57 * Otherwise only annotated tags are used.
59 if (!prefixcmp(path, "refs/tags/")) {
60 if (object->type == OBJ_TAG)
71 if (!tags && prio < 2)
74 add_to_known_names(all ? path + 5 : path + 10, commit, prio);
79 struct commit_name *name;
85 static int compare_pt(const void *a_, const void *b_)
87 struct possible_tag *a = (struct possible_tag *)a_;
88 struct possible_tag *b = (struct possible_tag *)b_;
89 if (a->name->prio != b->name->prio)
90 return b->name->prio - a->name->prio;
91 if (a->depth != b->depth)
92 return a->depth - b->depth;
93 if (a->found_order != b->found_order)
94 return a->found_order - b->found_order;
98 static unsigned long finish_depth_computation(
99 struct commit_list **list,
100 struct possible_tag *best)
102 unsigned long seen_commits = 0;
104 struct commit *c = pop_commit(list);
105 struct commit_list *parents = c->parents;
107 if (c->object.flags & best->flag_within) {
108 struct commit_list *a = *list;
110 struct commit *i = a->item;
111 if (!(i->object.flags & best->flag_within))
120 struct commit *p = parents->item;
122 if (!(p->object.flags & SEEN))
123 insert_by_date(p, list);
124 p->object.flags |= c->object.flags;
125 parents = parents->next;
131 static void describe(const char *arg, int last_one)
133 unsigned char sha1[20];
134 struct commit *cmit, *gave_up_on = NULL;
135 struct commit_list *list;
136 static int initialized = 0;
137 struct commit_name *n;
138 struct possible_tag all_matches[MAX_TAGS];
139 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
140 unsigned long seen_commits = 0;
142 if (get_sha1(arg, sha1))
143 die("Not a valid object name %s", arg);
144 cmit = lookup_commit_reference(sha1);
146 die("%s is not a valid '%s' object", arg, commit_type);
150 for_each_ref(get_name, NULL);
155 printf("%s\n", n->path);
160 fprintf(stderr, "searching to describe %s\n", arg);
163 cmit->object.flags = SEEN;
164 commit_list_insert(cmit, &list);
166 struct commit *c = pop_commit(&list);
167 struct commit_list *parents = c->parents;
171 if (match_cnt < max_candidates) {
172 struct possible_tag *t = &all_matches[match_cnt++];
174 t->depth = seen_commits - 1;
175 t->flag_within = 1u << match_cnt;
176 t->found_order = match_cnt;
177 c->object.flags |= t->flag_within;
186 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
187 struct possible_tag *t = &all_matches[cur_match];
188 if (!(c->object.flags & t->flag_within))
191 if (annotated_cnt && !list) {
193 fprintf(stderr, "finished search at %s\n",
194 sha1_to_hex(c->object.sha1));
198 struct commit *p = parents->item;
200 if (!(p->object.flags & SEEN))
201 insert_by_date(p, &list);
202 p->object.flags |= c->object.flags;
203 parents = parents->next;
208 die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
210 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
213 insert_by_date(gave_up_on, &list);
216 seen_commits += finish_depth_computation(&list, &all_matches[0]);
217 free_commit_list(list);
220 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
221 struct possible_tag *t = &all_matches[cur_match];
222 fprintf(stderr, " %-11s %8d %s\n",
223 prio_names[t->name->prio],
224 t->depth, t->name->path);
226 fprintf(stderr, "traversed %lu commits\n", seen_commits);
229 "more than %i tags found; listed %i most recent\n"
230 "gave up search at %s\n",
231 max_candidates, max_candidates,
232 sha1_to_hex(gave_up_on->object.sha1));
236 printf("%s\n", all_matches[0].name->path );
238 printf("%s-%d-g%s\n", all_matches[0].name->path,
239 all_matches[0].depth,
240 find_unique_abbrev(cmit->object.sha1, abbrev));
243 clear_commit_marks(cmit, -1);
246 int cmd_describe(int argc, const char **argv, const char *prefix)
249 struct option options[] = {
250 OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
251 OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
252 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
253 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
254 OPT__ABBREV(&abbrev),
255 OPT_INTEGER(0, "candidates", &max_candidates,
256 "consider <n> most recent tags (default: 10)"),
260 argc = parse_options(argc, argv, options, describe_usage, 0);
261 if (max_candidates < 1)
263 else if (max_candidates > MAX_TAGS)
264 max_candidates = MAX_TAGS;
266 save_commit_buffer = 0;
269 const char **args = xmalloc((4 + argc) * sizeof(char*));
270 args[0] = "name-rev";
271 args[1] = "--name-only";
273 memcpy(args + 3, argv, argc * sizeof(char*));
274 args[3 + argc] = NULL;
275 return cmd_name_rev(3 + argc, args, prefix);
282 describe(*argv++, argc == 0);