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