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