]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/name-rev.c
name-rev: release unused name strings
[thirdparty/git.git] / builtin / name-rev.c
CommitLineData
d6b64ed0 1#include "builtin.h"
bd321bcc 2#include "cache.h"
109cd76d 3#include "repository.h"
b2141fc1 4#include "config.h"
bd321bcc
JS
5#include "commit.h"
6#include "tag.h"
7#include "refs.h"
edefb1a2 8#include "parse-options.h"
49f7a2fd 9#include "prio-queue.h"
b23e0b93 10#include "sha1-lookup.h"
8fd79a73 11#include "commit-slab.h"
bd321bcc 12
2e09c012
SG
13/*
14 * One day. See the 'name a rev shortly after epoch' test in t6120 when
15 * changing this value
16 */
17#define CUTOFF_DATE_SLOP 86400
c075aea5 18
71620ca8 19struct rev_name {
2d539754 20 char *tip_name;
dddbad72 21 timestamp_t taggerdate;
bd321bcc 22 int generation;
ac076c29 23 int distance;
ef1e7406 24 int from_tag;
71620ca8 25};
bd321bcc 26
f13ca7ce 27define_commit_slab(commit_rev_name, struct rev_name);
8fd79a73 28
5589e87f 29static timestamp_t cutoff = TIME_MAX;
8fd79a73 30static struct commit_rev_name rev_names;
bd321bcc 31
ac076c29
JS
32/* How many generations are maximally preferred over _one_ merge traversal? */
33#define MERGE_TRAVERSAL_WEIGHT 65535
34
f13ca7ce
RS
35static int is_valid_rev_name(const struct rev_name *name)
36{
2d539754 37 return name && (name->generation || name->tip_name);
f13ca7ce
RS
38}
39
36d2419c 40static struct rev_name *get_commit_rev_name(const struct commit *commit)
8fd79a73 41{
f13ca7ce 42 struct rev_name *name = commit_rev_name_peek(&rev_names, commit);
8fd79a73 43
f13ca7ce 44 return is_valid_rev_name(name) ? name : NULL;
8fd79a73
NTND
45}
46
0041bf65 47static int is_better_name(struct rev_name *name,
78089b71 48 timestamp_t taggerdate,
ef1e7406
JH
49 int distance,
50 int from_tag)
0041bf65 51{
ef1e7406
JH
52 /*
53 * When comparing names based on tags, prefer names
54 * based on the older tag, even if it is farther away.
55 */
56 if (from_tag && name->from_tag)
57 return (name->taggerdate > taggerdate ||
58 (name->taggerdate == taggerdate &&
59 name->distance > distance));
60
61 /*
62 * We know that at least one of them is a non-tag at this point.
63 * favor a tag over a non-tag.
64 */
65 if (name->from_tag != from_tag)
66 return from_tag;
67
68 /*
69 * We are now looking at two non-tags. Tiebreak to favor
70 * shorter hops.
71 */
72 if (name->distance != distance)
73 return name->distance > distance;
74
75 /* ... or tiebreak to favor older date */
76 if (name->taggerdate != taggerdate)
77 return name->taggerdate > taggerdate;
78
79 /* keep the current one if we cannot decide */
80 return 0;
0041bf65
JH
81}
82
766f9e39 83static struct rev_name *create_or_update_name(struct commit *commit,
766f9e39
SG
84 timestamp_t taggerdate,
85 int generation, int distance,
86 int from_tag)
bd321bcc 87{
f13ca7ce 88 struct rev_name *name = commit_rev_name_at(&rev_names, commit);
bd321bcc 89
2d539754
RS
90 if (is_valid_rev_name(name)) {
91 if (!is_better_name(name, taggerdate, distance, from_tag))
92 return NULL;
93
94 /*
95 * This string might still be shared with ancestors
96 * (generation > 0). We can release it here regardless,
97 * because the new name that has just won will be better
98 * for them as well, so name_rev() will replace these
99 * stale pointers when it processes the parents.
100 */
101 if (!name->generation)
102 free(name->tip_name);
103 }
3e2feb0d 104
3e2feb0d
105 name->taggerdate = taggerdate;
106 name->generation = generation;
107 name->distance = distance;
108 name->from_tag = from_tag;
109
110 return name;
766f9e39
SG
111}
112
ddc42ec7
RS
113static char *get_parent_name(const struct rev_name *name, int parent_number)
114{
1c56fc20 115 struct strbuf sb = STRBUF_INIT;
ddc42ec7
RS
116 size_t len;
117
118 strip_suffix(name->tip_name, "^0", &len);
1c56fc20
RS
119 if (name->generation > 0) {
120 strbuf_grow(&sb, len +
121 1 + decimal_width(name->generation) +
122 1 + decimal_width(parent_number));
123 strbuf_addf(&sb, "%.*s~%d^%d", (int)len, name->tip_name,
124 name->generation, parent_number);
125 } else {
126 strbuf_grow(&sb, len +
127 1 + decimal_width(parent_number));
128 strbuf_addf(&sb, "%.*s^%d", (int)len, name->tip_name,
129 parent_number);
130 }
131 return strbuf_detach(&sb, NULL);
ddc42ec7
RS
132}
133
49f7a2fd 134static void name_rev(struct commit *start_commit,
dddbad72 135 const char *tip_name, timestamp_t taggerdate,
2866fd28 136 int from_tag, int deref)
bd321bcc 137{
49f7a2fd
SG
138 struct prio_queue queue;
139 struct commit *commit;
140 struct commit **parents_to_queue = NULL;
141 size_t parents_to_queue_nr, parents_to_queue_alloc = 0;
977dc191 142 struct rev_name *start_name;
2866fd28
SG
143
144 parse_commit(start_commit);
145 if (start_commit->date < cutoff)
146 return;
147
977dc191
RS
148 start_name = create_or_update_name(start_commit, taggerdate, 0, 0,
149 from_tag);
150 if (!start_name)
151 return;
2866fd28 152 if (deref)
977dc191 153 start_name->tip_name = xstrfmt("%s^0", tip_name);
15a4205d 154 else
977dc191 155 start_name->tip_name = xstrdup(tip_name);
bd321bcc 156
49f7a2fd
SG
157 memset(&queue, 0, sizeof(queue)); /* Use the prio_queue as LIFO */
158 prio_queue_put(&queue, start_commit);
159
160 while ((commit = prio_queue_get(&queue))) {
161 struct rev_name *name = get_commit_rev_name(commit);
162 struct commit_list *parents;
163 int parent_number = 1;
164
165 parents_to_queue_nr = 0;
166
167 for (parents = commit->parents;
168 parents;
169 parents = parents->next, parent_number++) {
170 struct commit *parent = parents->item;
977dc191 171 struct rev_name *parent_name;
49f7a2fd
SG
172 int generation, distance;
173
174 parse_commit(parent);
175 if (parent->date < cutoff)
176 continue;
177
178 if (parent_number > 1) {
49f7a2fd
SG
179 generation = 0;
180 distance = name->distance + MERGE_TRAVERSAL_WEIGHT;
181 } else {
49f7a2fd
SG
182 generation = name->generation + 1;
183 distance = name->distance + 1;
184 }
185
977dc191
RS
186 parent_name = create_or_update_name(parent, taggerdate,
187 generation,
188 distance, from_tag);
189 if (parent_name) {
190 if (parent_number > 1)
191 parent_name->tip_name =
192 get_parent_name(name,
193 parent_number);
194 else
195 parent_name->tip_name = name->tip_name;
49f7a2fd
SG
196 ALLOC_GROW(parents_to_queue,
197 parents_to_queue_nr + 1,
198 parents_to_queue_alloc);
199 parents_to_queue[parents_to_queue_nr] = parent;
200 parents_to_queue_nr++;
201 }
bd321bcc 202 }
3a521503 203
49f7a2fd
SG
204 /* The first parent must come out first from the prio_queue */
205 while (parents_to_queue_nr)
206 prio_queue_put(&queue,
207 parents_to_queue[--parents_to_queue_nr]);
bd321bcc 208 }
49f7a2fd
SG
209
210 clear_prio_queue(&queue);
211 free(parents_to_queue);
bd321bcc
JS
212}
213
98c5c4ad
NK
214static int subpath_matches(const char *path, const char *filter)
215{
216 const char *subpath = path;
217
218 while (subpath) {
55d34269 219 if (!wildmatch(filter, subpath, 0))
98c5c4ad
NK
220 return subpath - path;
221 subpath = strchr(subpath, '/');
222 if (subpath)
223 subpath++;
224 }
225 return -1;
226}
227
9608a190
JH
228static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
229{
230 if (shorten_unambiguous)
231 refname = shorten_unambiguous_ref(refname, 0);
2059e79c
RS
232 else if (skip_prefix(refname, "refs/heads/", &refname))
233 ; /* refname already advanced */
234 else
235 skip_prefix(refname, "refs/", &refname);
9608a190
JH
236 return refname;
237}
238
2afc29aa
JS
239struct name_ref_data {
240 int tags_only;
23615708 241 int name_only;
290be667 242 struct string_list ref_filters;
96415b49 243 struct string_list exclude_filters;
2afc29aa
JS
244};
245
b23e0b93
JH
246static struct tip_table {
247 struct tip_table_entry {
511dca80 248 struct object_id oid;
b23e0b93
JH
249 const char *refname;
250 } *table;
251 int nr;
252 int alloc;
253 int sorted;
254} tip_table;
255
511dca80 256static void add_to_tip_table(const struct object_id *oid, const char *refname,
b23e0b93
JH
257 int shorten_unambiguous)
258{
259 refname = name_ref_abbrev(refname, shorten_unambiguous);
260
261 ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
511dca80 262 oidcpy(&tip_table.table[tip_table.nr].oid, oid);
b23e0b93
JH
263 tip_table.table[tip_table.nr].refname = xstrdup(refname);
264 tip_table.nr++;
265 tip_table.sorted = 0;
266}
267
268static int tipcmp(const void *a_, const void *b_)
269{
270 const struct tip_table_entry *a = a_, *b = b_;
511dca80 271 return oidcmp(&a->oid, &b->oid);
b23e0b93
JH
272}
273
73868486 274static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
bd321bcc 275{
109cd76d 276 struct object *o = parse_object(the_repository, oid);
2afc29aa 277 struct name_ref_data *data = cb_data;
98c5c4ad 278 int can_abbreviate_output = data->tags_only && data->name_only;
bd321bcc 279 int deref = 0;
dddbad72 280 timestamp_t taggerdate = TIME_MAX;
bd321bcc 281
59556548 282 if (data->tags_only && !starts_with(path, "refs/tags/"))
2afc29aa
JS
283 return 0;
284
96415b49
JK
285 if (data->exclude_filters.nr) {
286 struct string_list_item *item;
287
288 for_each_string_list_item(item, &data->exclude_filters) {
289 if (subpath_matches(path, item->string) >= 0)
290 return 0;
291 }
292 }
293
290be667
JK
294 if (data->ref_filters.nr) {
295 struct string_list_item *item;
296 int matched = 0;
297
298 /* See if any of the patterns match. */
299 for_each_string_list_item(item, &data->ref_filters) {
300 /*
301 * Check all patterns even after finding a match, so
302 * that we can see if a match with a subpath exists.
303 * When a user asked for 'refs/tags/v*' and 'v1.*',
304 * both of which match, the user is showing her
305 * willingness to accept a shortened output by having
306 * the 'v1.*' in the acceptable refnames, so we
307 * shouldn't stop when seeing 'refs/tags/v1.4' matches
308 * 'refs/tags/v*'. We should show it as 'v1.4'.
309 */
310 switch (subpath_matches(path, item->string)) {
311 case -1: /* did not match */
312 break;
313 case 0: /* matched fully */
314 matched = 1;
315 break;
316 default: /* matched subpath */
317 matched = 1;
318 can_abbreviate_output = 1;
319 break;
320 }
98c5c4ad 321 }
290be667
JK
322
323 /* If none of the patterns matched, stop now */
324 if (!matched)
325 return 0;
98c5c4ad 326 }
bd321bcc 327
511dca80 328 add_to_tip_table(oid, path, can_abbreviate_output);
b23e0b93 329
1974632c 330 while (o && o->type == OBJ_TAG) {
bd321bcc
JS
331 struct tag *t = (struct tag *) o;
332 if (!t->tagged)
333 break; /* broken repository */
109cd76d 334 o = parse_object(the_repository, &t->tagged->oid);
bd321bcc 335 deref = 1;
75504248 336 taggerdate = t->date;
bd321bcc 337 }
1974632c 338 if (o && o->type == OBJ_COMMIT) {
bd321bcc 339 struct commit *commit = (struct commit *)o;
ef1e7406 340 int from_tag = starts_with(path, "refs/tags/");
bd321bcc 341
5554451d 342 if (taggerdate == TIME_MAX)
e0c4da6f 343 taggerdate = commit->date;
9608a190 344 path = name_ref_abbrev(path, can_abbreviate_output);
15a4205d 345 name_rev(commit, path, taggerdate, from_tag, deref);
bd321bcc
JS
346 }
347 return 0;
348}
349
b23e0b93
JH
350static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
351{
352 struct tip_table_entry *table = table_;
511dca80 353 return table[ix].oid.hash;
b23e0b93
JH
354}
355
356static const char *get_exact_ref_match(const struct object *o)
357{
358 int found;
359
360 if (!tip_table.table || !tip_table.nr)
361 return NULL;
362
363 if (!tip_table.sorted) {
9ed0d8d6 364 QSORT(tip_table.table, tip_table.nr, tipcmp);
b23e0b93
JH
365 tip_table.sorted = 1;
366 }
367
ed1c9977 368 found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr,
b23e0b93
JH
369 nth_tip_table_ent);
370 if (0 <= found)
371 return tip_table.table[found].refname;
372 return NULL;
373}
374
903fc7da
JK
375/* may return a constant string or use "buf" as scratch space */
376static const char *get_rev_name(const struct object *o, struct strbuf *buf)
bd321bcc 377{
d3ff6f55 378 struct rev_name *n;
36d2419c 379 const struct commit *c;
d3ff6f55 380
1974632c 381 if (o->type != OBJ_COMMIT)
b23e0b93 382 return get_exact_ref_match(o);
36d2419c 383 c = (const struct commit *) o;
8fd79a73 384 n = get_commit_rev_name(c);
bd321bcc 385 if (!n)
a2cf9f44 386 return NULL;
bd321bcc
JS
387
388 if (!n->generation)
389 return n->tip_name;
59d3f541 390 else {
903fc7da 391 strbuf_reset(buf);
c3794d4c
RS
392 strbuf_addstr(buf, n->tip_name);
393 strbuf_strip_suffix(buf, "^0");
394 strbuf_addf(buf, "~%d", n->generation);
903fc7da 395 return buf->buf;
59d3f541 396 }
bd321bcc 397}
1f1e895f 398
da2478db
JH
399static void show_name(const struct object *obj,
400 const char *caller_name,
401 int always, int allow_undefined, int name_only)
402{
403 const char *name;
f2fd0760 404 const struct object_id *oid = &obj->oid;
903fc7da 405 struct strbuf buf = STRBUF_INIT;
da2478db
JH
406
407 if (!name_only)
f2fd0760 408 printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
903fc7da 409 name = get_rev_name(obj, &buf);
da2478db
JH
410 if (name)
411 printf("%s\n", name);
412 else if (allow_undefined)
413 printf("undefined\n");
414 else if (always)
aab9583f 415 printf("%s\n", find_unique_abbrev(oid, DEFAULT_ABBREV));
da2478db 416 else
f2fd0760 417 die("cannot describe '%s'", oid_to_hex(oid));
903fc7da 418 strbuf_release(&buf);
da2478db
JH
419}
420
edefb1a2 421static char const * const name_rev_usage[] = {
9c9b4f2f
AH
422 N_("git name-rev [<options>] <commit>..."),
423 N_("git name-rev [<options>] --all"),
424 N_("git name-rev [<options>] --stdin"),
edefb1a2
PH
425 NULL
426};
427
e8b55fab
JH
428static void name_rev_line(char *p, struct name_ref_data *data)
429{
903fc7da 430 struct strbuf buf = STRBUF_INIT;
1c4675dc 431 int counter = 0;
e8b55fab 432 char *p_start;
1c4675dc 433 const unsigned hexsz = the_hash_algo->hexsz;
434
e8b55fab
JH
435 for (p_start = p; *p; p++) {
436#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
437 if (!ishex(*p))
1c4675dc 438 counter = 0;
439 else if (++counter == hexsz &&
e8b55fab 440 !ishex(*(p+1))) {
511dca80 441 struct object_id oid;
e8b55fab
JH
442 const char *name = NULL;
443 char c = *(p+1);
7c5b1675 444 int p_len = p - p_start + 1;
e8b55fab 445
1c4675dc 446 counter = 0;
e8b55fab
JH
447
448 *(p+1) = 0;
1c4675dc 449 if (!get_oid(p - (hexsz - 1), &oid)) {
e8b55fab 450 struct object *o =
d0229abd 451 lookup_object(the_repository, &oid);
e8b55fab 452 if (o)
903fc7da 453 name = get_rev_name(o, &buf);
e8b55fab
JH
454 }
455 *(p+1) = c;
456
457 if (!name)
458 continue;
459
7c5b1675 460 if (data->name_only)
1c4675dc 461 printf("%.*s%s", p_len - hexsz, p_start, name);
7c5b1675
RS
462 else
463 printf("%.*s (%s)", p_len, p_start, name);
e8b55fab
JH
464 p_start = p + 1;
465 }
466 }
467
468 /* flush */
469 if (p_start != p)
470 fwrite(p_start, p - p_start, 1, stdout);
903fc7da
JK
471
472 strbuf_release(&buf);
e8b55fab
JH
473}
474
d6b64ed0 475int cmd_name_rev(int argc, const char **argv, const char *prefix)
bd321bcc 476{
3cd47459 477 struct object_array revs = OBJECT_ARRAY_INIT;
adfc1857 478 int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
96415b49 479 struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
edefb1a2 480 struct option opts[] = {
d5d09d47
SB
481 OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
482 OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
290be667 483 OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
422cad0a 484 N_("only use refs matching <pattern>")),
96415b49
JK
485 OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
486 N_("ignore refs matching <pattern>")),
edefb1a2 487 OPT_GROUP(""),
d5d09d47
SB
488 OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
489 OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
490 OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
491 OPT_BOOL(0, "always", &always,
422cad0a 492 N_("show abbreviated commit object as fallback")),
adfc1857
JH
493 {
494 /* A Hidden OPT_BOOL */
495 OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
496 N_("dereference tags in the input (internal use)"),
497 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
498 },
edefb1a2
PH
499 OPT_END(),
500 };
bd321bcc 501
8fd79a73 502 init_commit_rev_name(&rev_names);
ef90d6d4 503 git_config(git_default_config, NULL);
37782920 504 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
05efb7b7 505 if (all + transform_stdin + !!argc > 1) {
edefb1a2
PH
506 error("Specify either a list, or --all, not both!");
507 usage_with_options(name_rev_usage, opts);
508 }
509 if (all || transform_stdin)
510 cutoff = 0;
bd321bcc 511
edefb1a2 512 for (; argc; argc--, argv++) {
511dca80 513 struct object_id oid;
118aa4ac 514 struct object *object;
bd321bcc
JS
515 struct commit *commit;
516
511dca80 517 if (get_oid(*argv, &oid)) {
bd321bcc
JS
518 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
519 *argv);
520 continue;
521 }
522
118aa4ac 523 commit = NULL;
109cd76d 524 object = parse_object(the_repository, &oid);
118aa4ac 525 if (object) {
a74093da
SB
526 struct object *peeled = deref_tag(the_repository,
527 object, *argv, 0);
118aa4ac
JH
528 if (peeled && peeled->type == OBJ_COMMIT)
529 commit = (struct commit *)peeled;
530 }
531
532 if (!object) {
533 fprintf(stderr, "Could not get object for %s. Skipping.\n",
bd321bcc
JS
534 *argv);
535 continue;
536 }
537
118aa4ac
JH
538 if (commit) {
539 if (cutoff > commit->date)
540 cutoff = commit->date;
541 }
adfc1857
JH
542
543 if (peel_tag) {
544 if (!commit) {
545 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
546 *argv);
547 continue;
548 }
549 object = (struct object *)commit;
550 }
118aa4ac 551 add_object_array(object, *argv, &revs);
bd321bcc
JS
552 }
553
2e09c012
SG
554 if (cutoff) {
555 /* check for undeflow */
556 if (cutoff > TIME_MIN + CUTOFF_DATE_SLOP)
557 cutoff = cutoff - CUTOFF_DATE_SLOP;
558 else
559 cutoff = TIME_MIN;
560 }
73868486 561 for_each_ref(name_ref, &data);
bd321bcc
JS
562
563 if (transform_stdin) {
564 char buffer[2048];
bd321bcc
JS
565
566 while (!feof(stdin)) {
e8b55fab 567 char *p = fgets(buffer, sizeof(buffer), stdin);
bd321bcc
JS
568 if (!p)
569 break;
e8b55fab 570 name_rev_line(p, &data);
bd321bcc
JS
571 }
572 } else if (all) {
fc046a75 573 int i, max;
bd321bcc 574
fc046a75 575 max = get_max_object_index();
a83123de
BS
576 for (i = 0; i < max; i++) {
577 struct object *obj = get_indexed_object(i);
e8b14d7e 578 if (!obj || obj->type != OBJ_COMMIT)
a83123de
BS
579 continue;
580 show_name(obj, NULL,
da2478db 581 always, allow_undefined, data.name_only);
a83123de 582 }
1f1e895f
LT
583 } else {
584 int i;
da2478db
JH
585 for (i = 0; i < revs.nr; i++)
586 show_name(revs.objects[i].item, revs.objects[i].name,
587 always, allow_undefined, data.name_only);
1f1e895f 588 }
bd321bcc 589
886e1084 590 UNLEAK(revs);
bd321bcc
JS
591 return 0;
592}