submodule: require the submodule path to contain directories only
[git/debian.git] / builtin / rev-list.c
blobd42db0b0cc9fa22ad8674ceddef8d1c735791f37
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 " --exclude-hidden=[receive|uploadpack]\n"
42 " --quiet\n"
43 " ordering output:\n"
44 " --topo-order\n"
45 " --date-order\n"
46 " --reverse\n"
47 " formatting output:\n"
48 " --parents\n"
49 " --children\n"
50 " --objects | --objects-edge\n"
51 " --disk-usage[=human]\n"
52 " --unpacked\n"
53 " --header | --pretty\n"
54 " --[no-]object-names\n"
55 " --abbrev=<n> | --no-abbrev\n"
56 " --abbrev-commit\n"
57 " --left-right\n"
58 " --count\n"
59 " special purpose:\n"
60 " --bisect\n"
61 " --bisect-vars\n"
62 " --bisect-all"
65 static struct progress *progress;
66 static unsigned progress_counter;
68 static struct oidset omitted_objects;
69 static int arg_print_omitted; /* print objects omitted by filter */
71 static struct oidset missing_objects;
72 enum missing_action {
73 MA_ERROR = 0, /* fail if any missing objects are encountered */
74 MA_ALLOW_ANY, /* silently allow ALL missing objects */
75 MA_PRINT, /* print ALL missing objects in special section */
76 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
78 static enum missing_action arg_missing_action;
80 /* display only the oid of each object encountered */
81 static int arg_show_object_names = 1;
83 #define DEFAULT_OIDSET_SIZE (16*1024)
85 static int show_disk_usage;
86 static off_t total_disk_usage;
87 static int human_readable;
89 static off_t get_object_disk_usage(struct object *obj)
91 off_t size;
92 struct object_info oi = OBJECT_INFO_INIT;
93 oi.disk_sizep = &size;
94 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
95 die(_("unable to get disk usage of %s"), oid_to_hex(&obj->oid));
96 return size;
99 static void finish_commit(struct commit *commit);
100 static void show_commit(struct commit *commit, void *data)
102 struct rev_list_info *info = data;
103 struct rev_info *revs = info->revs;
105 display_progress(progress, ++progress_counter);
107 if (show_disk_usage)
108 total_disk_usage += get_object_disk_usage(&commit->object);
110 if (info->flags & REV_LIST_QUIET) {
111 finish_commit(commit);
112 return;
115 graph_show_commit(revs->graph);
117 if (revs->count) {
118 if (commit->object.flags & PATCHSAME)
119 revs->count_same++;
120 else if (commit->object.flags & SYMMETRIC_LEFT)
121 revs->count_left++;
122 else
123 revs->count_right++;
124 finish_commit(commit);
125 return;
128 if (info->show_timestamp)
129 printf("%"PRItime" ", commit->date);
130 if (info->header_prefix)
131 fputs(info->header_prefix, stdout);
133 if (revs->include_header) {
134 if (!revs->graph)
135 fputs(get_revision_mark(revs, commit), stdout);
136 if (revs->abbrev_commit && revs->abbrev)
137 fputs(find_unique_abbrev(&commit->object.oid, revs->abbrev),
138 stdout);
139 else
140 fputs(oid_to_hex(&commit->object.oid), stdout);
142 if (revs->print_parents) {
143 struct commit_list *parents = commit->parents;
144 while (parents) {
145 printf(" %s", oid_to_hex(&parents->item->object.oid));
146 parents = parents->next;
149 if (revs->children.name) {
150 struct commit_list *children;
152 children = lookup_decoration(&revs->children, &commit->object);
153 while (children) {
154 printf(" %s", oid_to_hex(&children->item->object.oid));
155 children = children->next;
158 show_decorations(revs, commit);
159 if (revs->commit_format == CMIT_FMT_ONELINE)
160 putchar(' ');
161 else if (revs->include_header)
162 putchar('\n');
164 if (revs->verbose_header) {
165 struct strbuf buf = STRBUF_INIT;
166 struct pretty_print_context ctx = {0};
167 ctx.abbrev = revs->abbrev;
168 ctx.date_mode = revs->date_mode;
169 ctx.date_mode_explicit = revs->date_mode_explicit;
170 ctx.fmt = revs->commit_format;
171 ctx.output_encoding = get_log_output_encoding();
172 ctx.color = revs->diffopt.use_color;
173 pretty_print_commit(&ctx, commit, &buf);
174 if (buf.len) {
175 if (revs->commit_format != CMIT_FMT_ONELINE)
176 graph_show_oneline(revs->graph);
178 graph_show_commit_msg(revs->graph, stdout, &buf);
181 * Add a newline after the commit message.
183 * Usually, this newline produces a blank
184 * padding line between entries, in which case
185 * we need to add graph padding on this line.
187 * However, the commit message may not end in a
188 * newline. In this case the newline simply
189 * ends the last line of the commit message,
190 * and we don't need any graph output. (This
191 * always happens with CMIT_FMT_ONELINE, and it
192 * happens with CMIT_FMT_USERFORMAT when the
193 * format doesn't explicitly end in a newline.)
195 if (buf.len && buf.buf[buf.len - 1] == '\n')
196 graph_show_padding(revs->graph);
197 putchar(info->hdr_termination);
198 } else {
200 * If the message buffer is empty, just show
201 * the rest of the graph output for this
202 * commit.
204 if (graph_show_remainder(revs->graph))
205 putchar('\n');
206 if (revs->commit_format == CMIT_FMT_ONELINE)
207 putchar('\n');
209 strbuf_release(&buf);
210 } else {
211 if (graph_show_remainder(revs->graph))
212 putchar('\n');
214 maybe_flush_or_die(stdout, "stdout");
215 finish_commit(commit);
218 static void finish_commit(struct commit *commit)
220 free_commit_list(commit->parents);
221 commit->parents = NULL;
222 free_commit_buffer(the_repository->parsed_objects,
223 commit);
226 static inline void finish_object__ma(struct object *obj)
229 * Whether or not we try to dynamically fetch missing objects
230 * from the server, we currently DO NOT have the object. We
231 * can either print, allow (ignore), or conditionally allow
232 * (ignore) them.
234 switch (arg_missing_action) {
235 case MA_ERROR:
236 die("missing %s object '%s'",
237 type_name(obj->type), oid_to_hex(&obj->oid));
238 return;
240 case MA_ALLOW_ANY:
241 return;
243 case MA_PRINT:
244 oidset_insert(&missing_objects, &obj->oid);
245 return;
247 case MA_ALLOW_PROMISOR:
248 if (is_promisor_object(&obj->oid))
249 return;
250 die("unexpected missing %s object '%s'",
251 type_name(obj->type), oid_to_hex(&obj->oid));
252 return;
254 default:
255 BUG("unhandled missing_action");
256 return;
260 static int finish_object(struct object *obj, const char *name, void *cb_data)
262 struct rev_list_info *info = cb_data;
263 if (oid_object_info_extended(the_repository, &obj->oid, NULL, 0) < 0) {
264 finish_object__ma(obj);
265 return 1;
267 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
268 parse_object(the_repository, &obj->oid);
269 return 0;
272 static void show_object(struct object *obj, const char *name, void *cb_data)
274 struct rev_list_info *info = cb_data;
275 struct rev_info *revs = info->revs;
277 if (finish_object(obj, name, cb_data))
278 return;
279 display_progress(progress, ++progress_counter);
280 if (show_disk_usage)
281 total_disk_usage += get_object_disk_usage(obj);
282 if (info->flags & REV_LIST_QUIET)
283 return;
285 if (revs->count) {
287 * The object count is always accumulated in the .count_right
288 * field for traversal that is not a left-right traversal,
289 * and cmd_rev_list() made sure that a .count request that
290 * wants to count non-commit objects, which is handled by
291 * the show_object() callback, does not ask for .left_right.
293 revs->count_right++;
294 return;
297 if (arg_show_object_names)
298 show_object_with_name(stdout, obj, name);
299 else
300 printf("%s\n", oid_to_hex(&obj->oid));
303 static void show_edge(struct commit *commit)
305 printf("-%s\n", oid_to_hex(&commit->object.oid));
308 static void print_var_str(const char *var, const char *val)
310 printf("%s='%s'\n", var, val);
313 static void print_var_int(const char *var, int val)
315 printf("%s=%d\n", var, val);
318 static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
320 int cnt, flags = info->flags;
321 char hex[GIT_MAX_HEXSZ + 1] = "";
322 struct commit_list *tried;
323 struct rev_info *revs = info->revs;
325 if (!revs->commits)
326 return 1;
328 revs->commits = filter_skipped(revs->commits, &tried,
329 flags & BISECT_SHOW_ALL,
330 NULL, NULL);
333 * revs->commits can reach "reaches" commits among
334 * "all" commits. If it is good, then there are
335 * (all-reaches) commits left to be bisected.
336 * On the other hand, if it is bad, then the set
337 * to bisect is "reaches".
338 * A bisect set of size N has (N-1) commits further
339 * to test, as we already know one bad one.
341 cnt = all - reaches;
342 if (cnt < reaches)
343 cnt = reaches;
345 if (revs->commits)
346 oid_to_hex_r(hex, &revs->commits->item->object.oid);
348 if (flags & BISECT_SHOW_ALL) {
349 traverse_commit_list(revs, show_commit, show_object, info);
350 printf("------\n");
353 print_var_str("bisect_rev", hex);
354 print_var_int("bisect_nr", cnt - 1);
355 print_var_int("bisect_good", all - reaches - 1);
356 print_var_int("bisect_bad", reaches - 1);
357 print_var_int("bisect_all", all);
358 print_var_int("bisect_steps", estimate_bisect_steps(all));
360 return 0;
363 static int show_object_fast(
364 const struct object_id *oid,
365 enum object_type type,
366 int exclude,
367 uint32_t name_hash,
368 struct packed_git *found_pack,
369 off_t found_offset)
371 fprintf(stdout, "%s\n", oid_to_hex(oid));
372 return 1;
375 static void print_disk_usage(off_t size)
377 struct strbuf sb = STRBUF_INIT;
378 if (human_readable)
379 strbuf_humanise_bytes(&sb, size);
380 else
381 strbuf_addf(&sb, "%"PRIuMAX, (uintmax_t)size);
382 puts(sb.buf);
383 strbuf_release(&sb);
386 static inline int parse_missing_action_value(const char *value)
388 if (!strcmp(value, "error")) {
389 arg_missing_action = MA_ERROR;
390 return 1;
393 if (!strcmp(value, "allow-any")) {
394 arg_missing_action = MA_ALLOW_ANY;
395 fetch_if_missing = 0;
396 return 1;
399 if (!strcmp(value, "print")) {
400 arg_missing_action = MA_PRINT;
401 fetch_if_missing = 0;
402 return 1;
405 if (!strcmp(value, "allow-promisor")) {
406 arg_missing_action = MA_ALLOW_PROMISOR;
407 fetch_if_missing = 0;
408 return 1;
411 return 0;
414 static int try_bitmap_count(struct rev_info *revs,
415 int filter_provided_objects)
417 uint32_t commit_count = 0,
418 tag_count = 0,
419 tree_count = 0,
420 blob_count = 0;
421 int max_count;
422 struct bitmap_index *bitmap_git;
424 /* This function only handles counting, not general traversal. */
425 if (!revs->count)
426 return -1;
429 * A bitmap result can't know left/right, etc, because we don't
430 * actually traverse.
432 if (revs->left_right || revs->cherry_mark)
433 return -1;
436 * If we're counting reachable objects, we can't handle a max count of
437 * commits to traverse, since we don't know which objects go with which
438 * commit.
440 if (revs->max_count >= 0 &&
441 (revs->tag_objects || revs->tree_objects || revs->blob_objects))
442 return -1;
445 * This must be saved before doing any walking, since the revision
446 * machinery will count it down to zero while traversing.
448 max_count = revs->max_count;
450 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
451 if (!bitmap_git)
452 return -1;
454 count_bitmap_commit_list(bitmap_git, &commit_count,
455 revs->tree_objects ? &tree_count : NULL,
456 revs->blob_objects ? &blob_count : NULL,
457 revs->tag_objects ? &tag_count : NULL);
458 if (max_count >= 0 && max_count < commit_count)
459 commit_count = max_count;
461 printf("%d\n", commit_count + tree_count + blob_count + tag_count);
462 free_bitmap_index(bitmap_git);
463 return 0;
466 static int try_bitmap_traversal(struct rev_info *revs,
467 int filter_provided_objects)
469 struct bitmap_index *bitmap_git;
472 * We can't use a bitmap result with a traversal limit, since the set
473 * of commits we'd get would be essentially random.
475 if (revs->max_count >= 0)
476 return -1;
478 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
479 if (!bitmap_git)
480 return -1;
482 traverse_bitmap_commit_list(bitmap_git, revs, &show_object_fast);
483 free_bitmap_index(bitmap_git);
484 return 0;
487 static int try_bitmap_disk_usage(struct rev_info *revs,
488 int filter_provided_objects)
490 struct bitmap_index *bitmap_git;
491 off_t size_from_bitmap;
493 if (!show_disk_usage)
494 return -1;
496 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
497 if (!bitmap_git)
498 return -1;
500 size_from_bitmap = get_disk_usage_from_bitmap(bitmap_git, revs);
501 print_disk_usage(size_from_bitmap);
502 return 0;
505 int cmd_rev_list(int argc, const char **argv, const char *prefix)
507 struct rev_info revs;
508 struct rev_list_info info;
509 struct setup_revision_opt s_r_opt = {
510 .allow_exclude_promisor_objects = 1,
512 int i;
513 int bisect_list = 0;
514 int bisect_show_vars = 0;
515 int bisect_find_all = 0;
516 int use_bitmap_index = 0;
517 int filter_provided_objects = 0;
518 const char *show_progress = NULL;
519 int ret = 0;
521 if (argc == 2 && !strcmp(argv[1], "-h"))
522 usage(rev_list_usage);
524 git_config(git_default_config, NULL);
525 repo_init_revisions(the_repository, &revs, prefix);
526 revs.abbrev = DEFAULT_ABBREV;
527 revs.commit_format = CMIT_FMT_UNSPECIFIED;
528 revs.include_header = 1;
531 * Scan the argument list before invoking setup_revisions(), so that we
532 * know if fetch_if_missing needs to be set to 0.
534 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
535 * by not crossing the boundary from realized objects to promisor
536 * objects.
538 * Let "--missing" to conditionally set fetch_if_missing.
540 for (i = 1; i < argc; i++) {
541 const char *arg = argv[i];
542 if (!strcmp(arg, "--exclude-promisor-objects")) {
543 fetch_if_missing = 0;
544 revs.exclude_promisor_objects = 1;
545 break;
548 for (i = 1; i < argc; i++) {
549 const char *arg = argv[i];
550 if (skip_prefix(arg, "--missing=", &arg)) {
551 if (revs.exclude_promisor_objects)
552 die(_("options '%s' and '%s' cannot be used together"), "--exclude-promisor-objects", "--missing");
553 if (parse_missing_action_value(arg))
554 break;
558 if (arg_missing_action)
559 revs.do_not_die_on_missing_tree = 1;
561 argc = setup_revisions(argc, argv, &revs, &s_r_opt);
563 memset(&info, 0, sizeof(info));
564 info.revs = &revs;
565 if (revs.bisect)
566 bisect_list = 1;
568 if (revs.diffopt.flags.quick)
569 info.flags |= REV_LIST_QUIET;
570 for (i = 1 ; i < argc; i++) {
571 const char *arg = argv[i];
573 if (!strcmp(arg, "--header")) {
574 revs.verbose_header = 1;
575 continue;
577 if (!strcmp(arg, "--timestamp")) {
578 info.show_timestamp = 1;
579 continue;
581 if (!strcmp(arg, "--bisect")) {
582 bisect_list = 1;
583 continue;
585 if (!strcmp(arg, "--bisect-all")) {
586 bisect_list = 1;
587 bisect_find_all = 1;
588 info.flags |= BISECT_SHOW_ALL;
589 revs.show_decorations = 1;
590 continue;
592 if (!strcmp(arg, "--bisect-vars")) {
593 bisect_list = 1;
594 bisect_show_vars = 1;
595 continue;
597 if (!strcmp(arg, "--use-bitmap-index")) {
598 use_bitmap_index = 1;
599 continue;
601 if (!strcmp(arg, "--test-bitmap")) {
602 test_bitmap_walk(&revs);
603 goto cleanup;
605 if (skip_prefix(arg, "--progress=", &arg)) {
606 show_progress = arg;
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 (skip_prefix(arg, "--disk-usage", &arg)) {
644 if (*arg == '=') {
645 if (!strcmp(++arg, "human")) {
646 human_readable = 1;
647 } else
648 die(_("invalid value for '%s': '%s', the only allowed format is '%s'"),
649 "--disk-usage=<format>", arg, "human");
650 } else if (*arg) {
652 * Arguably should goto a label to continue chain of ifs?
653 * Doesn't matter unless we try to add --disk-usage-foo
654 * afterwards.
656 usage(rev_list_usage);
658 show_disk_usage = 1;
659 info.flags |= REV_LIST_QUIET;
660 continue;
663 usage(rev_list_usage);
666 if (revs.commit_format != CMIT_FMT_USERFORMAT)
667 revs.include_header = 1;
668 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
669 /* The command line has a --pretty */
670 info.hdr_termination = '\n';
671 if (revs.commit_format == CMIT_FMT_ONELINE || !revs.include_header)
672 info.header_prefix = "";
673 else
674 info.header_prefix = "commit ";
676 else if (revs.verbose_header)
677 /* Only --header was specified */
678 revs.commit_format = CMIT_FMT_RAW;
680 if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
681 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
682 !revs.pending.nr) &&
683 !revs.rev_input_given && !revs.read_from_stdin) ||
684 revs.diff)
685 usage(rev_list_usage);
687 if (revs.show_notes)
688 die(_("rev-list does not support display of notes"));
690 if (revs.count &&
691 (revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
692 (revs.left_right || revs.cherry_mark))
693 die(_("marked counting and '%s' cannot be used together"), "--objects");
695 save_commit_buffer = (revs.verbose_header ||
696 revs.grep_filter.pattern_list ||
697 revs.grep_filter.header_list);
698 if (bisect_list)
699 revs.limited = 1;
701 if (show_progress)
702 progress = start_delayed_progress(show_progress, 0);
704 if (use_bitmap_index) {
705 if (!try_bitmap_count(&revs, filter_provided_objects))
706 goto cleanup;
707 if (!try_bitmap_disk_usage(&revs, filter_provided_objects))
708 goto cleanup;
709 if (!try_bitmap_traversal(&revs, filter_provided_objects))
710 goto cleanup;
713 if (prepare_revision_walk(&revs))
714 die("revision walk setup failed");
715 if (revs.tree_objects)
716 mark_edges_uninteresting(&revs, show_edge, 0);
718 if (bisect_list) {
719 int reaches, all;
720 unsigned bisect_flags = 0;
722 if (bisect_find_all)
723 bisect_flags |= FIND_BISECTION_ALL;
725 if (revs.first_parent_only)
726 bisect_flags |= FIND_BISECTION_FIRST_PARENT_ONLY;
728 find_bisection(&revs.commits, &reaches, &all, bisect_flags);
730 if (bisect_show_vars) {
731 ret = show_bisect_vars(&info, reaches, all);
732 goto cleanup;
736 if (filter_provided_objects) {
737 struct commit_list *c;
738 for (i = 0; i < revs.pending.nr; i++) {
739 struct object_array_entry *pending = revs.pending.objects + i;
740 pending->item->flags |= NOT_USER_GIVEN;
742 for (c = revs.commits; c; c = c->next)
743 c->item->object.flags |= NOT_USER_GIVEN;
746 if (arg_print_omitted)
747 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
748 if (arg_missing_action == MA_PRINT)
749 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
751 traverse_commit_list_filtered(
752 &revs, show_commit, show_object, &info,
753 (arg_print_omitted ? &omitted_objects : NULL));
755 if (arg_print_omitted) {
756 struct oidset_iter iter;
757 struct object_id *oid;
758 oidset_iter_init(&omitted_objects, &iter);
759 while ((oid = oidset_iter_next(&iter)))
760 printf("~%s\n", oid_to_hex(oid));
761 oidset_clear(&omitted_objects);
763 if (arg_missing_action == MA_PRINT) {
764 struct oidset_iter iter;
765 struct object_id *oid;
766 oidset_iter_init(&missing_objects, &iter);
767 while ((oid = oidset_iter_next(&iter)))
768 printf("?%s\n", oid_to_hex(oid));
769 oidset_clear(&missing_objects);
772 stop_progress(&progress);
774 if (revs.count) {
775 if (revs.left_right && revs.cherry_mark)
776 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
777 else if (revs.left_right)
778 printf("%d\t%d\n", revs.count_left, revs.count_right);
779 else if (revs.cherry_mark)
780 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
781 else
782 printf("%d\n", revs.count_left + revs.count_right);
785 if (show_disk_usage)
786 print_disk_usage(total_disk_usage);
788 cleanup:
789 release_revisions(&revs);
790 return ret;