]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/name-rev.c
Merge branch 'tl/range-diff-custom-abbrev'
[thirdparty/git.git] / builtin / name-rev.c
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"
12 #include "commit-graph.h"
13
14 /*
15 * One day. See the 'name a rev shortly after epoch' test in t6120 when
16 * changing this value
17 */
18 #define CUTOFF_DATE_SLOP 86400
19
20 struct rev_name {
21 const char *tip_name;
22 timestamp_t taggerdate;
23 int generation;
24 int distance;
25 int from_tag;
26 };
27
28 define_commit_slab(commit_rev_name, struct rev_name);
29
30 static timestamp_t generation_cutoff = GENERATION_NUMBER_INFINITY;
31 static timestamp_t cutoff = TIME_MAX;
32 static struct commit_rev_name rev_names;
33
34 /* Disable the cutoff checks entirely */
35 static void disable_cutoff(void)
36 {
37 generation_cutoff = 0;
38 cutoff = 0;
39 }
40
41 /* Cutoff searching any commits older than this one */
42 static void set_commit_cutoff(struct commit *commit)
43 {
44
45 if (cutoff > commit->date)
46 cutoff = commit->date;
47
48 if (generation_cutoff) {
49 timestamp_t generation = commit_graph_generation(commit);
50
51 if (generation_cutoff > generation)
52 generation_cutoff = generation;
53 }
54 }
55
56 /* adjust the commit date cutoff with a slop to allow for slightly incorrect
57 * commit timestamps in case of clock skew.
58 */
59 static void adjust_cutoff_timestamp_for_slop(void)
60 {
61 if (cutoff) {
62 /* check for undeflow */
63 if (cutoff > TIME_MIN + CUTOFF_DATE_SLOP)
64 cutoff = cutoff - CUTOFF_DATE_SLOP;
65 else
66 cutoff = TIME_MIN;
67 }
68 }
69
70 /* Check if a commit is before the cutoff. Prioritize generation numbers
71 * first, but use the commit timestamp if we lack generation data.
72 */
73 static int commit_is_before_cutoff(struct commit *commit)
74 {
75 if (generation_cutoff < GENERATION_NUMBER_INFINITY)
76 return generation_cutoff &&
77 commit_graph_generation(commit) < generation_cutoff;
78
79 return commit->date < cutoff;
80 }
81
82 /* How many generations are maximally preferred over _one_ merge traversal? */
83 #define MERGE_TRAVERSAL_WEIGHT 65535
84
85 static int is_valid_rev_name(const struct rev_name *name)
86 {
87 return name && name->tip_name;
88 }
89
90 static struct rev_name *get_commit_rev_name(const struct commit *commit)
91 {
92 struct rev_name *name = commit_rev_name_peek(&rev_names, commit);
93
94 return is_valid_rev_name(name) ? name : NULL;
95 }
96
97 static int effective_distance(int distance, int generation)
98 {
99 return distance + (generation > 0 ? MERGE_TRAVERSAL_WEIGHT : 0);
100 }
101
102 static int is_better_name(struct rev_name *name,
103 timestamp_t taggerdate,
104 int generation,
105 int distance,
106 int from_tag)
107 {
108 int name_distance = effective_distance(name->distance, name->generation);
109 int new_distance = effective_distance(distance, generation);
110
111 /* If both are tags, we prefer the nearer one. */
112 if (from_tag && name->from_tag)
113 return name_distance > new_distance;
114
115 /* Favor a tag over a non-tag. */
116 if (name->from_tag != from_tag)
117 return from_tag;
118
119 /*
120 * We are now looking at two non-tags. Tiebreak to favor
121 * shorter hops.
122 */
123 if (name_distance != new_distance)
124 return name_distance > new_distance;
125
126 /* ... or tiebreak to favor older date */
127 if (name->taggerdate != taggerdate)
128 return name->taggerdate > taggerdate;
129
130 /* keep the current one if we cannot decide */
131 return 0;
132 }
133
134 static struct rev_name *create_or_update_name(struct commit *commit,
135 timestamp_t taggerdate,
136 int generation, int distance,
137 int from_tag)
138 {
139 struct rev_name *name = commit_rev_name_at(&rev_names, commit);
140
141 if (is_valid_rev_name(name) &&
142 !is_better_name(name, taggerdate, generation, distance, from_tag))
143 return NULL;
144
145 name->taggerdate = taggerdate;
146 name->generation = generation;
147 name->distance = distance;
148 name->from_tag = from_tag;
149
150 return name;
151 }
152
153 static char *get_parent_name(const struct rev_name *name, int parent_number)
154 {
155 struct strbuf sb = STRBUF_INIT;
156 size_t len;
157
158 strip_suffix(name->tip_name, "^0", &len);
159 if (name->generation > 0) {
160 strbuf_grow(&sb, len +
161 1 + decimal_width(name->generation) +
162 1 + decimal_width(parent_number));
163 strbuf_addf(&sb, "%.*s~%d^%d", (int)len, name->tip_name,
164 name->generation, parent_number);
165 } else {
166 strbuf_grow(&sb, len +
167 1 + decimal_width(parent_number));
168 strbuf_addf(&sb, "%.*s^%d", (int)len, name->tip_name,
169 parent_number);
170 }
171 return strbuf_detach(&sb, NULL);
172 }
173
174 static void name_rev(struct commit *start_commit,
175 const char *tip_name, timestamp_t taggerdate,
176 int from_tag, int deref)
177 {
178 struct prio_queue queue;
179 struct commit *commit;
180 struct commit **parents_to_queue = NULL;
181 size_t parents_to_queue_nr, parents_to_queue_alloc = 0;
182 struct rev_name *start_name;
183
184 parse_commit(start_commit);
185 if (commit_is_before_cutoff(start_commit))
186 return;
187
188 start_name = create_or_update_name(start_commit, taggerdate, 0, 0,
189 from_tag);
190 if (!start_name)
191 return;
192 if (deref)
193 start_name->tip_name = xstrfmt("%s^0", tip_name);
194 else
195 start_name->tip_name = xstrdup(tip_name);
196
197 memset(&queue, 0, sizeof(queue)); /* Use the prio_queue as LIFO */
198 prio_queue_put(&queue, start_commit);
199
200 while ((commit = prio_queue_get(&queue))) {
201 struct rev_name *name = get_commit_rev_name(commit);
202 struct commit_list *parents;
203 int parent_number = 1;
204
205 parents_to_queue_nr = 0;
206
207 for (parents = commit->parents;
208 parents;
209 parents = parents->next, parent_number++) {
210 struct commit *parent = parents->item;
211 struct rev_name *parent_name;
212 int generation, distance;
213
214 parse_commit(parent);
215 if (commit_is_before_cutoff(parent))
216 continue;
217
218 if (parent_number > 1) {
219 generation = 0;
220 distance = name->distance + MERGE_TRAVERSAL_WEIGHT;
221 } else {
222 generation = name->generation + 1;
223 distance = name->distance + 1;
224 }
225
226 parent_name = create_or_update_name(parent, taggerdate,
227 generation,
228 distance, from_tag);
229 if (parent_name) {
230 if (parent_number > 1)
231 parent_name->tip_name =
232 get_parent_name(name,
233 parent_number);
234 else
235 parent_name->tip_name = name->tip_name;
236 ALLOC_GROW(parents_to_queue,
237 parents_to_queue_nr + 1,
238 parents_to_queue_alloc);
239 parents_to_queue[parents_to_queue_nr] = parent;
240 parents_to_queue_nr++;
241 }
242 }
243
244 /* The first parent must come out first from the prio_queue */
245 while (parents_to_queue_nr)
246 prio_queue_put(&queue,
247 parents_to_queue[--parents_to_queue_nr]);
248 }
249
250 clear_prio_queue(&queue);
251 free(parents_to_queue);
252 }
253
254 static int subpath_matches(const char *path, const char *filter)
255 {
256 const char *subpath = path;
257
258 while (subpath) {
259 if (!wildmatch(filter, subpath, 0))
260 return subpath - path;
261 subpath = strchr(subpath, '/');
262 if (subpath)
263 subpath++;
264 }
265 return -1;
266 }
267
268 struct name_ref_data {
269 int tags_only;
270 int name_only;
271 struct string_list ref_filters;
272 struct string_list exclude_filters;
273 };
274
275 static struct tip_table {
276 struct tip_table_entry {
277 struct object_id oid;
278 const char *refname;
279 struct commit *commit;
280 timestamp_t taggerdate;
281 unsigned int from_tag:1;
282 unsigned int deref:1;
283 } *table;
284 int nr;
285 int alloc;
286 int sorted;
287 } tip_table;
288
289 static void add_to_tip_table(const struct object_id *oid, const char *refname,
290 int shorten_unambiguous, struct commit *commit,
291 timestamp_t taggerdate, int from_tag, int deref)
292 {
293 char *short_refname = NULL;
294
295 if (shorten_unambiguous)
296 short_refname = shorten_unambiguous_ref(refname, 0);
297 else if (skip_prefix(refname, "refs/heads/", &refname))
298 ; /* refname already advanced */
299 else
300 skip_prefix(refname, "refs/", &refname);
301
302 ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
303 oidcpy(&tip_table.table[tip_table.nr].oid, oid);
304 tip_table.table[tip_table.nr].refname = short_refname ?
305 short_refname : xstrdup(refname);
306 tip_table.table[tip_table.nr].commit = commit;
307 tip_table.table[tip_table.nr].taggerdate = taggerdate;
308 tip_table.table[tip_table.nr].from_tag = from_tag;
309 tip_table.table[tip_table.nr].deref = deref;
310 tip_table.nr++;
311 tip_table.sorted = 0;
312 }
313
314 static int tipcmp(const void *a_, const void *b_)
315 {
316 const struct tip_table_entry *a = a_, *b = b_;
317 return oidcmp(&a->oid, &b->oid);
318 }
319
320 static int cmp_by_tag_and_age(const void *a_, const void *b_)
321 {
322 const struct tip_table_entry *a = a_, *b = b_;
323 int cmp;
324
325 /* Prefer tags. */
326 cmp = b->from_tag - a->from_tag;
327 if (cmp)
328 return cmp;
329
330 /* Older is better. */
331 if (a->taggerdate < b->taggerdate)
332 return -1;
333 return a->taggerdate != b->taggerdate;
334 }
335
336 static int name_ref(const char *path, const struct object_id *oid,
337 int flags UNUSED, void *cb_data)
338 {
339 struct object *o = parse_object(the_repository, oid);
340 struct name_ref_data *data = cb_data;
341 int can_abbreviate_output = data->tags_only && data->name_only;
342 int deref = 0;
343 int from_tag = 0;
344 struct commit *commit = NULL;
345 timestamp_t taggerdate = TIME_MAX;
346
347 if (data->tags_only && !starts_with(path, "refs/tags/"))
348 return 0;
349
350 if (data->exclude_filters.nr) {
351 struct string_list_item *item;
352
353 for_each_string_list_item(item, &data->exclude_filters) {
354 if (subpath_matches(path, item->string) >= 0)
355 return 0;
356 }
357 }
358
359 if (data->ref_filters.nr) {
360 struct string_list_item *item;
361 int matched = 0;
362
363 /* See if any of the patterns match. */
364 for_each_string_list_item(item, &data->ref_filters) {
365 /*
366 * Check all patterns even after finding a match, so
367 * that we can see if a match with a subpath exists.
368 * When a user asked for 'refs/tags/v*' and 'v1.*',
369 * both of which match, the user is showing her
370 * willingness to accept a shortened output by having
371 * the 'v1.*' in the acceptable refnames, so we
372 * shouldn't stop when seeing 'refs/tags/v1.4' matches
373 * 'refs/tags/v*'. We should show it as 'v1.4'.
374 */
375 switch (subpath_matches(path, item->string)) {
376 case -1: /* did not match */
377 break;
378 case 0: /* matched fully */
379 matched = 1;
380 break;
381 default: /* matched subpath */
382 matched = 1;
383 can_abbreviate_output = 1;
384 break;
385 }
386 }
387
388 /* If none of the patterns matched, stop now */
389 if (!matched)
390 return 0;
391 }
392
393 while (o && o->type == OBJ_TAG) {
394 struct tag *t = (struct tag *) o;
395 if (!t->tagged)
396 break; /* broken repository */
397 o = parse_object(the_repository, &t->tagged->oid);
398 deref = 1;
399 taggerdate = t->date;
400 }
401 if (o && o->type == OBJ_COMMIT) {
402 commit = (struct commit *)o;
403 from_tag = starts_with(path, "refs/tags/");
404 if (taggerdate == TIME_MAX)
405 taggerdate = commit->date;
406 }
407
408 add_to_tip_table(oid, path, can_abbreviate_output, commit, taggerdate,
409 from_tag, deref);
410 return 0;
411 }
412
413 static void name_tips(void)
414 {
415 int i;
416
417 /*
418 * Try to set better names first, so that worse ones spread
419 * less.
420 */
421 QSORT(tip_table.table, tip_table.nr, cmp_by_tag_and_age);
422 for (i = 0; i < tip_table.nr; i++) {
423 struct tip_table_entry *e = &tip_table.table[i];
424 if (e->commit) {
425 name_rev(e->commit, e->refname, e->taggerdate,
426 e->from_tag, e->deref);
427 }
428 }
429 }
430
431 static const struct object_id *nth_tip_table_ent(size_t ix, const void *table_)
432 {
433 const struct tip_table_entry *table = table_;
434 return &table[ix].oid;
435 }
436
437 static const char *get_exact_ref_match(const struct object *o)
438 {
439 int found;
440
441 if (!tip_table.table || !tip_table.nr)
442 return NULL;
443
444 if (!tip_table.sorted) {
445 QSORT(tip_table.table, tip_table.nr, tipcmp);
446 tip_table.sorted = 1;
447 }
448
449 found = oid_pos(&o->oid, tip_table.table, tip_table.nr,
450 nth_tip_table_ent);
451 if (0 <= found)
452 return tip_table.table[found].refname;
453 return NULL;
454 }
455
456 /* may return a constant string or use "buf" as scratch space */
457 static const char *get_rev_name(const struct object *o, struct strbuf *buf)
458 {
459 struct rev_name *n;
460 const struct commit *c;
461
462 if (o->type != OBJ_COMMIT)
463 return get_exact_ref_match(o);
464 c = (const struct commit *) o;
465 n = get_commit_rev_name(c);
466 if (!n)
467 return NULL;
468
469 if (!n->generation)
470 return n->tip_name;
471 else {
472 strbuf_reset(buf);
473 strbuf_addstr(buf, n->tip_name);
474 strbuf_strip_suffix(buf, "^0");
475 strbuf_addf(buf, "~%d", n->generation);
476 return buf->buf;
477 }
478 }
479
480 static void show_name(const struct object *obj,
481 const char *caller_name,
482 int always, int allow_undefined, int name_only)
483 {
484 const char *name;
485 const struct object_id *oid = &obj->oid;
486 struct strbuf buf = STRBUF_INIT;
487
488 if (!name_only)
489 printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
490 name = get_rev_name(obj, &buf);
491 if (name)
492 printf("%s\n", name);
493 else if (allow_undefined)
494 printf("undefined\n");
495 else if (always)
496 printf("%s\n", find_unique_abbrev(oid, DEFAULT_ABBREV));
497 else
498 die("cannot describe '%s'", oid_to_hex(oid));
499 strbuf_release(&buf);
500 }
501
502 static char const * const name_rev_usage[] = {
503 N_("git name-rev [<options>] <commit>..."),
504 N_("git name-rev [<options>] --all"),
505 N_("git name-rev [<options>] --annotate-stdin"),
506 NULL
507 };
508
509 static void name_rev_line(char *p, struct name_ref_data *data)
510 {
511 struct strbuf buf = STRBUF_INIT;
512 int counter = 0;
513 char *p_start;
514 const unsigned hexsz = the_hash_algo->hexsz;
515
516 for (p_start = p; *p; p++) {
517 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
518 if (!ishex(*p))
519 counter = 0;
520 else if (++counter == hexsz &&
521 !ishex(*(p+1))) {
522 struct object_id oid;
523 const char *name = NULL;
524 char c = *(p+1);
525 int p_len = p - p_start + 1;
526
527 counter = 0;
528
529 *(p+1) = 0;
530 if (!get_oid(p - (hexsz - 1), &oid)) {
531 struct object *o =
532 lookup_object(the_repository, &oid);
533 if (o)
534 name = get_rev_name(o, &buf);
535 }
536 *(p+1) = c;
537
538 if (!name)
539 continue;
540
541 if (data->name_only)
542 printf("%.*s%s", p_len - hexsz, p_start, name);
543 else
544 printf("%.*s (%s)", p_len, p_start, name);
545 p_start = p + 1;
546 }
547 }
548
549 /* flush */
550 if (p_start != p)
551 fwrite(p_start, p - p_start, 1, stdout);
552
553 strbuf_release(&buf);
554 }
555
556 int cmd_name_rev(int argc, const char **argv, const char *prefix)
557 {
558 struct object_array revs = OBJECT_ARRAY_INIT;
559 int all = 0, annotate_stdin = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
560 struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
561 struct option opts[] = {
562 OPT_BOOL(0, "name-only", &data.name_only, N_("print only ref-based names (no object names)")),
563 OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
564 OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
565 N_("only use refs matching <pattern>")),
566 OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
567 N_("ignore refs matching <pattern>")),
568 OPT_GROUP(""),
569 OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
570 OPT_BOOL(0, "stdin", &transform_stdin, N_("deprecated: use --annotate-stdin instead")),
571 OPT_BOOL(0, "annotate-stdin", &annotate_stdin, N_("annotate text from stdin")),
572 OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
573 OPT_BOOL(0, "always", &always,
574 N_("show abbreviated commit object as fallback")),
575 {
576 /* A Hidden OPT_BOOL */
577 OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
578 N_("dereference tags in the input (internal use)"),
579 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
580 },
581 OPT_END(),
582 };
583
584 init_commit_rev_name(&rev_names);
585 git_config(git_default_config, NULL);
586 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
587
588 if (transform_stdin) {
589 warning("--stdin is deprecated. Please use --annotate-stdin instead, "
590 "which is functionally equivalent.\n"
591 "This option will be removed in a future release.");
592 annotate_stdin = 1;
593 }
594
595 if (all + annotate_stdin + !!argc > 1) {
596 error("Specify either a list, or --all, not both!");
597 usage_with_options(name_rev_usage, opts);
598 }
599 if (all || annotate_stdin)
600 disable_cutoff();
601
602 for (; argc; argc--, argv++) {
603 struct object_id oid;
604 struct object *object;
605 struct commit *commit;
606
607 if (get_oid(*argv, &oid)) {
608 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
609 *argv);
610 continue;
611 }
612
613 commit = NULL;
614 object = parse_object(the_repository, &oid);
615 if (object) {
616 struct object *peeled = deref_tag(the_repository,
617 object, *argv, 0);
618 if (peeled && peeled->type == OBJ_COMMIT)
619 commit = (struct commit *)peeled;
620 }
621
622 if (!object) {
623 fprintf(stderr, "Could not get object for %s. Skipping.\n",
624 *argv);
625 continue;
626 }
627
628 if (commit)
629 set_commit_cutoff(commit);
630
631 if (peel_tag) {
632 if (!commit) {
633 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
634 *argv);
635 continue;
636 }
637 object = (struct object *)commit;
638 }
639 add_object_array(object, *argv, &revs);
640 }
641
642 adjust_cutoff_timestamp_for_slop();
643
644 for_each_ref(name_ref, &data);
645 name_tips();
646
647 if (annotate_stdin) {
648 struct strbuf sb = STRBUF_INIT;
649
650 while (strbuf_getline(&sb, stdin) != EOF) {
651 strbuf_addch(&sb, '\n');
652 name_rev_line(sb.buf, &data);
653 }
654 strbuf_release(&sb);
655 } else if (all) {
656 int i, max;
657
658 max = get_max_object_index();
659 for (i = 0; i < max; i++) {
660 struct object *obj = get_indexed_object(i);
661 if (!obj || obj->type != OBJ_COMMIT)
662 continue;
663 show_name(obj, NULL,
664 always, allow_undefined, data.name_only);
665 }
666 } else {
667 int i;
668 for (i = 0; i < revs.nr; i++)
669 show_name(revs.objects[i].item, revs.objects[i].name,
670 always, allow_undefined, data.name_only);
671 }
672
673 UNLEAK(revs);
674 return 0;
675 }