Introduce usagef() that takes a printf-style format
[git.git] / builtin-describe.c
blobeaa8a9d229c97ebaab9ee3aa09d2456f68cd172c
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; /* Any valid ref can be used */
19 static int tags; /* Allow lightweight tags */
20 static int longformat;
21 static int abbrev = DEFAULT_ABBREV;
22 static int max_candidates = 10;
23 static int found_names;
24 static const char *pattern;
25 static int always;
27 struct commit_name {
28 struct tag *tag;
29 int prio; /* annotated tag = 2, tag = 1, head = 0 */
30 unsigned char sha1[20];
31 char path[FLEX_ARRAY]; /* more */
33 static const char *prio_names[] = {
34 "head", "lightweight", "annotated",
37 static void add_to_known_names(const char *path,
38 struct commit *commit,
39 int prio,
40 const unsigned char *sha1)
42 struct commit_name *e = commit->util;
43 if (!e || e->prio < prio) {
44 size_t len = strlen(path)+1;
45 free(e);
46 e = xmalloc(sizeof(struct commit_name) + len);
47 e->tag = NULL;
48 e->prio = prio;
49 hashcpy(e->sha1, sha1);
50 memcpy(e->path, path, len);
51 commit->util = e;
53 found_names = 1;
56 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
58 int might_be_tag = !prefixcmp(path, "refs/tags/");
59 struct commit *commit;
60 struct object *object;
61 unsigned char peeled[20];
62 int is_tag, prio;
64 if (!all && !might_be_tag)
65 return 0;
67 if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
68 commit = lookup_commit_reference_gently(peeled, 1);
69 if (!commit)
70 return 0;
71 is_tag = !!hashcmp(sha1, commit->object.sha1);
72 } else {
73 commit = lookup_commit_reference_gently(sha1, 1);
74 object = parse_object(sha1);
75 if (!commit || !object)
76 return 0;
77 is_tag = object->type == OBJ_TAG;
80 /* If --all, then any refs are used.
81 * If --tags, then any tags are used.
82 * Otherwise only annotated tags are used.
84 if (might_be_tag) {
85 if (is_tag)
86 prio = 2;
87 else
88 prio = 1;
90 if (pattern && fnmatch(pattern, path + 10, 0))
91 prio = 0;
93 else
94 prio = 0;
96 if (!all) {
97 if (!prio)
98 return 0;
99 if (!tags && prio < 2)
100 return 0;
102 add_to_known_names(all ? path + 5 : path + 10, commit, prio, sha1);
103 return 0;
106 struct possible_tag {
107 struct commit_name *name;
108 int depth;
109 int found_order;
110 unsigned flag_within;
113 static int compare_pt(const void *a_, const void *b_)
115 struct possible_tag *a = (struct possible_tag *)a_;
116 struct possible_tag *b = (struct possible_tag *)b_;
117 if (a->depth != b->depth)
118 return a->depth - b->depth;
119 if (a->found_order != b->found_order)
120 return a->found_order - b->found_order;
121 return 0;
124 static unsigned long finish_depth_computation(
125 struct commit_list **list,
126 struct possible_tag *best)
128 unsigned long seen_commits = 0;
129 while (*list) {
130 struct commit *c = pop_commit(list);
131 struct commit_list *parents = c->parents;
132 seen_commits++;
133 if (c->object.flags & best->flag_within) {
134 struct commit_list *a = *list;
135 while (a) {
136 struct commit *i = a->item;
137 if (!(i->object.flags & best->flag_within))
138 break;
139 a = a->next;
141 if (!a)
142 break;
143 } else
144 best->depth++;
145 while (parents) {
146 struct commit *p = parents->item;
147 parse_commit(p);
148 if (!(p->object.flags & SEEN))
149 insert_by_date(p, list);
150 p->object.flags |= c->object.flags;
151 parents = parents->next;
154 return seen_commits;
157 static void display_name(struct commit_name *n)
159 if (n->prio == 2 && !n->tag) {
160 n->tag = lookup_tag(n->sha1);
161 if (!n->tag || parse_tag(n->tag) || !n->tag->tag)
162 die("annotated tag %s not available", n->path);
163 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
164 warning("tag '%s' is really '%s' here", n->tag->tag, n->path);
167 if (n->tag)
168 printf("%s", n->tag->tag);
169 else
170 printf("%s", n->path);
173 static void show_suffix(int depth, const unsigned char *sha1)
175 printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
178 static void describe(const char *arg, int last_one)
180 unsigned char sha1[20];
181 struct commit *cmit, *gave_up_on = NULL;
182 struct commit_list *list;
183 struct commit_name *n;
184 struct possible_tag all_matches[MAX_TAGS];
185 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
186 unsigned long seen_commits = 0;
188 if (get_sha1(arg, sha1))
189 die("Not a valid object name %s", arg);
190 cmit = lookup_commit_reference(sha1);
191 if (!cmit)
192 die("%s is not a valid '%s' object", arg, commit_type);
194 n = cmit->util;
195 if (n) {
197 * Exact match to an existing ref.
199 display_name(n);
200 if (longformat)
201 show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
202 printf("\n");
203 return;
206 if (!max_candidates)
207 die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
208 if (debug)
209 fprintf(stderr, "searching to describe %s\n", arg);
211 list = NULL;
212 cmit->object.flags = SEEN;
213 commit_list_insert(cmit, &list);
214 while (list) {
215 struct commit *c = pop_commit(&list);
216 struct commit_list *parents = c->parents;
217 seen_commits++;
218 n = c->util;
219 if (n) {
220 if (match_cnt < max_candidates) {
221 struct possible_tag *t = &all_matches[match_cnt++];
222 t->name = n;
223 t->depth = seen_commits - 1;
224 t->flag_within = 1u << match_cnt;
225 t->found_order = match_cnt;
226 c->object.flags |= t->flag_within;
227 if (n->prio == 2)
228 annotated_cnt++;
230 else {
231 gave_up_on = c;
232 break;
235 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
236 struct possible_tag *t = &all_matches[cur_match];
237 if (!(c->object.flags & t->flag_within))
238 t->depth++;
240 if (annotated_cnt && !list) {
241 if (debug)
242 fprintf(stderr, "finished search at %s\n",
243 sha1_to_hex(c->object.sha1));
244 break;
246 while (parents) {
247 struct commit *p = parents->item;
248 parse_commit(p);
249 if (!(p->object.flags & SEEN))
250 insert_by_date(p, &list);
251 p->object.flags |= c->object.flags;
252 parents = parents->next;
256 if (!match_cnt) {
257 const unsigned char *sha1 = cmit->object.sha1;
258 if (always) {
259 printf("%s\n", find_unique_abbrev(sha1, abbrev));
260 return;
262 die("cannot describe '%s'", sha1_to_hex(sha1));
265 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
267 if (gave_up_on) {
268 insert_by_date(gave_up_on, &list);
269 seen_commits--;
271 seen_commits += finish_depth_computation(&list, &all_matches[0]);
272 free_commit_list(list);
274 if (debug) {
275 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
276 struct possible_tag *t = &all_matches[cur_match];
277 fprintf(stderr, " %-11s %8d %s\n",
278 prio_names[t->name->prio],
279 t->depth, t->name->path);
281 fprintf(stderr, "traversed %lu commits\n", seen_commits);
282 if (gave_up_on) {
283 fprintf(stderr,
284 "more than %i tags found; listed %i most recent\n"
285 "gave up search at %s\n",
286 max_candidates, max_candidates,
287 sha1_to_hex(gave_up_on->object.sha1));
291 display_name(all_matches[0].name);
292 if (abbrev)
293 show_suffix(all_matches[0].depth, cmit->object.sha1);
294 printf("\n");
296 if (!last_one)
297 clear_commit_marks(cmit, -1);
300 int cmd_describe(int argc, const char **argv, const char *prefix)
302 int contains = 0;
303 struct option options[] = {
304 OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
305 OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
306 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
307 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
308 OPT_BOOLEAN(0, "long", &longformat, "always use long format"),
309 OPT__ABBREV(&abbrev),
310 OPT_SET_INT(0, "exact-match", &max_candidates,
311 "only output exact matches", 0),
312 OPT_INTEGER(0, "candidates", &max_candidates,
313 "consider <n> most recent tags (default: 10)"),
314 OPT_STRING(0, "match", &pattern, "pattern",
315 "only consider tags matching <pattern>"),
316 OPT_BOOLEAN(0, "always", &always,
317 "show abbreviated commit object as fallback"),
318 OPT_END(),
321 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
322 if (max_candidates < 0)
323 max_candidates = 0;
324 else if (max_candidates > MAX_TAGS)
325 max_candidates = MAX_TAGS;
327 save_commit_buffer = 0;
329 if (longformat && abbrev == 0)
330 die("--long is incompatible with --abbrev=0");
332 if (contains) {
333 const char **args = xmalloc((7 + argc) * sizeof(char *));
334 int i = 0;
335 args[i++] = "name-rev";
336 args[i++] = "--name-only";
337 args[i++] = "--no-undefined";
338 if (always)
339 args[i++] = "--always";
340 if (!all) {
341 args[i++] = "--tags";
342 if (pattern) {
343 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
344 sprintf(s, "--refs=refs/tags/%s", pattern);
345 args[i++] = s;
348 memcpy(args + i, argv, argc * sizeof(char *));
349 args[i + argc] = NULL;
350 return cmd_name_rev(i + argc, args, prefix);
353 for_each_ref(get_name, NULL);
354 if (!found_names && !always)
355 die("No names found, cannot describe anything.");
357 if (argc == 0) {
358 describe("HEAD", 1);
359 } else {
360 while (argc-- > 0) {
361 describe(*argv++, argc == 0);
364 return 0;