Merge branch 'hn/reftable-coverity-fixes'
[git/debian.git] / builtin / name-rev.c
blob138e3c30a2b997adaf96b1a80d3cee741b8de982
1 #include "builtin.h"
2 #include "cache.h"
3 #include "repository.h"
4 #include "config.h"
5 #include "commit.h"
6 #include "tag.h"
7 #include "refs.h"
8 #include "parse-options.h"
9 #include "prio-queue.h"
10 #include "hash-lookup.h"
11 #include "commit-slab.h"
14 * One day. See the 'name a rev shortly after epoch' test in t6120 when
15 * changing this value
17 #define CUTOFF_DATE_SLOP 86400
19 struct rev_name {
20 char *tip_name;
21 timestamp_t taggerdate;
22 int generation;
23 int distance;
24 int from_tag;
27 define_commit_slab(commit_rev_name, struct rev_name);
29 static timestamp_t cutoff = TIME_MAX;
30 static struct commit_rev_name rev_names;
32 /* How many generations are maximally preferred over _one_ merge traversal? */
33 #define MERGE_TRAVERSAL_WEIGHT 65535
35 static int is_valid_rev_name(const struct rev_name *name)
37 return name && (name->generation || name->tip_name);
40 static struct rev_name *get_commit_rev_name(const struct commit *commit)
42 struct rev_name *name = commit_rev_name_peek(&rev_names, commit);
44 return is_valid_rev_name(name) ? name : NULL;
47 static int effective_distance(int distance, int generation)
49 return distance + (generation > 0 ? MERGE_TRAVERSAL_WEIGHT : 0);
52 static int is_better_name(struct rev_name *name,
53 timestamp_t taggerdate,
54 int generation,
55 int distance,
56 int from_tag)
58 int name_distance = effective_distance(name->distance, name->generation);
59 int new_distance = effective_distance(distance, generation);
62 * When comparing names based on tags, prefer names
63 * based on the older tag, even if it is farther away.
65 if (from_tag && name->from_tag)
66 return (name->taggerdate > taggerdate ||
67 (name->taggerdate == taggerdate &&
68 name_distance > new_distance));
71 * We know that at least one of them is a non-tag at this point.
72 * favor a tag over a non-tag.
74 if (name->from_tag != from_tag)
75 return from_tag;
78 * We are now looking at two non-tags. Tiebreak to favor
79 * shorter hops.
81 if (name_distance != new_distance)
82 return name_distance > new_distance;
84 /* ... or tiebreak to favor older date */
85 if (name->taggerdate != taggerdate)
86 return name->taggerdate > taggerdate;
88 /* keep the current one if we cannot decide */
89 return 0;
92 static struct rev_name *create_or_update_name(struct commit *commit,
93 timestamp_t taggerdate,
94 int generation, int distance,
95 int from_tag)
97 struct rev_name *name = commit_rev_name_at(&rev_names, commit);
99 if (is_valid_rev_name(name)) {
100 if (!is_better_name(name, taggerdate, generation, distance, from_tag))
101 return NULL;
104 * This string might still be shared with ancestors
105 * (generation > 0). We can release it here regardless,
106 * because the new name that has just won will be better
107 * for them as well, so name_rev() will replace these
108 * stale pointers when it processes the parents.
110 if (!name->generation)
111 free(name->tip_name);
114 name->taggerdate = taggerdate;
115 name->generation = generation;
116 name->distance = distance;
117 name->from_tag = from_tag;
119 return name;
122 static char *get_parent_name(const struct rev_name *name, int parent_number)
124 struct strbuf sb = STRBUF_INIT;
125 size_t len;
127 strip_suffix(name->tip_name, "^0", &len);
128 if (name->generation > 0) {
129 strbuf_grow(&sb, len +
130 1 + decimal_width(name->generation) +
131 1 + decimal_width(parent_number));
132 strbuf_addf(&sb, "%.*s~%d^%d", (int)len, name->tip_name,
133 name->generation, parent_number);
134 } else {
135 strbuf_grow(&sb, len +
136 1 + decimal_width(parent_number));
137 strbuf_addf(&sb, "%.*s^%d", (int)len, name->tip_name,
138 parent_number);
140 return strbuf_detach(&sb, NULL);
143 static void name_rev(struct commit *start_commit,
144 const char *tip_name, timestamp_t taggerdate,
145 int from_tag, int deref)
147 struct prio_queue queue;
148 struct commit *commit;
149 struct commit **parents_to_queue = NULL;
150 size_t parents_to_queue_nr, parents_to_queue_alloc = 0;
151 struct rev_name *start_name;
153 parse_commit(start_commit);
154 if (start_commit->date < cutoff)
155 return;
157 start_name = create_or_update_name(start_commit, taggerdate, 0, 0,
158 from_tag);
159 if (!start_name)
160 return;
161 if (deref)
162 start_name->tip_name = xstrfmt("%s^0", tip_name);
163 else
164 start_name->tip_name = xstrdup(tip_name);
166 memset(&queue, 0, sizeof(queue)); /* Use the prio_queue as LIFO */
167 prio_queue_put(&queue, start_commit);
169 while ((commit = prio_queue_get(&queue))) {
170 struct rev_name *name = get_commit_rev_name(commit);
171 struct commit_list *parents;
172 int parent_number = 1;
174 parents_to_queue_nr = 0;
176 for (parents = commit->parents;
177 parents;
178 parents = parents->next, parent_number++) {
179 struct commit *parent = parents->item;
180 struct rev_name *parent_name;
181 int generation, distance;
183 parse_commit(parent);
184 if (parent->date < cutoff)
185 continue;
187 if (parent_number > 1) {
188 generation = 0;
189 distance = name->distance + MERGE_TRAVERSAL_WEIGHT;
190 } else {
191 generation = name->generation + 1;
192 distance = name->distance + 1;
195 parent_name = create_or_update_name(parent, taggerdate,
196 generation,
197 distance, from_tag);
198 if (parent_name) {
199 if (parent_number > 1)
200 parent_name->tip_name =
201 get_parent_name(name,
202 parent_number);
203 else
204 parent_name->tip_name = name->tip_name;
205 ALLOC_GROW(parents_to_queue,
206 parents_to_queue_nr + 1,
207 parents_to_queue_alloc);
208 parents_to_queue[parents_to_queue_nr] = parent;
209 parents_to_queue_nr++;
213 /* The first parent must come out first from the prio_queue */
214 while (parents_to_queue_nr)
215 prio_queue_put(&queue,
216 parents_to_queue[--parents_to_queue_nr]);
219 clear_prio_queue(&queue);
220 free(parents_to_queue);
223 static int subpath_matches(const char *path, const char *filter)
225 const char *subpath = path;
227 while (subpath) {
228 if (!wildmatch(filter, subpath, 0))
229 return subpath - path;
230 subpath = strchr(subpath, '/');
231 if (subpath)
232 subpath++;
234 return -1;
237 static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
239 if (shorten_unambiguous)
240 refname = shorten_unambiguous_ref(refname, 0);
241 else if (skip_prefix(refname, "refs/heads/", &refname))
242 ; /* refname already advanced */
243 else
244 skip_prefix(refname, "refs/", &refname);
245 return refname;
248 struct name_ref_data {
249 int tags_only;
250 int name_only;
251 struct string_list ref_filters;
252 struct string_list exclude_filters;
255 static struct tip_table {
256 struct tip_table_entry {
257 struct object_id oid;
258 const char *refname;
259 struct commit *commit;
260 timestamp_t taggerdate;
261 unsigned int from_tag:1;
262 unsigned int deref:1;
263 } *table;
264 int nr;
265 int alloc;
266 int sorted;
267 } tip_table;
269 static void add_to_tip_table(const struct object_id *oid, const char *refname,
270 int shorten_unambiguous, struct commit *commit,
271 timestamp_t taggerdate, int from_tag, int deref)
273 refname = name_ref_abbrev(refname, shorten_unambiguous);
275 ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
276 oidcpy(&tip_table.table[tip_table.nr].oid, oid);
277 tip_table.table[tip_table.nr].refname = xstrdup(refname);
278 tip_table.table[tip_table.nr].commit = commit;
279 tip_table.table[tip_table.nr].taggerdate = taggerdate;
280 tip_table.table[tip_table.nr].from_tag = from_tag;
281 tip_table.table[tip_table.nr].deref = deref;
282 tip_table.nr++;
283 tip_table.sorted = 0;
286 static int tipcmp(const void *a_, const void *b_)
288 const struct tip_table_entry *a = a_, *b = b_;
289 return oidcmp(&a->oid, &b->oid);
292 static int cmp_by_tag_and_age(const void *a_, const void *b_)
294 const struct tip_table_entry *a = a_, *b = b_;
295 int cmp;
297 /* Prefer tags. */
298 cmp = b->from_tag - a->from_tag;
299 if (cmp)
300 return cmp;
302 /* Older is better. */
303 if (a->taggerdate < b->taggerdate)
304 return -1;
305 return a->taggerdate != b->taggerdate;
308 static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
310 struct object *o = parse_object(the_repository, oid);
311 struct name_ref_data *data = cb_data;
312 int can_abbreviate_output = data->tags_only && data->name_only;
313 int deref = 0;
314 int from_tag = 0;
315 struct commit *commit = NULL;
316 timestamp_t taggerdate = TIME_MAX;
318 if (data->tags_only && !starts_with(path, "refs/tags/"))
319 return 0;
321 if (data->exclude_filters.nr) {
322 struct string_list_item *item;
324 for_each_string_list_item(item, &data->exclude_filters) {
325 if (subpath_matches(path, item->string) >= 0)
326 return 0;
330 if (data->ref_filters.nr) {
331 struct string_list_item *item;
332 int matched = 0;
334 /* See if any of the patterns match. */
335 for_each_string_list_item(item, &data->ref_filters) {
337 * Check all patterns even after finding a match, so
338 * that we can see if a match with a subpath exists.
339 * When a user asked for 'refs/tags/v*' and 'v1.*',
340 * both of which match, the user is showing her
341 * willingness to accept a shortened output by having
342 * the 'v1.*' in the acceptable refnames, so we
343 * shouldn't stop when seeing 'refs/tags/v1.4' matches
344 * 'refs/tags/v*'. We should show it as 'v1.4'.
346 switch (subpath_matches(path, item->string)) {
347 case -1: /* did not match */
348 break;
349 case 0: /* matched fully */
350 matched = 1;
351 break;
352 default: /* matched subpath */
353 matched = 1;
354 can_abbreviate_output = 1;
355 break;
359 /* If none of the patterns matched, stop now */
360 if (!matched)
361 return 0;
364 while (o && o->type == OBJ_TAG) {
365 struct tag *t = (struct tag *) o;
366 if (!t->tagged)
367 break; /* broken repository */
368 o = parse_object(the_repository, &t->tagged->oid);
369 deref = 1;
370 taggerdate = t->date;
372 if (o && o->type == OBJ_COMMIT) {
373 commit = (struct commit *)o;
374 from_tag = starts_with(path, "refs/tags/");
375 if (taggerdate == TIME_MAX)
376 taggerdate = commit->date;
379 add_to_tip_table(oid, path, can_abbreviate_output, commit, taggerdate,
380 from_tag, deref);
381 return 0;
384 static void name_tips(void)
386 int i;
389 * Try to set better names first, so that worse ones spread
390 * less.
392 QSORT(tip_table.table, tip_table.nr, cmp_by_tag_and_age);
393 for (i = 0; i < tip_table.nr; i++) {
394 struct tip_table_entry *e = &tip_table.table[i];
395 if (e->commit) {
396 name_rev(e->commit, e->refname, e->taggerdate,
397 e->from_tag, e->deref);
402 static const struct object_id *nth_tip_table_ent(size_t ix, const void *table_)
404 const struct tip_table_entry *table = table_;
405 return &table[ix].oid;
408 static const char *get_exact_ref_match(const struct object *o)
410 int found;
412 if (!tip_table.table || !tip_table.nr)
413 return NULL;
415 if (!tip_table.sorted) {
416 QSORT(tip_table.table, tip_table.nr, tipcmp);
417 tip_table.sorted = 1;
420 found = oid_pos(&o->oid, tip_table.table, tip_table.nr,
421 nth_tip_table_ent);
422 if (0 <= found)
423 return tip_table.table[found].refname;
424 return NULL;
427 /* may return a constant string or use "buf" as scratch space */
428 static const char *get_rev_name(const struct object *o, struct strbuf *buf)
430 struct rev_name *n;
431 const struct commit *c;
433 if (o->type != OBJ_COMMIT)
434 return get_exact_ref_match(o);
435 c = (const struct commit *) o;
436 n = get_commit_rev_name(c);
437 if (!n)
438 return NULL;
440 if (!n->generation)
441 return n->tip_name;
442 else {
443 strbuf_reset(buf);
444 strbuf_addstr(buf, n->tip_name);
445 strbuf_strip_suffix(buf, "^0");
446 strbuf_addf(buf, "~%d", n->generation);
447 return buf->buf;
451 static void show_name(const struct object *obj,
452 const char *caller_name,
453 int always, int allow_undefined, int name_only)
455 const char *name;
456 const struct object_id *oid = &obj->oid;
457 struct strbuf buf = STRBUF_INIT;
459 if (!name_only)
460 printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
461 name = get_rev_name(obj, &buf);
462 if (name)
463 printf("%s\n", name);
464 else if (allow_undefined)
465 printf("undefined\n");
466 else if (always)
467 printf("%s\n", find_unique_abbrev(oid, DEFAULT_ABBREV));
468 else
469 die("cannot describe '%s'", oid_to_hex(oid));
470 strbuf_release(&buf);
473 static char const * const name_rev_usage[] = {
474 N_("git name-rev [<options>] <commit>..."),
475 N_("git name-rev [<options>] --all"),
476 N_("git name-rev [<options>] --stdin"),
477 NULL
480 static void name_rev_line(char *p, struct name_ref_data *data)
482 struct strbuf buf = STRBUF_INIT;
483 int counter = 0;
484 char *p_start;
485 const unsigned hexsz = the_hash_algo->hexsz;
487 for (p_start = p; *p; p++) {
488 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
489 if (!ishex(*p))
490 counter = 0;
491 else if (++counter == hexsz &&
492 !ishex(*(p+1))) {
493 struct object_id oid;
494 const char *name = NULL;
495 char c = *(p+1);
496 int p_len = p - p_start + 1;
498 counter = 0;
500 *(p+1) = 0;
501 if (!get_oid(p - (hexsz - 1), &oid)) {
502 struct object *o =
503 lookup_object(the_repository, &oid);
504 if (o)
505 name = get_rev_name(o, &buf);
507 *(p+1) = c;
509 if (!name)
510 continue;
512 if (data->name_only)
513 printf("%.*s%s", p_len - hexsz, p_start, name);
514 else
515 printf("%.*s (%s)", p_len, p_start, name);
516 p_start = p + 1;
520 /* flush */
521 if (p_start != p)
522 fwrite(p_start, p - p_start, 1, stdout);
524 strbuf_release(&buf);
527 int cmd_name_rev(int argc, const char **argv, const char *prefix)
529 struct object_array revs = OBJECT_ARRAY_INIT;
530 int all = 0, annotate_stdin = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
531 struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
532 struct option opts[] = {
533 OPT_BOOL(0, "name-only", &data.name_only, N_("print only ref-based names (no object names)")),
534 OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
535 OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
536 N_("only use refs matching <pattern>")),
537 OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
538 N_("ignore refs matching <pattern>")),
539 OPT_GROUP(""),
540 OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
541 OPT_BOOL(0, "stdin", &transform_stdin, N_("deprecated: use annotate-stdin instead")),
542 OPT_BOOL(0, "annotate-stdin", &annotate_stdin, N_("annotate text from stdin")),
543 OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
544 OPT_BOOL(0, "always", &always,
545 N_("show abbreviated commit object as fallback")),
547 /* A Hidden OPT_BOOL */
548 OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
549 N_("dereference tags in the input (internal use)"),
550 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
552 OPT_END(),
555 init_commit_rev_name(&rev_names);
556 git_config(git_default_config, NULL);
557 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
559 if (transform_stdin) {
560 warning("--stdin is deprecated. Please use --annotate-stdin instead, "
561 "which is functionally equivalent.\n"
562 "This option will be removed in a future release.");
563 annotate_stdin = 1;
566 if (all + annotate_stdin + !!argc > 1) {
567 error("Specify either a list, or --all, not both!");
568 usage_with_options(name_rev_usage, opts);
570 if (all || annotate_stdin)
571 cutoff = 0;
573 for (; argc; argc--, argv++) {
574 struct object_id oid;
575 struct object *object;
576 struct commit *commit;
578 if (get_oid(*argv, &oid)) {
579 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
580 *argv);
581 continue;
584 commit = NULL;
585 object = parse_object(the_repository, &oid);
586 if (object) {
587 struct object *peeled = deref_tag(the_repository,
588 object, *argv, 0);
589 if (peeled && peeled->type == OBJ_COMMIT)
590 commit = (struct commit *)peeled;
593 if (!object) {
594 fprintf(stderr, "Could not get object for %s. Skipping.\n",
595 *argv);
596 continue;
599 if (commit) {
600 if (cutoff > commit->date)
601 cutoff = commit->date;
604 if (peel_tag) {
605 if (!commit) {
606 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
607 *argv);
608 continue;
610 object = (struct object *)commit;
612 add_object_array(object, *argv, &revs);
615 if (cutoff) {
616 /* check for undeflow */
617 if (cutoff > TIME_MIN + CUTOFF_DATE_SLOP)
618 cutoff = cutoff - CUTOFF_DATE_SLOP;
619 else
620 cutoff = TIME_MIN;
622 for_each_ref(name_ref, &data);
623 name_tips();
625 if (annotate_stdin) {
626 struct strbuf sb = STRBUF_INIT;
628 while (strbuf_getline(&sb, stdin) != EOF) {
629 strbuf_addch(&sb, '\n');
630 name_rev_line(sb.buf, &data);
632 strbuf_release(&sb);
633 } else if (all) {
634 int i, max;
636 max = get_max_object_index();
637 for (i = 0; i < max; i++) {
638 struct object *obj = get_indexed_object(i);
639 if (!obj || obj->type != OBJ_COMMIT)
640 continue;
641 show_name(obj, NULL,
642 always, allow_undefined, data.name_only);
644 } else {
645 int i;
646 for (i = 0; i < revs.nr; i++)
647 show_name(revs.objects[i].item, revs.objects[i].name,
648 always, allow_undefined, data.name_only);
651 UNLEAK(revs);
652 return 0;