6 #include "list-objects.h"
7 #include "list-objects-filter.h"
8 #include "list-objects-filter-options.h"
10 #include "object-store.h"
12 #include "pack-bitmap.h"
18 #include "reflog-walk.h"
22 static const char rev_list_usage
[] =
23 "git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
26 " --max-age=<epoch>\n"
27 " --min-age=<epoch>\n"
30 " --min-parents=<n>\n"
32 " --max-parents=<n>\n"
45 " formatting output:\n"
48 " --objects | --objects-edge\n"
50 " --header | --pretty\n"
51 " --[no-]object-names\n"
52 " --abbrev=<n> | --no-abbrev\n"
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
;
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 int show_disk_usage
;
84 static off_t total_disk_usage
;
86 static off_t
get_object_disk_usage(struct object
*obj
)
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
));
96 static void finish_commit(struct commit
*commit
);
97 static void show_commit(struct commit
*commit
, void *data
)
99 struct rev_list_info
*info
= data
;
100 struct rev_info
*revs
= info
->revs
;
102 display_progress(progress
, ++progress_counter
);
105 total_disk_usage
+= get_object_disk_usage(&commit
->object
);
107 if (info
->flags
& REV_LIST_QUIET
) {
108 finish_commit(commit
);
112 graph_show_commit(revs
->graph
);
115 if (commit
->object
.flags
& PATCHSAME
)
117 else if (commit
->object
.flags
& SYMMETRIC_LEFT
)
121 finish_commit(commit
);
125 if (info
->show_timestamp
)
126 printf("%"PRItime
" ", commit
->date
);
127 if (info
->header_prefix
)
128 fputs(info
->header_prefix
, stdout
);
130 if (revs
->include_header
) {
132 fputs(get_revision_mark(revs
, commit
), stdout
);
133 if (revs
->abbrev_commit
&& revs
->abbrev
)
134 fputs(find_unique_abbrev(&commit
->object
.oid
, revs
->abbrev
),
137 fputs(oid_to_hex(&commit
->object
.oid
), stdout
);
139 if (revs
->print_parents
) {
140 struct commit_list
*parents
= commit
->parents
;
142 printf(" %s", oid_to_hex(&parents
->item
->object
.oid
));
143 parents
= parents
->next
;
146 if (revs
->children
.name
) {
147 struct commit_list
*children
;
149 children
= lookup_decoration(&revs
->children
, &commit
->object
);
151 printf(" %s", oid_to_hex(&children
->item
->object
.oid
));
152 children
= children
->next
;
155 show_decorations(revs
, commit
);
156 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
158 else if (revs
->include_header
)
161 if (revs
->verbose_header
) {
162 struct strbuf buf
= STRBUF_INIT
;
163 struct pretty_print_context ctx
= {0};
164 ctx
.abbrev
= revs
->abbrev
;
165 ctx
.date_mode
= revs
->date_mode
;
166 ctx
.date_mode_explicit
= revs
->date_mode_explicit
;
167 ctx
.fmt
= revs
->commit_format
;
168 ctx
.output_encoding
= get_log_output_encoding();
169 ctx
.color
= revs
->diffopt
.use_color
;
170 pretty_print_commit(&ctx
, commit
, &buf
);
172 if (revs
->commit_format
!= CMIT_FMT_ONELINE
)
173 graph_show_oneline(revs
->graph
);
175 graph_show_commit_msg(revs
->graph
, stdout
, &buf
);
178 * Add a newline after the commit message.
180 * Usually, this newline produces a blank
181 * padding line between entries, in which case
182 * we need to add graph padding on this line.
184 * However, the commit message may not end in a
185 * newline. In this case the newline simply
186 * ends the last line of the commit message,
187 * and we don't need any graph output. (This
188 * always happens with CMIT_FMT_ONELINE, and it
189 * happens with CMIT_FMT_USERFORMAT when the
190 * format doesn't explicitly end in a newline.)
192 if (buf
.len
&& buf
.buf
[buf
.len
- 1] == '\n')
193 graph_show_padding(revs
->graph
);
194 putchar(info
->hdr_termination
);
197 * If the message buffer is empty, just show
198 * the rest of the graph output for this
201 if (graph_show_remainder(revs
->graph
))
203 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
206 strbuf_release(&buf
);
208 if (graph_show_remainder(revs
->graph
))
211 maybe_flush_or_die(stdout
, "stdout");
212 finish_commit(commit
);
215 static void finish_commit(struct commit
*commit
)
217 if (commit
->parents
) {
218 free_commit_list(commit
->parents
);
219 commit
->parents
= NULL
;
221 free_commit_buffer(the_repository
->parsed_objects
,
225 static inline void finish_object__ma(struct object
*obj
)
228 * Whether or not we try to dynamically fetch missing objects
229 * from the server, we currently DO NOT have the object. We
230 * can either print, allow (ignore), or conditionally allow
233 switch (arg_missing_action
) {
235 die("missing %s object '%s'",
236 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
243 oidset_insert(&missing_objects
, &obj
->oid
);
246 case MA_ALLOW_PROMISOR
:
247 if (is_promisor_object(&obj
->oid
))
249 die("unexpected missing %s object '%s'",
250 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
254 BUG("unhandled missing_action");
259 static int finish_object(struct object
*obj
, const char *name
, void *cb_data
)
261 struct rev_list_info
*info
= cb_data
;
262 if (oid_object_info_extended(the_repository
, &obj
->oid
, NULL
, 0) < 0) {
263 finish_object__ma(obj
);
266 if (info
->revs
->verify_objects
&& !obj
->parsed
&& obj
->type
!= OBJ_COMMIT
)
267 parse_object(the_repository
, &obj
->oid
);
271 static void show_object(struct object
*obj
, const char *name
, void *cb_data
)
273 struct rev_list_info
*info
= cb_data
;
274 struct rev_info
*revs
= info
->revs
;
276 if (finish_object(obj
, name
, cb_data
))
278 display_progress(progress
, ++progress_counter
);
280 total_disk_usage
+= get_object_disk_usage(obj
);
281 if (info
->flags
& REV_LIST_QUIET
)
286 * The object count is always accumulated in the .count_right
287 * field for traversal that is not a left-right traversal,
288 * and cmd_rev_list() made sure that a .count request that
289 * wants to count non-commit objects, which is handled by
290 * the show_object() callback, does not ask for .left_right.
296 if (arg_show_object_names
)
297 show_object_with_name(stdout
, obj
, name
);
299 printf("%s\n", oid_to_hex(&obj
->oid
));
302 static void show_edge(struct commit
*commit
)
304 printf("-%s\n", oid_to_hex(&commit
->object
.oid
));
307 static void print_var_str(const char *var
, const char *val
)
309 printf("%s='%s'\n", var
, val
);
312 static void print_var_int(const char *var
, int val
)
314 printf("%s=%d\n", var
, val
);
317 static int show_bisect_vars(struct rev_list_info
*info
, int reaches
, int all
)
319 int cnt
, flags
= info
->flags
;
320 char hex
[GIT_MAX_HEXSZ
+ 1] = "";
321 struct commit_list
*tried
;
322 struct rev_info
*revs
= info
->revs
;
327 revs
->commits
= filter_skipped(revs
->commits
, &tried
,
328 flags
& BISECT_SHOW_ALL
,
332 * revs->commits can reach "reaches" commits among
333 * "all" commits. If it is good, then there are
334 * (all-reaches) commits left to be bisected.
335 * On the other hand, if it is bad, then the set
336 * to bisect is "reaches".
337 * A bisect set of size N has (N-1) commits further
338 * to test, as we already know one bad one.
345 oid_to_hex_r(hex
, &revs
->commits
->item
->object
.oid
);
347 if (flags
& BISECT_SHOW_ALL
) {
348 traverse_commit_list(revs
, show_commit
, show_object
, info
);
352 print_var_str("bisect_rev", hex
);
353 print_var_int("bisect_nr", cnt
- 1);
354 print_var_int("bisect_good", all
- reaches
- 1);
355 print_var_int("bisect_bad", reaches
- 1);
356 print_var_int("bisect_all", all
);
357 print_var_int("bisect_steps", estimate_bisect_steps(all
));
362 static int show_object_fast(
363 const struct object_id
*oid
,
364 enum object_type type
,
367 struct packed_git
*found_pack
,
370 fprintf(stdout
, "%s\n", oid_to_hex(oid
));
374 static inline int parse_missing_action_value(const char *value
)
376 if (!strcmp(value
, "error")) {
377 arg_missing_action
= MA_ERROR
;
381 if (!strcmp(value
, "allow-any")) {
382 arg_missing_action
= MA_ALLOW_ANY
;
383 fetch_if_missing
= 0;
387 if (!strcmp(value
, "print")) {
388 arg_missing_action
= MA_PRINT
;
389 fetch_if_missing
= 0;
393 if (!strcmp(value
, "allow-promisor")) {
394 arg_missing_action
= MA_ALLOW_PROMISOR
;
395 fetch_if_missing
= 0;
402 static int try_bitmap_count(struct rev_info
*revs
,
403 struct list_objects_filter_options
*filter
,
404 int filter_provided_objects
)
406 uint32_t commit_count
= 0,
411 struct bitmap_index
*bitmap_git
;
413 /* This function only handles counting, not general traversal. */
418 * A bitmap result can't know left/right, etc, because we don't
421 if (revs
->left_right
|| revs
->cherry_mark
)
425 * If we're counting reachable objects, we can't handle a max count of
426 * commits to traverse, since we don't know which objects go with which
429 if (revs
->max_count
>= 0 &&
430 (revs
->tag_objects
|| revs
->tree_objects
|| revs
->blob_objects
))
434 * This must be saved before doing any walking, since the revision
435 * machinery will count it down to zero while traversing.
437 max_count
= revs
->max_count
;
439 bitmap_git
= prepare_bitmap_walk(revs
, filter
, filter_provided_objects
);
443 count_bitmap_commit_list(bitmap_git
, &commit_count
,
444 revs
->tree_objects
? &tree_count
: NULL
,
445 revs
->blob_objects
? &blob_count
: NULL
,
446 revs
->tag_objects
? &tag_count
: NULL
);
447 if (max_count
>= 0 && max_count
< commit_count
)
448 commit_count
= max_count
;
450 printf("%d\n", commit_count
+ tree_count
+ blob_count
+ tag_count
);
451 free_bitmap_index(bitmap_git
);
455 static int try_bitmap_traversal(struct rev_info
*revs
,
456 struct list_objects_filter_options
*filter
,
457 int filter_provided_objects
)
459 struct bitmap_index
*bitmap_git
;
462 * We can't use a bitmap result with a traversal limit, since the set
463 * of commits we'd get would be essentially random.
465 if (revs
->max_count
>= 0)
468 bitmap_git
= prepare_bitmap_walk(revs
, filter
, filter_provided_objects
);
472 traverse_bitmap_commit_list(bitmap_git
, revs
, &show_object_fast
);
473 free_bitmap_index(bitmap_git
);
477 static int try_bitmap_disk_usage(struct rev_info
*revs
,
478 struct list_objects_filter_options
*filter
,
479 int filter_provided_objects
)
481 struct bitmap_index
*bitmap_git
;
483 if (!show_disk_usage
)
486 bitmap_git
= prepare_bitmap_walk(revs
, filter
, filter_provided_objects
);
490 printf("%"PRIuMAX
"\n",
491 (uintmax_t)get_disk_usage_from_bitmap(bitmap_git
, revs
));
495 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
497 struct rev_info revs
;
498 struct rev_list_info info
;
499 struct setup_revision_opt s_r_opt
= {
500 .allow_exclude_promisor_objects
= 1,
504 int bisect_show_vars
= 0;
505 int bisect_find_all
= 0;
506 int use_bitmap_index
= 0;
507 int filter_provided_objects
= 0;
508 const char *show_progress
= NULL
;
510 if (argc
== 2 && !strcmp(argv
[1], "-h"))
511 usage(rev_list_usage
);
513 git_config(git_default_config
, NULL
);
514 repo_init_revisions(the_repository
, &revs
, prefix
);
515 revs
.abbrev
= DEFAULT_ABBREV
;
516 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
517 revs
.include_header
= 1;
520 * Scan the argument list before invoking setup_revisions(), so that we
521 * know if fetch_if_missing needs to be set to 0.
523 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
524 * by not crossing the boundary from realized objects to promisor
527 * Let "--missing" to conditionally set fetch_if_missing.
529 for (i
= 1; i
< argc
; i
++) {
530 const char *arg
= argv
[i
];
531 if (!strcmp(arg
, "--exclude-promisor-objects")) {
532 fetch_if_missing
= 0;
533 revs
.exclude_promisor_objects
= 1;
537 for (i
= 1; i
< argc
; i
++) {
538 const char *arg
= argv
[i
];
539 if (skip_prefix(arg
, "--missing=", &arg
)) {
540 if (revs
.exclude_promisor_objects
)
541 die(_("cannot combine --exclude-promisor-objects and --missing"));
542 if (parse_missing_action_value(arg
))
547 if (arg_missing_action
)
548 revs
.do_not_die_on_missing_tree
= 1;
550 argc
= setup_revisions(argc
, argv
, &revs
, &s_r_opt
);
552 memset(&info
, 0, sizeof(info
));
557 if (revs
.diffopt
.flags
.quick
)
558 info
.flags
|= REV_LIST_QUIET
;
559 for (i
= 1 ; i
< argc
; i
++) {
560 const char *arg
= argv
[i
];
562 if (!strcmp(arg
, "--header")) {
563 revs
.verbose_header
= 1;
566 if (!strcmp(arg
, "--timestamp")) {
567 info
.show_timestamp
= 1;
570 if (!strcmp(arg
, "--bisect")) {
574 if (!strcmp(arg
, "--bisect-all")) {
577 info
.flags
|= BISECT_SHOW_ALL
;
578 revs
.show_decorations
= 1;
581 if (!strcmp(arg
, "--bisect-vars")) {
583 bisect_show_vars
= 1;
586 if (!strcmp(arg
, "--use-bitmap-index")) {
587 use_bitmap_index
= 1;
590 if (!strcmp(arg
, "--test-bitmap")) {
591 test_bitmap_walk(&revs
);
594 if (skip_prefix(arg
, "--progress=", &arg
)) {
599 if (skip_prefix(arg
, ("--" CL_ARG__FILTER
"="), &arg
)) {
600 parse_list_objects_filter(&filter_options
, arg
);
601 if (filter_options
.choice
&& !revs
.blob_objects
)
602 die(_("object filtering requires --objects"));
605 if (!strcmp(arg
, ("--no-" CL_ARG__FILTER
))) {
606 list_objects_filter_set_no_filter(&filter_options
);
609 if (!strcmp(arg
, "--filter-provided-objects")) {
610 filter_provided_objects
= 1;
613 if (!strcmp(arg
, "--filter-print-omitted")) {
614 arg_print_omitted
= 1;
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;
628 if (!strcmp(arg
, ("--object-names"))) {
629 arg_show_object_names
= 1;
633 if (!strcmp(arg
, ("--commit-header"))) {
634 revs
.include_header
= 1;
638 if (!strcmp(arg
, ("--no-commit-header"))) {
639 revs
.include_header
= 0;
643 if (!strcmp(arg
, "--disk-usage")) {
645 info
.flags
|= REV_LIST_QUIET
;
649 usage(rev_list_usage
);
652 if (revs
.commit_format
!= CMIT_FMT_USERFORMAT
)
653 revs
.include_header
= 1;
654 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
655 /* The command line has a --pretty */
656 info
.hdr_termination
= '\n';
657 if (revs
.commit_format
== CMIT_FMT_ONELINE
|| !revs
.include_header
)
658 info
.header_prefix
= "";
660 info
.header_prefix
= "commit ";
662 else if (revs
.verbose_header
)
663 /* Only --header was specified */
664 revs
.commit_format
= CMIT_FMT_RAW
;
666 if ((!revs
.commits
&& reflog_walk_empty(revs
.reflog_info
) &&
667 (!(revs
.tag_objects
|| revs
.tree_objects
|| revs
.blob_objects
) &&
669 !revs
.rev_input_given
&& !revs
.read_from_stdin
) ||
671 usage(rev_list_usage
);
674 die(_("rev-list does not support display of notes"));
677 (revs
.tag_objects
|| revs
.tree_objects
|| revs
.blob_objects
) &&
678 (revs
.left_right
|| revs
.cherry_mark
))
679 die(_("marked counting is incompatible with --objects"));
681 save_commit_buffer
= (revs
.verbose_header
||
682 revs
.grep_filter
.pattern_list
||
683 revs
.grep_filter
.header_list
);
688 progress
= start_delayed_progress(show_progress
, 0);
690 if (use_bitmap_index
) {
691 if (!try_bitmap_count(&revs
, &filter_options
, filter_provided_objects
))
693 if (!try_bitmap_disk_usage(&revs
, &filter_options
, filter_provided_objects
))
695 if (!try_bitmap_traversal(&revs
, &filter_options
, filter_provided_objects
))
699 if (prepare_revision_walk(&revs
))
700 die("revision walk setup failed");
701 if (revs
.tree_objects
)
702 mark_edges_uninteresting(&revs
, show_edge
, 0);
706 unsigned bisect_flags
= 0;
709 bisect_flags
|= FIND_BISECTION_ALL
;
711 if (revs
.first_parent_only
)
712 bisect_flags
|= FIND_BISECTION_FIRST_PARENT_ONLY
;
714 find_bisection(&revs
.commits
, &reaches
, &all
, bisect_flags
);
716 if (bisect_show_vars
)
717 return show_bisect_vars(&info
, reaches
, all
);
720 if (filter_provided_objects
) {
721 struct commit_list
*c
;
722 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
723 struct object_array_entry
*pending
= revs
.pending
.objects
+ i
;
724 pending
->item
->flags
|= NOT_USER_GIVEN
;
726 for (c
= revs
.commits
; c
; c
= c
->next
)
727 c
->item
->object
.flags
|= NOT_USER_GIVEN
;
730 if (arg_print_omitted
)
731 oidset_init(&omitted_objects
, DEFAULT_OIDSET_SIZE
);
732 if (arg_missing_action
== MA_PRINT
)
733 oidset_init(&missing_objects
, DEFAULT_OIDSET_SIZE
);
735 traverse_commit_list_filtered(
736 &filter_options
, &revs
, show_commit
, show_object
, &info
,
737 (arg_print_omitted
? &omitted_objects
: NULL
));
739 if (arg_print_omitted
) {
740 struct oidset_iter iter
;
741 struct object_id
*oid
;
742 oidset_iter_init(&omitted_objects
, &iter
);
743 while ((oid
= oidset_iter_next(&iter
)))
744 printf("~%s\n", oid_to_hex(oid
));
745 oidset_clear(&omitted_objects
);
747 if (arg_missing_action
== MA_PRINT
) {
748 struct oidset_iter iter
;
749 struct object_id
*oid
;
750 oidset_iter_init(&missing_objects
, &iter
);
751 while ((oid
= oidset_iter_next(&iter
)))
752 printf("?%s\n", oid_to_hex(oid
));
753 oidset_clear(&missing_objects
);
756 stop_progress(&progress
);
759 if (revs
.left_right
&& revs
.cherry_mark
)
760 printf("%d\t%d\t%d\n", revs
.count_left
, revs
.count_right
, revs
.count_same
);
761 else if (revs
.left_right
)
762 printf("%d\t%d\n", revs
.count_left
, revs
.count_right
);
763 else if (revs
.cherry_mark
)
764 printf("%d\t%d\n", revs
.count_left
+ revs
.count_right
, revs
.count_same
);
766 printf("%d\n", revs
.count_left
+ revs
.count_right
);
770 printf("%"PRIuMAX
"\n", (uintmax_t)total_disk_usage
);