documentation: fix apostrophe usage
[git.git] / builtin / name-rev.c
blob2dd1807c4e09f8dc7ff557c628fdd850dc653361
1 #include "builtin.h"
2 #include "environment.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "repository.h"
6 #include "config.h"
7 #include "commit.h"
8 #include "tag.h"
9 #include "refs.h"
10 #include "object-name.h"
11 #include "pager.h"
12 #include "parse-options.h"
13 #include "prio-queue.h"
14 #include "hash-lookup.h"
15 #include "commit-slab.h"
16 #include "commit-graph.h"
17 #include "wildmatch.h"
20 * One day. See the 'name a rev shortly after epoch' test in t6120 when
21 * changing this value
23 #define CUTOFF_DATE_SLOP 86400
25 struct rev_name {
26 const char *tip_name;
27 timestamp_t taggerdate;
28 int generation;
29 int distance;
30 int from_tag;
33 define_commit_slab(commit_rev_name, struct rev_name);
35 static timestamp_t generation_cutoff = GENERATION_NUMBER_INFINITY;
36 static timestamp_t cutoff = TIME_MAX;
37 static struct commit_rev_name rev_names;
39 /* Disable the cutoff checks entirely */
40 static void disable_cutoff(void)
42 generation_cutoff = 0;
43 cutoff = 0;
46 /* Cutoff searching any commits older than this one */
47 static void set_commit_cutoff(struct commit *commit)
50 if (cutoff > commit->date)
51 cutoff = commit->date;
53 if (generation_cutoff) {
54 timestamp_t generation = commit_graph_generation(commit);
56 if (generation_cutoff > generation)
57 generation_cutoff = generation;
61 /* adjust the commit date cutoff with a slop to allow for slightly incorrect
62 * commit timestamps in case of clock skew.
64 static void adjust_cutoff_timestamp_for_slop(void)
66 if (cutoff) {
67 /* check for undeflow */
68 if (cutoff > TIME_MIN + CUTOFF_DATE_SLOP)
69 cutoff = cutoff - CUTOFF_DATE_SLOP;
70 else
71 cutoff = TIME_MIN;
75 /* Check if a commit is before the cutoff. Prioritize generation numbers
76 * first, but use the commit timestamp if we lack generation data.
78 static int commit_is_before_cutoff(struct commit *commit)
80 if (generation_cutoff < GENERATION_NUMBER_INFINITY)
81 return generation_cutoff &&
82 commit_graph_generation(commit) < generation_cutoff;
84 return commit->date < cutoff;
87 /* How many generations are maximally preferred over _one_ merge traversal? */
88 #define MERGE_TRAVERSAL_WEIGHT 65535
90 static int is_valid_rev_name(const struct rev_name *name)
92 return name && name->tip_name;
95 static struct rev_name *get_commit_rev_name(const struct commit *commit)
97 struct rev_name *name = commit_rev_name_peek(&rev_names, commit);
99 return is_valid_rev_name(name) ? name : NULL;
102 static int effective_distance(int distance, int generation)
104 return distance + (generation > 0 ? MERGE_TRAVERSAL_WEIGHT : 0);
107 static int is_better_name(struct rev_name *name,
108 timestamp_t taggerdate,
109 int generation,
110 int distance,
111 int from_tag)
113 int name_distance = effective_distance(name->distance, name->generation);
114 int new_distance = effective_distance(distance, generation);
116 /* If both are tags, we prefer the nearer one. */
117 if (from_tag && name->from_tag)
118 return name_distance > new_distance;
120 /* Favor a tag over a non-tag. */
121 if (name->from_tag != from_tag)
122 return from_tag;
125 * We are now looking at two non-tags. Tiebreak to favor
126 * shorter hops.
128 if (name_distance != new_distance)
129 return name_distance > new_distance;
131 /* ... or tiebreak to favor older date */
132 if (name->taggerdate != taggerdate)
133 return name->taggerdate > taggerdate;
135 /* keep the current one if we cannot decide */
136 return 0;
139 static struct rev_name *create_or_update_name(struct commit *commit,
140 timestamp_t taggerdate,
141 int generation, int distance,
142 int from_tag)
144 struct rev_name *name = commit_rev_name_at(&rev_names, commit);
146 if (is_valid_rev_name(name) &&
147 !is_better_name(name, taggerdate, generation, distance, from_tag))
148 return NULL;
150 name->taggerdate = taggerdate;
151 name->generation = generation;
152 name->distance = distance;
153 name->from_tag = from_tag;
155 return name;
158 static char *get_parent_name(const struct rev_name *name, int parent_number)
160 struct strbuf sb = STRBUF_INIT;
161 size_t len;
163 strip_suffix(name->tip_name, "^0", &len);
164 if (name->generation > 0) {
165 strbuf_grow(&sb, len +
166 1 + decimal_width(name->generation) +
167 1 + decimal_width(parent_number));
168 strbuf_addf(&sb, "%.*s~%d^%d", (int)len, name->tip_name,
169 name->generation, parent_number);
170 } else {
171 strbuf_grow(&sb, len +
172 1 + decimal_width(parent_number));
173 strbuf_addf(&sb, "%.*s^%d", (int)len, name->tip_name,
174 parent_number);
176 return strbuf_detach(&sb, NULL);
179 static void name_rev(struct commit *start_commit,
180 const char *tip_name, timestamp_t taggerdate,
181 int from_tag, int deref)
183 struct prio_queue queue;
184 struct commit *commit;
185 struct commit **parents_to_queue = NULL;
186 size_t parents_to_queue_nr, parents_to_queue_alloc = 0;
187 struct rev_name *start_name;
189 repo_parse_commit(the_repository, start_commit);
190 if (commit_is_before_cutoff(start_commit))
191 return;
193 start_name = create_or_update_name(start_commit, taggerdate, 0, 0,
194 from_tag);
195 if (!start_name)
196 return;
197 if (deref)
198 start_name->tip_name = xstrfmt("%s^0", tip_name);
199 else
200 start_name->tip_name = xstrdup(tip_name);
202 memset(&queue, 0, sizeof(queue)); /* Use the prio_queue as LIFO */
203 prio_queue_put(&queue, start_commit);
205 while ((commit = prio_queue_get(&queue))) {
206 struct rev_name *name = get_commit_rev_name(commit);
207 struct commit_list *parents;
208 int parent_number = 1;
210 parents_to_queue_nr = 0;
212 for (parents = commit->parents;
213 parents;
214 parents = parents->next, parent_number++) {
215 struct commit *parent = parents->item;
216 struct rev_name *parent_name;
217 int generation, distance;
219 repo_parse_commit(the_repository, parent);
220 if (commit_is_before_cutoff(parent))
221 continue;
223 if (parent_number > 1) {
224 generation = 0;
225 distance = name->distance + MERGE_TRAVERSAL_WEIGHT;
226 } else {
227 generation = name->generation + 1;
228 distance = name->distance + 1;
231 parent_name = create_or_update_name(parent, taggerdate,
232 generation,
233 distance, from_tag);
234 if (parent_name) {
235 if (parent_number > 1)
236 parent_name->tip_name =
237 get_parent_name(name,
238 parent_number);
239 else
240 parent_name->tip_name = name->tip_name;
241 ALLOC_GROW(parents_to_queue,
242 parents_to_queue_nr + 1,
243 parents_to_queue_alloc);
244 parents_to_queue[parents_to_queue_nr] = parent;
245 parents_to_queue_nr++;
249 /* The first parent must come out first from the prio_queue */
250 while (parents_to_queue_nr)
251 prio_queue_put(&queue,
252 parents_to_queue[--parents_to_queue_nr]);
255 clear_prio_queue(&queue);
256 free(parents_to_queue);
259 static int subpath_matches(const char *path, const char *filter)
261 const char *subpath = path;
263 while (subpath) {
264 if (!wildmatch(filter, subpath, 0))
265 return subpath - path;
266 subpath = strchr(subpath, '/');
267 if (subpath)
268 subpath++;
270 return -1;
273 struct name_ref_data {
274 int tags_only;
275 int name_only;
276 struct string_list ref_filters;
277 struct string_list exclude_filters;
280 static struct tip_table {
281 struct tip_table_entry {
282 struct object_id oid;
283 const char *refname;
284 struct commit *commit;
285 timestamp_t taggerdate;
286 unsigned int from_tag:1;
287 unsigned int deref:1;
288 } *table;
289 int nr;
290 int alloc;
291 int sorted;
292 } tip_table;
294 static void add_to_tip_table(const struct object_id *oid, const char *refname,
295 int shorten_unambiguous, struct commit *commit,
296 timestamp_t taggerdate, int from_tag, int deref)
298 char *short_refname = NULL;
300 if (shorten_unambiguous)
301 short_refname = shorten_unambiguous_ref(refname, 0);
302 else if (skip_prefix(refname, "refs/heads/", &refname))
303 ; /* refname already advanced */
304 else
305 skip_prefix(refname, "refs/", &refname);
307 ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
308 oidcpy(&tip_table.table[tip_table.nr].oid, oid);
309 tip_table.table[tip_table.nr].refname = short_refname ?
310 short_refname : xstrdup(refname);
311 tip_table.table[tip_table.nr].commit = commit;
312 tip_table.table[tip_table.nr].taggerdate = taggerdate;
313 tip_table.table[tip_table.nr].from_tag = from_tag;
314 tip_table.table[tip_table.nr].deref = deref;
315 tip_table.nr++;
316 tip_table.sorted = 0;
319 static int tipcmp(const void *a_, const void *b_)
321 const struct tip_table_entry *a = a_, *b = b_;
322 return oidcmp(&a->oid, &b->oid);
325 static int cmp_by_tag_and_age(const void *a_, const void *b_)
327 const struct tip_table_entry *a = a_, *b = b_;
328 int cmp;
330 /* Prefer tags. */
331 cmp = b->from_tag - a->from_tag;
332 if (cmp)
333 return cmp;
335 /* Older is better. */
336 if (a->taggerdate < b->taggerdate)
337 return -1;
338 return a->taggerdate != b->taggerdate;
341 static int name_ref(const char *path, const struct object_id *oid,
342 int flags UNUSED, void *cb_data)
344 struct object *o = parse_object(the_repository, oid);
345 struct name_ref_data *data = cb_data;
346 int can_abbreviate_output = data->tags_only && data->name_only;
347 int deref = 0;
348 int from_tag = 0;
349 struct commit *commit = NULL;
350 timestamp_t taggerdate = TIME_MAX;
352 if (data->tags_only && !starts_with(path, "refs/tags/"))
353 return 0;
355 if (data->exclude_filters.nr) {
356 struct string_list_item *item;
358 for_each_string_list_item(item, &data->exclude_filters) {
359 if (subpath_matches(path, item->string) >= 0)
360 return 0;
364 if (data->ref_filters.nr) {
365 struct string_list_item *item;
366 int matched = 0;
368 /* See if any of the patterns match. */
369 for_each_string_list_item(item, &data->ref_filters) {
371 * Check all patterns even after finding a match, so
372 * that we can see if a match with a subpath exists.
373 * When a user asked for 'refs/tags/v*' and 'v1.*',
374 * both of which match, the user is showing her
375 * willingness to accept a shortened output by having
376 * the 'v1.*' in the acceptable refnames, so we
377 * shouldn't stop when seeing 'refs/tags/v1.4' matches
378 * 'refs/tags/v*'. We should show it as 'v1.4'.
380 switch (subpath_matches(path, item->string)) {
381 case -1: /* did not match */
382 break;
383 case 0: /* matched fully */
384 matched = 1;
385 break;
386 default: /* matched subpath */
387 matched = 1;
388 can_abbreviate_output = 1;
389 break;
393 /* If none of the patterns matched, stop now */
394 if (!matched)
395 return 0;
398 while (o && o->type == OBJ_TAG) {
399 struct tag *t = (struct tag *) o;
400 if (!t->tagged)
401 break; /* broken repository */
402 o = parse_object(the_repository, &t->tagged->oid);
403 deref = 1;
404 taggerdate = t->date;
406 if (o && o->type == OBJ_COMMIT) {
407 commit = (struct commit *)o;
408 from_tag = starts_with(path, "refs/tags/");
409 if (taggerdate == TIME_MAX)
410 taggerdate = commit->date;
413 add_to_tip_table(oid, path, can_abbreviate_output, commit, taggerdate,
414 from_tag, deref);
415 return 0;
418 static void name_tips(void)
420 int i;
423 * Try to set better names first, so that worse ones spread
424 * less.
426 QSORT(tip_table.table, tip_table.nr, cmp_by_tag_and_age);
427 for (i = 0; i < tip_table.nr; i++) {
428 struct tip_table_entry *e = &tip_table.table[i];
429 if (e->commit) {
430 name_rev(e->commit, e->refname, e->taggerdate,
431 e->from_tag, e->deref);
436 static const struct object_id *nth_tip_table_ent(size_t ix, const void *table_)
438 const struct tip_table_entry *table = table_;
439 return &table[ix].oid;
442 static const char *get_exact_ref_match(const struct object *o)
444 int found;
446 if (!tip_table.table || !tip_table.nr)
447 return NULL;
449 if (!tip_table.sorted) {
450 QSORT(tip_table.table, tip_table.nr, tipcmp);
451 tip_table.sorted = 1;
454 found = oid_pos(&o->oid, tip_table.table, tip_table.nr,
455 nth_tip_table_ent);
456 if (0 <= found)
457 return tip_table.table[found].refname;
458 return NULL;
461 /* may return a constant string or use "buf" as scratch space */
462 static const char *get_rev_name(const struct object *o, struct strbuf *buf)
464 struct rev_name *n;
465 const struct commit *c;
467 if (o->type != OBJ_COMMIT)
468 return get_exact_ref_match(o);
469 c = (const struct commit *) o;
470 n = get_commit_rev_name(c);
471 if (!n)
472 return NULL;
474 if (!n->generation)
475 return n->tip_name;
476 else {
477 strbuf_reset(buf);
478 strbuf_addstr(buf, n->tip_name);
479 strbuf_strip_suffix(buf, "^0");
480 strbuf_addf(buf, "~%d", n->generation);
481 return buf->buf;
485 static void show_name(const struct object *obj,
486 const char *caller_name,
487 int always, int allow_undefined, int name_only)
489 const char *name;
490 const struct object_id *oid = &obj->oid;
491 struct strbuf buf = STRBUF_INIT;
493 if (!name_only)
494 printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
495 name = get_rev_name(obj, &buf);
496 if (name)
497 printf("%s\n", name);
498 else if (allow_undefined)
499 printf("undefined\n");
500 else if (always)
501 printf("%s\n",
502 repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV));
503 else
504 die("cannot describe '%s'", oid_to_hex(oid));
505 strbuf_release(&buf);
508 static char const * const name_rev_usage[] = {
509 N_("git name-rev [<options>] <commit>..."),
510 N_("git name-rev [<options>] --all"),
511 N_("git name-rev [<options>] --annotate-stdin"),
512 NULL
515 static void name_rev_line(char *p, struct name_ref_data *data)
517 struct strbuf buf = STRBUF_INIT;
518 int counter = 0;
519 char *p_start;
520 const unsigned hexsz = the_hash_algo->hexsz;
522 for (p_start = p; *p; p++) {
523 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
524 if (!ishex(*p))
525 counter = 0;
526 else if (++counter == hexsz &&
527 !ishex(*(p+1))) {
528 struct object_id oid;
529 const char *name = NULL;
530 char c = *(p+1);
531 int p_len = p - p_start + 1;
533 counter = 0;
535 *(p+1) = 0;
536 if (!repo_get_oid(the_repository, p - (hexsz - 1), &oid)) {
537 struct object *o =
538 lookup_object(the_repository, &oid);
539 if (o)
540 name = get_rev_name(o, &buf);
542 *(p+1) = c;
544 if (!name)
545 continue;
547 if (data->name_only)
548 printf("%.*s%s", p_len - hexsz, p_start, name);
549 else
550 printf("%.*s (%s)", p_len, p_start, name);
551 p_start = p + 1;
555 /* flush */
556 if (p_start != p)
557 fwrite(p_start, p - p_start, 1, stdout);
559 strbuf_release(&buf);
562 int cmd_name_rev(int argc, const char **argv, const char *prefix)
564 struct object_array revs = OBJECT_ARRAY_INIT;
565 int all = 0, annotate_stdin = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
566 struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
567 struct option opts[] = {
568 OPT_BOOL(0, "name-only", &data.name_only, N_("print only ref-based names (no object names)")),
569 OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
570 OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
571 N_("only use refs matching <pattern>")),
572 OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
573 N_("ignore refs matching <pattern>")),
574 OPT_GROUP(""),
575 OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
576 OPT_BOOL_F(0,
577 "stdin",
578 &transform_stdin,
579 N_("deprecated: use --annotate-stdin instead"),
580 PARSE_OPT_HIDDEN),
581 OPT_BOOL(0, "annotate-stdin", &annotate_stdin, N_("annotate text from stdin")),
582 OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
583 OPT_BOOL(0, "always", &always,
584 N_("show abbreviated commit object as fallback")),
585 OPT_HIDDEN_BOOL(0, "peel-tag", &peel_tag,
586 N_("dereference tags in the input (internal use)")),
587 OPT_END(),
590 init_commit_rev_name(&rev_names);
591 git_config(git_default_config, NULL);
592 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
594 if (transform_stdin) {
595 warning("--stdin is deprecated. Please use --annotate-stdin instead, "
596 "which is functionally equivalent.\n"
597 "This option will be removed in a future release.");
598 annotate_stdin = 1;
601 if (all + annotate_stdin + !!argc > 1) {
602 error("Specify either a list, or --all, not both!");
603 usage_with_options(name_rev_usage, opts);
605 if (all || annotate_stdin)
606 disable_cutoff();
608 for (; argc; argc--, argv++) {
609 struct object_id oid;
610 struct object *object;
611 struct commit *commit;
613 if (repo_get_oid(the_repository, *argv, &oid)) {
614 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
615 *argv);
616 continue;
619 commit = NULL;
620 object = parse_object(the_repository, &oid);
621 if (object) {
622 struct object *peeled = deref_tag(the_repository,
623 object, *argv, 0);
624 if (peeled && peeled->type == OBJ_COMMIT)
625 commit = (struct commit *)peeled;
628 if (!object) {
629 fprintf(stderr, "Could not get object for %s. Skipping.\n",
630 *argv);
631 continue;
634 if (commit)
635 set_commit_cutoff(commit);
637 if (peel_tag) {
638 if (!commit) {
639 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
640 *argv);
641 continue;
643 object = (struct object *)commit;
645 add_object_array(object, *argv, &revs);
648 adjust_cutoff_timestamp_for_slop();
650 for_each_ref(name_ref, &data);
651 name_tips();
653 if (annotate_stdin) {
654 struct strbuf sb = STRBUF_INIT;
656 while (strbuf_getline(&sb, stdin) != EOF) {
657 strbuf_addch(&sb, '\n');
658 name_rev_line(sb.buf, &data);
660 strbuf_release(&sb);
661 } else if (all) {
662 int i, max;
664 max = get_max_object_index();
665 for (i = 0; i < max; i++) {
666 struct object *obj = get_indexed_object(i);
667 if (!obj || obj->type != OBJ_COMMIT)
668 continue;
669 show_name(obj, NULL,
670 always, allow_undefined, data.name_only);
672 } else {
673 int i;
674 for (i = 0; i < revs.nr; i++)
675 show_name(revs.objects[i].item, revs.objects[i].name,
676 always, allow_undefined, data.name_only);
679 UNLEAK(revs);
680 return 0;