]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/describe.c
The fifth batch
[thirdparty/git.git] / builtin / describe.c
CommitLineData
03eae9af 1#define USE_THE_REPOSITORY_VARIABLE
41f43b82
PS
2#define DISABLE_SIGN_COMPARE_WARNINGS
3
bc5c5ec0 4#include "builtin.h"
b2141fc1 5#include "config.h"
32a8f510 6#include "environment.h"
f394e093 7#include "gettext.h"
41771fa4 8#include "hex.h"
697cc8ef 9#include "lockfile.h"
908e5310 10#include "commit.h"
635d4134 11#include "tag.h"
908e5310 12#include "refs.h"
dabab1d6 13#include "object-name.h"
166185be 14#include "parse-options.h"
08c46a49 15#include "read-cache-ll.h"
be26d2b2 16#include "revision.h"
9f67d2e8 17#include "diff.h"
29d8a834 18#include "hashmap.h"
e38da487 19#include "setup.h"
dbbcd44f 20#include "strvec.h"
b0176ce6 21#include "run-command.h"
68cd492a 22#include "object-store.h"
644eb60b 23#include "list-objects.h"
c6b7206b 24#include "commit-slab.h"
dd77d587 25#include "wildmatch.h"
908e5310 26
8713ab30 27#define MAX_TAGS (FLAG_BITS - 1)
c95ae3ff 28#define DEFAULT_CANDIDATES 10
8713ab30 29
c6b7206b
NTND
30define_commit_slab(commit_names, struct commit_name *);
31
166185be 32static const char * const describe_usage[] = {
8c9e292d
ÆAB
33 N_("git describe [--all] [--tags] [--contains] [--abbrev=<n>] [<commit-ish>...]"),
34 N_("git describe [--all] [--tags] [--contains] [--abbrev=<n>] --dirty[=<mark>]"),
35 N_("git describe <blob>"),
166185be
PH
36 NULL
37};
908e5310 38
8713ab30 39static int debug; /* Display lots of verbose info */
7e425c4f
SP
40static int all; /* Any valid ref can be used */
41static int tags; /* Allow lightweight tags */
518120e3 42static int longformat;
e00dd1e9 43static int first_parent;
dce96489 44static int abbrev = -1; /* unspecified */
c95ae3ff 45static int max_candidates = DEFAULT_CANDIDATES;
29d8a834 46static struct hashmap names;
d1645d02 47static int have_util;
43f8080e 48static struct string_list patterns = STRING_LIST_INIT_NODUP;
77d21f29 49static struct string_list exclude_patterns = STRING_LIST_INIT_NODUP;
da2478db 50static int always;
b0176ce6 51static const char *suffix, *dirty, *broken;
c6b7206b 52static struct commit_names commit_names;
9f67d2e8
JP
53
54/* diff-index command arguments to check if working tree is dirty. */
55static const char *diff_index_args[] = {
56 "diff-index", "--quiet", "HEAD", "--", NULL
57};
58
b8ae42e2
AS
59static const char *update_index_args[] = {
60 "update-index", "--unmerged", "-q", "--refresh", NULL
61};
62
e7eb5034 63struct commit_name {
29d8a834 64 struct hashmap_entry entry;
6439b5d9 65 struct object_id peeled;
212945d4 66 struct tag *tag;
03e8b541
SP
67 unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
68 unsigned name_checked:1;
ff165f00 69 unsigned misnamed:1;
6439b5d9 70 struct object_id oid;
219a0f33 71 char *path;
e7eb5034 72};
c4472643 73
cf69fd49 74static const char *prio_names[] = {
646c3bd1 75 N_("head"), N_("lightweight"), N_("annotated"),
cf69fd49 76};
908e5310 77
5cf88fd8 78static int commit_name_neq(const void *cmp_data UNUSED,
939af16e
EW
79 const struct hashmap_entry *eptr,
80 const struct hashmap_entry *entry_or_key,
7663cdc8 81 const void *peeled)
29d8a834 82{
939af16e
EW
83 const struct commit_name *cn1, *cn2;
84
85 cn1 = container_of(eptr, const struct commit_name, entry);
86 cn2 = container_of(entry_or_key, const struct commit_name, entry);
0068cede 87
cc00e5ce 88 return !oideq(&cn1->peeled, peeled ? peeled : &cn2->peeled);
29d8a834
KB
89}
90
6439b5d9 91static inline struct commit_name *find_commit_name(const struct object_id *peeled)
3cfa4db3 92{
f23a4651
EW
93 return hashmap_get_entry_from_hash(&names, oidhash(peeled), peeled,
94 struct commit_name, entry);
d1645d02
AK
95}
96
03e8b541
SP
97static int replace_name(struct commit_name *e,
98 int prio,
6439b5d9 99 const struct object_id *oid,
03e8b541
SP
100 struct tag **tag)
101{
102 if (!e || e->prio < prio)
103 return 1;
104
105 if (e->prio == 2 && prio == 2) {
106 /* Multiple annotated tags point to the same commit.
107 * Select one to keep based upon their tagger date.
108 */
109 struct tag *t;
110
111 if (!e->tag) {
ce71efb7 112 t = lookup_tag(the_repository, &e->oid);
03e8b541
SP
113 if (!t || parse_tag(t))
114 return 1;
115 e->tag = t;
116 }
117
ce71efb7 118 t = lookup_tag(the_repository, oid);
03e8b541
SP
119 if (!t || parse_tag(t))
120 return 0;
121 *tag = t;
122
123 if (e->tag->date < t->date)
124 return 1;
125 }
126
127 return 0;
128}
129
64deb858 130static void add_to_known_names(const char *path,
6439b5d9 131 const struct object_id *peeled,
212945d4 132 int prio,
6439b5d9 133 const struct object_id *oid)
908e5310 134{
3cfa4db3 135 struct commit_name *e = find_commit_name(peeled);
03e8b541 136 struct tag *tag = NULL;
6439b5d9 137 if (replace_name(e, prio, oid, &tag)) {
1e1ade18
AK
138 if (!e) {
139 e = xmalloc(sizeof(struct commit_name));
6439b5d9 140 oidcpy(&e->peeled, peeled);
d22245a2 141 hashmap_entry_init(&e->entry, oidhash(peeled));
b94e5c1d 142 hashmap_add(&names, &e->entry);
219a0f33 143 e->path = NULL;
1e1ade18 144 }
03e8b541 145 e->tag = tag;
e7eb5034 146 e->prio = prio;
03e8b541 147 e->name_checked = 0;
ff165f00 148 e->misnamed = 0;
6439b5d9 149 oidcpy(&e->oid, oid);
219a0f33
MH
150 free(e->path);
151 e->path = xstrdup(path);
908e5310 152 }
908e5310
LT
153}
154
e8207717 155static int get_name(const char *path, const char *referent UNUSED, const struct object_id *oid,
5cf88fd8 156 int flag UNUSED, void *cb_data UNUSED)
908e5310 157{
6d68b2ab 158 int is_tag = 0;
99a2cfbf 159 struct object_id peeled;
46e1d6eb 160 int is_annotated, prio;
6d68b2ab
MK
161 const char *path_to_match = NULL;
162
163 if (skip_prefix(path, "refs/tags/", &path_to_match)) {
164 is_tag = 1;
165 } else if (all) {
166 if ((exclude_patterns.nr || patterns.nr) &&
167 !skip_prefix(path, "refs/heads/", &path_to_match) &&
168 !skip_prefix(path, "refs/remotes/", &path_to_match)) {
169 /* Only accept reference of known type if there are match/exclude patterns */
170 return 0;
171 }
172 } else {
173 /* Reject anything outside refs/tags/ unless --all */
8a5a1884 174 return 0;
6d68b2ab 175 }
8a5a1884 176
77d21f29
JK
177 /*
178 * If we're given exclude patterns, first exclude any tag which match
179 * any of the exclude pattern.
180 */
181 if (exclude_patterns.nr) {
182 struct string_list_item *item;
183
77d21f29 184 for_each_string_list_item(item, &exclude_patterns) {
6d68b2ab 185 if (!wildmatch(item->string, path_to_match, 0))
77d21f29
JK
186 return 0;
187 }
188 }
189
43f8080e
JK
190 /*
191 * If we're given patterns, accept only tags which match at least one
192 * pattern.
193 */
194 if (patterns.nr) {
da769d29 195 int found = 0;
43f8080e
JK
196 struct string_list_item *item;
197
43f8080e 198 for_each_string_list_item(item, &patterns) {
6d68b2ab 199 if (!wildmatch(item->string, path_to_match, 0)) {
da769d29 200 found = 1;
43f8080e 201 break;
da769d29
MK
202 }
203 }
43f8080e 204
da769d29 205 if (!found)
43f8080e 206 return 0;
43f8080e 207 }
46e1d6eb
JH
208
209 /* Is it annotated? */
30aaff43 210 if (!peel_iterated_oid(the_repository, oid, &peeled)) {
4a7e27e9 211 is_annotated = !oideq(oid, &peeled);
feededd0 212 } else {
99a2cfbf 213 oidcpy(&peeled, oid);
46e1d6eb 214 is_annotated = 0;
feededd0 215 }
64deb858 216
46e1d6eb
JH
217 /*
218 * By default, we only use annotated tags, but with --tags
219 * we fall back to lightweight ones (even without --tags,
220 * we still remember lightweight ones, only to give hints
221 * in an error message). --all allows any refs to be used.
2d9e7c9f 222 */
46e1d6eb
JH
223 if (is_annotated)
224 prio = 2;
225 else if (is_tag)
226 prio = 1;
64deb858
JH
227 else
228 prio = 0;
229
6439b5d9 230 add_to_known_names(all ? path + 5 : path + 10, &peeled, prio, oid);
908e5310
LT
231 return 0;
232}
233
80dbae03 234struct possible_tag {
80dbae03 235 struct commit_name *name;
cf69fd49
SP
236 int depth;
237 int found_order;
8713ab30 238 unsigned flag_within;
80dbae03
SP
239};
240
cf69fd49
SP
241static int compare_pt(const void *a_, const void *b_)
242{
243 struct possible_tag *a = (struct possible_tag *)a_;
244 struct possible_tag *b = (struct possible_tag *)b_;
cf69fd49
SP
245 if (a->depth != b->depth)
246 return a->depth - b->depth;
247 if (a->found_order != b->found_order)
248 return a->found_order - b->found_order;
249 return 0;
250}
251
1b600e65
SP
252static unsigned long finish_depth_computation(
253 struct commit_list **list,
254 struct possible_tag *best)
255{
256 unsigned long seen_commits = 0;
257 while (*list) {
258 struct commit *c = pop_commit(list);
259 struct commit_list *parents = c->parents;
260 seen_commits++;
261 if (c->object.flags & best->flag_within) {
262 struct commit_list *a = *list;
263 while (a) {
264 struct commit *i = a->item;
265 if (!(i->object.flags & best->flag_within))
266 break;
267 a = a->next;
268 }
269 if (!a)
270 break;
271 } else
272 best->depth++;
273 while (parents) {
274 struct commit *p = parents->item;
ecb5091f 275 repo_parse_commit(the_repository, p);
1b600e65 276 if (!(p->object.flags & SEEN))
47e44ed1 277 commit_list_insert_by_date(p, list);
1b600e65
SP
278 p->object.flags |= c->object.flags;
279 parents = parents->next;
280 }
281 }
282 return seen_commits;
283}
284
4dbc59a4 285static void append_name(struct commit_name *n, struct strbuf *dst)
212945d4
SP
286{
287 if (n->prio == 2 && !n->tag) {
ce71efb7 288 n->tag = lookup_tag(the_repository, &n->oid);
03e8b541 289 if (!n->tag || parse_tag(n->tag))
e41f1cb3 290 die(_("annotated tag %s not available"), n->path);
03e8b541
SP
291 }
292 if (n->tag && !n->name_checked) {
ff165f00
JH
293 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path)) {
294 warning(_("tag '%s' is externally known as '%s'"),
295 n->path, n->tag->tag);
296 n->misnamed = 1;
297 }
03e8b541 298 n->name_checked = 1;
212945d4
SP
299 }
300
1bba0013
DKF
301 if (n->tag) {
302 if (all)
fac64e01 303 strbuf_addstr(dst, "tags/");
4dbc59a4 304 strbuf_addstr(dst, n->tag->tag);
1bba0013 305 } else {
4dbc59a4 306 strbuf_addstr(dst, n->path);
1bba0013 307 }
870cf7d6
JH
308}
309
4dbc59a4 310static void append_suffix(int depth, const struct object_id *oid, struct strbuf *dst)
870cf7d6 311{
d850b7a5
ÆAB
312 strbuf_addf(dst, "-%d-g%s", depth,
313 repo_find_unique_abbrev(the_repository, oid, abbrev));
212945d4
SP
314}
315
4dbc59a4 316static void describe_commit(struct object_id *oid, struct strbuf *dst)
908e5310 317{
8713ab30 318 struct commit *cmit, *gave_up_on = NULL;
908e5310 319 struct commit_list *list;
908e5310 320 struct commit_name *n;
cf69fd49 321 struct possible_tag all_matches[MAX_TAGS];
8713ab30
SP
322 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
323 unsigned long seen_commits = 0;
4d23660e 324 unsigned int unannotated_cnt = 0;
908e5310 325
2122f675 326 cmit = lookup_commit_reference(the_repository, oid);
4c34a2c5 327
6439b5d9 328 n = find_commit_name(&cmit->object.oid);
7a0d61bb 329 if (n && (tags || all || n->prio == 2)) {
870cf7d6
JH
330 /*
331 * Exact match to an existing ref.
332 */
4dbc59a4 333 append_name(n, dst);
ff165f00 334 if (n->misnamed || longformat)
c77722b3 335 append_suffix(0, n->tag ? get_tagged_oid(n->tag) : oid, dst);
b0176ce6 336 if (suffix)
4dbc59a4 337 strbuf_addstr(dst, suffix);
908e5310
LT
338 return;
339 }
340
2c33f757 341 if (!max_candidates)
f2fd0760 342 die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit->object.oid));
8713ab30 343 if (debug)
cdaed0cf 344 fprintf(stderr, _("No exact match on refs or tags, searching to describe\n"));
8713ab30 345
d1645d02 346 if (!have_util) {
29d8a834
KB
347 struct hashmap_iter iter;
348 struct commit *c;
c6b7206b
NTND
349 struct commit_name *n;
350
351 init_commit_names(&commit_names);
23dee69f 352 hashmap_for_each_entry(&names, &iter, n,
87571c3f 353 entry /* member name */) {
21e1ee8f
SB
354 c = lookup_commit_reference_gently(the_repository,
355 &n->peeled, 1);
29d8a834 356 if (c)
c6b7206b 357 *commit_names_at(&commit_names, c) = n;
29d8a834 358 }
d1645d02
AK
359 have_util = 1;
360 }
361
908e5310 362 list = NULL;
8713ab30 363 cmit->object.flags = SEEN;
908e5310
LT
364 commit_list_insert(cmit, &list);
365 while (list) {
80dbae03 366 struct commit *c = pop_commit(&list);
dccd0c2a 367 struct commit_list *parents = c->parents;
c6b7206b
NTND
368 struct commit_name **slot;
369
8713ab30 370 seen_commits++;
73790462 371
db162862
JK
372 if (match_cnt == max_candidates ||
373 match_cnt == hashmap_get_size(&names)) {
73790462
JK
374 gave_up_on = c;
375 break;
376 }
377
c6b7206b
NTND
378 slot = commit_names_peek(&commit_names, c);
379 n = slot ? *slot : NULL;
908e5310 380 if (n) {
4d23660e
TR
381 if (!tags && !all && n->prio < 2) {
382 unannotated_cnt++;
383 } else if (match_cnt < max_candidates) {
8713ab30
SP
384 struct possible_tag *t = &all_matches[match_cnt++];
385 t->name = n;
386 t->depth = seen_commits - 1;
387 t->flag_within = 1u << match_cnt;
8a8169c0 388 t->found_order = match_cnt;
8713ab30
SP
389 c->object.flags |= t->flag_within;
390 if (n->prio == 2)
391 annotated_cnt++;
392 }
8713ab30
SP
393 }
394 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
395 struct possible_tag *t = &all_matches[cur_match];
396 if (!(c->object.flags & t->flag_within))
397 t->depth++;
398 }
30b1c7ad 399 /* Stop if last remaining path already covered by best candidate(s) */
8713ab30 400 if (annotated_cnt && !list) {
30b1c7ad
BE
401 int best_depth = INT_MAX;
402 unsigned best_within = 0;
403 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
404 struct possible_tag *t = &all_matches[cur_match];
405 if (t->depth < best_depth) {
406 best_depth = t->depth;
407 best_within = t->flag_within;
408 } else if (t->depth == best_depth) {
409 best_within |= t->flag_within;
410 }
411 }
412 if ((c->object.flags & best_within) == best_within) {
413 if (debug)
414 fprintf(stderr, _("finished search at %s\n"),
415 oid_to_hex(&c->object.oid));
416 break;
417 }
dccd0c2a
SP
418 }
419 while (parents) {
420 struct commit *p = parents->item;
ecb5091f 421 repo_parse_commit(the_repository, p);
8713ab30 422 if (!(p->object.flags & SEEN))
47e44ed1 423 commit_list_insert_by_date(p, &list);
8713ab30 424 p->object.flags |= c->object.flags;
dccd0c2a 425 parents = parents->next;
e00dd1e9
MC
426
427 if (first_parent)
428 break;
80dbae03
SP
429 }
430 }
431
da2478db 432 if (!match_cnt) {
c87b653c 433 struct object_id *cmit_oid = &cmit->object.oid;
da2478db 434 if (always) {
aab9583f 435 strbuf_add_unique_abbrev(dst, cmit_oid, abbrev);
b0176ce6 436 if (suffix)
4dbc59a4 437 strbuf_addstr(dst, suffix);
da2478db
JH
438 return;
439 }
4d23660e 440 if (unannotated_cnt)
e41f1cb3
ÆAB
441 die(_("No annotated tags can describe '%s'.\n"
442 "However, there were unannotated tags: try --tags."),
c87b653c 443 oid_to_hex(cmit_oid));
4d23660e 444 else
e41f1cb3
ÆAB
445 die(_("No tags can describe '%s'.\n"
446 "Try --always, or create some tags."),
c87b653c 447 oid_to_hex(cmit_oid));
da2478db 448 }
80dbae03 449
9ed0d8d6 450 QSORT(all_matches, match_cnt, compare_pt);
1b600e65
SP
451
452 if (gave_up_on) {
47e44ed1 453 commit_list_insert_by_date(gave_up_on, &list);
1b600e65
SP
454 seen_commits--;
455 }
456 seen_commits += finish_depth_computation(&list, &all_matches[0]);
457 free_commit_list(list);
458
8713ab30 459 if (debug) {
646c3bd1
MG
460 static int label_width = -1;
461 if (label_width < 0) {
462 int i, w;
463 for (i = 0; i < ARRAY_SIZE(prio_names); i++) {
464 w = strlen(_(prio_names[i]));
465 if (label_width < w)
466 label_width = w;
467 }
468 }
8713ab30
SP
469 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
470 struct possible_tag *t = &all_matches[cur_match];
646c3bd1
MG
471 fprintf(stderr, " %-*s %8d %s\n",
472 label_width, _(prio_names[t->name->prio]),
8713ab30
SP
473 t->depth, t->name->path);
474 }
e41f1cb3 475 fprintf(stderr, _("traversed %lu commits\n"), seen_commits);
8713ab30
SP
476 if (gave_up_on) {
477 fprintf(stderr,
73790462
JK
478 _("found %i tags; gave up search at %s\n"),
479 max_candidates,
f2fd0760 480 oid_to_hex(&gave_up_on->object.oid));
8713ab30 481 }
80dbae03 482 }
212945d4 483
4dbc59a4 484 append_name(all_matches[0].name, dst);
ff165f00 485 if (all_matches[0].name->misnamed || abbrev)
4dbc59a4 486 append_suffix(all_matches[0].depth, &cmit->object.oid, dst);
b0176ce6 487 if (suffix)
4dbc59a4
SB
488 strbuf_addstr(dst, suffix);
489}
490
644eb60b
SB
491struct process_commit_data {
492 struct object_id current_commit;
493 struct object_id looking_for;
494 struct strbuf *dst;
495 struct rev_info *revs;
496};
497
498static void process_commit(struct commit *commit, void *data)
499{
500 struct process_commit_data *pcd = data;
501 pcd->current_commit = commit->object.oid;
502}
503
504static void process_object(struct object *obj, const char *path, void *data)
505{
506 struct process_commit_data *pcd = data;
507
4a7e27e9 508 if (oideq(&pcd->looking_for, &obj->oid) && !pcd->dst->len) {
644eb60b
SB
509 reset_revision_walk();
510 describe_commit(&pcd->current_commit, pcd->dst);
511 strbuf_addf(pcd->dst, ":%s", path);
512 free_commit_list(pcd->revs->commits);
513 pcd->revs->commits = NULL;
514 }
515}
516
517static void describe_blob(struct object_id oid, struct strbuf *dst)
518{
519 struct rev_info revs;
22f9b7f3 520 struct strvec args = STRVEC_INIT;
7d70b29c 521 struct process_commit_data pcd = { *null_oid(the_hash_algo), oid, dst, &revs};
644eb60b 522
22f9b7f3 523 strvec_pushl(&args, "internal: The first arg is not parsed",
f6d8942b
JK
524 "--objects", "--in-commit-order", "--reverse", "HEAD",
525 NULL);
644eb60b 526
2abf3503 527 repo_init_revisions(the_repository, &revs, NULL);
d70a9eb6 528 if (setup_revisions(args.nr, args.v, &revs, NULL) > 1)
644eb60b
SB
529 BUG("setup_revisions could not handle all args?");
530
531 if (prepare_revision_walk(&revs))
532 die("revision walk setup failed");
533
534 traverse_commit_list(&revs, process_commit, process_object, &pcd);
535 reset_revision_walk();
2108fe4a 536 release_revisions(&revs);
ed041007 537 strvec_clear(&args);
644eb60b
SB
538}
539
4dbc59a4
SB
540static void describe(const char *arg, int last_one)
541{
542 struct object_id oid;
543 struct commit *cmit;
544 struct strbuf sb = STRBUF_INIT;
545
546 if (debug)
547 fprintf(stderr, _("describe %s\n"), arg);
548
d850b7a5 549 if (repo_get_oid(the_repository, arg, &oid))
4dbc59a4 550 die(_("Not a valid object name %s"), arg);
21e1ee8f 551 cmit = lookup_commit_reference_gently(the_repository, &oid, 1);
4dbc59a4 552
644eb60b
SB
553 if (cmit)
554 describe_commit(&oid, &sb);
0df8e965 555 else if (oid_object_info(the_repository, &oid, NULL) == OBJ_BLOB)
644eb60b
SB
556 describe_blob(oid, &sb);
557 else
558 die(_("%s is neither a commit nor blob"), arg);
4dbc59a4
SB
559
560 puts(sb.buf);
80dbae03 561
8713ab30
SP
562 if (!last_one)
563 clear_commit_marks(cmit, -1);
4dbc59a4
SB
564
565 strbuf_release(&sb);
908e5310
LT
566}
567
c95ae3ff
RS
568static int option_parse_exact_match(const struct option *opt, const char *arg,
569 int unset)
570{
66e33092
JK
571 int *val = opt->value;
572
c95ae3ff
RS
573 BUG_ON_OPT_ARG(arg);
574
66e33092 575 *val = unset ? DEFAULT_CANDIDATES : 0;
c95ae3ff
RS
576 return 0;
577}
578
9b1cb507
JC
579int cmd_describe(int argc,
580 const char **argv,
581 const char *prefix,
582 struct repository *repo UNUSED )
908e5310 583{
23615708 584 int contains = 0;
166185be 585 struct option options[] = {
d5d09d47
SB
586 OPT_BOOL(0, "contains", &contains, N_("find the tag that comes after the commit")),
587 OPT_BOOL(0, "debug", &debug, N_("debug search strategy on stderr")),
588 OPT_BOOL(0, "all", &all, N_("use any ref")),
589 OPT_BOOL(0, "tags", &tags, N_("use any tag, even unannotated")),
590 OPT_BOOL(0, "long", &longformat, N_("always use long format")),
591 OPT_BOOL(0, "first-parent", &first_parent, N_("only follow first parent")),
166185be 592 OPT__ABBREV(&abbrev),
66e33092 593 OPT_CALLBACK_F(0, "exact-match", &max_candidates, NULL,
c95ae3ff
RS
594 N_("only output exact matches"),
595 PARSE_OPT_NOARG, option_parse_exact_match),
166185be 596 OPT_INTEGER(0, "candidates", &max_candidates,
bfb0737d 597 N_("consider <n> most recent tags (default: 10)")),
43f8080e 598 OPT_STRING_LIST(0, "match", &patterns, N_("pattern"),
bfb0737d 599 N_("only consider tags matching <pattern>")),
77d21f29
JK
600 OPT_STRING_LIST(0, "exclude", &exclude_patterns, N_("pattern"),
601 N_("do not consider tags matching <pattern>")),
d5d09d47
SB
602 OPT_BOOL(0, "always", &always,
603 N_("show abbreviated commit object as fallback")),
d012ceb5
PS
604 {
605 .type = OPTION_STRING,
606 .long_name = "dirty",
607 .value = &dirty,
608 .argh = N_("mark"),
609 .help = N_("append <mark> on dirty working tree (default: \"-dirty\")"),
610 .flags = PARSE_OPT_OPTARG,
611 .defval = (intptr_t) "-dirty",
612 },
613 {
614 .type = OPTION_STRING,
615 .long_name = "broken",
616 .value = &broken,
617 .argh = N_("mark"),
618 .help = N_("append <mark> on broken working tree (default: \"-broken\")"),
619 .flags = PARSE_OPT_OPTARG,
620 .defval = (intptr_t) "-broken",
621 },
166185be
PH
622 OPT_END(),
623 };
908e5310 624
dce96489 625 git_config(git_default_config, NULL);
37782920 626 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
dce96489
LT
627 if (abbrev < 0)
628 abbrev = DEFAULT_ABBREV;
629
2c33f757
SP
630 if (max_candidates < 0)
631 max_candidates = 0;
166185be
PH
632 else if (max_candidates > MAX_TAGS)
633 max_candidates = MAX_TAGS;
4c34a2c5 634
8c599c74 635 save_commit_buffer = 0;
8112894d 636
518120e3 637 if (longformat && abbrev == 0)
12909b6b 638 die(_("options '%s' and '%s' cannot be used together"), "--long", "--abbrev=0");
518120e3 639
23615708 640 if (contains) {
43f8080e 641 struct string_list_item *item;
22f9b7f3 642 struct strvec args;
8e2e2879
PS
643 const char **argv_copy;
644 int ret;
45bc950b 645
22f9b7f3
JK
646 strvec_init(&args);
647 strvec_pushl(&args, "name-rev",
f6d8942b
JK
648 "--peel-tag", "--name-only", "--no-undefined",
649 NULL);
da2478db 650 if (always)
22f9b7f3 651 strvec_push(&args, "--always");
30ffa603 652 if (!all) {
22f9b7f3 653 strvec_push(&args, "--tags");
43f8080e 654 for_each_string_list_item(item, &patterns)
22f9b7f3 655 strvec_pushf(&args, "--refs=refs/tags/%s", item->string);
77d21f29 656 for_each_string_list_item(item, &exclude_patterns)
22f9b7f3 657 strvec_pushf(&args, "--exclude=refs/tags/%s", item->string);
45bc950b 658 }
2bd07065 659 if (argc)
22f9b7f3 660 strvec_pushv(&args, argv);
2bd07065 661 else
22f9b7f3 662 strvec_push(&args, "HEAD");
8e2e2879
PS
663
664 /*
665 * `cmd_name_rev()` modifies the array, so we'd leak its
666 * contained strings if we didn't do a copy here.
667 */
668 ALLOC_ARRAY(argv_copy, args.nr + 1);
669 for (size_t i = 0; i < args.nr; i++)
670 argv_copy[i] = args.v[i];
671 argv_copy[args.nr] = NULL;
672
9b1cb507 673 ret = cmd_name_rev(args.nr, argv_copy, prefix, the_repository);
8e2e2879
PS
674
675 strvec_clear(&args);
676 free(argv_copy);
677 return ret;
23615708
SP
678 }
679
cc00e5ce 680 hashmap_init(&names, commit_name_neq, NULL, 0);
2e5c4758
PS
681 refs_for_each_rawref(get_main_ref_store(the_repository), get_name,
682 NULL);
8b604d19 683 if (!hashmap_get_size(&names) && !always)
e41f1cb3 684 die(_("No names found, cannot describe anything."));
fb423da0 685
166185be 686 if (argc == 0) {
b0176ce6
SB
687 if (broken) {
688 struct child_process cp = CHILD_PROCESS_INIT;
b8ae42e2
AS
689
690 strvec_pushv(&cp.args, update_index_args);
691 cp.git_cmd = 1;
692 cp.no_stdin = 1;
693 cp.no_stdout = 1;
694 run_command(&cp);
695
696 child_process_init(&cp);
22f9b7f3 697 strvec_pushv(&cp.args, diff_index_args);
b0176ce6
SB
698 cp.git_cmd = 1;
699 cp.no_stdin = 1;
700 cp.no_stdout = 1;
701
702 if (!dirty)
703 dirty = "-dirty";
704
705 switch (run_command(&cp)) {
706 case 0:
707 suffix = NULL;
708 break;
709 case 1:
710 suffix = dirty;
711 break;
712 default:
713 /* diff-index aborted abnormally */
714 suffix = broken;
715 }
716 } else if (dirty) {
b2275868 717 struct lock_file index_lock = LOCK_INIT;
be26d2b2 718 struct rev_info revs;
25bd3acd 719 int fd;
bb571486 720
2ed5c8e1 721 setup_work_tree();
748b8d66
RN
722 prepare_repo_settings(the_repository);
723 the_repository->settings.command_requires_full_index = 0;
07047d68 724 repo_read_index(the_repository);
f59aa5e0 725 refresh_index(the_repository->index, REFRESH_QUIET|REFRESH_UNMERGED,
bb571486 726 NULL, NULL, NULL);
07047d68
ÆAB
727 fd = repo_hold_locked_index(the_repository,
728 &index_lock, 0);
bb571486 729 if (0 <= fd)
1b0d968b 730 repo_update_index_if_able(the_repository, &index_lock);
bb571486 731
2abf3503 732 repo_init_revisions(the_repository, &revs, prefix);
5a1e1e5d
PS
733
734 if (setup_revisions(ARRAY_SIZE(diff_index_args) - 1,
735 diff_index_args, &revs, NULL) != 1)
be26d2b2 736 BUG("malformed internal diff-index command line");
25bd3acd 737 run_diff_index(&revs, 0);
be26d2b2 738
4460e052 739 if (!diff_result_code(&revs))
b0176ce6
SB
740 suffix = NULL;
741 else
742 suffix = dirty;
2108fe4a 743 release_revisions(&revs);
bb571486 744 }
fec9ebf1 745 describe("HEAD", 1);
9f67d2e8 746 } else if (dirty) {
246cac85 747 die(_("option '%s' and commit-ishes cannot be used together"), "--dirty");
b0176ce6 748 } else if (broken) {
246cac85 749 die(_("option '%s' and commit-ishes cannot be used together"), "--broken");
166185be 750 } else {
c4472643 751 while (argc-- > 0)
166185be 752 describe(*argv++, argc == 0);
166185be 753 }
908e5310
LT
754 return 0;
755}