Merge branch 'jx/pkt-line-doc-count-fix'
[git.git] / builtin / name-rev.c
bloba9dcd25e46477ef5294dd501f1048816b27b3cdf
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 "prio-queue.h"
10 #include "sha1-lookup.h"
11 #include "commit-slab.h"
14 * One day. See the 'name a rev shortly after epoch' test in t6120 when
15 * changing this value
17 #define CUTOFF_DATE_SLOP 86400
19 struct rev_name {
20 char *tip_name;
21 timestamp_t taggerdate;
22 int generation;
23 int distance;
24 int from_tag;
27 define_commit_slab(commit_rev_name, struct rev_name);
29 static timestamp_t cutoff = TIME_MAX;
30 static struct commit_rev_name rev_names;
32 /* How many generations are maximally preferred over _one_ merge traversal? */
33 #define MERGE_TRAVERSAL_WEIGHT 65535
35 static int is_valid_rev_name(const struct rev_name *name)
37 return name && (name->generation || name->tip_name);
40 static struct rev_name *get_commit_rev_name(const struct commit *commit)
42 struct rev_name *name = commit_rev_name_peek(&rev_names, commit);
44 return is_valid_rev_name(name) ? name : NULL;
47 static int is_better_name(struct rev_name *name,
48 timestamp_t taggerdate,
49 int distance,
50 int from_tag)
53 * When comparing names based on tags, prefer names
54 * based on the older tag, even if it is farther away.
56 if (from_tag && name->from_tag)
57 return (name->taggerdate > taggerdate ||
58 (name->taggerdate == taggerdate &&
59 name->distance > distance));
62 * We know that at least one of them is a non-tag at this point.
63 * favor a tag over a non-tag.
65 if (name->from_tag != from_tag)
66 return from_tag;
69 * We are now looking at two non-tags. Tiebreak to favor
70 * shorter hops.
72 if (name->distance != distance)
73 return name->distance > distance;
75 /* ... or tiebreak to favor older date */
76 if (name->taggerdate != taggerdate)
77 return name->taggerdate > taggerdate;
79 /* keep the current one if we cannot decide */
80 return 0;
83 static struct rev_name *create_or_update_name(struct commit *commit,
84 timestamp_t taggerdate,
85 int generation, int distance,
86 int from_tag)
88 struct rev_name *name = commit_rev_name_at(&rev_names, commit);
90 if (is_valid_rev_name(name)) {
91 if (!is_better_name(name, taggerdate, distance, from_tag))
92 return NULL;
95 * This string might still be shared with ancestors
96 * (generation > 0). We can release it here regardless,
97 * because the new name that has just won will be better
98 * for them as well, so name_rev() will replace these
99 * stale pointers when it processes the parents.
101 if (!name->generation)
102 free(name->tip_name);
105 name->taggerdate = taggerdate;
106 name->generation = generation;
107 name->distance = distance;
108 name->from_tag = from_tag;
110 return name;
113 static char *get_parent_name(const struct rev_name *name, int parent_number)
115 struct strbuf sb = STRBUF_INIT;
116 size_t len;
118 strip_suffix(name->tip_name, "^0", &len);
119 if (name->generation > 0) {
120 strbuf_grow(&sb, len +
121 1 + decimal_width(name->generation) +
122 1 + decimal_width(parent_number));
123 strbuf_addf(&sb, "%.*s~%d^%d", (int)len, name->tip_name,
124 name->generation, parent_number);
125 } else {
126 strbuf_grow(&sb, len +
127 1 + decimal_width(parent_number));
128 strbuf_addf(&sb, "%.*s^%d", (int)len, name->tip_name,
129 parent_number);
131 return strbuf_detach(&sb, NULL);
134 static void name_rev(struct commit *start_commit,
135 const char *tip_name, timestamp_t taggerdate,
136 int from_tag, int deref)
138 struct prio_queue queue;
139 struct commit *commit;
140 struct commit **parents_to_queue = NULL;
141 size_t parents_to_queue_nr, parents_to_queue_alloc = 0;
142 struct rev_name *start_name;
144 parse_commit(start_commit);
145 if (start_commit->date < cutoff)
146 return;
148 start_name = create_or_update_name(start_commit, taggerdate, 0, 0,
149 from_tag);
150 if (!start_name)
151 return;
152 if (deref)
153 start_name->tip_name = xstrfmt("%s^0", tip_name);
154 else
155 start_name->tip_name = xstrdup(tip_name);
157 memset(&queue, 0, sizeof(queue)); /* Use the prio_queue as LIFO */
158 prio_queue_put(&queue, start_commit);
160 while ((commit = prio_queue_get(&queue))) {
161 struct rev_name *name = get_commit_rev_name(commit);
162 struct commit_list *parents;
163 int parent_number = 1;
165 parents_to_queue_nr = 0;
167 for (parents = commit->parents;
168 parents;
169 parents = parents->next, parent_number++) {
170 struct commit *parent = parents->item;
171 struct rev_name *parent_name;
172 int generation, distance;
174 parse_commit(parent);
175 if (parent->date < cutoff)
176 continue;
178 if (parent_number > 1) {
179 generation = 0;
180 distance = name->distance + MERGE_TRAVERSAL_WEIGHT;
181 } else {
182 generation = name->generation + 1;
183 distance = name->distance + 1;
186 parent_name = create_or_update_name(parent, taggerdate,
187 generation,
188 distance, from_tag);
189 if (parent_name) {
190 if (parent_number > 1)
191 parent_name->tip_name =
192 get_parent_name(name,
193 parent_number);
194 else
195 parent_name->tip_name = name->tip_name;
196 ALLOC_GROW(parents_to_queue,
197 parents_to_queue_nr + 1,
198 parents_to_queue_alloc);
199 parents_to_queue[parents_to_queue_nr] = parent;
200 parents_to_queue_nr++;
204 /* The first parent must come out first from the prio_queue */
205 while (parents_to_queue_nr)
206 prio_queue_put(&queue,
207 parents_to_queue[--parents_to_queue_nr]);
210 clear_prio_queue(&queue);
211 free(parents_to_queue);
214 static int subpath_matches(const char *path, const char *filter)
216 const char *subpath = path;
218 while (subpath) {
219 if (!wildmatch(filter, subpath, 0))
220 return subpath - path;
221 subpath = strchr(subpath, '/');
222 if (subpath)
223 subpath++;
225 return -1;
228 static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
230 if (shorten_unambiguous)
231 refname = shorten_unambiguous_ref(refname, 0);
232 else if (skip_prefix(refname, "refs/heads/", &refname))
233 ; /* refname already advanced */
234 else
235 skip_prefix(refname, "refs/", &refname);
236 return refname;
239 struct name_ref_data {
240 int tags_only;
241 int name_only;
242 struct string_list ref_filters;
243 struct string_list exclude_filters;
246 static struct tip_table {
247 struct tip_table_entry {
248 struct object_id oid;
249 const char *refname;
250 struct commit *commit;
251 timestamp_t taggerdate;
252 unsigned int from_tag:1;
253 unsigned int deref:1;
254 } *table;
255 int nr;
256 int alloc;
257 int sorted;
258 } tip_table;
260 static void add_to_tip_table(const struct object_id *oid, const char *refname,
261 int shorten_unambiguous, struct commit *commit,
262 timestamp_t taggerdate, int from_tag, int deref)
264 refname = name_ref_abbrev(refname, shorten_unambiguous);
266 ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
267 oidcpy(&tip_table.table[tip_table.nr].oid, oid);
268 tip_table.table[tip_table.nr].refname = xstrdup(refname);
269 tip_table.table[tip_table.nr].commit = commit;
270 tip_table.table[tip_table.nr].taggerdate = taggerdate;
271 tip_table.table[tip_table.nr].from_tag = from_tag;
272 tip_table.table[tip_table.nr].deref = deref;
273 tip_table.nr++;
274 tip_table.sorted = 0;
277 static int tipcmp(const void *a_, const void *b_)
279 const struct tip_table_entry *a = a_, *b = b_;
280 return oidcmp(&a->oid, &b->oid);
283 static int cmp_by_tag_and_age(const void *a_, const void *b_)
285 const struct tip_table_entry *a = a_, *b = b_;
286 int cmp;
288 /* Prefer tags. */
289 cmp = b->from_tag - a->from_tag;
290 if (cmp)
291 return cmp;
293 /* Older is better. */
294 if (a->taggerdate < b->taggerdate)
295 return -1;
296 return a->taggerdate != b->taggerdate;
299 static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
301 struct object *o = parse_object(the_repository, oid);
302 struct name_ref_data *data = cb_data;
303 int can_abbreviate_output = data->tags_only && data->name_only;
304 int deref = 0;
305 int from_tag = 0;
306 struct commit *commit = NULL;
307 timestamp_t taggerdate = TIME_MAX;
309 if (data->tags_only && !starts_with(path, "refs/tags/"))
310 return 0;
312 if (data->exclude_filters.nr) {
313 struct string_list_item *item;
315 for_each_string_list_item(item, &data->exclude_filters) {
316 if (subpath_matches(path, item->string) >= 0)
317 return 0;
321 if (data->ref_filters.nr) {
322 struct string_list_item *item;
323 int matched = 0;
325 /* See if any of the patterns match. */
326 for_each_string_list_item(item, &data->ref_filters) {
328 * Check all patterns even after finding a match, so
329 * that we can see if a match with a subpath exists.
330 * When a user asked for 'refs/tags/v*' and 'v1.*',
331 * both of which match, the user is showing her
332 * willingness to accept a shortened output by having
333 * the 'v1.*' in the acceptable refnames, so we
334 * shouldn't stop when seeing 'refs/tags/v1.4' matches
335 * 'refs/tags/v*'. We should show it as 'v1.4'.
337 switch (subpath_matches(path, item->string)) {
338 case -1: /* did not match */
339 break;
340 case 0: /* matched fully */
341 matched = 1;
342 break;
343 default: /* matched subpath */
344 matched = 1;
345 can_abbreviate_output = 1;
346 break;
350 /* If none of the patterns matched, stop now */
351 if (!matched)
352 return 0;
355 while (o && o->type == OBJ_TAG) {
356 struct tag *t = (struct tag *) o;
357 if (!t->tagged)
358 break; /* broken repository */
359 o = parse_object(the_repository, &t->tagged->oid);
360 deref = 1;
361 taggerdate = t->date;
363 if (o && o->type == OBJ_COMMIT) {
364 commit = (struct commit *)o;
365 from_tag = starts_with(path, "refs/tags/");
366 if (taggerdate == TIME_MAX)
367 taggerdate = commit->date;
370 add_to_tip_table(oid, path, can_abbreviate_output, commit, taggerdate,
371 from_tag, deref);
372 return 0;
375 static void name_tips(void)
377 int i;
380 * Try to set better names first, so that worse ones spread
381 * less.
383 QSORT(tip_table.table, tip_table.nr, cmp_by_tag_and_age);
384 for (i = 0; i < tip_table.nr; i++) {
385 struct tip_table_entry *e = &tip_table.table[i];
386 if (e->commit) {
387 name_rev(e->commit, e->refname, e->taggerdate,
388 e->from_tag, e->deref);
393 static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
395 struct tip_table_entry *table = table_;
396 return table[ix].oid.hash;
399 static const char *get_exact_ref_match(const struct object *o)
401 int found;
403 if (!tip_table.table || !tip_table.nr)
404 return NULL;
406 if (!tip_table.sorted) {
407 QSORT(tip_table.table, tip_table.nr, tipcmp);
408 tip_table.sorted = 1;
411 found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr,
412 nth_tip_table_ent);
413 if (0 <= found)
414 return tip_table.table[found].refname;
415 return NULL;
418 /* may return a constant string or use "buf" as scratch space */
419 static const char *get_rev_name(const struct object *o, struct strbuf *buf)
421 struct rev_name *n;
422 const struct commit *c;
424 if (o->type != OBJ_COMMIT)
425 return get_exact_ref_match(o);
426 c = (const struct commit *) o;
427 n = get_commit_rev_name(c);
428 if (!n)
429 return NULL;
431 if (!n->generation)
432 return n->tip_name;
433 else {
434 strbuf_reset(buf);
435 strbuf_addstr(buf, n->tip_name);
436 strbuf_strip_suffix(buf, "^0");
437 strbuf_addf(buf, "~%d", n->generation);
438 return buf->buf;
442 static void show_name(const struct object *obj,
443 const char *caller_name,
444 int always, int allow_undefined, int name_only)
446 const char *name;
447 const struct object_id *oid = &obj->oid;
448 struct strbuf buf = STRBUF_INIT;
450 if (!name_only)
451 printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
452 name = get_rev_name(obj, &buf);
453 if (name)
454 printf("%s\n", name);
455 else if (allow_undefined)
456 printf("undefined\n");
457 else if (always)
458 printf("%s\n", find_unique_abbrev(oid, DEFAULT_ABBREV));
459 else
460 die("cannot describe '%s'", oid_to_hex(oid));
461 strbuf_release(&buf);
464 static char const * const name_rev_usage[] = {
465 N_("git name-rev [<options>] <commit>..."),
466 N_("git name-rev [<options>] --all"),
467 N_("git name-rev [<options>] --stdin"),
468 NULL
471 static void name_rev_line(char *p, struct name_ref_data *data)
473 struct strbuf buf = STRBUF_INIT;
474 int counter = 0;
475 char *p_start;
476 const unsigned hexsz = the_hash_algo->hexsz;
478 for (p_start = p; *p; p++) {
479 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
480 if (!ishex(*p))
481 counter = 0;
482 else if (++counter == hexsz &&
483 !ishex(*(p+1))) {
484 struct object_id oid;
485 const char *name = NULL;
486 char c = *(p+1);
487 int p_len = p - p_start + 1;
489 counter = 0;
491 *(p+1) = 0;
492 if (!get_oid(p - (hexsz - 1), &oid)) {
493 struct object *o =
494 lookup_object(the_repository, &oid);
495 if (o)
496 name = get_rev_name(o, &buf);
498 *(p+1) = c;
500 if (!name)
501 continue;
503 if (data->name_only)
504 printf("%.*s%s", p_len - hexsz, p_start, name);
505 else
506 printf("%.*s (%s)", p_len, p_start, name);
507 p_start = p + 1;
511 /* flush */
512 if (p_start != p)
513 fwrite(p_start, p - p_start, 1, stdout);
515 strbuf_release(&buf);
518 int cmd_name_rev(int argc, const char **argv, const char *prefix)
520 struct object_array revs = OBJECT_ARRAY_INIT;
521 int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
522 struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
523 struct option opts[] = {
524 OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
525 OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
526 OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
527 N_("only use refs matching <pattern>")),
528 OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
529 N_("ignore refs matching <pattern>")),
530 OPT_GROUP(""),
531 OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
532 OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
533 OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
534 OPT_BOOL(0, "always", &always,
535 N_("show abbreviated commit object as fallback")),
537 /* A Hidden OPT_BOOL */
538 OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
539 N_("dereference tags in the input (internal use)"),
540 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
542 OPT_END(),
545 init_commit_rev_name(&rev_names);
546 git_config(git_default_config, NULL);
547 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
548 if (all + transform_stdin + !!argc > 1) {
549 error("Specify either a list, or --all, not both!");
550 usage_with_options(name_rev_usage, opts);
552 if (all || transform_stdin)
553 cutoff = 0;
555 for (; argc; argc--, argv++) {
556 struct object_id oid;
557 struct object *object;
558 struct commit *commit;
560 if (get_oid(*argv, &oid)) {
561 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
562 *argv);
563 continue;
566 commit = NULL;
567 object = parse_object(the_repository, &oid);
568 if (object) {
569 struct object *peeled = deref_tag(the_repository,
570 object, *argv, 0);
571 if (peeled && peeled->type == OBJ_COMMIT)
572 commit = (struct commit *)peeled;
575 if (!object) {
576 fprintf(stderr, "Could not get object for %s. Skipping.\n",
577 *argv);
578 continue;
581 if (commit) {
582 if (cutoff > commit->date)
583 cutoff = commit->date;
586 if (peel_tag) {
587 if (!commit) {
588 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
589 *argv);
590 continue;
592 object = (struct object *)commit;
594 add_object_array(object, *argv, &revs);
597 if (cutoff) {
598 /* check for undeflow */
599 if (cutoff > TIME_MIN + CUTOFF_DATE_SLOP)
600 cutoff = cutoff - CUTOFF_DATE_SLOP;
601 else
602 cutoff = TIME_MIN;
604 for_each_ref(name_ref, &data);
605 name_tips();
607 if (transform_stdin) {
608 char buffer[2048];
610 while (!feof(stdin)) {
611 char *p = fgets(buffer, sizeof(buffer), stdin);
612 if (!p)
613 break;
614 name_rev_line(p, &data);
616 } else if (all) {
617 int i, max;
619 max = get_max_object_index();
620 for (i = 0; i < max; i++) {
621 struct object *obj = get_indexed_object(i);
622 if (!obj || obj->type != OBJ_COMMIT)
623 continue;
624 show_name(obj, NULL,
625 always, allow_undefined, data.name_only);
627 } else {
628 int i;
629 for (i = 0; i < revs.nr; i++)
630 show_name(revs.objects[i].item, revs.objects[i].name,
631 always, allow_undefined, data.name_only);
634 UNLEAK(revs);
635 return 0;