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