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