Merge branch 'ab/t7610-timeout'
[alt-git.git] / builtin / rev-list.c
blob3acd93f71e1cf71edb4e01d48708adf2554986ad
1 #include "cache.h"
2 #include "config.h"
3 #include "commit.h"
4 #include "diff.h"
5 #include "revision.h"
6 #include "list-objects.h"
7 #include "list-objects-filter.h"
8 #include "list-objects-filter-options.h"
9 #include "object.h"
10 #include "object-store.h"
11 #include "pack.h"
12 #include "pack-bitmap.h"
13 #include "builtin.h"
14 #include "log-tree.h"
15 #include "graph.h"
16 #include "bisect.h"
17 #include "progress.h"
18 #include "reflog-walk.h"
19 #include "oidset.h"
20 #include "packfile.h"
22 static const char rev_list_usage[] =
23 "git rev-list [<options>] <commit>... [--] [<path>...]\n"
24 "\n"
25 " limiting output:\n"
26 " --max-count=<n>\n"
27 " --max-age=<epoch>\n"
28 " --min-age=<epoch>\n"
29 " --sparse\n"
30 " --no-merges\n"
31 " --min-parents=<n>\n"
32 " --no-min-parents\n"
33 " --max-parents=<n>\n"
34 " --no-max-parents\n"
35 " --remove-empty\n"
36 " --all\n"
37 " --branches\n"
38 " --tags\n"
39 " --remotes\n"
40 " --stdin\n"
41 " --quiet\n"
42 " ordering output:\n"
43 " --topo-order\n"
44 " --date-order\n"
45 " --reverse\n"
46 " formatting output:\n"
47 " --parents\n"
48 " --children\n"
49 " --objects | --objects-edge\n"
50 " --disk-usage[=human]\n"
51 " --unpacked\n"
52 " --header | --pretty\n"
53 " --[no-]object-names\n"
54 " --abbrev=<n> | --no-abbrev\n"
55 " --abbrev-commit\n"
56 " --left-right\n"
57 " --count\n"
58 " special purpose:\n"
59 " --bisect\n"
60 " --bisect-vars\n"
61 " --bisect-all"
64 static struct progress *progress;
65 static unsigned progress_counter;
67 static struct oidset omitted_objects;
68 static int arg_print_omitted; /* print objects omitted by filter */
70 static struct oidset missing_objects;
71 enum missing_action {
72 MA_ERROR = 0, /* fail if any missing objects are encountered */
73 MA_ALLOW_ANY, /* silently allow ALL missing objects */
74 MA_PRINT, /* print ALL missing objects in special section */
75 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
77 static enum missing_action arg_missing_action;
79 /* display only the oid of each object encountered */
80 static int arg_show_object_names = 1;
82 #define DEFAULT_OIDSET_SIZE (16*1024)
84 static int show_disk_usage;
85 static off_t total_disk_usage;
86 static int human_readable;
88 static off_t get_object_disk_usage(struct object *obj)
90 off_t size;
91 struct object_info oi = OBJECT_INFO_INIT;
92 oi.disk_sizep = &size;
93 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
94 die(_("unable to get disk usage of %s"), oid_to_hex(&obj->oid));
95 return size;
98 static void finish_commit(struct commit *commit);
99 static void show_commit(struct commit *commit, void *data)
101 struct rev_list_info *info = data;
102 struct rev_info *revs = info->revs;
104 display_progress(progress, ++progress_counter);
106 if (show_disk_usage)
107 total_disk_usage += get_object_disk_usage(&commit->object);
109 if (info->flags & REV_LIST_QUIET) {
110 finish_commit(commit);
111 return;
114 graph_show_commit(revs->graph);
116 if (revs->count) {
117 if (commit->object.flags & PATCHSAME)
118 revs->count_same++;
119 else if (commit->object.flags & SYMMETRIC_LEFT)
120 revs->count_left++;
121 else
122 revs->count_right++;
123 finish_commit(commit);
124 return;
127 if (info->show_timestamp)
128 printf("%"PRItime" ", commit->date);
129 if (info->header_prefix)
130 fputs(info->header_prefix, stdout);
132 if (revs->include_header) {
133 if (!revs->graph)
134 fputs(get_revision_mark(revs, commit), stdout);
135 if (revs->abbrev_commit && revs->abbrev)
136 fputs(find_unique_abbrev(&commit->object.oid, revs->abbrev),
137 stdout);
138 else
139 fputs(oid_to_hex(&commit->object.oid), stdout);
141 if (revs->print_parents) {
142 struct commit_list *parents = commit->parents;
143 while (parents) {
144 printf(" %s", oid_to_hex(&parents->item->object.oid));
145 parents = parents->next;
148 if (revs->children.name) {
149 struct commit_list *children;
151 children = lookup_decoration(&revs->children, &commit->object);
152 while (children) {
153 printf(" %s", oid_to_hex(&children->item->object.oid));
154 children = children->next;
157 show_decorations(revs, commit);
158 if (revs->commit_format == CMIT_FMT_ONELINE)
159 putchar(' ');
160 else if (revs->include_header)
161 putchar('\n');
163 if (revs->verbose_header) {
164 struct strbuf buf = STRBUF_INIT;
165 struct pretty_print_context ctx = {0};
166 ctx.abbrev = revs->abbrev;
167 ctx.date_mode = revs->date_mode;
168 ctx.date_mode_explicit = revs->date_mode_explicit;
169 ctx.fmt = revs->commit_format;
170 ctx.output_encoding = get_log_output_encoding();
171 ctx.color = revs->diffopt.use_color;
172 pretty_print_commit(&ctx, commit, &buf);
173 if (buf.len) {
174 if (revs->commit_format != CMIT_FMT_ONELINE)
175 graph_show_oneline(revs->graph);
177 graph_show_commit_msg(revs->graph, stdout, &buf);
180 * Add a newline after the commit message.
182 * Usually, this newline produces a blank
183 * padding line between entries, in which case
184 * we need to add graph padding on this line.
186 * However, the commit message may not end in a
187 * newline. In this case the newline simply
188 * ends the last line of the commit message,
189 * and we don't need any graph output. (This
190 * always happens with CMIT_FMT_ONELINE, and it
191 * happens with CMIT_FMT_USERFORMAT when the
192 * format doesn't explicitly end in a newline.)
194 if (buf.len && buf.buf[buf.len - 1] == '\n')
195 graph_show_padding(revs->graph);
196 putchar(info->hdr_termination);
197 } else {
199 * If the message buffer is empty, just show
200 * the rest of the graph output for this
201 * commit.
203 if (graph_show_remainder(revs->graph))
204 putchar('\n');
205 if (revs->commit_format == CMIT_FMT_ONELINE)
206 putchar('\n');
208 strbuf_release(&buf);
209 } else {
210 if (graph_show_remainder(revs->graph))
211 putchar('\n');
213 maybe_flush_or_die(stdout, "stdout");
214 finish_commit(commit);
217 static void finish_commit(struct commit *commit)
219 free_commit_list(commit->parents);
220 commit->parents = NULL;
221 free_commit_buffer(the_repository->parsed_objects,
222 commit);
225 static inline void finish_object__ma(struct object *obj)
228 * Whether or not we try to dynamically fetch missing objects
229 * from the server, we currently DO NOT have the object. We
230 * can either print, allow (ignore), or conditionally allow
231 * (ignore) them.
233 switch (arg_missing_action) {
234 case MA_ERROR:
235 die("missing %s object '%s'",
236 type_name(obj->type), oid_to_hex(&obj->oid));
237 return;
239 case MA_ALLOW_ANY:
240 return;
242 case MA_PRINT:
243 oidset_insert(&missing_objects, &obj->oid);
244 return;
246 case MA_ALLOW_PROMISOR:
247 if (is_promisor_object(&obj->oid))
248 return;
249 die("unexpected missing %s object '%s'",
250 type_name(obj->type), oid_to_hex(&obj->oid));
251 return;
253 default:
254 BUG("unhandled missing_action");
255 return;
259 static int finish_object(struct object *obj, const char *name, void *cb_data)
261 struct rev_list_info *info = cb_data;
262 if (oid_object_info_extended(the_repository, &obj->oid, NULL, 0) < 0) {
263 finish_object__ma(obj);
264 return 1;
266 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
267 parse_object(the_repository, &obj->oid);
268 return 0;
271 static void show_object(struct object *obj, const char *name, void *cb_data)
273 struct rev_list_info *info = cb_data;
274 struct rev_info *revs = info->revs;
276 if (finish_object(obj, name, cb_data))
277 return;
278 display_progress(progress, ++progress_counter);
279 if (show_disk_usage)
280 total_disk_usage += get_object_disk_usage(obj);
281 if (info->flags & REV_LIST_QUIET)
282 return;
284 if (revs->count) {
286 * The object count is always accumulated in the .count_right
287 * field for traversal that is not a left-right traversal,
288 * and cmd_rev_list() made sure that a .count request that
289 * wants to count non-commit objects, which is handled by
290 * the show_object() callback, does not ask for .left_right.
292 revs->count_right++;
293 return;
296 if (arg_show_object_names)
297 show_object_with_name(stdout, obj, name);
298 else
299 printf("%s\n", oid_to_hex(&obj->oid));
302 static void show_edge(struct commit *commit)
304 printf("-%s\n", oid_to_hex(&commit->object.oid));
307 static void print_var_str(const char *var, const char *val)
309 printf("%s='%s'\n", var, val);
312 static void print_var_int(const char *var, int val)
314 printf("%s=%d\n", var, val);
317 static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
319 int cnt, flags = info->flags;
320 char hex[GIT_MAX_HEXSZ + 1] = "";
321 struct commit_list *tried;
322 struct rev_info *revs = info->revs;
324 if (!revs->commits)
325 return 1;
327 revs->commits = filter_skipped(revs->commits, &tried,
328 flags & BISECT_SHOW_ALL,
329 NULL, NULL);
332 * revs->commits can reach "reaches" commits among
333 * "all" commits. If it is good, then there are
334 * (all-reaches) commits left to be bisected.
335 * On the other hand, if it is bad, then the set
336 * to bisect is "reaches".
337 * A bisect set of size N has (N-1) commits further
338 * to test, as we already know one bad one.
340 cnt = all - reaches;
341 if (cnt < reaches)
342 cnt = reaches;
344 if (revs->commits)
345 oid_to_hex_r(hex, &revs->commits->item->object.oid);
347 if (flags & BISECT_SHOW_ALL) {
348 traverse_commit_list(revs, show_commit, show_object, info);
349 printf("------\n");
352 print_var_str("bisect_rev", hex);
353 print_var_int("bisect_nr", cnt - 1);
354 print_var_int("bisect_good", all - reaches - 1);
355 print_var_int("bisect_bad", reaches - 1);
356 print_var_int("bisect_all", all);
357 print_var_int("bisect_steps", estimate_bisect_steps(all));
359 return 0;
362 static int show_object_fast(
363 const struct object_id *oid,
364 enum object_type type,
365 int exclude,
366 uint32_t name_hash,
367 struct packed_git *found_pack,
368 off_t found_offset)
370 fprintf(stdout, "%s\n", oid_to_hex(oid));
371 return 1;
374 static void print_disk_usage(off_t size)
376 struct strbuf sb = STRBUF_INIT;
377 if (human_readable)
378 strbuf_humanise_bytes(&sb, size);
379 else
380 strbuf_addf(&sb, "%"PRIuMAX, (uintmax_t)size);
381 puts(sb.buf);
382 strbuf_release(&sb);
385 static inline int parse_missing_action_value(const char *value)
387 if (!strcmp(value, "error")) {
388 arg_missing_action = MA_ERROR;
389 return 1;
392 if (!strcmp(value, "allow-any")) {
393 arg_missing_action = MA_ALLOW_ANY;
394 fetch_if_missing = 0;
395 return 1;
398 if (!strcmp(value, "print")) {
399 arg_missing_action = MA_PRINT;
400 fetch_if_missing = 0;
401 return 1;
404 if (!strcmp(value, "allow-promisor")) {
405 arg_missing_action = MA_ALLOW_PROMISOR;
406 fetch_if_missing = 0;
407 return 1;
410 return 0;
413 static int try_bitmap_count(struct rev_info *revs,
414 int filter_provided_objects)
416 uint32_t commit_count = 0,
417 tag_count = 0,
418 tree_count = 0,
419 blob_count = 0;
420 int max_count;
421 struct bitmap_index *bitmap_git;
423 /* This function only handles counting, not general traversal. */
424 if (!revs->count)
425 return -1;
428 * A bitmap result can't know left/right, etc, because we don't
429 * actually traverse.
431 if (revs->left_right || revs->cherry_mark)
432 return -1;
435 * If we're counting reachable objects, we can't handle a max count of
436 * commits to traverse, since we don't know which objects go with which
437 * commit.
439 if (revs->max_count >= 0 &&
440 (revs->tag_objects || revs->tree_objects || revs->blob_objects))
441 return -1;
444 * This must be saved before doing any walking, since the revision
445 * machinery will count it down to zero while traversing.
447 max_count = revs->max_count;
449 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
450 if (!bitmap_git)
451 return -1;
453 count_bitmap_commit_list(bitmap_git, &commit_count,
454 revs->tree_objects ? &tree_count : NULL,
455 revs->blob_objects ? &blob_count : NULL,
456 revs->tag_objects ? &tag_count : NULL);
457 if (max_count >= 0 && max_count < commit_count)
458 commit_count = max_count;
460 printf("%d\n", commit_count + tree_count + blob_count + tag_count);
461 free_bitmap_index(bitmap_git);
462 return 0;
465 static int try_bitmap_traversal(struct rev_info *revs,
466 int filter_provided_objects)
468 struct bitmap_index *bitmap_git;
471 * We can't use a bitmap result with a traversal limit, since the set
472 * of commits we'd get would be essentially random.
474 if (revs->max_count >= 0)
475 return -1;
477 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
478 if (!bitmap_git)
479 return -1;
481 traverse_bitmap_commit_list(bitmap_git, revs, &show_object_fast);
482 free_bitmap_index(bitmap_git);
483 return 0;
486 static int try_bitmap_disk_usage(struct rev_info *revs,
487 int filter_provided_objects)
489 struct bitmap_index *bitmap_git;
490 off_t size_from_bitmap;
492 if (!show_disk_usage)
493 return -1;
495 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
496 if (!bitmap_git)
497 return -1;
499 size_from_bitmap = get_disk_usage_from_bitmap(bitmap_git, revs);
500 print_disk_usage(size_from_bitmap);
501 return 0;
504 int cmd_rev_list(int argc, const char **argv, const char *prefix)
506 struct rev_info revs;
507 struct rev_list_info info;
508 struct setup_revision_opt s_r_opt = {
509 .allow_exclude_promisor_objects = 1,
511 int i;
512 int bisect_list = 0;
513 int bisect_show_vars = 0;
514 int bisect_find_all = 0;
515 int use_bitmap_index = 0;
516 int filter_provided_objects = 0;
517 const char *show_progress = NULL;
518 int ret = 0;
520 if (argc == 2 && !strcmp(argv[1], "-h"))
521 usage(rev_list_usage);
523 git_config(git_default_config, NULL);
524 repo_init_revisions(the_repository, &revs, prefix);
525 revs.abbrev = DEFAULT_ABBREV;
526 revs.commit_format = CMIT_FMT_UNSPECIFIED;
527 revs.include_header = 1;
530 * Scan the argument list before invoking setup_revisions(), so that we
531 * know if fetch_if_missing needs to be set to 0.
533 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
534 * by not crossing the boundary from realized objects to promisor
535 * objects.
537 * Let "--missing" to conditionally set fetch_if_missing.
539 for (i = 1; i < argc; i++) {
540 const char *arg = argv[i];
541 if (!strcmp(arg, "--exclude-promisor-objects")) {
542 fetch_if_missing = 0;
543 revs.exclude_promisor_objects = 1;
544 break;
547 for (i = 1; i < argc; i++) {
548 const char *arg = argv[i];
549 if (skip_prefix(arg, "--missing=", &arg)) {
550 if (revs.exclude_promisor_objects)
551 die(_("options '%s' and '%s' cannot be used together"), "--exclude-promisor-objects", "--missing");
552 if (parse_missing_action_value(arg))
553 break;
557 if (arg_missing_action)
558 revs.do_not_die_on_missing_tree = 1;
560 argc = setup_revisions(argc, argv, &revs, &s_r_opt);
562 memset(&info, 0, sizeof(info));
563 info.revs = &revs;
564 if (revs.bisect)
565 bisect_list = 1;
567 if (revs.diffopt.flags.quick)
568 info.flags |= REV_LIST_QUIET;
569 for (i = 1 ; i < argc; i++) {
570 const char *arg = argv[i];
572 if (!strcmp(arg, "--header")) {
573 revs.verbose_header = 1;
574 continue;
576 if (!strcmp(arg, "--timestamp")) {
577 info.show_timestamp = 1;
578 continue;
580 if (!strcmp(arg, "--bisect")) {
581 bisect_list = 1;
582 continue;
584 if (!strcmp(arg, "--bisect-all")) {
585 bisect_list = 1;
586 bisect_find_all = 1;
587 info.flags |= BISECT_SHOW_ALL;
588 revs.show_decorations = 1;
589 continue;
591 if (!strcmp(arg, "--bisect-vars")) {
592 bisect_list = 1;
593 bisect_show_vars = 1;
594 continue;
596 if (!strcmp(arg, "--use-bitmap-index")) {
597 use_bitmap_index = 1;
598 continue;
600 if (!strcmp(arg, "--test-bitmap")) {
601 test_bitmap_walk(&revs);
602 goto cleanup;
604 if (skip_prefix(arg, "--progress=", &arg)) {
605 show_progress = arg;
606 continue;
608 if (!strcmp(arg, "--filter-provided-objects")) {
609 filter_provided_objects = 1;
610 continue;
612 if (!strcmp(arg, "--filter-print-omitted")) {
613 arg_print_omitted = 1;
614 continue;
617 if (!strcmp(arg, "--exclude-promisor-objects"))
618 continue; /* already handled above */
619 if (skip_prefix(arg, "--missing=", &arg))
620 continue; /* already handled above */
622 if (!strcmp(arg, ("--no-object-names"))) {
623 arg_show_object_names = 0;
624 continue;
627 if (!strcmp(arg, ("--object-names"))) {
628 arg_show_object_names = 1;
629 continue;
632 if (!strcmp(arg, ("--commit-header"))) {
633 revs.include_header = 1;
634 continue;
637 if (!strcmp(arg, ("--no-commit-header"))) {
638 revs.include_header = 0;
639 continue;
642 if (skip_prefix(arg, "--disk-usage", &arg)) {
643 if (*arg == '=') {
644 if (!strcmp(++arg, "human")) {
645 human_readable = 1;
646 } else
647 die(_("invalid value for '%s': '%s', the only allowed format is '%s'"),
648 "--disk-usage=<format>", arg, "human");
649 } else if (*arg) {
651 * Arguably should goto a label to continue chain of ifs?
652 * Doesn't matter unless we try to add --disk-usage-foo
653 * afterwards.
655 usage(rev_list_usage);
657 show_disk_usage = 1;
658 info.flags |= REV_LIST_QUIET;
659 continue;
662 usage(rev_list_usage);
665 if (revs.commit_format != CMIT_FMT_USERFORMAT)
666 revs.include_header = 1;
667 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
668 /* The command line has a --pretty */
669 info.hdr_termination = '\n';
670 if (revs.commit_format == CMIT_FMT_ONELINE || !revs.include_header)
671 info.header_prefix = "";
672 else
673 info.header_prefix = "commit ";
675 else if (revs.verbose_header)
676 /* Only --header was specified */
677 revs.commit_format = CMIT_FMT_RAW;
679 if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
680 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
681 !revs.pending.nr) &&
682 !revs.rev_input_given && !revs.read_from_stdin) ||
683 revs.diff)
684 usage(rev_list_usage);
686 if (revs.show_notes)
687 die(_("rev-list does not support display of notes"));
689 if (revs.count &&
690 (revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
691 (revs.left_right || revs.cherry_mark))
692 die(_("marked counting and '%s' cannot be used together"), "--objects");
694 save_commit_buffer = (revs.verbose_header ||
695 revs.grep_filter.pattern_list ||
696 revs.grep_filter.header_list);
697 if (bisect_list)
698 revs.limited = 1;
700 if (show_progress)
701 progress = start_delayed_progress(show_progress, 0);
703 if (use_bitmap_index) {
704 if (!try_bitmap_count(&revs, filter_provided_objects))
705 goto cleanup;
706 if (!try_bitmap_disk_usage(&revs, filter_provided_objects))
707 goto cleanup;
708 if (!try_bitmap_traversal(&revs, filter_provided_objects))
709 goto cleanup;
712 if (prepare_revision_walk(&revs))
713 die("revision walk setup failed");
714 if (revs.tree_objects)
715 mark_edges_uninteresting(&revs, show_edge, 0);
717 if (bisect_list) {
718 int reaches, all;
719 unsigned bisect_flags = 0;
721 if (bisect_find_all)
722 bisect_flags |= FIND_BISECTION_ALL;
724 if (revs.first_parent_only)
725 bisect_flags |= FIND_BISECTION_FIRST_PARENT_ONLY;
727 find_bisection(&revs.commits, &reaches, &all, bisect_flags);
729 if (bisect_show_vars) {
730 ret = show_bisect_vars(&info, reaches, all);
731 goto cleanup;
735 if (filter_provided_objects) {
736 struct commit_list *c;
737 for (i = 0; i < revs.pending.nr; i++) {
738 struct object_array_entry *pending = revs.pending.objects + i;
739 pending->item->flags |= NOT_USER_GIVEN;
741 for (c = revs.commits; c; c = c->next)
742 c->item->object.flags |= NOT_USER_GIVEN;
745 if (arg_print_omitted)
746 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
747 if (arg_missing_action == MA_PRINT)
748 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
750 traverse_commit_list_filtered(
751 &revs, show_commit, show_object, &info,
752 (arg_print_omitted ? &omitted_objects : NULL));
754 if (arg_print_omitted) {
755 struct oidset_iter iter;
756 struct object_id *oid;
757 oidset_iter_init(&omitted_objects, &iter);
758 while ((oid = oidset_iter_next(&iter)))
759 printf("~%s\n", oid_to_hex(oid));
760 oidset_clear(&omitted_objects);
762 if (arg_missing_action == MA_PRINT) {
763 struct oidset_iter iter;
764 struct object_id *oid;
765 oidset_iter_init(&missing_objects, &iter);
766 while ((oid = oidset_iter_next(&iter)))
767 printf("?%s\n", oid_to_hex(oid));
768 oidset_clear(&missing_objects);
771 stop_progress(&progress);
773 if (revs.count) {
774 if (revs.left_right && revs.cherry_mark)
775 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
776 else if (revs.left_right)
777 printf("%d\t%d\n", revs.count_left, revs.count_right);
778 else if (revs.cherry_mark)
779 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
780 else
781 printf("%d\n", revs.count_left + revs.count_right);
784 if (show_disk_usage)
785 print_disk_usage(total_disk_usage);
787 cleanup:
788 release_revisions(&revs);
789 return ret;