leak tests: mark passing SANITIZE=leak tests as leak-free
[git/debian.git] / builtin / rev-list.c
blob30fd8e83eaf2ca767802343d2dcff1825f6b1b50
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 " --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 oidset omitted_objects;
66 static int arg_print_omitted; /* print objects omitted by filter */
68 static struct oidset missing_objects;
69 enum missing_action {
70 MA_ERROR = 0, /* fail if any missing objects are encountered */
71 MA_ALLOW_ANY, /* silently allow ALL missing objects */
72 MA_PRINT, /* print ALL missing objects in special section */
73 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
75 static enum missing_action arg_missing_action;
77 /* display only the oid of each object encountered */
78 static int arg_show_object_names = 1;
80 #define DEFAULT_OIDSET_SIZE (16*1024)
82 static int show_disk_usage;
83 static off_t total_disk_usage;
85 static off_t get_object_disk_usage(struct object *obj)
87 off_t size;
88 struct object_info oi = OBJECT_INFO_INIT;
89 oi.disk_sizep = &size;
90 if (oid_object_info_extended(the_repository, &obj->oid, &oi, 0) < 0)
91 die(_("unable to get disk usage of %s"), oid_to_hex(&obj->oid));
92 return size;
95 static void finish_commit(struct commit *commit);
96 static void show_commit(struct commit *commit, void *data)
98 struct rev_list_info *info = data;
99 struct rev_info *revs = info->revs;
101 display_progress(progress, ++progress_counter);
103 if (show_disk_usage)
104 total_disk_usage += get_object_disk_usage(&commit->object);
106 if (info->flags & REV_LIST_QUIET) {
107 finish_commit(commit);
108 return;
111 graph_show_commit(revs->graph);
113 if (revs->count) {
114 if (commit->object.flags & PATCHSAME)
115 revs->count_same++;
116 else if (commit->object.flags & SYMMETRIC_LEFT)
117 revs->count_left++;
118 else
119 revs->count_right++;
120 finish_commit(commit);
121 return;
124 if (info->show_timestamp)
125 printf("%"PRItime" ", commit->date);
126 if (info->header_prefix)
127 fputs(info->header_prefix, stdout);
129 if (revs->include_header) {
130 if (!revs->graph)
131 fputs(get_revision_mark(revs, commit), stdout);
132 if (revs->abbrev_commit && revs->abbrev)
133 fputs(find_unique_abbrev(&commit->object.oid, revs->abbrev),
134 stdout);
135 else
136 fputs(oid_to_hex(&commit->object.oid), stdout);
138 if (revs->print_parents) {
139 struct commit_list *parents = commit->parents;
140 while (parents) {
141 printf(" %s", oid_to_hex(&parents->item->object.oid));
142 parents = parents->next;
145 if (revs->children.name) {
146 struct commit_list *children;
148 children = lookup_decoration(&revs->children, &commit->object);
149 while (children) {
150 printf(" %s", oid_to_hex(&children->item->object.oid));
151 children = children->next;
154 show_decorations(revs, commit);
155 if (revs->commit_format == CMIT_FMT_ONELINE)
156 putchar(' ');
157 else if (revs->include_header)
158 putchar('\n');
160 if (revs->verbose_header) {
161 struct strbuf buf = STRBUF_INIT;
162 struct pretty_print_context ctx = {0};
163 ctx.abbrev = revs->abbrev;
164 ctx.date_mode = revs->date_mode;
165 ctx.date_mode_explicit = revs->date_mode_explicit;
166 ctx.fmt = revs->commit_format;
167 ctx.output_encoding = get_log_output_encoding();
168 ctx.color = revs->diffopt.use_color;
169 pretty_print_commit(&ctx, commit, &buf);
170 if (buf.len) {
171 if (revs->commit_format != CMIT_FMT_ONELINE)
172 graph_show_oneline(revs->graph);
174 graph_show_commit_msg(revs->graph, stdout, &buf);
177 * Add a newline after the commit message.
179 * Usually, this newline produces a blank
180 * padding line between entries, in which case
181 * we need to add graph padding on this line.
183 * However, the commit message may not end in a
184 * newline. In this case the newline simply
185 * ends the last line of the commit message,
186 * and we don't need any graph output. (This
187 * always happens with CMIT_FMT_ONELINE, and it
188 * happens with CMIT_FMT_USERFORMAT when the
189 * format doesn't explicitly end in a newline.)
191 if (buf.len && buf.buf[buf.len - 1] == '\n')
192 graph_show_padding(revs->graph);
193 putchar(info->hdr_termination);
194 } else {
196 * If the message buffer is empty, just show
197 * the rest of the graph output for this
198 * commit.
200 if (graph_show_remainder(revs->graph))
201 putchar('\n');
202 if (revs->commit_format == CMIT_FMT_ONELINE)
203 putchar('\n');
205 strbuf_release(&buf);
206 } else {
207 if (graph_show_remainder(revs->graph))
208 putchar('\n');
210 maybe_flush_or_die(stdout, "stdout");
211 finish_commit(commit);
214 static void finish_commit(struct commit *commit)
216 free_commit_list(commit->parents);
217 commit->parents = NULL;
218 free_commit_buffer(the_repository->parsed_objects,
219 commit);
222 static inline void finish_object__ma(struct object *obj)
225 * Whether or not we try to dynamically fetch missing objects
226 * from the server, we currently DO NOT have the object. We
227 * can either print, allow (ignore), or conditionally allow
228 * (ignore) them.
230 switch (arg_missing_action) {
231 case MA_ERROR:
232 die("missing %s object '%s'",
233 type_name(obj->type), oid_to_hex(&obj->oid));
234 return;
236 case MA_ALLOW_ANY:
237 return;
239 case MA_PRINT:
240 oidset_insert(&missing_objects, &obj->oid);
241 return;
243 case MA_ALLOW_PROMISOR:
244 if (is_promisor_object(&obj->oid))
245 return;
246 die("unexpected missing %s object '%s'",
247 type_name(obj->type), oid_to_hex(&obj->oid));
248 return;
250 default:
251 BUG("unhandled missing_action");
252 return;
256 static int finish_object(struct object *obj, const char *name, void *cb_data)
258 struct rev_list_info *info = cb_data;
259 if (oid_object_info_extended(the_repository, &obj->oid, NULL, 0) < 0) {
260 finish_object__ma(obj);
261 return 1;
263 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
264 parse_object(the_repository, &obj->oid);
265 return 0;
268 static void show_object(struct object *obj, const char *name, void *cb_data)
270 struct rev_list_info *info = cb_data;
271 struct rev_info *revs = info->revs;
273 if (finish_object(obj, name, cb_data))
274 return;
275 display_progress(progress, ++progress_counter);
276 if (show_disk_usage)
277 total_disk_usage += get_object_disk_usage(obj);
278 if (info->flags & REV_LIST_QUIET)
279 return;
281 if (revs->count) {
283 * The object count is always accumulated in the .count_right
284 * field for traversal that is not a left-right traversal,
285 * and cmd_rev_list() made sure that a .count request that
286 * wants to count non-commit objects, which is handled by
287 * the show_object() callback, does not ask for .left_right.
289 revs->count_right++;
290 return;
293 if (arg_show_object_names)
294 show_object_with_name(stdout, obj, name);
295 else
296 printf("%s\n", oid_to_hex(&obj->oid));
299 static void show_edge(struct commit *commit)
301 printf("-%s\n", oid_to_hex(&commit->object.oid));
304 static void print_var_str(const char *var, const char *val)
306 printf("%s='%s'\n", var, val);
309 static void print_var_int(const char *var, int val)
311 printf("%s=%d\n", var, val);
314 static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
316 int cnt, flags = info->flags;
317 char hex[GIT_MAX_HEXSZ + 1] = "";
318 struct commit_list *tried;
319 struct rev_info *revs = info->revs;
321 if (!revs->commits)
322 return 1;
324 revs->commits = filter_skipped(revs->commits, &tried,
325 flags & BISECT_SHOW_ALL,
326 NULL, NULL);
329 * revs->commits can reach "reaches" commits among
330 * "all" commits. If it is good, then there are
331 * (all-reaches) commits left to be bisected.
332 * On the other hand, if it is bad, then the set
333 * to bisect is "reaches".
334 * A bisect set of size N has (N-1) commits further
335 * to test, as we already know one bad one.
337 cnt = all - reaches;
338 if (cnt < reaches)
339 cnt = reaches;
341 if (revs->commits)
342 oid_to_hex_r(hex, &revs->commits->item->object.oid);
344 if (flags & BISECT_SHOW_ALL) {
345 traverse_commit_list(revs, show_commit, show_object, info);
346 printf("------\n");
349 print_var_str("bisect_rev", hex);
350 print_var_int("bisect_nr", cnt - 1);
351 print_var_int("bisect_good", all - reaches - 1);
352 print_var_int("bisect_bad", reaches - 1);
353 print_var_int("bisect_all", all);
354 print_var_int("bisect_steps", estimate_bisect_steps(all));
356 return 0;
359 static int show_object_fast(
360 const struct object_id *oid,
361 enum object_type type,
362 int exclude,
363 uint32_t name_hash,
364 struct packed_git *found_pack,
365 off_t found_offset)
367 fprintf(stdout, "%s\n", oid_to_hex(oid));
368 return 1;
371 static inline int parse_missing_action_value(const char *value)
373 if (!strcmp(value, "error")) {
374 arg_missing_action = MA_ERROR;
375 return 1;
378 if (!strcmp(value, "allow-any")) {
379 arg_missing_action = MA_ALLOW_ANY;
380 fetch_if_missing = 0;
381 return 1;
384 if (!strcmp(value, "print")) {
385 arg_missing_action = MA_PRINT;
386 fetch_if_missing = 0;
387 return 1;
390 if (!strcmp(value, "allow-promisor")) {
391 arg_missing_action = MA_ALLOW_PROMISOR;
392 fetch_if_missing = 0;
393 return 1;
396 return 0;
399 static int try_bitmap_count(struct rev_info *revs,
400 int filter_provided_objects)
402 uint32_t commit_count = 0,
403 tag_count = 0,
404 tree_count = 0,
405 blob_count = 0;
406 int max_count;
407 struct bitmap_index *bitmap_git;
409 /* This function only handles counting, not general traversal. */
410 if (!revs->count)
411 return -1;
414 * A bitmap result can't know left/right, etc, because we don't
415 * actually traverse.
417 if (revs->left_right || revs->cherry_mark)
418 return -1;
421 * If we're counting reachable objects, we can't handle a max count of
422 * commits to traverse, since we don't know which objects go with which
423 * commit.
425 if (revs->max_count >= 0 &&
426 (revs->tag_objects || revs->tree_objects || revs->blob_objects))
427 return -1;
430 * This must be saved before doing any walking, since the revision
431 * machinery will count it down to zero while traversing.
433 max_count = revs->max_count;
435 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
436 if (!bitmap_git)
437 return -1;
439 count_bitmap_commit_list(bitmap_git, &commit_count,
440 revs->tree_objects ? &tree_count : NULL,
441 revs->blob_objects ? &blob_count : NULL,
442 revs->tag_objects ? &tag_count : NULL);
443 if (max_count >= 0 && max_count < commit_count)
444 commit_count = max_count;
446 printf("%d\n", commit_count + tree_count + blob_count + tag_count);
447 free_bitmap_index(bitmap_git);
448 return 0;
451 static int try_bitmap_traversal(struct rev_info *revs,
452 int filter_provided_objects)
454 struct bitmap_index *bitmap_git;
457 * We can't use a bitmap result with a traversal limit, since the set
458 * of commits we'd get would be essentially random.
460 if (revs->max_count >= 0)
461 return -1;
463 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
464 if (!bitmap_git)
465 return -1;
467 traverse_bitmap_commit_list(bitmap_git, revs, &show_object_fast);
468 free_bitmap_index(bitmap_git);
469 return 0;
472 static int try_bitmap_disk_usage(struct rev_info *revs,
473 int filter_provided_objects)
475 struct bitmap_index *bitmap_git;
477 if (!show_disk_usage)
478 return -1;
480 bitmap_git = prepare_bitmap_walk(revs, filter_provided_objects);
481 if (!bitmap_git)
482 return -1;
484 printf("%"PRIuMAX"\n",
485 (uintmax_t)get_disk_usage_from_bitmap(bitmap_git, revs));
486 return 0;
489 int cmd_rev_list(int argc, const char **argv, const char *prefix)
491 struct rev_info revs;
492 struct rev_list_info info;
493 struct setup_revision_opt s_r_opt = {
494 .allow_exclude_promisor_objects = 1,
496 int i;
497 int bisect_list = 0;
498 int bisect_show_vars = 0;
499 int bisect_find_all = 0;
500 int use_bitmap_index = 0;
501 int filter_provided_objects = 0;
502 const char *show_progress = NULL;
503 int ret = 0;
505 if (argc == 2 && !strcmp(argv[1], "-h"))
506 usage(rev_list_usage);
508 git_config(git_default_config, NULL);
509 repo_init_revisions(the_repository, &revs, prefix);
510 revs.abbrev = DEFAULT_ABBREV;
511 revs.commit_format = CMIT_FMT_UNSPECIFIED;
512 revs.include_header = 1;
515 * Scan the argument list before invoking setup_revisions(), so that we
516 * know if fetch_if_missing needs to be set to 0.
518 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
519 * by not crossing the boundary from realized objects to promisor
520 * objects.
522 * Let "--missing" to conditionally set fetch_if_missing.
524 for (i = 1; i < argc; i++) {
525 const char *arg = argv[i];
526 if (!strcmp(arg, "--exclude-promisor-objects")) {
527 fetch_if_missing = 0;
528 revs.exclude_promisor_objects = 1;
529 break;
532 for (i = 1; i < argc; i++) {
533 const char *arg = argv[i];
534 if (skip_prefix(arg, "--missing=", &arg)) {
535 if (revs.exclude_promisor_objects)
536 die(_("options '%s' and '%s' cannot be used together"), "--exclude-promisor-objects", "--missing");
537 if (parse_missing_action_value(arg))
538 break;
542 if (arg_missing_action)
543 revs.do_not_die_on_missing_tree = 1;
545 argc = setup_revisions(argc, argv, &revs, &s_r_opt);
547 memset(&info, 0, sizeof(info));
548 info.revs = &revs;
549 if (revs.bisect)
550 bisect_list = 1;
552 if (revs.diffopt.flags.quick)
553 info.flags |= REV_LIST_QUIET;
554 for (i = 1 ; i < argc; i++) {
555 const char *arg = argv[i];
557 if (!strcmp(arg, "--header")) {
558 revs.verbose_header = 1;
559 continue;
561 if (!strcmp(arg, "--timestamp")) {
562 info.show_timestamp = 1;
563 continue;
565 if (!strcmp(arg, "--bisect")) {
566 bisect_list = 1;
567 continue;
569 if (!strcmp(arg, "--bisect-all")) {
570 bisect_list = 1;
571 bisect_find_all = 1;
572 info.flags |= BISECT_SHOW_ALL;
573 revs.show_decorations = 1;
574 continue;
576 if (!strcmp(arg, "--bisect-vars")) {
577 bisect_list = 1;
578 bisect_show_vars = 1;
579 continue;
581 if (!strcmp(arg, "--use-bitmap-index")) {
582 use_bitmap_index = 1;
583 continue;
585 if (!strcmp(arg, "--test-bitmap")) {
586 test_bitmap_walk(&revs);
587 goto cleanup;
589 if (skip_prefix(arg, "--progress=", &arg)) {
590 show_progress = arg;
591 continue;
593 if (!strcmp(arg, "--filter-provided-objects")) {
594 filter_provided_objects = 1;
595 continue;
597 if (!strcmp(arg, "--filter-print-omitted")) {
598 arg_print_omitted = 1;
599 continue;
602 if (!strcmp(arg, "--exclude-promisor-objects"))
603 continue; /* already handled above */
604 if (skip_prefix(arg, "--missing=", &arg))
605 continue; /* already handled above */
607 if (!strcmp(arg, ("--no-object-names"))) {
608 arg_show_object_names = 0;
609 continue;
612 if (!strcmp(arg, ("--object-names"))) {
613 arg_show_object_names = 1;
614 continue;
617 if (!strcmp(arg, ("--commit-header"))) {
618 revs.include_header = 1;
619 continue;
622 if (!strcmp(arg, ("--no-commit-header"))) {
623 revs.include_header = 0;
624 continue;
627 if (!strcmp(arg, "--disk-usage")) {
628 show_disk_usage = 1;
629 info.flags |= REV_LIST_QUIET;
630 continue;
633 usage(rev_list_usage);
636 if (revs.commit_format != CMIT_FMT_USERFORMAT)
637 revs.include_header = 1;
638 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
639 /* The command line has a --pretty */
640 info.hdr_termination = '\n';
641 if (revs.commit_format == CMIT_FMT_ONELINE || !revs.include_header)
642 info.header_prefix = "";
643 else
644 info.header_prefix = "commit ";
646 else if (revs.verbose_header)
647 /* Only --header was specified */
648 revs.commit_format = CMIT_FMT_RAW;
650 if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
651 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
652 !revs.pending.nr) &&
653 !revs.rev_input_given && !revs.read_from_stdin) ||
654 revs.diff)
655 usage(rev_list_usage);
657 if (revs.show_notes)
658 die(_("rev-list does not support display of notes"));
660 if (revs.count &&
661 (revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
662 (revs.left_right || revs.cherry_mark))
663 die(_("marked counting and '%s' cannot be used together"), "--objects");
665 save_commit_buffer = (revs.verbose_header ||
666 revs.grep_filter.pattern_list ||
667 revs.grep_filter.header_list);
668 if (bisect_list)
669 revs.limited = 1;
671 if (show_progress)
672 progress = start_delayed_progress(show_progress, 0);
674 if (use_bitmap_index) {
675 if (!try_bitmap_count(&revs, filter_provided_objects))
676 goto cleanup;
677 if (!try_bitmap_disk_usage(&revs, filter_provided_objects))
678 goto cleanup;
679 if (!try_bitmap_traversal(&revs, filter_provided_objects))
680 goto cleanup;
683 if (prepare_revision_walk(&revs))
684 die("revision walk setup failed");
685 if (revs.tree_objects)
686 mark_edges_uninteresting(&revs, show_edge, 0);
688 if (bisect_list) {
689 int reaches, all;
690 unsigned bisect_flags = 0;
692 if (bisect_find_all)
693 bisect_flags |= FIND_BISECTION_ALL;
695 if (revs.first_parent_only)
696 bisect_flags |= FIND_BISECTION_FIRST_PARENT_ONLY;
698 find_bisection(&revs.commits, &reaches, &all, bisect_flags);
700 if (bisect_show_vars) {
701 ret = show_bisect_vars(&info, reaches, all);
702 goto cleanup;
706 if (filter_provided_objects) {
707 struct commit_list *c;
708 for (i = 0; i < revs.pending.nr; i++) {
709 struct object_array_entry *pending = revs.pending.objects + i;
710 pending->item->flags |= NOT_USER_GIVEN;
712 for (c = revs.commits; c; c = c->next)
713 c->item->object.flags |= NOT_USER_GIVEN;
716 if (arg_print_omitted)
717 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
718 if (arg_missing_action == MA_PRINT)
719 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
721 traverse_commit_list_filtered(
722 &revs, show_commit, show_object, &info,
723 (arg_print_omitted ? &omitted_objects : NULL));
725 if (arg_print_omitted) {
726 struct oidset_iter iter;
727 struct object_id *oid;
728 oidset_iter_init(&omitted_objects, &iter);
729 while ((oid = oidset_iter_next(&iter)))
730 printf("~%s\n", oid_to_hex(oid));
731 oidset_clear(&omitted_objects);
733 if (arg_missing_action == MA_PRINT) {
734 struct oidset_iter iter;
735 struct object_id *oid;
736 oidset_iter_init(&missing_objects, &iter);
737 while ((oid = oidset_iter_next(&iter)))
738 printf("?%s\n", oid_to_hex(oid));
739 oidset_clear(&missing_objects);
742 stop_progress(&progress);
744 if (revs.count) {
745 if (revs.left_right && revs.cherry_mark)
746 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
747 else if (revs.left_right)
748 printf("%d\t%d\n", revs.count_left, revs.count_right);
749 else if (revs.cherry_mark)
750 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
751 else
752 printf("%d\n", revs.count_left + revs.count_right);
755 if (show_disk_usage)
756 printf("%"PRIuMAX"\n", (uintmax_t)total_disk_usage);
758 cleanup:
759 release_revisions(&revs);
760 return ret;