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