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