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