ci: remove the pipe after "p4 -V" to catch errors
[alt-git.git] / builtin / rev-list.c
blobfba6f5d51f32d1217b89298e1361169b43feb38b
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-id>... [-- <path>...]\n"
24 " limiting output:\n"
25 " --max-count=<n>\n"
26 " --max-age=<epoch>\n"
27 " --min-age=<epoch>\n"
28 " --sparse\n"
29 " --no-merges\n"
30 " --min-parents=<n>\n"
31 " --no-min-parents\n"
32 " --max-parents=<n>\n"
33 " --no-max-parents\n"
34 " --remove-empty\n"
35 " --all\n"
36 " --branches\n"
37 " --tags\n"
38 " --remotes\n"
39 " --stdin\n"
40 " --quiet\n"
41 " ordering output:\n"
42 " --topo-order\n"
43 " --date-order\n"
44 " --reverse\n"
45 " formatting output:\n"
46 " --parents\n"
47 " --children\n"
48 " --objects | --objects-edge\n"
49 " --disk-usage[=human]\n"
50 " --unpacked\n"
51 " --header | --pretty\n"
52 " --[no-]object-names\n"
53 " --abbrev=<n> | --no-abbrev\n"
54 " --abbrev-commit\n"
55 " --left-right\n"
56 " --count\n"
57 " special purpose:\n"
58 " --bisect\n"
59 " --bisect-vars\n"
60 " --bisect-all"
63 static struct progress *progress;
64 static unsigned progress_counter;
66 static struct oidset omitted_objects;
67 static int arg_print_omitted; /* print objects omitted by filter */
69 static struct oidset missing_objects;
70 enum missing_action {
71 MA_ERROR = 0, /* fail if any missing objects are encountered */
72 MA_ALLOW_ANY, /* silently allow ALL missing objects */
73 MA_PRINT, /* print ALL missing objects in special section */
74 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
76 static enum missing_action arg_missing_action;
78 /* display only the oid of each object encountered */
79 static int arg_show_object_names = 1;
81 #define DEFAULT_OIDSET_SIZE (16*1024)
83 static int show_disk_usage;
84 static off_t total_disk_usage;
85 static int human_readable;
87 static off_t get_object_disk_usage(struct object *obj)
89 off_t size;
90 struct object_info oi = OBJECT_INFO_INIT;
91 oi.disk_sizep = &size;
92 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
93 die(_("unable to get disk usage of %s"), oid_to_hex(&obj->oid));
94 return size;
97 static void finish_commit(struct commit *commit);
98 static void show_commit(struct commit *commit, void *data)
100 struct rev_list_info *info = data;
101 struct rev_info *revs = info->revs;
103 display_progress(progress, ++progress_counter);
105 if (show_disk_usage)
106 total_disk_usage += get_object_disk_usage(&commit->object);
108 if (info->flags & REV_LIST_QUIET) {
109 finish_commit(commit);
110 return;
113 graph_show_commit(revs->graph);
115 if (revs->count) {
116 if (commit->object.flags & PATCHSAME)
117 revs->count_same++;
118 else if (commit->object.flags & SYMMETRIC_LEFT)
119 revs->count_left++;
120 else
121 revs->count_right++;
122 finish_commit(commit);
123 return;
126 if (info->show_timestamp)
127 printf("%"PRItime" ", commit->date);
128 if (info->header_prefix)
129 fputs(info->header_prefix, stdout);
131 if (revs->include_header) {
132 if (!revs->graph)
133 fputs(get_revision_mark(revs, commit), stdout);
134 if (revs->abbrev_commit && revs->abbrev)
135 fputs(find_unique_abbrev(&commit->object.oid, revs->abbrev),
136 stdout);
137 else
138 fputs(oid_to_hex(&commit->object.oid), stdout);
140 if (revs->print_parents) {
141 struct commit_list *parents = commit->parents;
142 while (parents) {
143 printf(" %s", oid_to_hex(&parents->item->object.oid));
144 parents = parents->next;
147 if (revs->children.name) {
148 struct commit_list *children;
150 children = lookup_decoration(&revs->children, &commit->object);
151 while (children) {
152 printf(" %s", oid_to_hex(&children->item->object.oid));
153 children = children->next;
156 show_decorations(revs, commit);
157 if (revs->commit_format == CMIT_FMT_ONELINE)
158 putchar(' ');
159 else if (revs->include_header)
160 putchar('\n');
162 if (revs->verbose_header) {
163 struct strbuf buf = STRBUF_INIT;
164 struct pretty_print_context ctx = {0};
165 ctx.abbrev = revs->abbrev;
166 ctx.date_mode = revs->date_mode;
167 ctx.date_mode_explicit = revs->date_mode_explicit;
168 ctx.fmt = revs->commit_format;
169 ctx.output_encoding = get_log_output_encoding();
170 ctx.color = revs->diffopt.use_color;
171 pretty_print_commit(&ctx, commit, &buf);
172 if (buf.len) {
173 if (revs->commit_format != CMIT_FMT_ONELINE)
174 graph_show_oneline(revs->graph);
176 graph_show_commit_msg(revs->graph, stdout, &buf);
179 * Add a newline after the commit message.
181 * Usually, this newline produces a blank
182 * padding line between entries, in which case
183 * we need to add graph padding on this line.
185 * However, the commit message may not end in a
186 * newline. In this case the newline simply
187 * ends the last line of the commit message,
188 * and we don't need any graph output. (This
189 * always happens with CMIT_FMT_ONELINE, and it
190 * happens with CMIT_FMT_USERFORMAT when the
191 * format doesn't explicitly end in a newline.)
193 if (buf.len && buf.buf[buf.len - 1] == '\n')
194 graph_show_padding(revs->graph);
195 putchar(info->hdr_termination);
196 } else {
198 * If the message buffer is empty, just show
199 * the rest of the graph output for this
200 * commit.
202 if (graph_show_remainder(revs->graph))
203 putchar('\n');
204 if (revs->commit_format == CMIT_FMT_ONELINE)
205 putchar('\n');
207 strbuf_release(&buf);
208 } else {
209 if (graph_show_remainder(revs->graph))
210 putchar('\n');
212 maybe_flush_or_die(stdout, "stdout");
213 finish_commit(commit);
216 static void finish_commit(struct commit *commit)
218 free_commit_list(commit->parents);
219 commit->parents = NULL;
220 free_commit_buffer(the_repository->parsed_objects,
221 commit);
224 static inline void finish_object__ma(struct object *obj)
227 * Whether or not we try to dynamically fetch missing objects
228 * from the server, we currently DO NOT have the object. We
229 * can either print, allow (ignore), or conditionally allow
230 * (ignore) them.
232 switch (arg_missing_action) {
233 case MA_ERROR:
234 die("missing %s object '%s'",
235 type_name(obj->type), oid_to_hex(&obj->oid));
236 return;
238 case MA_ALLOW_ANY:
239 return;
241 case MA_PRINT:
242 oidset_insert(&missing_objects, &obj->oid);
243 return;
245 case MA_ALLOW_PROMISOR:
246 if (is_promisor_object(&obj->oid))
247 return;
248 die("unexpected missing %s object '%s'",
249 type_name(obj->type), oid_to_hex(&obj->oid));
250 return;
252 default:
253 BUG("unhandled missing_action");
254 return;
258 static int finish_object(struct object *obj, const char *name, void *cb_data)
260 struct rev_list_info *info = cb_data;
261 if (oid_object_info_extended(the_repository, &obj->oid, NULL, 0) < 0) {
262 finish_object__ma(obj);
263 return 1;
265 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
266 parse_object(the_repository, &obj->oid);
267 return 0;
270 static void show_object(struct object *obj, const char *name, void *cb_data)
272 struct rev_list_info *info = cb_data;
273 struct rev_info *revs = info->revs;
275 if (finish_object(obj, name, cb_data))
276 return;
277 display_progress(progress, ++progress_counter);
278 if (show_disk_usage)
279 total_disk_usage += get_object_disk_usage(obj);
280 if (info->flags & REV_LIST_QUIET)
281 return;
283 if (revs->count) {
285 * The object count is always accumulated in the .count_right
286 * field for traversal that is not a left-right traversal,
287 * and cmd_rev_list() made sure that a .count request that
288 * wants to count non-commit objects, which is handled by
289 * the show_object() callback, does not ask for .left_right.
291 revs->count_right++;
292 return;
295 if (arg_show_object_names)
296 show_object_with_name(stdout, obj, name);
297 else
298 printf("%s\n", oid_to_hex(&obj->oid));
301 static void show_edge(struct commit *commit)
303 printf("-%s\n", oid_to_hex(&commit->object.oid));
306 static void print_var_str(const char *var, const char *val)
308 printf("%s='%s'\n", var, val);
311 static void print_var_int(const char *var, int val)
313 printf("%s=%d\n", var, val);
316 static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
318 int cnt, flags = info->flags;
319 char hex[GIT_MAX_HEXSZ + 1] = "";
320 struct commit_list *tried;
321 struct rev_info *revs = info->revs;
323 if (!revs->commits)
324 return 1;
326 revs->commits = filter_skipped(revs->commits, &tried,
327 flags & BISECT_SHOW_ALL,
328 NULL, NULL);
331 * revs->commits can reach "reaches" commits among
332 * "all" commits. If it is good, then there are
333 * (all-reaches) commits left to be bisected.
334 * On the other hand, if it is bad, then the set
335 * to bisect is "reaches".
336 * A bisect set of size N has (N-1) commits further
337 * to test, as we already know one bad one.
339 cnt = all - reaches;
340 if (cnt < reaches)
341 cnt = reaches;
343 if (revs->commits)
344 oid_to_hex_r(hex, &revs->commits->item->object.oid);
346 if (flags & BISECT_SHOW_ALL) {
347 traverse_commit_list(revs, show_commit, show_object, info);
348 printf("------\n");
351 print_var_str("bisect_rev", hex);
352 print_var_int("bisect_nr", cnt - 1);
353 print_var_int("bisect_good", all - reaches - 1);
354 print_var_int("bisect_bad", reaches - 1);
355 print_var_int("bisect_all", all);
356 print_var_int("bisect_steps", estimate_bisect_steps(all));
358 return 0;
361 static int show_object_fast(
362 const struct object_id *oid,
363 enum object_type type,
364 int exclude,
365 uint32_t name_hash,
366 struct packed_git *found_pack,
367 off_t found_offset)
369 fprintf(stdout, "%s\n", oid_to_hex(oid));
370 return 1;
373 static void print_disk_usage(off_t size)
375 struct strbuf sb = STRBUF_INIT;
376 if (human_readable)
377 strbuf_humanise_bytes(&sb, size);
378 else
379 strbuf_addf(&sb, "%"PRIuMAX, (uintmax_t)size);
380 puts(sb.buf);
381 strbuf_release(&sb);
384 static inline int parse_missing_action_value(const char *value)
386 if (!strcmp(value, "error")) {
387 arg_missing_action = MA_ERROR;
388 return 1;
391 if (!strcmp(value, "allow-any")) {
392 arg_missing_action = MA_ALLOW_ANY;
393 fetch_if_missing = 0;
394 return 1;
397 if (!strcmp(value, "print")) {
398 arg_missing_action = MA_PRINT;
399 fetch_if_missing = 0;
400 return 1;
403 if (!strcmp(value, "allow-promisor")) {
404 arg_missing_action = MA_ALLOW_PROMISOR;
405 fetch_if_missing = 0;
406 return 1;
409 return 0;
412 static int try_bitmap_count(struct rev_info *revs,
413 int filter_provided_objects)
415 uint32_t commit_count = 0,
416 tag_count = 0,
417 tree_count = 0,
418 blob_count = 0;
419 int max_count;
420 struct bitmap_index *bitmap_git;
422 /* This function only handles counting, not general traversal. */
423 if (!revs->count)
424 return -1;
427 * A bitmap result can't know left/right, etc, because we don't
428 * actually traverse.
430 if (revs->left_right || revs->cherry_mark)
431 return -1;
434 * If we're counting reachable objects, we can't handle a max count of
435 * commits to traverse, since we don't know which objects go with which
436 * commit.
438 if (revs->max_count >= 0 &&
439 (revs->tag_objects || revs->tree_objects || revs->blob_objects))
440 return -1;
443 * This must be saved before doing any walking, since the revision
444 * machinery will count it down to zero while traversing.
446 max_count = revs->max_count;
448 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
449 if (!bitmap_git)
450 return -1;
452 count_bitmap_commit_list(bitmap_git, &commit_count,
453 revs->tree_objects ? &tree_count : NULL,
454 revs->blob_objects ? &blob_count : NULL,
455 revs->tag_objects ? &tag_count : NULL);
456 if (max_count >= 0 && max_count < commit_count)
457 commit_count = max_count;
459 printf("%d\n", commit_count + tree_count + blob_count + tag_count);
460 free_bitmap_index(bitmap_git);
461 return 0;
464 static int try_bitmap_traversal(struct rev_info *revs,
465 int filter_provided_objects)
467 struct bitmap_index *bitmap_git;
470 * We can't use a bitmap result with a traversal limit, since the set
471 * of commits we'd get would be essentially random.
473 if (revs->max_count >= 0)
474 return -1;
476 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
477 if (!bitmap_git)
478 return -1;
480 traverse_bitmap_commit_list(bitmap_git, revs, &show_object_fast);
481 free_bitmap_index(bitmap_git);
482 return 0;
485 static int try_bitmap_disk_usage(struct rev_info *revs,
486 int filter_provided_objects)
488 struct bitmap_index *bitmap_git;
489 off_t size_from_bitmap;
491 if (!show_disk_usage)
492 return -1;
494 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
495 if (!bitmap_git)
496 return -1;
498 size_from_bitmap = get_disk_usage_from_bitmap(bitmap_git, revs);
499 print_disk_usage(size_from_bitmap);
500 return 0;
503 int cmd_rev_list(int argc, const char **argv, const char *prefix)
505 struct rev_info revs;
506 struct rev_list_info info;
507 struct setup_revision_opt s_r_opt = {
508 .allow_exclude_promisor_objects = 1,
510 int i;
511 int bisect_list = 0;
512 int bisect_show_vars = 0;
513 int bisect_find_all = 0;
514 int use_bitmap_index = 0;
515 int filter_provided_objects = 0;
516 const char *show_progress = NULL;
517 int ret = 0;
519 if (argc == 2 && !strcmp(argv[1], "-h"))
520 usage(rev_list_usage);
522 git_config(git_default_config, NULL);
523 repo_init_revisions(the_repository, &revs, prefix);
524 revs.abbrev = DEFAULT_ABBREV;
525 revs.commit_format = CMIT_FMT_UNSPECIFIED;
526 revs.include_header = 1;
529 * Scan the argument list before invoking setup_revisions(), so that we
530 * know if fetch_if_missing needs to be set to 0.
532 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
533 * by not crossing the boundary from realized objects to promisor
534 * objects.
536 * Let "--missing" to conditionally set fetch_if_missing.
538 for (i = 1; i < argc; i++) {
539 const char *arg = argv[i];
540 if (!strcmp(arg, "--exclude-promisor-objects")) {
541 fetch_if_missing = 0;
542 revs.exclude_promisor_objects = 1;
543 break;
546 for (i = 1; i < argc; i++) {
547 const char *arg = argv[i];
548 if (skip_prefix(arg, "--missing=", &arg)) {
549 if (revs.exclude_promisor_objects)
550 die(_("options '%s' and '%s' cannot be used together"), "--exclude-promisor-objects", "--missing");
551 if (parse_missing_action_value(arg))
552 break;
556 if (arg_missing_action)
557 revs.do_not_die_on_missing_tree = 1;
559 argc = setup_revisions(argc, argv, &revs, &s_r_opt);
561 memset(&info, 0, sizeof(info));
562 info.revs = &revs;
563 if (revs.bisect)
564 bisect_list = 1;
566 if (revs.diffopt.flags.quick)
567 info.flags |= REV_LIST_QUIET;
568 for (i = 1 ; i < argc; i++) {
569 const char *arg = argv[i];
571 if (!strcmp(arg, "--header")) {
572 revs.verbose_header = 1;
573 continue;
575 if (!strcmp(arg, "--timestamp")) {
576 info.show_timestamp = 1;
577 continue;
579 if (!strcmp(arg, "--bisect")) {
580 bisect_list = 1;
581 continue;
583 if (!strcmp(arg, "--bisect-all")) {
584 bisect_list = 1;
585 bisect_find_all = 1;
586 info.flags |= BISECT_SHOW_ALL;
587 revs.show_decorations = 1;
588 continue;
590 if (!strcmp(arg, "--bisect-vars")) {
591 bisect_list = 1;
592 bisect_show_vars = 1;
593 continue;
595 if (!strcmp(arg, "--use-bitmap-index")) {
596 use_bitmap_index = 1;
597 continue;
599 if (!strcmp(arg, "--test-bitmap")) {
600 test_bitmap_walk(&revs);
601 goto cleanup;
603 if (skip_prefix(arg, "--progress=", &arg)) {
604 show_progress = arg;
605 continue;
607 if (!strcmp(arg, "--filter-provided-objects")) {
608 filter_provided_objects = 1;
609 continue;
611 if (!strcmp(arg, "--filter-print-omitted")) {
612 arg_print_omitted = 1;
613 continue;
616 if (!strcmp(arg, "--exclude-promisor-objects"))
617 continue; /* already handled above */
618 if (skip_prefix(arg, "--missing=", &arg))
619 continue; /* already handled above */
621 if (!strcmp(arg, ("--no-object-names"))) {
622 arg_show_object_names = 0;
623 continue;
626 if (!strcmp(arg, ("--object-names"))) {
627 arg_show_object_names = 1;
628 continue;
631 if (!strcmp(arg, ("--commit-header"))) {
632 revs.include_header = 1;
633 continue;
636 if (!strcmp(arg, ("--no-commit-header"))) {
637 revs.include_header = 0;
638 continue;
641 if (skip_prefix(arg, "--disk-usage", &arg)) {
642 if (*arg == '=') {
643 if (!strcmp(++arg, "human")) {
644 human_readable = 1;
645 } else
646 die(_("invalid value for '%s': '%s', the only allowed format is '%s'"),
647 "--disk-usage=<format>", arg, "human");
648 } else if (*arg) {
650 * Arguably should goto a label to continue chain of ifs?
651 * Doesn't matter unless we try to add --disk-usage-foo
652 * afterwards.
654 usage(rev_list_usage);
656 show_disk_usage = 1;
657 info.flags |= REV_LIST_QUIET;
658 continue;
661 usage(rev_list_usage);
664 if (revs.commit_format != CMIT_FMT_USERFORMAT)
665 revs.include_header = 1;
666 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
667 /* The command line has a --pretty */
668 info.hdr_termination = '\n';
669 if (revs.commit_format == CMIT_FMT_ONELINE || !revs.include_header)
670 info.header_prefix = "";
671 else
672 info.header_prefix = "commit ";
674 else if (revs.verbose_header)
675 /* Only --header was specified */
676 revs.commit_format = CMIT_FMT_RAW;
678 if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
679 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
680 !revs.pending.nr) &&
681 !revs.rev_input_given && !revs.read_from_stdin) ||
682 revs.diff)
683 usage(rev_list_usage);
685 if (revs.show_notes)
686 die(_("rev-list does not support display of notes"));
688 if (revs.count &&
689 (revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
690 (revs.left_right || revs.cherry_mark))
691 die(_("marked counting and '%s' cannot be used together"), "--objects");
693 save_commit_buffer = (revs.verbose_header ||
694 revs.grep_filter.pattern_list ||
695 revs.grep_filter.header_list);
696 if (bisect_list)
697 revs.limited = 1;
699 if (show_progress)
700 progress = start_delayed_progress(show_progress, 0);
702 if (use_bitmap_index) {
703 if (!try_bitmap_count(&revs, filter_provided_objects))
704 goto cleanup;
705 if (!try_bitmap_disk_usage(&revs, filter_provided_objects))
706 goto cleanup;
707 if (!try_bitmap_traversal(&revs, filter_provided_objects))
708 goto cleanup;
711 if (prepare_revision_walk(&revs))
712 die("revision walk setup failed");
713 if (revs.tree_objects)
714 mark_edges_uninteresting(&revs, show_edge, 0);
716 if (bisect_list) {
717 int reaches, all;
718 unsigned bisect_flags = 0;
720 if (bisect_find_all)
721 bisect_flags |= FIND_BISECTION_ALL;
723 if (revs.first_parent_only)
724 bisect_flags |= FIND_BISECTION_FIRST_PARENT_ONLY;
726 find_bisection(&revs.commits, &reaches, &all, bisect_flags);
728 if (bisect_show_vars) {
729 ret = show_bisect_vars(&info, reaches, all);
730 goto cleanup;
734 if (filter_provided_objects) {
735 struct commit_list *c;
736 for (i = 0; i < revs.pending.nr; i++) {
737 struct object_array_entry *pending = revs.pending.objects + i;
738 pending->item->flags |= NOT_USER_GIVEN;
740 for (c = revs.commits; c; c = c->next)
741 c->item->object.flags |= NOT_USER_GIVEN;
744 if (arg_print_omitted)
745 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
746 if (arg_missing_action == MA_PRINT)
747 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
749 traverse_commit_list_filtered(
750 &revs, show_commit, show_object, &info,
751 (arg_print_omitted ? &omitted_objects : NULL));
753 if (arg_print_omitted) {
754 struct oidset_iter iter;
755 struct object_id *oid;
756 oidset_iter_init(&omitted_objects, &iter);
757 while ((oid = oidset_iter_next(&iter)))
758 printf("~%s\n", oid_to_hex(oid));
759 oidset_clear(&omitted_objects);
761 if (arg_missing_action == MA_PRINT) {
762 struct oidset_iter iter;
763 struct object_id *oid;
764 oidset_iter_init(&missing_objects, &iter);
765 while ((oid = oidset_iter_next(&iter)))
766 printf("?%s\n", oid_to_hex(oid));
767 oidset_clear(&missing_objects);
770 stop_progress(&progress);
772 if (revs.count) {
773 if (revs.left_right && revs.cherry_mark)
774 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
775 else if (revs.left_right)
776 printf("%d\t%d\n", revs.count_left, revs.count_right);
777 else if (revs.cherry_mark)
778 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
779 else
780 printf("%d\n", revs.count_left + revs.count_right);
783 if (show_disk_usage)
784 print_disk_usage(total_disk_usage);
786 cleanup:
787 release_revisions(&revs);
788 return ret;