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