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