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