Merge branch 'en/merge-ort-impl'
[git/debian.git] / builtin / rev-list.c
blob25c6c3b38d4b120687bb02220c3db25af2ae66de
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 void finish_commit(struct commit *commit);
84 static void show_commit(struct commit *commit, void *data)
86 struct rev_list_info *info = data;
87 struct rev_info *revs = info->revs;
89 display_progress(progress, ++progress_counter);
91 if (info->flags & REV_LIST_QUIET) {
92 finish_commit(commit);
93 return;
96 graph_show_commit(revs->graph);
98 if (revs->count) {
99 if (commit->object.flags & PATCHSAME)
100 revs->count_same++;
101 else if (commit->object.flags & SYMMETRIC_LEFT)
102 revs->count_left++;
103 else
104 revs->count_right++;
105 finish_commit(commit);
106 return;
109 if (info->show_timestamp)
110 printf("%"PRItime" ", commit->date);
111 if (info->header_prefix)
112 fputs(info->header_prefix, stdout);
114 if (!revs->graph)
115 fputs(get_revision_mark(revs, commit), stdout);
116 if (revs->abbrev_commit && revs->abbrev)
117 fputs(find_unique_abbrev(&commit->object.oid, revs->abbrev),
118 stdout);
119 else
120 fputs(oid_to_hex(&commit->object.oid), stdout);
121 if (revs->print_parents) {
122 struct commit_list *parents = commit->parents;
123 while (parents) {
124 printf(" %s", oid_to_hex(&parents->item->object.oid));
125 parents = parents->next;
128 if (revs->children.name) {
129 struct commit_list *children;
131 children = lookup_decoration(&revs->children, &commit->object);
132 while (children) {
133 printf(" %s", oid_to_hex(&children->item->object.oid));
134 children = children->next;
137 show_decorations(revs, commit);
138 if (revs->commit_format == CMIT_FMT_ONELINE)
139 putchar(' ');
140 else
141 putchar('\n');
143 if (revs->verbose_header) {
144 struct strbuf buf = STRBUF_INIT;
145 struct pretty_print_context ctx = {0};
146 ctx.abbrev = revs->abbrev;
147 ctx.date_mode = revs->date_mode;
148 ctx.date_mode_explicit = revs->date_mode_explicit;
149 ctx.fmt = revs->commit_format;
150 ctx.output_encoding = get_log_output_encoding();
151 ctx.color = revs->diffopt.use_color;
152 pretty_print_commit(&ctx, commit, &buf);
153 if (buf.len) {
154 if (revs->commit_format != CMIT_FMT_ONELINE)
155 graph_show_oneline(revs->graph);
157 graph_show_commit_msg(revs->graph, stdout, &buf);
160 * Add a newline after the commit message.
162 * Usually, this newline produces a blank
163 * padding line between entries, in which case
164 * we need to add graph padding on this line.
166 * However, the commit message may not end in a
167 * newline. In this case the newline simply
168 * ends the last line of the commit message,
169 * and we don't need any graph output. (This
170 * always happens with CMIT_FMT_ONELINE, and it
171 * happens with CMIT_FMT_USERFORMAT when the
172 * format doesn't explicitly end in a newline.)
174 if (buf.len && buf.buf[buf.len - 1] == '\n')
175 graph_show_padding(revs->graph);
176 putchar(info->hdr_termination);
177 } else {
179 * If the message buffer is empty, just show
180 * the rest of the graph output for this
181 * commit.
183 if (graph_show_remainder(revs->graph))
184 putchar('\n');
185 if (revs->commit_format == CMIT_FMT_ONELINE)
186 putchar('\n');
188 strbuf_release(&buf);
189 } else {
190 if (graph_show_remainder(revs->graph))
191 putchar('\n');
193 maybe_flush_or_die(stdout, "stdout");
194 finish_commit(commit);
197 static void finish_commit(struct commit *commit)
199 if (commit->parents) {
200 free_commit_list(commit->parents);
201 commit->parents = NULL;
203 free_commit_buffer(the_repository->parsed_objects,
204 commit);
207 static inline void finish_object__ma(struct object *obj)
210 * Whether or not we try to dynamically fetch missing objects
211 * from the server, we currently DO NOT have the object. We
212 * can either print, allow (ignore), or conditionally allow
213 * (ignore) them.
215 switch (arg_missing_action) {
216 case MA_ERROR:
217 die("missing %s object '%s'",
218 type_name(obj->type), oid_to_hex(&obj->oid));
219 return;
221 case MA_ALLOW_ANY:
222 return;
224 case MA_PRINT:
225 oidset_insert(&missing_objects, &obj->oid);
226 return;
228 case MA_ALLOW_PROMISOR:
229 if (is_promisor_object(&obj->oid))
230 return;
231 die("unexpected missing %s object '%s'",
232 type_name(obj->type), oid_to_hex(&obj->oid));
233 return;
235 default:
236 BUG("unhandled missing_action");
237 return;
241 static int finish_object(struct object *obj, const char *name, void *cb_data)
243 struct rev_list_info *info = cb_data;
244 if (oid_object_info_extended(the_repository, &obj->oid, NULL, 0) < 0) {
245 finish_object__ma(obj);
246 return 1;
248 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
249 parse_object(the_repository, &obj->oid);
250 return 0;
253 static void show_object(struct object *obj, const char *name, void *cb_data)
255 struct rev_list_info *info = cb_data;
256 struct rev_info *revs = info->revs;
258 if (finish_object(obj, name, cb_data))
259 return;
260 display_progress(progress, ++progress_counter);
261 if (info->flags & REV_LIST_QUIET)
262 return;
264 if (revs->count) {
266 * The object count is always accumulated in the .count_right
267 * field for traversal that is not a left-right traversal,
268 * and cmd_rev_list() made sure that a .count request that
269 * wants to count non-commit objects, which is handled by
270 * the show_object() callback, does not ask for .left_right.
272 revs->count_right++;
273 return;
276 if (arg_show_object_names)
277 show_object_with_name(stdout, obj, name);
278 else
279 printf("%s\n", oid_to_hex(&obj->oid));
282 static void show_edge(struct commit *commit)
284 printf("-%s\n", oid_to_hex(&commit->object.oid));
287 static void print_var_str(const char *var, const char *val)
289 printf("%s='%s'\n", var, val);
292 static void print_var_int(const char *var, int val)
294 printf("%s=%d\n", var, val);
297 static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
299 int cnt, flags = info->flags;
300 char hex[GIT_MAX_HEXSZ + 1] = "";
301 struct commit_list *tried;
302 struct rev_info *revs = info->revs;
304 if (!revs->commits)
305 return 1;
307 revs->commits = filter_skipped(revs->commits, &tried,
308 flags & BISECT_SHOW_ALL,
309 NULL, NULL);
312 * revs->commits can reach "reaches" commits among
313 * "all" commits. If it is good, then there are
314 * (all-reaches) commits left to be bisected.
315 * On the other hand, if it is bad, then the set
316 * to bisect is "reaches".
317 * A bisect set of size N has (N-1) commits further
318 * to test, as we already know one bad one.
320 cnt = all - reaches;
321 if (cnt < reaches)
322 cnt = reaches;
324 if (revs->commits)
325 oid_to_hex_r(hex, &revs->commits->item->object.oid);
327 if (flags & BISECT_SHOW_ALL) {
328 traverse_commit_list(revs, show_commit, show_object, info);
329 printf("------\n");
332 print_var_str("bisect_rev", hex);
333 print_var_int("bisect_nr", cnt - 1);
334 print_var_int("bisect_good", all - reaches - 1);
335 print_var_int("bisect_bad", reaches - 1);
336 print_var_int("bisect_all", all);
337 print_var_int("bisect_steps", estimate_bisect_steps(all));
339 return 0;
342 static int show_object_fast(
343 const struct object_id *oid,
344 enum object_type type,
345 int exclude,
346 uint32_t name_hash,
347 struct packed_git *found_pack,
348 off_t found_offset)
350 fprintf(stdout, "%s\n", oid_to_hex(oid));
351 return 1;
354 static inline int parse_missing_action_value(const char *value)
356 if (!strcmp(value, "error")) {
357 arg_missing_action = MA_ERROR;
358 return 1;
361 if (!strcmp(value, "allow-any")) {
362 arg_missing_action = MA_ALLOW_ANY;
363 fetch_if_missing = 0;
364 return 1;
367 if (!strcmp(value, "print")) {
368 arg_missing_action = MA_PRINT;
369 fetch_if_missing = 0;
370 return 1;
373 if (!strcmp(value, "allow-promisor")) {
374 arg_missing_action = MA_ALLOW_PROMISOR;
375 fetch_if_missing = 0;
376 return 1;
379 return 0;
382 static int try_bitmap_count(struct rev_info *revs,
383 struct list_objects_filter_options *filter)
385 uint32_t commit_count = 0,
386 tag_count = 0,
387 tree_count = 0,
388 blob_count = 0;
389 int max_count;
390 struct bitmap_index *bitmap_git;
392 /* This function only handles counting, not general traversal. */
393 if (!revs->count)
394 return -1;
397 * A bitmap result can't know left/right, etc, because we don't
398 * actually traverse.
400 if (revs->left_right || revs->cherry_mark)
401 return -1;
404 * If we're counting reachable objects, we can't handle a max count of
405 * commits to traverse, since we don't know which objects go with which
406 * commit.
408 if (revs->max_count >= 0 &&
409 (revs->tag_objects || revs->tree_objects || revs->blob_objects))
410 return -1;
413 * This must be saved before doing any walking, since the revision
414 * machinery will count it down to zero while traversing.
416 max_count = revs->max_count;
418 bitmap_git = prepare_bitmap_walk(revs, filter);
419 if (!bitmap_git)
420 return -1;
422 count_bitmap_commit_list(bitmap_git, &commit_count,
423 revs->tree_objects ? &tree_count : NULL,
424 revs->blob_objects ? &blob_count : NULL,
425 revs->tag_objects ? &tag_count : NULL);
426 if (max_count >= 0 && max_count < commit_count)
427 commit_count = max_count;
429 printf("%d\n", commit_count + tree_count + blob_count + tag_count);
430 free_bitmap_index(bitmap_git);
431 return 0;
434 static int try_bitmap_traversal(struct rev_info *revs,
435 struct list_objects_filter_options *filter)
437 struct bitmap_index *bitmap_git;
440 * We can't use a bitmap result with a traversal limit, since the set
441 * of commits we'd get would be essentially random.
443 if (revs->max_count >= 0)
444 return -1;
446 bitmap_git = prepare_bitmap_walk(revs, filter);
447 if (!bitmap_git)
448 return -1;
450 traverse_bitmap_commit_list(bitmap_git, revs, &show_object_fast);
451 free_bitmap_index(bitmap_git);
452 return 0;
455 int cmd_rev_list(int argc, const char **argv, const char *prefix)
457 struct rev_info revs;
458 struct rev_list_info info;
459 struct setup_revision_opt s_r_opt = {
460 .allow_exclude_promisor_objects = 1,
462 int i;
463 int bisect_list = 0;
464 int bisect_show_vars = 0;
465 int bisect_find_all = 0;
466 int use_bitmap_index = 0;
467 const char *show_progress = NULL;
469 if (argc == 2 && !strcmp(argv[1], "-h"))
470 usage(rev_list_usage);
472 git_config(git_default_config, NULL);
473 repo_init_revisions(the_repository, &revs, prefix);
474 revs.abbrev = DEFAULT_ABBREV;
475 revs.commit_format = CMIT_FMT_UNSPECIFIED;
478 * Scan the argument list before invoking setup_revisions(), so that we
479 * know if fetch_if_missing needs to be set to 0.
481 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
482 * by not crossing the boundary from realized objects to promisor
483 * objects.
485 * Let "--missing" to conditionally set fetch_if_missing.
487 for (i = 1; i < argc; i++) {
488 const char *arg = argv[i];
489 if (!strcmp(arg, "--exclude-promisor-objects")) {
490 fetch_if_missing = 0;
491 revs.exclude_promisor_objects = 1;
492 break;
495 for (i = 1; i < argc; i++) {
496 const char *arg = argv[i];
497 if (skip_prefix(arg, "--missing=", &arg)) {
498 if (revs.exclude_promisor_objects)
499 die(_("cannot combine --exclude-promisor-objects and --missing"));
500 if (parse_missing_action_value(arg))
501 break;
505 if (arg_missing_action)
506 revs.do_not_die_on_missing_tree = 1;
508 argc = setup_revisions(argc, argv, &revs, &s_r_opt);
510 memset(&info, 0, sizeof(info));
511 info.revs = &revs;
512 if (revs.bisect)
513 bisect_list = 1;
515 if (revs.diffopt.flags.quick)
516 info.flags |= REV_LIST_QUIET;
517 for (i = 1 ; i < argc; i++) {
518 const char *arg = argv[i];
520 if (!strcmp(arg, "--header")) {
521 revs.verbose_header = 1;
522 continue;
524 if (!strcmp(arg, "--timestamp")) {
525 info.show_timestamp = 1;
526 continue;
528 if (!strcmp(arg, "--bisect")) {
529 bisect_list = 1;
530 continue;
532 if (!strcmp(arg, "--bisect-all")) {
533 bisect_list = 1;
534 bisect_find_all = 1;
535 info.flags |= BISECT_SHOW_ALL;
536 revs.show_decorations = 1;
537 continue;
539 if (!strcmp(arg, "--bisect-vars")) {
540 bisect_list = 1;
541 bisect_show_vars = 1;
542 continue;
544 if (!strcmp(arg, "--use-bitmap-index")) {
545 use_bitmap_index = 1;
546 continue;
548 if (!strcmp(arg, "--test-bitmap")) {
549 test_bitmap_walk(&revs);
550 return 0;
552 if (skip_prefix(arg, "--progress=", &arg)) {
553 show_progress = arg;
554 continue;
557 if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
558 parse_list_objects_filter(&filter_options, arg);
559 if (filter_options.choice && !revs.blob_objects)
560 die(_("object filtering requires --objects"));
561 continue;
563 if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
564 list_objects_filter_set_no_filter(&filter_options);
565 continue;
567 if (!strcmp(arg, "--filter-print-omitted")) {
568 arg_print_omitted = 1;
569 continue;
572 if (!strcmp(arg, "--exclude-promisor-objects"))
573 continue; /* already handled above */
574 if (skip_prefix(arg, "--missing=", &arg))
575 continue; /* already handled above */
577 if (!strcmp(arg, ("--no-object-names"))) {
578 arg_show_object_names = 0;
579 continue;
582 if (!strcmp(arg, ("--object-names"))) {
583 arg_show_object_names = 1;
584 continue;
587 usage(rev_list_usage);
590 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
591 /* The command line has a --pretty */
592 info.hdr_termination = '\n';
593 if (revs.commit_format == CMIT_FMT_ONELINE)
594 info.header_prefix = "";
595 else
596 info.header_prefix = "commit ";
598 else if (revs.verbose_header)
599 /* Only --header was specified */
600 revs.commit_format = CMIT_FMT_RAW;
602 if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
603 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
604 !revs.pending.nr) &&
605 !revs.rev_input_given && !revs.read_from_stdin) ||
606 revs.diff)
607 usage(rev_list_usage);
609 if (revs.show_notes)
610 die(_("rev-list does not support display of notes"));
612 if (revs.count &&
613 (revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
614 (revs.left_right || revs.cherry_mark))
615 die(_("marked counting is incompatible with --objects"));
617 save_commit_buffer = (revs.verbose_header ||
618 revs.grep_filter.pattern_list ||
619 revs.grep_filter.header_list);
620 if (bisect_list)
621 revs.limited = 1;
623 if (show_progress)
624 progress = start_delayed_progress(show_progress, 0);
626 if (use_bitmap_index) {
627 if (!try_bitmap_count(&revs, &filter_options))
628 return 0;
629 if (!try_bitmap_traversal(&revs, &filter_options))
630 return 0;
633 if (prepare_revision_walk(&revs))
634 die("revision walk setup failed");
635 if (revs.tree_objects)
636 mark_edges_uninteresting(&revs, show_edge, 0);
638 if (bisect_list) {
639 int reaches, all;
640 unsigned bisect_flags = 0;
642 if (bisect_find_all)
643 bisect_flags |= FIND_BISECTION_ALL;
645 if (revs.first_parent_only)
646 bisect_flags |= FIND_BISECTION_FIRST_PARENT_ONLY;
648 find_bisection(&revs.commits, &reaches, &all, bisect_flags);
650 if (bisect_show_vars)
651 return show_bisect_vars(&info, reaches, all);
654 if (arg_print_omitted)
655 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
656 if (arg_missing_action == MA_PRINT)
657 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
659 traverse_commit_list_filtered(
660 &filter_options, &revs, show_commit, show_object, &info,
661 (arg_print_omitted ? &omitted_objects : NULL));
663 if (arg_print_omitted) {
664 struct oidset_iter iter;
665 struct object_id *oid;
666 oidset_iter_init(&omitted_objects, &iter);
667 while ((oid = oidset_iter_next(&iter)))
668 printf("~%s\n", oid_to_hex(oid));
669 oidset_clear(&omitted_objects);
671 if (arg_missing_action == MA_PRINT) {
672 struct oidset_iter iter;
673 struct object_id *oid;
674 oidset_iter_init(&missing_objects, &iter);
675 while ((oid = oidset_iter_next(&iter)))
676 printf("?%s\n", oid_to_hex(oid));
677 oidset_clear(&missing_objects);
680 stop_progress(&progress);
682 if (revs.count) {
683 if (revs.left_right && revs.cherry_mark)
684 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
685 else if (revs.left_right)
686 printf("%d\t%d\n", revs.count_left, revs.count_right);
687 else if (revs.cherry_mark)
688 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
689 else
690 printf("%d\n", revs.count_left + revs.count_right);
693 return 0;