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