Merge branch 'maint'
[debian-git.git] / builtin-describe.c
blob7a5ab012b1fabe3f67cd2d9bc776794e34431d80
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "refs.h"
5 #include "builtin.h"
6 #include "exec_cmd.h"
7 #include "parse-options.h"
9 #define SEEN (1u<<0)
10 #define MAX_TAGS (FLAG_BITS - 1)
12 static const char * const describe_usage[] = {
13 "git-describe [options] <committish>*",
14 NULL
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 longformat;
21 static int abbrev = DEFAULT_ABBREV;
22 static int max_candidates = 10;
23 const char *pattern = NULL;
25 struct commit_name {
26 struct tag *tag;
27 int prio; /* annotated tag = 2, tag = 1, head = 0 */
28 unsigned char sha1[20];
29 char path[FLEX_ARRAY]; /* more */
31 static const char *prio_names[] = {
32 "head", "lightweight", "annotated",
35 static void add_to_known_names(const char *path,
36 struct commit *commit,
37 int prio,
38 const unsigned char *sha1)
40 struct commit_name *e = commit->util;
41 if (!e || e->prio < prio) {
42 size_t len = strlen(path)+1;
43 free(e);
44 e = xmalloc(sizeof(struct commit_name) + len);
45 e->tag = NULL;
46 e->prio = prio;
47 hashcpy(e->sha1, sha1);
48 memcpy(e->path, path, len);
49 commit->util = e;
53 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
55 int might_be_tag = !prefixcmp(path, "refs/tags/");
56 struct commit *commit;
57 struct object *object;
58 unsigned char peeled[20];
59 int is_tag, prio;
61 if (!all && !might_be_tag)
62 return 0;
64 if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
65 commit = lookup_commit_reference_gently(peeled, 1);
66 if (!commit)
67 return 0;
68 is_tag = !!hashcmp(sha1, commit->object.sha1);
69 } else {
70 commit = lookup_commit_reference_gently(sha1, 1);
71 object = parse_object(sha1);
72 if (!commit || !object)
73 return 0;
74 is_tag = object->type == OBJ_TAG;
77 /* If --all, then any refs are used.
78 * If --tags, then any tags are used.
79 * Otherwise only annotated tags are used.
81 if (might_be_tag) {
82 if (is_tag) {
83 prio = 2;
84 if (pattern && fnmatch(pattern, path + 10, 0))
85 prio = 0;
86 } else
87 prio = 1;
89 else
90 prio = 0;
92 if (!all) {
93 if (!prio)
94 return 0;
95 if (!tags && prio < 2)
96 return 0;
98 add_to_known_names(all ? path + 5 : path + 10, commit, prio, sha1);
99 return 0;
102 struct possible_tag {
103 struct commit_name *name;
104 int depth;
105 int found_order;
106 unsigned flag_within;
109 static int compare_pt(const void *a_, const void *b_)
111 struct possible_tag *a = (struct possible_tag *)a_;
112 struct possible_tag *b = (struct possible_tag *)b_;
113 if (a->name->prio != b->name->prio)
114 return b->name->prio - a->name->prio;
115 if (a->depth != b->depth)
116 return a->depth - b->depth;
117 if (a->found_order != b->found_order)
118 return a->found_order - b->found_order;
119 return 0;
122 static unsigned long finish_depth_computation(
123 struct commit_list **list,
124 struct possible_tag *best)
126 unsigned long seen_commits = 0;
127 while (*list) {
128 struct commit *c = pop_commit(list);
129 struct commit_list *parents = c->parents;
130 seen_commits++;
131 if (c->object.flags & best->flag_within) {
132 struct commit_list *a = *list;
133 while (a) {
134 struct commit *i = a->item;
135 if (!(i->object.flags & best->flag_within))
136 break;
137 a = a->next;
139 if (!a)
140 break;
141 } else
142 best->depth++;
143 while (parents) {
144 struct commit *p = parents->item;
145 parse_commit(p);
146 if (!(p->object.flags & SEEN))
147 insert_by_date(p, list);
148 p->object.flags |= c->object.flags;
149 parents = parents->next;
152 return seen_commits;
155 static void display_name(struct commit_name *n)
157 if (n->prio == 2 && !n->tag) {
158 n->tag = lookup_tag(n->sha1);
159 if (!n->tag || parse_tag(n->tag) || !n->tag->tag)
160 die("annotated tag %s not available", n->path);
161 if (strcmp(n->tag->tag, n->path))
162 warning("tag '%s' is really '%s' here", n->tag->tag, n->path);
165 if (n->tag)
166 printf("%s", n->tag->tag);
167 else
168 printf("%s", n->path);
171 static void show_suffix(int depth, const unsigned char *sha1)
173 printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
176 static void describe(const char *arg, int last_one)
178 unsigned char sha1[20];
179 struct commit *cmit, *gave_up_on = NULL;
180 struct commit_list *list;
181 static int initialized = 0;
182 struct commit_name *n;
183 struct possible_tag all_matches[MAX_TAGS];
184 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
185 unsigned long seen_commits = 0;
187 if (get_sha1(arg, sha1))
188 die("Not a valid object name %s", arg);
189 cmit = lookup_commit_reference(sha1);
190 if (!cmit)
191 die("%s is not a valid '%s' object", arg, commit_type);
193 if (!initialized) {
194 initialized = 1;
195 for_each_ref(get_name, NULL);
198 n = cmit->util;
199 if (n) {
201 * Exact match to an existing ref.
203 display_name(n);
204 if (longformat)
205 show_suffix(0, n->tag->tagged->sha1);
206 printf("\n");
207 return;
210 if (!max_candidates)
211 die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
212 if (debug)
213 fprintf(stderr, "searching to describe %s\n", arg);
215 list = NULL;
216 cmit->object.flags = SEEN;
217 commit_list_insert(cmit, &list);
218 while (list) {
219 struct commit *c = pop_commit(&list);
220 struct commit_list *parents = c->parents;
221 seen_commits++;
222 n = c->util;
223 if (n) {
224 if (match_cnt < max_candidates) {
225 struct possible_tag *t = &all_matches[match_cnt++];
226 t->name = n;
227 t->depth = seen_commits - 1;
228 t->flag_within = 1u << match_cnt;
229 t->found_order = match_cnt;
230 c->object.flags |= t->flag_within;
231 if (n->prio == 2)
232 annotated_cnt++;
234 else {
235 gave_up_on = c;
236 break;
239 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
240 struct possible_tag *t = &all_matches[cur_match];
241 if (!(c->object.flags & t->flag_within))
242 t->depth++;
244 if (annotated_cnt && !list) {
245 if (debug)
246 fprintf(stderr, "finished search at %s\n",
247 sha1_to_hex(c->object.sha1));
248 break;
250 while (parents) {
251 struct commit *p = parents->item;
252 parse_commit(p);
253 if (!(p->object.flags & SEEN))
254 insert_by_date(p, &list);
255 p->object.flags |= c->object.flags;
256 parents = parents->next;
260 if (!match_cnt)
261 die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
263 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
265 if (gave_up_on) {
266 insert_by_date(gave_up_on, &list);
267 seen_commits--;
269 seen_commits += finish_depth_computation(&list, &all_matches[0]);
270 free_commit_list(list);
272 if (debug) {
273 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
274 struct possible_tag *t = &all_matches[cur_match];
275 fprintf(stderr, " %-11s %8d %s\n",
276 prio_names[t->name->prio],
277 t->depth, t->name->path);
279 fprintf(stderr, "traversed %lu commits\n", seen_commits);
280 if (gave_up_on) {
281 fprintf(stderr,
282 "more than %i tags found; listed %i most recent\n"
283 "gave up search at %s\n",
284 max_candidates, max_candidates,
285 sha1_to_hex(gave_up_on->object.sha1));
289 display_name(all_matches[0].name);
290 if (abbrev)
291 show_suffix(all_matches[0].depth, cmit->object.sha1);
292 printf("\n");
294 if (!last_one)
295 clear_commit_marks(cmit, -1);
298 int cmd_describe(int argc, const char **argv, const char *prefix)
300 int contains = 0;
301 struct option options[] = {
302 OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
303 OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
304 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
305 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
306 OPT_BOOLEAN(0, "long", &longformat, "always use long format"),
307 OPT__ABBREV(&abbrev),
308 OPT_SET_INT(0, "exact-match", &max_candidates,
309 "only output exact matches", 0),
310 OPT_INTEGER(0, "candidates", &max_candidates,
311 "consider <n> most recent tags (default: 10)"),
312 OPT_STRING(0, "match", &pattern, "pattern",
313 "only consider tags matching <pattern>"),
314 OPT_END(),
317 argc = parse_options(argc, argv, options, describe_usage, 0);
318 if (max_candidates < 0)
319 max_candidates = 0;
320 else if (max_candidates > MAX_TAGS)
321 max_candidates = MAX_TAGS;
323 save_commit_buffer = 0;
325 if (longformat && abbrev == 0)
326 die("--long is incompatible with --abbrev=0");
328 if (contains) {
329 const char **args = xmalloc((6 + argc) * sizeof(char*));
330 int i = 0;
331 args[i++] = "name-rev";
332 args[i++] = "--name-only";
333 args[i++] = "--no-undefined";
334 if (!all) {
335 args[i++] = "--tags";
336 if (pattern) {
337 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
338 sprintf(s, "--refs=refs/tags/%s", pattern);
339 args[i++] = s;
342 memcpy(args + i, argv, argc * sizeof(char*));
343 args[i + argc] = NULL;
344 return cmd_name_rev(i + argc, args, prefix);
347 if (argc == 0) {
348 describe("HEAD", 1);
349 } else {
350 while (argc-- > 0) {
351 describe(*argv++, argc == 0);
354 return 0;