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