The eighth batch
[git.git] / builtin / rev-list.c
blob36cb909ebaa51940024dce982dd53e5109c653eb
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 [OPTION] <commit-id>... [ -- paths... ]\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 " --unpacked\n"
50 " --header | --pretty\n"
51 " --[no-]object-names\n"
52 " --abbrev=<n> | --no-abbrev\n"
53 " --abbrev-commit\n"
54 " --left-right\n"
55 " --count\n"
56 " special purpose:\n"
57 " --bisect\n"
58 " --bisect-vars\n"
59 " --bisect-all"
62 static struct progress *progress;
63 static unsigned progress_counter;
65 static struct list_objects_filter_options filter_options;
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;
86 static off_t get_object_disk_usage(struct object *obj)
88 off_t size;
89 struct object_info oi = OBJECT_INFO_INIT;
90 oi.disk_sizep = &size;
91 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
92 die(_("unable to get disk usage of %s"), oid_to_hex(&obj->oid));
93 return size;
96 static void finish_commit(struct commit *commit);
97 static void show_commit(struct commit *commit, void *data)
99 struct rev_list_info *info = data;
100 struct rev_info *revs = info->revs;
102 display_progress(progress, ++progress_counter);
104 if (show_disk_usage)
105 total_disk_usage += get_object_disk_usage(&commit->object);
107 if (info->flags & REV_LIST_QUIET) {
108 finish_commit(commit);
109 return;
112 graph_show_commit(revs->graph);
114 if (revs->count) {
115 if (commit->object.flags & PATCHSAME)
116 revs->count_same++;
117 else if (commit->object.flags & SYMMETRIC_LEFT)
118 revs->count_left++;
119 else
120 revs->count_right++;
121 finish_commit(commit);
122 return;
125 if (info->show_timestamp)
126 printf("%"PRItime" ", commit->date);
127 if (info->header_prefix)
128 fputs(info->header_prefix, stdout);
130 if (revs->include_header) {
131 if (!revs->graph)
132 fputs(get_revision_mark(revs, commit), stdout);
133 if (revs->abbrev_commit && revs->abbrev)
134 fputs(find_unique_abbrev(&commit->object.oid, revs->abbrev),
135 stdout);
136 else
137 fputs(oid_to_hex(&commit->object.oid), stdout);
139 if (revs->print_parents) {
140 struct commit_list *parents = commit->parents;
141 while (parents) {
142 printf(" %s", oid_to_hex(&parents->item->object.oid));
143 parents = parents->next;
146 if (revs->children.name) {
147 struct commit_list *children;
149 children = lookup_decoration(&revs->children, &commit->object);
150 while (children) {
151 printf(" %s", oid_to_hex(&children->item->object.oid));
152 children = children->next;
155 show_decorations(revs, commit);
156 if (revs->commit_format == CMIT_FMT_ONELINE)
157 putchar(' ');
158 else if (revs->include_header)
159 putchar('\n');
161 if (revs->verbose_header) {
162 struct strbuf buf = STRBUF_INIT;
163 struct pretty_print_context ctx = {0};
164 ctx.abbrev = revs->abbrev;
165 ctx.date_mode = revs->date_mode;
166 ctx.date_mode_explicit = revs->date_mode_explicit;
167 ctx.fmt = revs->commit_format;
168 ctx.output_encoding = get_log_output_encoding();
169 ctx.color = revs->diffopt.use_color;
170 pretty_print_commit(&ctx, commit, &buf);
171 if (buf.len) {
172 if (revs->commit_format != CMIT_FMT_ONELINE)
173 graph_show_oneline(revs->graph);
175 graph_show_commit_msg(revs->graph, stdout, &buf);
178 * Add a newline after the commit message.
180 * Usually, this newline produces a blank
181 * padding line between entries, in which case
182 * we need to add graph padding on this line.
184 * However, the commit message may not end in a
185 * newline. In this case the newline simply
186 * ends the last line of the commit message,
187 * and we don't need any graph output. (This
188 * always happens with CMIT_FMT_ONELINE, and it
189 * happens with CMIT_FMT_USERFORMAT when the
190 * format doesn't explicitly end in a newline.)
192 if (buf.len && buf.buf[buf.len - 1] == '\n')
193 graph_show_padding(revs->graph);
194 putchar(info->hdr_termination);
195 } else {
197 * If the message buffer is empty, just show
198 * the rest of the graph output for this
199 * commit.
201 if (graph_show_remainder(revs->graph))
202 putchar('\n');
203 if (revs->commit_format == CMIT_FMT_ONELINE)
204 putchar('\n');
206 strbuf_release(&buf);
207 } else {
208 if (graph_show_remainder(revs->graph))
209 putchar('\n');
211 maybe_flush_or_die(stdout, "stdout");
212 finish_commit(commit);
215 static void finish_commit(struct commit *commit)
217 if (commit->parents) {
218 free_commit_list(commit->parents);
219 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 inline int parse_missing_action_value(const char *value)
376 if (!strcmp(value, "error")) {
377 arg_missing_action = MA_ERROR;
378 return 1;
381 if (!strcmp(value, "allow-any")) {
382 arg_missing_action = MA_ALLOW_ANY;
383 fetch_if_missing = 0;
384 return 1;
387 if (!strcmp(value, "print")) {
388 arg_missing_action = MA_PRINT;
389 fetch_if_missing = 0;
390 return 1;
393 if (!strcmp(value, "allow-promisor")) {
394 arg_missing_action = MA_ALLOW_PROMISOR;
395 fetch_if_missing = 0;
396 return 1;
399 return 0;
402 static int try_bitmap_count(struct rev_info *revs,
403 struct list_objects_filter_options *filter,
404 int filter_provided_objects)
406 uint32_t commit_count = 0,
407 tag_count = 0,
408 tree_count = 0,
409 blob_count = 0;
410 int max_count;
411 struct bitmap_index *bitmap_git;
413 /* This function only handles counting, not general traversal. */
414 if (!revs->count)
415 return -1;
418 * A bitmap result can't know left/right, etc, because we don't
419 * actually traverse.
421 if (revs->left_right || revs->cherry_mark)
422 return -1;
425 * If we're counting reachable objects, we can't handle a max count of
426 * commits to traverse, since we don't know which objects go with which
427 * commit.
429 if (revs->max_count >= 0 &&
430 (revs->tag_objects || revs->tree_objects || revs->blob_objects))
431 return -1;
434 * This must be saved before doing any walking, since the revision
435 * machinery will count it down to zero while traversing.
437 max_count = revs->max_count;
439 bitmap_git = prepare_bitmap_walk(revs, filter, filter_provided_objects);
440 if (!bitmap_git)
441 return -1;
443 count_bitmap_commit_list(bitmap_git, &commit_count,
444 revs->tree_objects ? &tree_count : NULL,
445 revs->blob_objects ? &blob_count : NULL,
446 revs->tag_objects ? &tag_count : NULL);
447 if (max_count >= 0 && max_count < commit_count)
448 commit_count = max_count;
450 printf("%d\n", commit_count + tree_count + blob_count + tag_count);
451 free_bitmap_index(bitmap_git);
452 return 0;
455 static int try_bitmap_traversal(struct rev_info *revs,
456 struct list_objects_filter_options *filter,
457 int filter_provided_objects)
459 struct bitmap_index *bitmap_git;
462 * We can't use a bitmap result with a traversal limit, since the set
463 * of commits we'd get would be essentially random.
465 if (revs->max_count >= 0)
466 return -1;
468 bitmap_git = prepare_bitmap_walk(revs, filter, filter_provided_objects);
469 if (!bitmap_git)
470 return -1;
472 traverse_bitmap_commit_list(bitmap_git, revs, &show_object_fast);
473 free_bitmap_index(bitmap_git);
474 return 0;
477 static int try_bitmap_disk_usage(struct rev_info *revs,
478 struct list_objects_filter_options *filter,
479 int filter_provided_objects)
481 struct bitmap_index *bitmap_git;
483 if (!show_disk_usage)
484 return -1;
486 bitmap_git = prepare_bitmap_walk(revs, filter, filter_provided_objects);
487 if (!bitmap_git)
488 return -1;
490 printf("%"PRIuMAX"\n",
491 (uintmax_t)get_disk_usage_from_bitmap(bitmap_git, revs));
492 return 0;
495 int cmd_rev_list(int argc, const char **argv, const char *prefix)
497 struct rev_info revs;
498 struct rev_list_info info;
499 struct setup_revision_opt s_r_opt = {
500 .allow_exclude_promisor_objects = 1,
502 int i;
503 int bisect_list = 0;
504 int bisect_show_vars = 0;
505 int bisect_find_all = 0;
506 int use_bitmap_index = 0;
507 int filter_provided_objects = 0;
508 const char *show_progress = NULL;
510 if (argc == 2 && !strcmp(argv[1], "-h"))
511 usage(rev_list_usage);
513 git_config(git_default_config, NULL);
514 repo_init_revisions(the_repository, &revs, prefix);
515 revs.abbrev = DEFAULT_ABBREV;
516 revs.commit_format = CMIT_FMT_UNSPECIFIED;
517 revs.include_header = 1;
520 * Scan the argument list before invoking setup_revisions(), so that we
521 * know if fetch_if_missing needs to be set to 0.
523 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
524 * by not crossing the boundary from realized objects to promisor
525 * objects.
527 * Let "--missing" to conditionally set fetch_if_missing.
529 for (i = 1; i < argc; i++) {
530 const char *arg = argv[i];
531 if (!strcmp(arg, "--exclude-promisor-objects")) {
532 fetch_if_missing = 0;
533 revs.exclude_promisor_objects = 1;
534 break;
537 for (i = 1; i < argc; i++) {
538 const char *arg = argv[i];
539 if (skip_prefix(arg, "--missing=", &arg)) {
540 if (revs.exclude_promisor_objects)
541 die(_("cannot combine --exclude-promisor-objects and --missing"));
542 if (parse_missing_action_value(arg))
543 break;
547 if (arg_missing_action)
548 revs.do_not_die_on_missing_tree = 1;
550 argc = setup_revisions(argc, argv, &revs, &s_r_opt);
552 memset(&info, 0, sizeof(info));
553 info.revs = &revs;
554 if (revs.bisect)
555 bisect_list = 1;
557 if (revs.diffopt.flags.quick)
558 info.flags |= REV_LIST_QUIET;
559 for (i = 1 ; i < argc; i++) {
560 const char *arg = argv[i];
562 if (!strcmp(arg, "--header")) {
563 revs.verbose_header = 1;
564 continue;
566 if (!strcmp(arg, "--timestamp")) {
567 info.show_timestamp = 1;
568 continue;
570 if (!strcmp(arg, "--bisect")) {
571 bisect_list = 1;
572 continue;
574 if (!strcmp(arg, "--bisect-all")) {
575 bisect_list = 1;
576 bisect_find_all = 1;
577 info.flags |= BISECT_SHOW_ALL;
578 revs.show_decorations = 1;
579 continue;
581 if (!strcmp(arg, "--bisect-vars")) {
582 bisect_list = 1;
583 bisect_show_vars = 1;
584 continue;
586 if (!strcmp(arg, "--use-bitmap-index")) {
587 use_bitmap_index = 1;
588 continue;
590 if (!strcmp(arg, "--test-bitmap")) {
591 test_bitmap_walk(&revs);
592 return 0;
594 if (skip_prefix(arg, "--progress=", &arg)) {
595 show_progress = arg;
596 continue;
599 if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
600 parse_list_objects_filter(&filter_options, arg);
601 if (filter_options.choice && !revs.blob_objects)
602 die(_("object filtering requires --objects"));
603 continue;
605 if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
606 list_objects_filter_set_no_filter(&filter_options);
607 continue;
609 if (!strcmp(arg, "--filter-provided-objects")) {
610 filter_provided_objects = 1;
611 continue;
613 if (!strcmp(arg, "--filter-print-omitted")) {
614 arg_print_omitted = 1;
615 continue;
618 if (!strcmp(arg, "--exclude-promisor-objects"))
619 continue; /* already handled above */
620 if (skip_prefix(arg, "--missing=", &arg))
621 continue; /* already handled above */
623 if (!strcmp(arg, ("--no-object-names"))) {
624 arg_show_object_names = 0;
625 continue;
628 if (!strcmp(arg, ("--object-names"))) {
629 arg_show_object_names = 1;
630 continue;
633 if (!strcmp(arg, ("--commit-header"))) {
634 revs.include_header = 1;
635 continue;
638 if (!strcmp(arg, ("--no-commit-header"))) {
639 revs.include_header = 0;
640 continue;
643 if (!strcmp(arg, "--disk-usage")) {
644 show_disk_usage = 1;
645 info.flags |= REV_LIST_QUIET;
646 continue;
649 usage(rev_list_usage);
652 if (revs.commit_format != CMIT_FMT_USERFORMAT)
653 revs.include_header = 1;
654 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
655 /* The command line has a --pretty */
656 info.hdr_termination = '\n';
657 if (revs.commit_format == CMIT_FMT_ONELINE || !revs.include_header)
658 info.header_prefix = "";
659 else
660 info.header_prefix = "commit ";
662 else if (revs.verbose_header)
663 /* Only --header was specified */
664 revs.commit_format = CMIT_FMT_RAW;
666 if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
667 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
668 !revs.pending.nr) &&
669 !revs.rev_input_given && !revs.read_from_stdin) ||
670 revs.diff)
671 usage(rev_list_usage);
673 if (revs.show_notes)
674 die(_("rev-list does not support display of notes"));
676 if (revs.count &&
677 (revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
678 (revs.left_right || revs.cherry_mark))
679 die(_("marked counting is incompatible with --objects"));
681 save_commit_buffer = (revs.verbose_header ||
682 revs.grep_filter.pattern_list ||
683 revs.grep_filter.header_list);
684 if (bisect_list)
685 revs.limited = 1;
687 if (show_progress)
688 progress = start_delayed_progress(show_progress, 0);
690 if (use_bitmap_index) {
691 if (!try_bitmap_count(&revs, &filter_options, filter_provided_objects))
692 return 0;
693 if (!try_bitmap_disk_usage(&revs, &filter_options, filter_provided_objects))
694 return 0;
695 if (!try_bitmap_traversal(&revs, &filter_options, filter_provided_objects))
696 return 0;
699 if (prepare_revision_walk(&revs))
700 die("revision walk setup failed");
701 if (revs.tree_objects)
702 mark_edges_uninteresting(&revs, show_edge, 0);
704 if (bisect_list) {
705 int reaches, all;
706 unsigned bisect_flags = 0;
708 if (bisect_find_all)
709 bisect_flags |= FIND_BISECTION_ALL;
711 if (revs.first_parent_only)
712 bisect_flags |= FIND_BISECTION_FIRST_PARENT_ONLY;
714 find_bisection(&revs.commits, &reaches, &all, bisect_flags);
716 if (bisect_show_vars)
717 return show_bisect_vars(&info, reaches, all);
720 if (filter_provided_objects) {
721 struct commit_list *c;
722 for (i = 0; i < revs.pending.nr; i++) {
723 struct object_array_entry *pending = revs.pending.objects + i;
724 pending->item->flags |= NOT_USER_GIVEN;
726 for (c = revs.commits; c; c = c->next)
727 c->item->object.flags |= NOT_USER_GIVEN;
730 if (arg_print_omitted)
731 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
732 if (arg_missing_action == MA_PRINT)
733 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
735 traverse_commit_list_filtered(
736 &filter_options, &revs, show_commit, show_object, &info,
737 (arg_print_omitted ? &omitted_objects : NULL));
739 if (arg_print_omitted) {
740 struct oidset_iter iter;
741 struct object_id *oid;
742 oidset_iter_init(&omitted_objects, &iter);
743 while ((oid = oidset_iter_next(&iter)))
744 printf("~%s\n", oid_to_hex(oid));
745 oidset_clear(&omitted_objects);
747 if (arg_missing_action == MA_PRINT) {
748 struct oidset_iter iter;
749 struct object_id *oid;
750 oidset_iter_init(&missing_objects, &iter);
751 while ((oid = oidset_iter_next(&iter)))
752 printf("?%s\n", oid_to_hex(oid));
753 oidset_clear(&missing_objects);
756 stop_progress(&progress);
758 if (revs.count) {
759 if (revs.left_right && revs.cherry_mark)
760 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
761 else if (revs.left_right)
762 printf("%d\t%d\n", revs.count_left, revs.count_right);
763 else if (revs.cherry_mark)
764 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
765 else
766 printf("%d\n", revs.count_left + revs.count_right);
769 if (show_disk_usage)
770 printf("%"PRIuMAX"\n", (uintmax_t)total_disk_usage);
772 return 0;