tag.c: Parse tagger date (if present)
[git/git.git] / builtin-describe.c
CommitLineData
908e5310
LT
1#include "cache.h"
2#include "commit.h"
635d4134 3#include "tag.h"
908e5310 4#include "refs.h"
9a0eaf83 5#include "builtin.h"
23615708 6#include "exec_cmd.h"
166185be 7#include "parse-options.h"
9f67d2e8 8#include "diff.h"
908e5310 9
8713ab30
SP
10#define SEEN (1u<<0)
11#define MAX_TAGS (FLAG_BITS - 1)
12
166185be 13static const char * const describe_usage[] = {
1b1dd23f 14 "git describe [options] <committish>*",
9f67d2e8 15 "git describe [options] --dirty",
166185be
PH
16 NULL
17};
908e5310 18
8713ab30 19static int debug; /* Display lots of verbose info */
7e425c4f
SP
20static int all; /* Any valid ref can be used */
21static int tags; /* Allow lightweight tags */
518120e3 22static int longformat;
2d9e7c9f 23static int abbrev = DEFAULT_ABBREV;
8713ab30 24static int max_candidates = 10;
d68dc34c 25static int found_names;
cb2a1cc2 26static const char *pattern;
da2478db 27static int always;
9f67d2e8
JP
28static const char *dirty;
29
30/* diff-index command arguments to check if working tree is dirty. */
31static const char *diff_index_args[] = {
32 "diff-index", "--quiet", "HEAD", "--", NULL
33};
34
908e5310 35
e7eb5034 36struct commit_name {
212945d4 37 struct tag *tag;
64deb858 38 int prio; /* annotated tag = 2, tag = 1, head = 0 */
212945d4 39 unsigned char sha1[20];
5a2282de 40 char path[FLEX_ARRAY]; /* more */
e7eb5034 41};
cf69fd49
SP
42static const char *prio_names[] = {
43 "head", "lightweight", "annotated",
44};
908e5310 45
64deb858 46static void add_to_known_names(const char *path,
80dbae03 47 struct commit *commit,
212945d4
SP
48 int prio,
49 const unsigned char *sha1)
908e5310 50{
e7eb5034
SP
51 struct commit_name *e = commit->util;
52 if (!e || e->prio < prio) {
53 size_t len = strlen(path)+1;
54 free(e);
55 e = xmalloc(sizeof(struct commit_name) + len);
212945d4 56 e->tag = NULL;
e7eb5034 57 e->prio = prio;
212945d4 58 hashcpy(e->sha1, sha1);
e7eb5034
SP
59 memcpy(e->path, path, len);
60 commit->util = e;
908e5310 61 }
d68dc34c 62 found_names = 1;
908e5310
LT
63}
64
8da19775 65static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
908e5310 66{
8a5a1884 67 int might_be_tag = !prefixcmp(path, "refs/tags/");
feededd0 68 struct commit *commit;
64deb858 69 struct object *object;
feededd0
SP
70 unsigned char peeled[20];
71 int is_tag, prio;
72
8a5a1884
SP
73 if (!all && !might_be_tag)
74 return 0;
75
feededd0
SP
76 if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
77 commit = lookup_commit_reference_gently(peeled, 1);
78 if (!commit)
79 return 0;
80 is_tag = !!hashcmp(sha1, commit->object.sha1);
81 } else {
82 commit = lookup_commit_reference_gently(sha1, 1);
83 object = parse_object(sha1);
84 if (!commit || !object)
85 return 0;
86 is_tag = object->type == OBJ_TAG;
87 }
64deb858 88
2d9e7c9f
JH
89 /* If --all, then any refs are used.
90 * If --tags, then any tags are used.
91 * Otherwise only annotated tags are used.
92 */
8a5a1884 93 if (might_be_tag) {
4ed19a3c 94 if (is_tag)
64deb858 95 prio = 2;
4ed19a3c 96 else
64deb858 97 prio = 1;
4ed19a3c
MD
98
99 if (pattern && fnmatch(pattern, path + 10, 0))
100 prio = 0;
64deb858
JH
101 }
102 else
103 prio = 0;
104
635d4134 105 if (!all) {
64deb858
JH
106 if (!prio)
107 return 0;
635d4134 108 }
212945d4 109 add_to_known_names(all ? path + 5 : path + 10, commit, prio, sha1);
908e5310
LT
110 return 0;
111}
112
80dbae03 113struct possible_tag {
80dbae03 114 struct commit_name *name;
cf69fd49
SP
115 int depth;
116 int found_order;
8713ab30 117 unsigned flag_within;
80dbae03
SP
118};
119
cf69fd49
SP
120static int compare_pt(const void *a_, const void *b_)
121{
122 struct possible_tag *a = (struct possible_tag *)a_;
123 struct possible_tag *b = (struct possible_tag *)b_;
cf69fd49
SP
124 if (a->depth != b->depth)
125 return a->depth - b->depth;
126 if (a->found_order != b->found_order)
127 return a->found_order - b->found_order;
128 return 0;
129}
130
1b600e65
SP
131static unsigned long finish_depth_computation(
132 struct commit_list **list,
133 struct possible_tag *best)
134{
135 unsigned long seen_commits = 0;
136 while (*list) {
137 struct commit *c = pop_commit(list);
138 struct commit_list *parents = c->parents;
139 seen_commits++;
140 if (c->object.flags & best->flag_within) {
141 struct commit_list *a = *list;
142 while (a) {
143 struct commit *i = a->item;
144 if (!(i->object.flags & best->flag_within))
145 break;
146 a = a->next;
147 }
148 if (!a)
149 break;
150 } else
151 best->depth++;
152 while (parents) {
153 struct commit *p = parents->item;
154 parse_commit(p);
155 if (!(p->object.flags & SEEN))
156 insert_by_date(p, list);
157 p->object.flags |= c->object.flags;
158 parents = parents->next;
159 }
160 }
161 return seen_commits;
162}
163
212945d4
SP
164static void display_name(struct commit_name *n)
165{
166 if (n->prio == 2 && !n->tag) {
167 n->tag = lookup_tag(n->sha1);
3167d725 168 if (!n->tag || parse_tag(n->tag) || !n->tag->tag)
212945d4 169 die("annotated tag %s not available", n->path);
81dc223d 170 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
212945d4
SP
171 warning("tag '%s' is really '%s' here", n->tag->tag, n->path);
172 }
173
174 if (n->tag)
175 printf("%s", n->tag->tag);
176 else
177 printf("%s", n->path);
870cf7d6
JH
178}
179
180static void show_suffix(int depth, const unsigned char *sha1)
181{
182 printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
212945d4
SP
183}
184
554fe20d 185static void describe(const char *arg, int last_one)
908e5310 186{
4c34a2c5 187 unsigned char sha1[20];
8713ab30 188 struct commit *cmit, *gave_up_on = NULL;
908e5310 189 struct commit_list *list;
908e5310 190 struct commit_name *n;
cf69fd49 191 struct possible_tag all_matches[MAX_TAGS];
8713ab30
SP
192 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
193 unsigned long seen_commits = 0;
4d23660e 194 unsigned int unannotated_cnt = 0;
908e5310 195
31fff305
DL
196 if (get_sha1(arg, sha1))
197 die("Not a valid object name %s", arg);
4c34a2c5
JH
198 cmit = lookup_commit_reference(sha1);
199 if (!cmit)
31fff305 200 die("%s is not a valid '%s' object", arg, commit_type);
4c34a2c5 201
e7eb5034 202 n = cmit->util;
7a0d61bb 203 if (n && (tags || all || n->prio == 2)) {
870cf7d6
JH
204 /*
205 * Exact match to an existing ref.
206 */
212945d4 207 display_name(n);
870cf7d6 208 if (longformat)
14d4642e 209 show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
9f67d2e8
JP
210 if (dirty)
211 printf("%s", dirty);
212945d4 212 printf("\n");
908e5310
LT
213 return;
214 }
215
2c33f757
SP
216 if (!max_candidates)
217 die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
8713ab30
SP
218 if (debug)
219 fprintf(stderr, "searching to describe %s\n", arg);
220
908e5310 221 list = NULL;
8713ab30 222 cmit->object.flags = SEEN;
908e5310
LT
223 commit_list_insert(cmit, &list);
224 while (list) {
80dbae03 225 struct commit *c = pop_commit(&list);
dccd0c2a 226 struct commit_list *parents = c->parents;
8713ab30 227 seen_commits++;
e7eb5034 228 n = c->util;
908e5310 229 if (n) {
4d23660e
TR
230 if (!tags && !all && n->prio < 2) {
231 unannotated_cnt++;
232 } else if (match_cnt < max_candidates) {
8713ab30
SP
233 struct possible_tag *t = &all_matches[match_cnt++];
234 t->name = n;
235 t->depth = seen_commits - 1;
236 t->flag_within = 1u << match_cnt;
8a8169c0 237 t->found_order = match_cnt;
8713ab30
SP
238 c->object.flags |= t->flag_within;
239 if (n->prio == 2)
240 annotated_cnt++;
241 }
242 else {
243 gave_up_on = c;
244 break;
245 }
246 }
247 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
248 struct possible_tag *t = &all_matches[cur_match];
249 if (!(c->object.flags & t->flag_within))
250 t->depth++;
251 }
252 if (annotated_cnt && !list) {
253 if (debug)
254 fprintf(stderr, "finished search at %s\n",
255 sha1_to_hex(c->object.sha1));
256 break;
dccd0c2a
SP
257 }
258 while (parents) {
259 struct commit *p = parents->item;
260 parse_commit(p);
8713ab30 261 if (!(p->object.flags & SEEN))
dccd0c2a 262 insert_by_date(p, &list);
8713ab30 263 p->object.flags |= c->object.flags;
dccd0c2a 264 parents = parents->next;
80dbae03
SP
265 }
266 }
267
da2478db
JH
268 if (!match_cnt) {
269 const unsigned char *sha1 = cmit->object.sha1;
270 if (always) {
9f67d2e8
JP
271 printf("%s", find_unique_abbrev(sha1, abbrev));
272 if (dirty)
273 printf("%s", dirty);
274 printf("\n");
da2478db
JH
275 return;
276 }
4d23660e
TR
277 if (unannotated_cnt)
278 die("No annotated tags can describe '%s'.\n"
279 "However, there were unannotated tags: try --tags.",
280 sha1_to_hex(sha1));
281 else
282 die("No tags can describe '%s'.\n"
283 "Try --always, or create some tags.",
284 sha1_to_hex(sha1));
da2478db 285 }
80dbae03 286
cf69fd49 287 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
1b600e65
SP
288
289 if (gave_up_on) {
290 insert_by_date(gave_up_on, &list);
291 seen_commits--;
292 }
293 seen_commits += finish_depth_computation(&list, &all_matches[0]);
294 free_commit_list(list);
295
8713ab30
SP
296 if (debug) {
297 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
298 struct possible_tag *t = &all_matches[cur_match];
cf69fd49
SP
299 fprintf(stderr, " %-11s %8d %s\n",
300 prio_names[t->name->prio],
8713ab30
SP
301 t->depth, t->name->path);
302 }
303 fprintf(stderr, "traversed %lu commits\n", seen_commits);
304 if (gave_up_on) {
305 fprintf(stderr,
306 "more than %i tags found; listed %i most recent\n"
307 "gave up search at %s\n",
308 max_candidates, max_candidates,
309 sha1_to_hex(gave_up_on->object.sha1));
310 }
80dbae03 311 }
212945d4
SP
312
313 display_name(all_matches[0].name);
314 if (abbrev)
870cf7d6 315 show_suffix(all_matches[0].depth, cmit->object.sha1);
9f67d2e8
JP
316 if (dirty)
317 printf("%s", dirty);
212945d4 318 printf("\n");
80dbae03 319
8713ab30
SP
320 if (!last_one)
321 clear_commit_marks(cmit, -1);
908e5310
LT
322}
323
9a0eaf83 324int cmd_describe(int argc, const char **argv, const char *prefix)
908e5310 325{
23615708 326 int contains = 0;
166185be
PH
327 struct option options[] = {
328 OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
329 OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
330 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
331 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
518120e3 332 OPT_BOOLEAN(0, "long", &longformat, "always use long format"),
166185be 333 OPT__ABBREV(&abbrev),
2c33f757
SP
334 OPT_SET_INT(0, "exact-match", &max_candidates,
335 "only output exact matches", 0),
166185be 336 OPT_INTEGER(0, "candidates", &max_candidates,
30ffa603
PH
337 "consider <n> most recent tags (default: 10)"),
338 OPT_STRING(0, "match", &pattern, "pattern",
339 "only consider tags matching <pattern>"),
da2478db
JH
340 OPT_BOOLEAN(0, "always", &always,
341 "show abbreviated commit object as fallback"),
9f67d2e8
JP
342 {OPTION_STRING, 0, "dirty", &dirty, "mark",
343 "append <mark> on dirty working tree (default: \"-dirty\")",
344 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
166185be
PH
345 OPT_END(),
346 };
908e5310 347
37782920 348 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
2c33f757
SP
349 if (max_candidates < 0)
350 max_candidates = 0;
166185be
PH
351 else if (max_candidates > MAX_TAGS)
352 max_candidates = MAX_TAGS;
4c34a2c5 353
8c599c74 354 save_commit_buffer = 0;
8112894d 355
518120e3
SB
356 if (longformat && abbrev == 0)
357 die("--long is incompatible with --abbrev=0");
358
23615708 359 if (contains) {
4b25d091 360 const char **args = xmalloc((7 + argc) * sizeof(char *));
3f7701a4
NP
361 int i = 0;
362 args[i++] = "name-rev";
363 args[i++] = "--name-only";
a2cf9f44 364 args[i++] = "--no-undefined";
da2478db
JH
365 if (always)
366 args[i++] = "--always";
30ffa603 367 if (!all) {
3f7701a4 368 args[i++] = "--tags";
30ffa603
PH
369 if (pattern) {
370 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
371 sprintf(s, "--refs=refs/tags/%s", pattern);
372 args[i++] = s;
373 }
374 }
4b25d091 375 memcpy(args + i, argv, argc * sizeof(char *));
3f7701a4
NP
376 args[i + argc] = NULL;
377 return cmd_name_rev(i + argc, args, prefix);
23615708
SP
378 }
379
fb423da0 380 for_each_ref(get_name, NULL);
02d56fab 381 if (!found_names && !always)
fb423da0
RS
382 die("No names found, cannot describe anything.");
383
166185be 384 if (argc == 0) {
9f67d2e8
JP
385 if (dirty && !cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1, diff_index_args, prefix))
386 dirty = NULL;
fec9ebf1 387 describe("HEAD", 1);
9f67d2e8
JP
388 } else if (dirty) {
389 die("--dirty is incompatible with committishes");
166185be
PH
390 } else {
391 while (argc-- > 0) {
392 describe(*argv++, argc == 0);
fec9ebf1 393 }
166185be 394 }
908e5310
LT
395 return 0;
396}