]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/rev-list.c
Merge branch 'en/ort-perf-batch-9'
[thirdparty/git.git] / builtin / rev-list.c
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"
21
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"
60 ;
61
62 static struct progress *progress;
63 static unsigned progress_counter;
64
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 */
68
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 */
75 };
76 static enum missing_action arg_missing_action;
77
78 /* display only the oid of each object encountered */
79 static int arg_show_object_names = 1;
80
81 #define DEFAULT_OIDSET_SIZE (16*1024)
82
83 static int show_disk_usage;
84 static off_t total_disk_usage;
85
86 static off_t get_object_disk_usage(struct object *obj)
87 {
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;
94 }
95
96 static void finish_commit(struct commit *commit);
97 static void show_commit(struct commit *commit, void *data)
98 {
99 struct rev_list_info *info = data;
100 struct rev_info *revs = info->revs;
101
102 display_progress(progress, ++progress_counter);
103
104 if (show_disk_usage)
105 total_disk_usage += get_object_disk_usage(&commit->object);
106
107 if (info->flags & REV_LIST_QUIET) {
108 finish_commit(commit);
109 return;
110 }
111
112 graph_show_commit(revs->graph);
113
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;
123 }
124
125 if (info->show_timestamp)
126 printf("%"PRItime" ", commit->date);
127 if (info->header_prefix)
128 fputs(info->header_prefix, stdout);
129
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);
137 if (revs->print_parents) {
138 struct commit_list *parents = commit->parents;
139 while (parents) {
140 printf(" %s", oid_to_hex(&parents->item->object.oid));
141 parents = parents->next;
142 }
143 }
144 if (revs->children.name) {
145 struct commit_list *children;
146
147 children = lookup_decoration(&revs->children, &commit->object);
148 while (children) {
149 printf(" %s", oid_to_hex(&children->item->object.oid));
150 children = children->next;
151 }
152 }
153 show_decorations(revs, commit);
154 if (revs->commit_format == CMIT_FMT_ONELINE)
155 putchar(' ');
156 else
157 putchar('\n');
158
159 if (revs->verbose_header) {
160 struct strbuf buf = STRBUF_INIT;
161 struct pretty_print_context ctx = {0};
162 ctx.abbrev = revs->abbrev;
163 ctx.date_mode = revs->date_mode;
164 ctx.date_mode_explicit = revs->date_mode_explicit;
165 ctx.fmt = revs->commit_format;
166 ctx.output_encoding = get_log_output_encoding();
167 ctx.color = revs->diffopt.use_color;
168 pretty_print_commit(&ctx, commit, &buf);
169 if (buf.len) {
170 if (revs->commit_format != CMIT_FMT_ONELINE)
171 graph_show_oneline(revs->graph);
172
173 graph_show_commit_msg(revs->graph, stdout, &buf);
174
175 /*
176 * Add a newline after the commit message.
177 *
178 * Usually, this newline produces a blank
179 * padding line between entries, in which case
180 * we need to add graph padding on this line.
181 *
182 * However, the commit message may not end in a
183 * newline. In this case the newline simply
184 * ends the last line of the commit message,
185 * and we don't need any graph output. (This
186 * always happens with CMIT_FMT_ONELINE, and it
187 * happens with CMIT_FMT_USERFORMAT when the
188 * format doesn't explicitly end in a newline.)
189 */
190 if (buf.len && buf.buf[buf.len - 1] == '\n')
191 graph_show_padding(revs->graph);
192 putchar(info->hdr_termination);
193 } else {
194 /*
195 * If the message buffer is empty, just show
196 * the rest of the graph output for this
197 * commit.
198 */
199 if (graph_show_remainder(revs->graph))
200 putchar('\n');
201 if (revs->commit_format == CMIT_FMT_ONELINE)
202 putchar('\n');
203 }
204 strbuf_release(&buf);
205 } else {
206 if (graph_show_remainder(revs->graph))
207 putchar('\n');
208 }
209 maybe_flush_or_die(stdout, "stdout");
210 finish_commit(commit);
211 }
212
213 static void finish_commit(struct commit *commit)
214 {
215 if (commit->parents) {
216 free_commit_list(commit->parents);
217 commit->parents = NULL;
218 }
219 free_commit_buffer(the_repository->parsed_objects,
220 commit);
221 }
222
223 static inline void finish_object__ma(struct object *obj)
224 {
225 /*
226 * Whether or not we try to dynamically fetch missing objects
227 * from the server, we currently DO NOT have the object. We
228 * can either print, allow (ignore), or conditionally allow
229 * (ignore) them.
230 */
231 switch (arg_missing_action) {
232 case MA_ERROR:
233 die("missing %s object '%s'",
234 type_name(obj->type), oid_to_hex(&obj->oid));
235 return;
236
237 case MA_ALLOW_ANY:
238 return;
239
240 case MA_PRINT:
241 oidset_insert(&missing_objects, &obj->oid);
242 return;
243
244 case MA_ALLOW_PROMISOR:
245 if (is_promisor_object(&obj->oid))
246 return;
247 die("unexpected missing %s object '%s'",
248 type_name(obj->type), oid_to_hex(&obj->oid));
249 return;
250
251 default:
252 BUG("unhandled missing_action");
253 return;
254 }
255 }
256
257 static int finish_object(struct object *obj, const char *name, void *cb_data)
258 {
259 struct rev_list_info *info = cb_data;
260 if (oid_object_info_extended(the_repository, &obj->oid, NULL, 0) < 0) {
261 finish_object__ma(obj);
262 return 1;
263 }
264 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
265 parse_object(the_repository, &obj->oid);
266 return 0;
267 }
268
269 static void show_object(struct object *obj, const char *name, void *cb_data)
270 {
271 struct rev_list_info *info = cb_data;
272 struct rev_info *revs = info->revs;
273
274 if (finish_object(obj, name, cb_data))
275 return;
276 display_progress(progress, ++progress_counter);
277 if (show_disk_usage)
278 total_disk_usage += get_object_disk_usage(obj);
279 if (info->flags & REV_LIST_QUIET)
280 return;
281
282 if (revs->count) {
283 /*
284 * The object count is always accumulated in the .count_right
285 * field for traversal that is not a left-right traversal,
286 * and cmd_rev_list() made sure that a .count request that
287 * wants to count non-commit objects, which is handled by
288 * the show_object() callback, does not ask for .left_right.
289 */
290 revs->count_right++;
291 return;
292 }
293
294 if (arg_show_object_names)
295 show_object_with_name(stdout, obj, name);
296 else
297 printf("%s\n", oid_to_hex(&obj->oid));
298 }
299
300 static void show_edge(struct commit *commit)
301 {
302 printf("-%s\n", oid_to_hex(&commit->object.oid));
303 }
304
305 static void print_var_str(const char *var, const char *val)
306 {
307 printf("%s='%s'\n", var, val);
308 }
309
310 static void print_var_int(const char *var, int val)
311 {
312 printf("%s=%d\n", var, val);
313 }
314
315 static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
316 {
317 int cnt, flags = info->flags;
318 char hex[GIT_MAX_HEXSZ + 1] = "";
319 struct commit_list *tried;
320 struct rev_info *revs = info->revs;
321
322 if (!revs->commits)
323 return 1;
324
325 revs->commits = filter_skipped(revs->commits, &tried,
326 flags & BISECT_SHOW_ALL,
327 NULL, NULL);
328
329 /*
330 * revs->commits can reach "reaches" commits among
331 * "all" commits. If it is good, then there are
332 * (all-reaches) commits left to be bisected.
333 * On the other hand, if it is bad, then the set
334 * to bisect is "reaches".
335 * A bisect set of size N has (N-1) commits further
336 * to test, as we already know one bad one.
337 */
338 cnt = all - reaches;
339 if (cnt < reaches)
340 cnt = reaches;
341
342 if (revs->commits)
343 oid_to_hex_r(hex, &revs->commits->item->object.oid);
344
345 if (flags & BISECT_SHOW_ALL) {
346 traverse_commit_list(revs, show_commit, show_object, info);
347 printf("------\n");
348 }
349
350 print_var_str("bisect_rev", hex);
351 print_var_int("bisect_nr", cnt - 1);
352 print_var_int("bisect_good", all - reaches - 1);
353 print_var_int("bisect_bad", reaches - 1);
354 print_var_int("bisect_all", all);
355 print_var_int("bisect_steps", estimate_bisect_steps(all));
356
357 return 0;
358 }
359
360 static int show_object_fast(
361 const struct object_id *oid,
362 enum object_type type,
363 int exclude,
364 uint32_t name_hash,
365 struct packed_git *found_pack,
366 off_t found_offset)
367 {
368 fprintf(stdout, "%s\n", oid_to_hex(oid));
369 return 1;
370 }
371
372 static inline int parse_missing_action_value(const char *value)
373 {
374 if (!strcmp(value, "error")) {
375 arg_missing_action = MA_ERROR;
376 return 1;
377 }
378
379 if (!strcmp(value, "allow-any")) {
380 arg_missing_action = MA_ALLOW_ANY;
381 fetch_if_missing = 0;
382 return 1;
383 }
384
385 if (!strcmp(value, "print")) {
386 arg_missing_action = MA_PRINT;
387 fetch_if_missing = 0;
388 return 1;
389 }
390
391 if (!strcmp(value, "allow-promisor")) {
392 arg_missing_action = MA_ALLOW_PROMISOR;
393 fetch_if_missing = 0;
394 return 1;
395 }
396
397 return 0;
398 }
399
400 static int try_bitmap_count(struct rev_info *revs,
401 struct list_objects_filter_options *filter)
402 {
403 uint32_t commit_count = 0,
404 tag_count = 0,
405 tree_count = 0,
406 blob_count = 0;
407 int max_count;
408 struct bitmap_index *bitmap_git;
409
410 /* This function only handles counting, not general traversal. */
411 if (!revs->count)
412 return -1;
413
414 /*
415 * A bitmap result can't know left/right, etc, because we don't
416 * actually traverse.
417 */
418 if (revs->left_right || revs->cherry_mark)
419 return -1;
420
421 /*
422 * If we're counting reachable objects, we can't handle a max count of
423 * commits to traverse, since we don't know which objects go with which
424 * commit.
425 */
426 if (revs->max_count >= 0 &&
427 (revs->tag_objects || revs->tree_objects || revs->blob_objects))
428 return -1;
429
430 /*
431 * This must be saved before doing any walking, since the revision
432 * machinery will count it down to zero while traversing.
433 */
434 max_count = revs->max_count;
435
436 bitmap_git = prepare_bitmap_walk(revs, filter);
437 if (!bitmap_git)
438 return -1;
439
440 count_bitmap_commit_list(bitmap_git, &commit_count,
441 revs->tree_objects ? &tree_count : NULL,
442 revs->blob_objects ? &blob_count : NULL,
443 revs->tag_objects ? &tag_count : NULL);
444 if (max_count >= 0 && max_count < commit_count)
445 commit_count = max_count;
446
447 printf("%d\n", commit_count + tree_count + blob_count + tag_count);
448 free_bitmap_index(bitmap_git);
449 return 0;
450 }
451
452 static int try_bitmap_traversal(struct rev_info *revs,
453 struct list_objects_filter_options *filter)
454 {
455 struct bitmap_index *bitmap_git;
456
457 /*
458 * We can't use a bitmap result with a traversal limit, since the set
459 * of commits we'd get would be essentially random.
460 */
461 if (revs->max_count >= 0)
462 return -1;
463
464 bitmap_git = prepare_bitmap_walk(revs, filter);
465 if (!bitmap_git)
466 return -1;
467
468 traverse_bitmap_commit_list(bitmap_git, revs, &show_object_fast);
469 free_bitmap_index(bitmap_git);
470 return 0;
471 }
472
473 static int try_bitmap_disk_usage(struct rev_info *revs,
474 struct list_objects_filter_options *filter)
475 {
476 struct bitmap_index *bitmap_git;
477
478 if (!show_disk_usage)
479 return -1;
480
481 bitmap_git = prepare_bitmap_walk(revs, filter);
482 if (!bitmap_git)
483 return -1;
484
485 printf("%"PRIuMAX"\n",
486 (uintmax_t)get_disk_usage_from_bitmap(bitmap_git, revs));
487 return 0;
488 }
489
490 int cmd_rev_list(int argc, const char **argv, const char *prefix)
491 {
492 struct rev_info revs;
493 struct rev_list_info info;
494 struct setup_revision_opt s_r_opt = {
495 .allow_exclude_promisor_objects = 1,
496 };
497 int i;
498 int bisect_list = 0;
499 int bisect_show_vars = 0;
500 int bisect_find_all = 0;
501 int use_bitmap_index = 0;
502 const char *show_progress = NULL;
503
504 if (argc == 2 && !strcmp(argv[1], "-h"))
505 usage(rev_list_usage);
506
507 git_config(git_default_config, NULL);
508 repo_init_revisions(the_repository, &revs, prefix);
509 revs.abbrev = DEFAULT_ABBREV;
510 revs.commit_format = CMIT_FMT_UNSPECIFIED;
511
512 /*
513 * Scan the argument list before invoking setup_revisions(), so that we
514 * know if fetch_if_missing needs to be set to 0.
515 *
516 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
517 * by not crossing the boundary from realized objects to promisor
518 * objects.
519 *
520 * Let "--missing" to conditionally set fetch_if_missing.
521 */
522 for (i = 1; i < argc; i++) {
523 const char *arg = argv[i];
524 if (!strcmp(arg, "--exclude-promisor-objects")) {
525 fetch_if_missing = 0;
526 revs.exclude_promisor_objects = 1;
527 break;
528 }
529 }
530 for (i = 1; i < argc; i++) {
531 const char *arg = argv[i];
532 if (skip_prefix(arg, "--missing=", &arg)) {
533 if (revs.exclude_promisor_objects)
534 die(_("cannot combine --exclude-promisor-objects and --missing"));
535 if (parse_missing_action_value(arg))
536 break;
537 }
538 }
539
540 if (arg_missing_action)
541 revs.do_not_die_on_missing_tree = 1;
542
543 argc = setup_revisions(argc, argv, &revs, &s_r_opt);
544
545 memset(&info, 0, sizeof(info));
546 info.revs = &revs;
547 if (revs.bisect)
548 bisect_list = 1;
549
550 if (revs.diffopt.flags.quick)
551 info.flags |= REV_LIST_QUIET;
552 for (i = 1 ; i < argc; i++) {
553 const char *arg = argv[i];
554
555 if (!strcmp(arg, "--header")) {
556 revs.verbose_header = 1;
557 continue;
558 }
559 if (!strcmp(arg, "--timestamp")) {
560 info.show_timestamp = 1;
561 continue;
562 }
563 if (!strcmp(arg, "--bisect")) {
564 bisect_list = 1;
565 continue;
566 }
567 if (!strcmp(arg, "--bisect-all")) {
568 bisect_list = 1;
569 bisect_find_all = 1;
570 info.flags |= BISECT_SHOW_ALL;
571 revs.show_decorations = 1;
572 continue;
573 }
574 if (!strcmp(arg, "--bisect-vars")) {
575 bisect_list = 1;
576 bisect_show_vars = 1;
577 continue;
578 }
579 if (!strcmp(arg, "--use-bitmap-index")) {
580 use_bitmap_index = 1;
581 continue;
582 }
583 if (!strcmp(arg, "--test-bitmap")) {
584 test_bitmap_walk(&revs);
585 return 0;
586 }
587 if (skip_prefix(arg, "--progress=", &arg)) {
588 show_progress = arg;
589 continue;
590 }
591
592 if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
593 parse_list_objects_filter(&filter_options, arg);
594 if (filter_options.choice && !revs.blob_objects)
595 die(_("object filtering requires --objects"));
596 continue;
597 }
598 if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
599 list_objects_filter_set_no_filter(&filter_options);
600 continue;
601 }
602 if (!strcmp(arg, "--filter-print-omitted")) {
603 arg_print_omitted = 1;
604 continue;
605 }
606
607 if (!strcmp(arg, "--exclude-promisor-objects"))
608 continue; /* already handled above */
609 if (skip_prefix(arg, "--missing=", &arg))
610 continue; /* already handled above */
611
612 if (!strcmp(arg, ("--no-object-names"))) {
613 arg_show_object_names = 0;
614 continue;
615 }
616
617 if (!strcmp(arg, ("--object-names"))) {
618 arg_show_object_names = 1;
619 continue;
620 }
621
622 if (!strcmp(arg, "--disk-usage")) {
623 show_disk_usage = 1;
624 info.flags |= REV_LIST_QUIET;
625 continue;
626 }
627
628 usage(rev_list_usage);
629
630 }
631 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
632 /* The command line has a --pretty */
633 info.hdr_termination = '\n';
634 if (revs.commit_format == CMIT_FMT_ONELINE)
635 info.header_prefix = "";
636 else
637 info.header_prefix = "commit ";
638 }
639 else if (revs.verbose_header)
640 /* Only --header was specified */
641 revs.commit_format = CMIT_FMT_RAW;
642
643 if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
644 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
645 !revs.pending.nr) &&
646 !revs.rev_input_given && !revs.read_from_stdin) ||
647 revs.diff)
648 usage(rev_list_usage);
649
650 if (revs.show_notes)
651 die(_("rev-list does not support display of notes"));
652
653 if (revs.count &&
654 (revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
655 (revs.left_right || revs.cherry_mark))
656 die(_("marked counting is incompatible with --objects"));
657
658 save_commit_buffer = (revs.verbose_header ||
659 revs.grep_filter.pattern_list ||
660 revs.grep_filter.header_list);
661 if (bisect_list)
662 revs.limited = 1;
663
664 if (show_progress)
665 progress = start_delayed_progress(show_progress, 0);
666
667 if (use_bitmap_index) {
668 if (!try_bitmap_count(&revs, &filter_options))
669 return 0;
670 if (!try_bitmap_disk_usage(&revs, &filter_options))
671 return 0;
672 if (!try_bitmap_traversal(&revs, &filter_options))
673 return 0;
674 }
675
676 if (prepare_revision_walk(&revs))
677 die("revision walk setup failed");
678 if (revs.tree_objects)
679 mark_edges_uninteresting(&revs, show_edge, 0);
680
681 if (bisect_list) {
682 int reaches, all;
683 unsigned bisect_flags = 0;
684
685 if (bisect_find_all)
686 bisect_flags |= FIND_BISECTION_ALL;
687
688 if (revs.first_parent_only)
689 bisect_flags |= FIND_BISECTION_FIRST_PARENT_ONLY;
690
691 find_bisection(&revs.commits, &reaches, &all, bisect_flags);
692
693 if (bisect_show_vars)
694 return show_bisect_vars(&info, reaches, all);
695 }
696
697 if (arg_print_omitted)
698 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
699 if (arg_missing_action == MA_PRINT)
700 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
701
702 traverse_commit_list_filtered(
703 &filter_options, &revs, show_commit, show_object, &info,
704 (arg_print_omitted ? &omitted_objects : NULL));
705
706 if (arg_print_omitted) {
707 struct oidset_iter iter;
708 struct object_id *oid;
709 oidset_iter_init(&omitted_objects, &iter);
710 while ((oid = oidset_iter_next(&iter)))
711 printf("~%s\n", oid_to_hex(oid));
712 oidset_clear(&omitted_objects);
713 }
714 if (arg_missing_action == MA_PRINT) {
715 struct oidset_iter iter;
716 struct object_id *oid;
717 oidset_iter_init(&missing_objects, &iter);
718 while ((oid = oidset_iter_next(&iter)))
719 printf("?%s\n", oid_to_hex(oid));
720 oidset_clear(&missing_objects);
721 }
722
723 stop_progress(&progress);
724
725 if (revs.count) {
726 if (revs.left_right && revs.cherry_mark)
727 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
728 else if (revs.left_right)
729 printf("%d\t%d\n", revs.count_left, revs.count_right);
730 else if (revs.cherry_mark)
731 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
732 else
733 printf("%d\n", revs.count_left + revs.count_right);
734 }
735
736 if (show_disk_usage)
737 printf("%"PRIuMAX"\n", (uintmax_t)total_disk_usage);
738
739 return 0;
740 }