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