Remove deprecated OPTION_BOOLEAN for parsing arguments
[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"
3cfa4db3 9#include "hash.h"
45bc950b 10#include "argv-array.h"
908e5310 11
8713ab30
SP
12#define SEEN (1u<<0)
13#define MAX_TAGS (FLAG_BITS - 1)
14
166185be 15static const char * const describe_usage[] = {
bfb0737d
NTND
16 N_("git describe [options] <committish>*"),
17 N_("git describe [options] --dirty"),
166185be
PH
18 NULL
19};
908e5310 20
8713ab30 21static int debug; /* Display lots of verbose info */
7e425c4f
SP
22static int all; /* Any valid ref can be used */
23static int tags; /* Allow lightweight tags */
518120e3 24static int longformat;
e00dd1e9 25static int first_parent;
dce96489 26static int abbrev = -1; /* unspecified */
8713ab30 27static int max_candidates = 10;
3cfa4db3 28static struct hash_table names;
d1645d02 29static int have_util;
cb2a1cc2 30static const char *pattern;
da2478db 31static int always;
9f67d2e8
JP
32static const char *dirty;
33
34/* diff-index command arguments to check if working tree is dirty. */
35static const char *diff_index_args[] = {
36 "diff-index", "--quiet", "HEAD", "--", NULL
37};
38
908e5310 39
e7eb5034 40struct commit_name {
3cfa4db3
AK
41 struct commit_name *next;
42 unsigned char peeled[20];
212945d4 43 struct tag *tag;
03e8b541
SP
44 unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
45 unsigned name_checked:1;
212945d4 46 unsigned char sha1[20];
219a0f33 47 char *path;
e7eb5034 48};
cf69fd49
SP
49static const char *prio_names[] = {
50 "head", "lightweight", "annotated",
51};
908e5310 52
3cfa4db3
AK
53static inline unsigned int hash_sha1(const unsigned char *sha1)
54{
55 unsigned int hash;
56 memcpy(&hash, sha1, sizeof(hash));
57 return hash;
58}
59
60static inline struct commit_name *find_commit_name(const unsigned char *peeled)
61{
62 struct commit_name *n = lookup_hash(hash_sha1(peeled), &names);
63 while (n && !!hashcmp(peeled, n->peeled))
64 n = n->next;
65 return n;
66}
67
11f944dd 68static int set_util(void *chain, void *data)
d1645d02
AK
69{
70 struct commit_name *n;
71 for (n = chain; n; n = n->next) {
72 struct commit *c = lookup_commit_reference_gently(n->peeled, 1);
73 if (c)
74 c->util = n;
75 }
76 return 0;
77}
78
03e8b541
SP
79static int replace_name(struct commit_name *e,
80 int prio,
81 const unsigned char *sha1,
82 struct tag **tag)
83{
84 if (!e || e->prio < prio)
85 return 1;
86
87 if (e->prio == 2 && prio == 2) {
88 /* Multiple annotated tags point to the same commit.
89 * Select one to keep based upon their tagger date.
90 */
91 struct tag *t;
92
93 if (!e->tag) {
94 t = lookup_tag(e->sha1);
95 if (!t || parse_tag(t))
96 return 1;
97 e->tag = t;
98 }
99
100 t = lookup_tag(sha1);
101 if (!t || parse_tag(t))
102 return 0;
103 *tag = t;
104
105 if (e->tag->date < t->date)
106 return 1;
107 }
108
109 return 0;
110}
111
64deb858 112static void add_to_known_names(const char *path,
d1645d02 113 const unsigned char *peeled,
212945d4
SP
114 int prio,
115 const unsigned char *sha1)
908e5310 116{
3cfa4db3 117 struct commit_name *e = find_commit_name(peeled);
03e8b541
SP
118 struct tag *tag = NULL;
119 if (replace_name(e, prio, sha1, &tag)) {
1e1ade18 120 if (!e) {
3cfa4db3 121 void **pos;
1e1ade18 122 e = xmalloc(sizeof(struct commit_name));
3cfa4db3
AK
123 hashcpy(e->peeled, peeled);
124 pos = insert_hash(hash_sha1(peeled), e, &names);
125 if (pos) {
126 e->next = *pos;
127 *pos = e;
128 } else {
129 e->next = NULL;
130 }
219a0f33 131 e->path = NULL;
1e1ade18 132 }
03e8b541 133 e->tag = tag;
e7eb5034 134 e->prio = prio;
03e8b541 135 e->name_checked = 0;
212945d4 136 hashcpy(e->sha1, sha1);
219a0f33
MH
137 free(e->path);
138 e->path = xstrdup(path);
908e5310 139 }
908e5310
LT
140}
141
8da19775 142static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
908e5310 143{
46e1d6eb 144 int is_tag = !prefixcmp(path, "refs/tags/");
feededd0 145 unsigned char peeled[20];
46e1d6eb 146 int is_annotated, prio;
feededd0 147
46e1d6eb
JH
148 /* Reject anything outside refs/tags/ unless --all */
149 if (!all && !is_tag)
8a5a1884
SP
150 return 0;
151
46e1d6eb
JH
152 /* Accept only tags that match the pattern, if given */
153 if (pattern && (!is_tag || fnmatch(pattern, path + 10, 0)))
154 return 0;
155
156 /* Is it annotated? */
e6dbffa6 157 if (!peel_ref(path, peeled)) {
46e1d6eb 158 is_annotated = !!hashcmp(sha1, peeled);
feededd0 159 } else {
d1645d02 160 hashcpy(peeled, sha1);
46e1d6eb 161 is_annotated = 0;
feededd0 162 }
64deb858 163
46e1d6eb
JH
164 /*
165 * By default, we only use annotated tags, but with --tags
166 * we fall back to lightweight ones (even without --tags,
167 * we still remember lightweight ones, only to give hints
168 * in an error message). --all allows any refs to be used.
2d9e7c9f 169 */
46e1d6eb
JH
170 if (is_annotated)
171 prio = 2;
172 else if (is_tag)
173 prio = 1;
64deb858
JH
174 else
175 prio = 0;
176
d1645d02 177 add_to_known_names(all ? path + 5 : path + 10, peeled, prio, sha1);
908e5310
LT
178 return 0;
179}
180
80dbae03 181struct possible_tag {
80dbae03 182 struct commit_name *name;
cf69fd49
SP
183 int depth;
184 int found_order;
8713ab30 185 unsigned flag_within;
80dbae03
SP
186};
187
cf69fd49
SP
188static int compare_pt(const void *a_, const void *b_)
189{
190 struct possible_tag *a = (struct possible_tag *)a_;
191 struct possible_tag *b = (struct possible_tag *)b_;
cf69fd49
SP
192 if (a->depth != b->depth)
193 return a->depth - b->depth;
194 if (a->found_order != b->found_order)
195 return a->found_order - b->found_order;
196 return 0;
197}
198
1b600e65
SP
199static unsigned long finish_depth_computation(
200 struct commit_list **list,
201 struct possible_tag *best)
202{
203 unsigned long seen_commits = 0;
204 while (*list) {
205 struct commit *c = pop_commit(list);
206 struct commit_list *parents = c->parents;
207 seen_commits++;
208 if (c->object.flags & best->flag_within) {
209 struct commit_list *a = *list;
210 while (a) {
211 struct commit *i = a->item;
212 if (!(i->object.flags & best->flag_within))
213 break;
214 a = a->next;
215 }
216 if (!a)
217 break;
218 } else
219 best->depth++;
220 while (parents) {
221 struct commit *p = parents->item;
222 parse_commit(p);
223 if (!(p->object.flags & SEEN))
47e44ed1 224 commit_list_insert_by_date(p, list);
1b600e65
SP
225 p->object.flags |= c->object.flags;
226 parents = parents->next;
227 }
228 }
229 return seen_commits;
230}
231
212945d4
SP
232static void display_name(struct commit_name *n)
233{
234 if (n->prio == 2 && !n->tag) {
235 n->tag = lookup_tag(n->sha1);
03e8b541 236 if (!n->tag || parse_tag(n->tag))
e41f1cb3 237 die(_("annotated tag %s not available"), n->path);
03e8b541
SP
238 }
239 if (n->tag && !n->name_checked) {
240 if (!n->tag->tag)
e41f1cb3 241 die(_("annotated tag %s has no embedded name"), n->path);
81dc223d 242 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
e41f1cb3 243 warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path);
03e8b541 244 n->name_checked = 1;
212945d4
SP
245 }
246
247 if (n->tag)
248 printf("%s", n->tag->tag);
249 else
250 printf("%s", n->path);
870cf7d6
JH
251}
252
253static void show_suffix(int depth, const unsigned char *sha1)
254{
255 printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
212945d4
SP
256}
257
554fe20d 258static void describe(const char *arg, int last_one)
908e5310 259{
4c34a2c5 260 unsigned char sha1[20];
8713ab30 261 struct commit *cmit, *gave_up_on = NULL;
908e5310 262 struct commit_list *list;
908e5310 263 struct commit_name *n;
cf69fd49 264 struct possible_tag all_matches[MAX_TAGS];
8713ab30
SP
265 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
266 unsigned long seen_commits = 0;
4d23660e 267 unsigned int unannotated_cnt = 0;
908e5310 268
31fff305 269 if (get_sha1(arg, sha1))
e41f1cb3 270 die(_("Not a valid object name %s"), arg);
4c34a2c5
JH
271 cmit = lookup_commit_reference(sha1);
272 if (!cmit)
e41f1cb3 273 die(_("%s is not a valid '%s' object"), arg, commit_type);
4c34a2c5 274
3cfa4db3 275 n = find_commit_name(cmit->object.sha1);
7a0d61bb 276 if (n && (tags || all || n->prio == 2)) {
870cf7d6
JH
277 /*
278 * Exact match to an existing ref.
279 */
212945d4 280 display_name(n);
870cf7d6 281 if (longformat)
14d4642e 282 show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
9f67d2e8
JP
283 if (dirty)
284 printf("%s", dirty);
212945d4 285 printf("\n");
908e5310
LT
286 return;
287 }
288
2c33f757 289 if (!max_candidates)
e41f1cb3 290 die(_("no tag exactly matches '%s'"), sha1_to_hex(cmit->object.sha1));
8713ab30 291 if (debug)
e41f1cb3 292 fprintf(stderr, _("searching to describe %s\n"), arg);
8713ab30 293
d1645d02 294 if (!have_util) {
11f944dd 295 for_each_hash(&names, set_util, NULL);
d1645d02
AK
296 have_util = 1;
297 }
298
908e5310 299 list = NULL;
8713ab30 300 cmit->object.flags = SEEN;
908e5310
LT
301 commit_list_insert(cmit, &list);
302 while (list) {
80dbae03 303 struct commit *c = pop_commit(&list);
dccd0c2a 304 struct commit_list *parents = c->parents;
8713ab30 305 seen_commits++;
e7eb5034 306 n = c->util;
908e5310 307 if (n) {
4d23660e
TR
308 if (!tags && !all && n->prio < 2) {
309 unannotated_cnt++;
310 } else if (match_cnt < max_candidates) {
8713ab30
SP
311 struct possible_tag *t = &all_matches[match_cnt++];
312 t->name = n;
313 t->depth = seen_commits - 1;
314 t->flag_within = 1u << match_cnt;
8a8169c0 315 t->found_order = match_cnt;
8713ab30
SP
316 c->object.flags |= t->flag_within;
317 if (n->prio == 2)
318 annotated_cnt++;
319 }
320 else {
321 gave_up_on = c;
322 break;
323 }
324 }
325 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
326 struct possible_tag *t = &all_matches[cur_match];
327 if (!(c->object.flags & t->flag_within))
328 t->depth++;
329 }
330 if (annotated_cnt && !list) {
331 if (debug)
e41f1cb3 332 fprintf(stderr, _("finished search at %s\n"),
8713ab30
SP
333 sha1_to_hex(c->object.sha1));
334 break;
dccd0c2a
SP
335 }
336 while (parents) {
337 struct commit *p = parents->item;
338 parse_commit(p);
8713ab30 339 if (!(p->object.flags & SEEN))
47e44ed1 340 commit_list_insert_by_date(p, &list);
8713ab30 341 p->object.flags |= c->object.flags;
dccd0c2a 342 parents = parents->next;
e00dd1e9
MC
343
344 if (first_parent)
345 break;
80dbae03
SP
346 }
347 }
348
da2478db
JH
349 if (!match_cnt) {
350 const unsigned char *sha1 = cmit->object.sha1;
351 if (always) {
9f67d2e8
JP
352 printf("%s", find_unique_abbrev(sha1, abbrev));
353 if (dirty)
354 printf("%s", dirty);
355 printf("\n");
da2478db
JH
356 return;
357 }
4d23660e 358 if (unannotated_cnt)
e41f1cb3
ÆAB
359 die(_("No annotated tags can describe '%s'.\n"
360 "However, there were unannotated tags: try --tags."),
4d23660e
TR
361 sha1_to_hex(sha1));
362 else
e41f1cb3
ÆAB
363 die(_("No tags can describe '%s'.\n"
364 "Try --always, or create some tags."),
4d23660e 365 sha1_to_hex(sha1));
da2478db 366 }
80dbae03 367
cf69fd49 368 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
1b600e65
SP
369
370 if (gave_up_on) {
47e44ed1 371 commit_list_insert_by_date(gave_up_on, &list);
1b600e65
SP
372 seen_commits--;
373 }
374 seen_commits += finish_depth_computation(&list, &all_matches[0]);
375 free_commit_list(list);
376
8713ab30
SP
377 if (debug) {
378 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
379 struct possible_tag *t = &all_matches[cur_match];
cf69fd49
SP
380 fprintf(stderr, " %-11s %8d %s\n",
381 prio_names[t->name->prio],
8713ab30
SP
382 t->depth, t->name->path);
383 }
e41f1cb3 384 fprintf(stderr, _("traversed %lu commits\n"), seen_commits);
8713ab30
SP
385 if (gave_up_on) {
386 fprintf(stderr,
e41f1cb3
ÆAB
387 _("more than %i tags found; listed %i most recent\n"
388 "gave up search at %s\n"),
8713ab30
SP
389 max_candidates, max_candidates,
390 sha1_to_hex(gave_up_on->object.sha1));
391 }
80dbae03 392 }
212945d4
SP
393
394 display_name(all_matches[0].name);
395 if (abbrev)
870cf7d6 396 show_suffix(all_matches[0].depth, cmit->object.sha1);
9f67d2e8
JP
397 if (dirty)
398 printf("%s", dirty);
212945d4 399 printf("\n");
80dbae03 400
8713ab30
SP
401 if (!last_one)
402 clear_commit_marks(cmit, -1);
908e5310
LT
403}
404
9a0eaf83 405int cmd_describe(int argc, const char **argv, const char *prefix)
908e5310 406{
23615708 407 int contains = 0;
166185be 408 struct option options[] = {
bfb0737d
NTND
409 OPT_BOOLEAN(0, "contains", &contains, N_("find the tag that comes after the commit")),
410 OPT_BOOLEAN(0, "debug", &debug, N_("debug search strategy on stderr")),
48dfe969
GP
411 OPT_BOOLEAN(0, "all", &all, N_("use any ref")),
412 OPT_BOOLEAN(0, "tags", &tags, N_("use any tag, even unannotated")),
bfb0737d 413 OPT_BOOLEAN(0, "long", &longformat, N_("always use long format")),
e00dd1e9 414 OPT_BOOLEAN(0, "first-parent", &first_parent, N_("only follow first parent")),
166185be 415 OPT__ABBREV(&abbrev),
2c33f757 416 OPT_SET_INT(0, "exact-match", &max_candidates,
bfb0737d 417 N_("only output exact matches"), 0),
166185be 418 OPT_INTEGER(0, "candidates", &max_candidates,
bfb0737d
NTND
419 N_("consider <n> most recent tags (default: 10)")),
420 OPT_STRING(0, "match", &pattern, N_("pattern"),
421 N_("only consider tags matching <pattern>")),
da2478db 422 OPT_BOOLEAN(0, "always", &always,
bfb0737d
NTND
423 N_("show abbreviated commit object as fallback")),
424 {OPTION_STRING, 0, "dirty", &dirty, N_("mark"),
425 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
9f67d2e8 426 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
166185be
PH
427 OPT_END(),
428 };
908e5310 429
dce96489 430 git_config(git_default_config, NULL);
37782920 431 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
dce96489
LT
432 if (abbrev < 0)
433 abbrev = DEFAULT_ABBREV;
434
2c33f757
SP
435 if (max_candidates < 0)
436 max_candidates = 0;
166185be
PH
437 else if (max_candidates > MAX_TAGS)
438 max_candidates = MAX_TAGS;
4c34a2c5 439
8c599c74 440 save_commit_buffer = 0;
8112894d 441
518120e3 442 if (longformat && abbrev == 0)
e41f1cb3 443 die(_("--long is incompatible with --abbrev=0"));
518120e3 444
23615708 445 if (contains) {
45bc950b
JH
446 struct argv_array args;
447
448 argv_array_init(&args);
adfc1857
JH
449 argv_array_pushl(&args, "name-rev",
450 "--peel-tag", "--name-only", "--no-undefined",
45bc950b 451 NULL);
da2478db 452 if (always)
45bc950b 453 argv_array_push(&args, "--always");
30ffa603 454 if (!all) {
45bc950b
JH
455 argv_array_push(&args, "--tags");
456 if (pattern)
457 argv_array_pushf(&args, "--refs=refs/tags/%s", pattern);
458 }
459 while (*argv) {
460 argv_array_push(&args, *argv);
461 argv++;
30ffa603 462 }
45bc950b 463 return cmd_name_rev(args.argc, args.argv, prefix);
23615708
SP
464 }
465
3cfa4db3 466 init_hash(&names);
56a5f3af 467 for_each_rawref(get_name, NULL);
3cfa4db3 468 if (!names.nr && !always)
e41f1cb3 469 die(_("No names found, cannot describe anything."));
fb423da0 470
166185be 471 if (argc == 0) {
bb571486
AC
472 if (dirty) {
473 static struct lock_file index_lock;
474 int fd;
475
476 read_cache_preload(NULL);
477 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
478 NULL, NULL, NULL);
479 fd = hold_locked_index(&index_lock, 0);
480 if (0 <= fd)
481 update_index_if_able(&the_index, &index_lock);
482
483 if (!cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1,
484 diff_index_args, prefix))
485 dirty = NULL;
486 }
fec9ebf1 487 describe("HEAD", 1);
9f67d2e8 488 } else if (dirty) {
e41f1cb3 489 die(_("--dirty is incompatible with committishes"));
166185be
PH
490 } else {
491 while (argc-- > 0) {
492 describe(*argv++, argc == 0);
fec9ebf1 493 }
166185be 494 }
908e5310
LT
495 return 0;
496}