pretty: drop print_email_subject flag
[git.git] / builtin / rev-list.c
blob98024b5614d8d43f27bfaf128ba027222da62ba3
1 #include "builtin.h"
2 #include "config.h"
3 #include "commit.h"
4 #include "diff.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "revision.h"
9 #include "list-objects.h"
10 #include "list-objects-filter-options.h"
11 #include "object.h"
12 #include "object-name.h"
13 #include "object-file.h"
14 #include "object-store-ll.h"
15 #include "pack-bitmap.h"
16 #include "log-tree.h"
17 #include "graph.h"
18 #include "bisect.h"
19 #include "progress.h"
20 #include "reflog-walk.h"
21 #include "oidset.h"
22 #include "packfile.h"
24 static const char rev_list_usage[] =
25 "git rev-list [<options>] <commit>... [--] [<path>...]\n"
26 "\n"
27 " limiting output:\n"
28 " --max-count=<n>\n"
29 " --max-age=<epoch>\n"
30 " --min-age=<epoch>\n"
31 " --sparse\n"
32 " --no-merges\n"
33 " --min-parents=<n>\n"
34 " --no-min-parents\n"
35 " --max-parents=<n>\n"
36 " --no-max-parents\n"
37 " --remove-empty\n"
38 " --all\n"
39 " --branches\n"
40 " --tags\n"
41 " --remotes\n"
42 " --stdin\n"
43 " --exclude-hidden=[fetch|receive|uploadpack]\n"
44 " --quiet\n"
45 " ordering output:\n"
46 " --topo-order\n"
47 " --date-order\n"
48 " --reverse\n"
49 " formatting output:\n"
50 " --parents\n"
51 " --children\n"
52 " --objects | --objects-edge\n"
53 " --disk-usage[=human]\n"
54 " --unpacked\n"
55 " --header | --pretty\n"
56 " --[no-]object-names\n"
57 " --abbrev=<n> | --no-abbrev\n"
58 " --abbrev-commit\n"
59 " --left-right\n"
60 " --count\n"
61 " special purpose:\n"
62 " --bisect\n"
63 " --bisect-vars\n"
64 " --bisect-all"
67 static struct progress *progress;
68 static unsigned progress_counter;
70 static struct oidset omitted_objects;
71 static int arg_print_omitted; /* print objects omitted by filter */
73 static struct oidset missing_objects;
74 enum missing_action {
75 MA_ERROR = 0, /* fail if any missing objects are encountered */
76 MA_ALLOW_ANY, /* silently allow ALL missing objects */
77 MA_PRINT, /* print ALL missing objects in special section */
78 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
80 static enum missing_action arg_missing_action;
82 /* display only the oid of each object encountered */
83 static int arg_show_object_names = 1;
85 #define DEFAULT_OIDSET_SIZE (16*1024)
87 static int show_disk_usage;
88 static off_t total_disk_usage;
89 static int human_readable;
91 static off_t get_object_disk_usage(struct object *obj)
93 off_t size;
94 struct object_info oi = OBJECT_INFO_INIT;
95 oi.disk_sizep = &size;
96 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
97 die(_("unable to get disk usage of %s"), oid_to_hex(&obj->oid));
98 return size;
101 static inline void finish_object__ma(struct object *obj)
104 * Whether or not we try to dynamically fetch missing objects
105 * from the server, we currently DO NOT have the object. We
106 * can either print, allow (ignore), or conditionally allow
107 * (ignore) them.
109 switch (arg_missing_action) {
110 case MA_ERROR:
111 die("missing %s object '%s'",
112 type_name(obj->type), oid_to_hex(&obj->oid));
113 return;
115 case MA_ALLOW_ANY:
116 return;
118 case MA_PRINT:
119 oidset_insert(&missing_objects, &obj->oid);
120 return;
122 case MA_ALLOW_PROMISOR:
123 if (is_promisor_object(&obj->oid))
124 return;
125 die("unexpected missing %s object '%s'",
126 type_name(obj->type), oid_to_hex(&obj->oid));
127 return;
129 default:
130 BUG("unhandled missing_action");
131 return;
135 static void finish_commit(struct commit *commit)
137 free_commit_list(commit->parents);
138 commit->parents = NULL;
139 free_commit_buffer(the_repository->parsed_objects,
140 commit);
143 static void show_commit(struct commit *commit, void *data)
145 struct rev_list_info *info = data;
146 struct rev_info *revs = info->revs;
148 display_progress(progress, ++progress_counter);
150 if (revs->do_not_die_on_missing_objects &&
151 oidset_contains(&revs->missing_commits, &commit->object.oid)) {
152 finish_object__ma(&commit->object);
153 return;
156 if (show_disk_usage)
157 total_disk_usage += get_object_disk_usage(&commit->object);
159 if (info->flags & REV_LIST_QUIET) {
160 finish_commit(commit);
161 return;
164 graph_show_commit(revs->graph);
166 if (revs->count) {
167 if (commit->object.flags & PATCHSAME)
168 revs->count_same++;
169 else if (commit->object.flags & SYMMETRIC_LEFT)
170 revs->count_left++;
171 else
172 revs->count_right++;
173 finish_commit(commit);
174 return;
177 if (info->show_timestamp)
178 printf("%"PRItime" ", commit->date);
179 if (info->header_prefix)
180 fputs(info->header_prefix, stdout);
182 if (revs->include_header) {
183 if (!revs->graph)
184 fputs(get_revision_mark(revs, commit), stdout);
185 if (revs->abbrev_commit && revs->abbrev)
186 fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, revs->abbrev),
187 stdout);
188 else
189 fputs(oid_to_hex(&commit->object.oid), stdout);
191 if (revs->print_parents) {
192 struct commit_list *parents = commit->parents;
193 while (parents) {
194 printf(" %s", oid_to_hex(&parents->item->object.oid));
195 parents = parents->next;
198 if (revs->children.name) {
199 struct commit_list *children;
201 children = lookup_decoration(&revs->children, &commit->object);
202 while (children) {
203 printf(" %s", oid_to_hex(&children->item->object.oid));
204 children = children->next;
207 show_decorations(revs, commit);
208 if (revs->commit_format == CMIT_FMT_ONELINE)
209 putchar(' ');
210 else if (revs->include_header)
211 putchar('\n');
213 if (revs->verbose_header) {
214 struct strbuf buf = STRBUF_INIT;
215 struct pretty_print_context ctx = {0};
216 ctx.abbrev = revs->abbrev;
217 ctx.date_mode = revs->date_mode;
218 ctx.date_mode_explicit = revs->date_mode_explicit;
219 ctx.fmt = revs->commit_format;
220 ctx.output_encoding = get_log_output_encoding();
221 ctx.color = revs->diffopt.use_color;
222 ctx.rev = revs;
223 pretty_print_commit(&ctx, commit, &buf);
224 if (buf.len) {
225 if (revs->commit_format != CMIT_FMT_ONELINE)
226 graph_show_oneline(revs->graph);
228 graph_show_commit_msg(revs->graph, stdout, &buf);
231 * Add a newline after the commit message.
233 * Usually, this newline produces a blank
234 * padding line between entries, in which case
235 * we need to add graph padding on this line.
237 * However, the commit message may not end in a
238 * newline. In this case the newline simply
239 * ends the last line of the commit message,
240 * and we don't need any graph output. (This
241 * always happens with CMIT_FMT_ONELINE, and it
242 * happens with CMIT_FMT_USERFORMAT when the
243 * format doesn't explicitly end in a newline.)
245 if (buf.len && buf.buf[buf.len - 1] == '\n')
246 graph_show_padding(revs->graph);
247 putchar(info->hdr_termination);
248 } else {
250 * If the message buffer is empty, just show
251 * the rest of the graph output for this
252 * commit.
254 if (graph_show_remainder(revs->graph))
255 putchar('\n');
256 if (revs->commit_format == CMIT_FMT_ONELINE)
257 putchar('\n');
259 strbuf_release(&buf);
260 } else {
261 if (graph_show_remainder(revs->graph))
262 putchar('\n');
264 maybe_flush_or_die(stdout, "stdout");
265 finish_commit(commit);
268 static int finish_object(struct object *obj, const char *name UNUSED,
269 void *cb_data)
271 struct rev_list_info *info = cb_data;
272 if (oid_object_info_extended(the_repository, &obj->oid, NULL, 0) < 0) {
273 finish_object__ma(obj);
274 return 1;
276 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
277 parse_object(the_repository, &obj->oid);
278 return 0;
281 static void show_object(struct object *obj, const char *name, void *cb_data)
283 struct rev_list_info *info = cb_data;
284 struct rev_info *revs = info->revs;
286 if (finish_object(obj, name, cb_data))
287 return;
288 display_progress(progress, ++progress_counter);
289 if (show_disk_usage)
290 total_disk_usage += get_object_disk_usage(obj);
291 if (info->flags & REV_LIST_QUIET)
292 return;
294 if (revs->count) {
296 * The object count is always accumulated in the .count_right
297 * field for traversal that is not a left-right traversal,
298 * and cmd_rev_list() made sure that a .count request that
299 * wants to count non-commit objects, which is handled by
300 * the show_object() callback, does not ask for .left_right.
302 revs->count_right++;
303 return;
306 if (arg_show_object_names)
307 show_object_with_name(stdout, obj, name);
308 else
309 printf("%s\n", oid_to_hex(&obj->oid));
312 static void show_edge(struct commit *commit)
314 printf("-%s\n", oid_to_hex(&commit->object.oid));
317 static void print_var_str(const char *var, const char *val)
319 printf("%s='%s'\n", var, val);
322 static void print_var_int(const char *var, int val)
324 printf("%s=%d\n", var, val);
327 static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
329 int cnt, flags = info->flags;
330 char hex[GIT_MAX_HEXSZ + 1] = "";
331 struct commit_list *tried;
332 struct rev_info *revs = info->revs;
334 if (!revs->commits)
335 return 1;
337 revs->commits = filter_skipped(revs->commits, &tried,
338 flags & BISECT_SHOW_ALL,
339 NULL, NULL);
342 * revs->commits can reach "reaches" commits among
343 * "all" commits. If it is good, then there are
344 * (all-reaches) commits left to be bisected.
345 * On the other hand, if it is bad, then the set
346 * to bisect is "reaches".
347 * A bisect set of size N has (N-1) commits further
348 * to test, as we already know one bad one.
350 cnt = all - reaches;
351 if (cnt < reaches)
352 cnt = reaches;
354 if (revs->commits)
355 oid_to_hex_r(hex, &revs->commits->item->object.oid);
357 if (flags & BISECT_SHOW_ALL) {
358 traverse_commit_list(revs, show_commit, show_object, info);
359 printf("------\n");
362 print_var_str("bisect_rev", hex);
363 print_var_int("bisect_nr", cnt - 1);
364 print_var_int("bisect_good", all - reaches - 1);
365 print_var_int("bisect_bad", reaches - 1);
366 print_var_int("bisect_all", all);
367 print_var_int("bisect_steps", estimate_bisect_steps(all));
369 return 0;
372 static int show_object_fast(
373 const struct object_id *oid,
374 enum object_type type UNUSED,
375 int exclude UNUSED,
376 uint32_t name_hash UNUSED,
377 struct packed_git *found_pack UNUSED,
378 off_t found_offset UNUSED)
380 fprintf(stdout, "%s\n", oid_to_hex(oid));
381 return 1;
384 static void print_disk_usage(off_t size)
386 struct strbuf sb = STRBUF_INIT;
387 if (human_readable)
388 strbuf_humanise_bytes(&sb, size);
389 else
390 strbuf_addf(&sb, "%"PRIuMAX, (uintmax_t)size);
391 puts(sb.buf);
392 strbuf_release(&sb);
395 static inline int parse_missing_action_value(const char *value)
397 if (!strcmp(value, "error")) {
398 arg_missing_action = MA_ERROR;
399 return 1;
402 if (!strcmp(value, "allow-any")) {
403 arg_missing_action = MA_ALLOW_ANY;
404 fetch_if_missing = 0;
405 return 1;
408 if (!strcmp(value, "print")) {
409 arg_missing_action = MA_PRINT;
410 fetch_if_missing = 0;
411 return 1;
414 if (!strcmp(value, "allow-promisor")) {
415 arg_missing_action = MA_ALLOW_PROMISOR;
416 fetch_if_missing = 0;
417 return 1;
420 return 0;
423 static int try_bitmap_count(struct rev_info *revs,
424 int filter_provided_objects)
426 uint32_t commit_count = 0,
427 tag_count = 0,
428 tree_count = 0,
429 blob_count = 0;
430 int max_count;
431 struct bitmap_index *bitmap_git;
433 /* This function only handles counting, not general traversal. */
434 if (!revs->count)
435 return -1;
438 * A bitmap result can't know left/right, etc, because we don't
439 * actually traverse.
441 if (revs->left_right || revs->cherry_mark)
442 return -1;
445 * If we're counting reachable objects, we can't handle a max count of
446 * commits to traverse, since we don't know which objects go with which
447 * commit.
449 if (revs->max_count >= 0 &&
450 (revs->tag_objects || revs->tree_objects || revs->blob_objects))
451 return -1;
454 * This must be saved before doing any walking, since the revision
455 * machinery will count it down to zero while traversing.
457 max_count = revs->max_count;
459 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
460 if (!bitmap_git)
461 return -1;
463 count_bitmap_commit_list(bitmap_git, &commit_count,
464 revs->tree_objects ? &tree_count : NULL,
465 revs->blob_objects ? &blob_count : NULL,
466 revs->tag_objects ? &tag_count : NULL);
467 if (max_count >= 0 && max_count < commit_count)
468 commit_count = max_count;
470 printf("%d\n", commit_count + tree_count + blob_count + tag_count);
471 free_bitmap_index(bitmap_git);
472 return 0;
475 static int try_bitmap_traversal(struct rev_info *revs,
476 int filter_provided_objects)
478 struct bitmap_index *bitmap_git;
481 * We can't use a bitmap result with a traversal limit, since the set
482 * of commits we'd get would be essentially random.
484 if (revs->max_count >= 0)
485 return -1;
487 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
488 if (!bitmap_git)
489 return -1;
491 traverse_bitmap_commit_list(bitmap_git, revs, &show_object_fast);
492 free_bitmap_index(bitmap_git);
493 return 0;
496 static int try_bitmap_disk_usage(struct rev_info *revs,
497 int filter_provided_objects)
499 struct bitmap_index *bitmap_git;
500 off_t size_from_bitmap;
502 if (!show_disk_usage)
503 return -1;
505 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
506 if (!bitmap_git)
507 return -1;
509 size_from_bitmap = get_disk_usage_from_bitmap(bitmap_git, revs);
510 print_disk_usage(size_from_bitmap);
511 return 0;
514 int cmd_rev_list(int argc, const char **argv, const char *prefix)
516 struct rev_info revs;
517 struct rev_list_info info;
518 struct setup_revision_opt s_r_opt = {
519 .allow_exclude_promisor_objects = 1,
521 int i;
522 int bisect_list = 0;
523 int bisect_show_vars = 0;
524 int bisect_find_all = 0;
525 int use_bitmap_index = 0;
526 int filter_provided_objects = 0;
527 const char *show_progress = NULL;
528 int ret = 0;
530 if (argc == 2 && !strcmp(argv[1], "-h"))
531 usage(rev_list_usage);
533 git_config(git_default_config, NULL);
534 repo_init_revisions(the_repository, &revs, prefix);
535 revs.abbrev = DEFAULT_ABBREV;
536 revs.commit_format = CMIT_FMT_UNSPECIFIED;
537 revs.include_header = 1;
540 * Scan the argument list before invoking setup_revisions(), so that we
541 * know if fetch_if_missing needs to be set to 0.
543 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
544 * by not crossing the boundary from realized objects to promisor
545 * objects.
547 * Let "--missing" to conditionally set fetch_if_missing.
549 for (i = 1; i < argc; i++) {
550 const char *arg = argv[i];
551 if (!strcmp(arg, "--exclude-promisor-objects")) {
552 fetch_if_missing = 0;
553 revs.exclude_promisor_objects = 1;
554 break;
557 for (i = 1; i < argc; i++) {
558 const char *arg = argv[i];
559 if (skip_prefix(arg, "--missing=", &arg)) {
560 if (revs.exclude_promisor_objects)
561 die(_("options '%s' and '%s' cannot be used together"), "--exclude-promisor-objects", "--missing");
562 if (parse_missing_action_value(arg))
563 break;
567 if (arg_missing_action)
568 revs.do_not_die_on_missing_objects = 1;
570 argc = setup_revisions(argc, argv, &revs, &s_r_opt);
572 memset(&info, 0, sizeof(info));
573 info.revs = &revs;
574 if (revs.bisect)
575 bisect_list = 1;
577 if (revs.diffopt.flags.quick)
578 info.flags |= REV_LIST_QUIET;
579 for (i = 1 ; i < argc; i++) {
580 const char *arg = argv[i];
582 if (!strcmp(arg, "--header")) {
583 revs.verbose_header = 1;
584 continue;
586 if (!strcmp(arg, "--timestamp")) {
587 info.show_timestamp = 1;
588 continue;
590 if (!strcmp(arg, "--bisect")) {
591 bisect_list = 1;
592 continue;
594 if (!strcmp(arg, "--bisect-all")) {
595 bisect_list = 1;
596 bisect_find_all = 1;
597 info.flags |= BISECT_SHOW_ALL;
598 revs.show_decorations = 1;
599 continue;
601 if (!strcmp(arg, "--bisect-vars")) {
602 bisect_list = 1;
603 bisect_show_vars = 1;
604 continue;
606 if (!strcmp(arg, "--use-bitmap-index")) {
607 use_bitmap_index = 1;
608 continue;
610 if (!strcmp(arg, "--test-bitmap")) {
611 test_bitmap_walk(&revs);
612 goto cleanup;
614 if (skip_prefix(arg, "--progress=", &arg)) {
615 show_progress = arg;
616 continue;
618 if (!strcmp(arg, "--filter-provided-objects")) {
619 filter_provided_objects = 1;
620 continue;
622 if (!strcmp(arg, "--filter-print-omitted")) {
623 arg_print_omitted = 1;
624 continue;
627 if (!strcmp(arg, "--exclude-promisor-objects"))
628 continue; /* already handled above */
629 if (skip_prefix(arg, "--missing=", &arg))
630 continue; /* already handled above */
632 if (!strcmp(arg, ("--no-object-names"))) {
633 arg_show_object_names = 0;
634 continue;
637 if (!strcmp(arg, ("--object-names"))) {
638 arg_show_object_names = 1;
639 continue;
642 if (!strcmp(arg, ("--commit-header"))) {
643 revs.include_header = 1;
644 continue;
647 if (!strcmp(arg, ("--no-commit-header"))) {
648 revs.include_header = 0;
649 continue;
652 if (skip_prefix(arg, "--disk-usage", &arg)) {
653 if (*arg == '=') {
654 if (!strcmp(++arg, "human")) {
655 human_readable = 1;
656 } else
657 die(_("invalid value for '%s': '%s', the only allowed format is '%s'"),
658 "--disk-usage=<format>", arg, "human");
659 } else if (*arg) {
661 * Arguably should goto a label to continue chain of ifs?
662 * Doesn't matter unless we try to add --disk-usage-foo
663 * afterwards.
665 usage(rev_list_usage);
667 show_disk_usage = 1;
668 info.flags |= REV_LIST_QUIET;
669 continue;
672 usage(rev_list_usage);
675 if (revs.commit_format != CMIT_FMT_USERFORMAT)
676 revs.include_header = 1;
677 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
678 /* The command line has a --pretty */
679 info.hdr_termination = '\n';
680 if (revs.commit_format == CMIT_FMT_ONELINE || !revs.include_header)
681 info.header_prefix = "";
682 else
683 info.header_prefix = "commit ";
685 else if (revs.verbose_header)
686 /* Only --header was specified */
687 revs.commit_format = CMIT_FMT_RAW;
689 if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
690 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
691 !revs.pending.nr) &&
692 !revs.rev_input_given && !revs.read_from_stdin) ||
693 revs.diff)
694 usage(rev_list_usage);
696 if (revs.show_notes)
697 die(_("rev-list does not support display of notes"));
699 if (revs.count &&
700 (revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
701 (revs.left_right || revs.cherry_mark))
702 die(_("marked counting and '%s' cannot be used together"), "--objects");
704 save_commit_buffer = (revs.verbose_header ||
705 revs.grep_filter.pattern_list ||
706 revs.grep_filter.header_list);
707 if (bisect_list)
708 revs.limited = 1;
710 if (show_progress)
711 progress = start_delayed_progress(show_progress, 0);
713 if (use_bitmap_index) {
714 if (!try_bitmap_count(&revs, filter_provided_objects))
715 goto cleanup;
716 if (!try_bitmap_disk_usage(&revs, filter_provided_objects))
717 goto cleanup;
718 if (!try_bitmap_traversal(&revs, filter_provided_objects))
719 goto cleanup;
722 if (prepare_revision_walk(&revs))
723 die("revision walk setup failed");
724 if (revs.tree_objects)
725 mark_edges_uninteresting(&revs, show_edge, 0);
727 if (bisect_list) {
728 int reaches, all;
729 unsigned bisect_flags = 0;
731 if (bisect_find_all)
732 bisect_flags |= FIND_BISECTION_ALL;
734 if (revs.first_parent_only)
735 bisect_flags |= FIND_BISECTION_FIRST_PARENT_ONLY;
737 find_bisection(&revs.commits, &reaches, &all, bisect_flags);
739 if (bisect_show_vars) {
740 ret = show_bisect_vars(&info, reaches, all);
741 goto cleanup;
745 if (filter_provided_objects) {
746 struct commit_list *c;
747 for (i = 0; i < revs.pending.nr; i++) {
748 struct object_array_entry *pending = revs.pending.objects + i;
749 pending->item->flags |= NOT_USER_GIVEN;
751 for (c = revs.commits; c; c = c->next)
752 c->item->object.flags |= NOT_USER_GIVEN;
755 if (arg_print_omitted)
756 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
757 if (arg_missing_action == MA_PRINT)
758 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
760 traverse_commit_list_filtered(
761 &revs, show_commit, show_object, &info,
762 (arg_print_omitted ? &omitted_objects : NULL));
764 if (arg_print_omitted) {
765 struct oidset_iter iter;
766 struct object_id *oid;
767 oidset_iter_init(&omitted_objects, &iter);
768 while ((oid = oidset_iter_next(&iter)))
769 printf("~%s\n", oid_to_hex(oid));
770 oidset_clear(&omitted_objects);
772 if (arg_missing_action == MA_PRINT) {
773 struct oidset_iter iter;
774 struct object_id *oid;
775 oidset_iter_init(&missing_objects, &iter);
776 while ((oid = oidset_iter_next(&iter)))
777 printf("?%s\n", oid_to_hex(oid));
778 oidset_clear(&missing_objects);
781 stop_progress(&progress);
783 if (revs.count) {
784 if (revs.left_right && revs.cherry_mark)
785 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
786 else if (revs.left_right)
787 printf("%d\t%d\n", revs.count_left, revs.count_right);
788 else if (revs.cherry_mark)
789 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
790 else
791 printf("%d\n", revs.count_left + revs.count_right);
794 if (show_disk_usage)
795 print_disk_usage(total_disk_usage);
797 cleanup:
798 release_revisions(&revs);
799 return ret;