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