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 [<options>] <commit>... [--] [<path>...]\n"
27 " --max-age=<epoch>\n"
28 " --min-age=<epoch>\n"
31 " --min-parents=<n>\n"
33 " --max-parents=<n>\n"
46 " formatting output:\n"
49 " --objects | --objects-edge\n"
50 " --disk-usage[=human]\n"
52 " --header | --pretty\n"
53 " --[no-]object-names\n"
54 " --abbrev=<n> | --no-abbrev\n"
64 static struct progress
*progress
;
65 static unsigned progress_counter
;
67 static struct oidset omitted_objects
;
68 static int arg_print_omitted
; /* print objects omitted by filter */
70 static struct oidset missing_objects
;
72 MA_ERROR
= 0, /* fail if any missing objects are encountered */
73 MA_ALLOW_ANY
, /* silently allow ALL missing objects */
74 MA_PRINT
, /* print ALL missing objects in special section */
75 MA_ALLOW_PROMISOR
, /* silently allow all missing PROMISOR objects */
77 static enum missing_action arg_missing_action
;
79 /* display only the oid of each object encountered */
80 static int arg_show_object_names
= 1;
82 #define DEFAULT_OIDSET_SIZE (16*1024)
84 static int show_disk_usage
;
85 static off_t total_disk_usage
;
86 static int human_readable
;
88 static off_t
get_object_disk_usage(struct object
*obj
)
91 struct object_info oi
= OBJECT_INFO_INIT
;
92 oi
.disk_sizep
= &size
;
93 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
94 die(_("unable to get disk usage of %s"), oid_to_hex(&obj
->oid
));
98 static void finish_commit(struct commit
*commit
);
99 static void show_commit(struct commit
*commit
, void *data
)
101 struct rev_list_info
*info
= data
;
102 struct rev_info
*revs
= info
->revs
;
104 display_progress(progress
, ++progress_counter
);
107 total_disk_usage
+= get_object_disk_usage(&commit
->object
);
109 if (info
->flags
& REV_LIST_QUIET
) {
110 finish_commit(commit
);
114 graph_show_commit(revs
->graph
);
117 if (commit
->object
.flags
& PATCHSAME
)
119 else if (commit
->object
.flags
& SYMMETRIC_LEFT
)
123 finish_commit(commit
);
127 if (info
->show_timestamp
)
128 printf("%"PRItime
" ", commit
->date
);
129 if (info
->header_prefix
)
130 fputs(info
->header_prefix
, stdout
);
132 if (revs
->include_header
) {
134 fputs(get_revision_mark(revs
, commit
), stdout
);
135 if (revs
->abbrev_commit
&& revs
->abbrev
)
136 fputs(find_unique_abbrev(&commit
->object
.oid
, revs
->abbrev
),
139 fputs(oid_to_hex(&commit
->object
.oid
), stdout
);
141 if (revs
->print_parents
) {
142 struct commit_list
*parents
= commit
->parents
;
144 printf(" %s", oid_to_hex(&parents
->item
->object
.oid
));
145 parents
= parents
->next
;
148 if (revs
->children
.name
) {
149 struct commit_list
*children
;
151 children
= lookup_decoration(&revs
->children
, &commit
->object
);
153 printf(" %s", oid_to_hex(&children
->item
->object
.oid
));
154 children
= children
->next
;
157 show_decorations(revs
, commit
);
158 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
160 else if (revs
->include_header
)
163 if (revs
->verbose_header
) {
164 struct strbuf buf
= STRBUF_INIT
;
165 struct pretty_print_context ctx
= {0};
166 ctx
.abbrev
= revs
->abbrev
;
167 ctx
.date_mode
= revs
->date_mode
;
168 ctx
.date_mode_explicit
= revs
->date_mode_explicit
;
169 ctx
.fmt
= revs
->commit_format
;
170 ctx
.output_encoding
= get_log_output_encoding();
171 ctx
.color
= revs
->diffopt
.use_color
;
172 pretty_print_commit(&ctx
, commit
, &buf
);
174 if (revs
->commit_format
!= CMIT_FMT_ONELINE
)
175 graph_show_oneline(revs
->graph
);
177 graph_show_commit_msg(revs
->graph
, stdout
, &buf
);
180 * Add a newline after the commit message.
182 * Usually, this newline produces a blank
183 * padding line between entries, in which case
184 * we need to add graph padding on this line.
186 * However, the commit message may not end in a
187 * newline. In this case the newline simply
188 * ends the last line of the commit message,
189 * and we don't need any graph output. (This
190 * always happens with CMIT_FMT_ONELINE, and it
191 * happens with CMIT_FMT_USERFORMAT when the
192 * format doesn't explicitly end in a newline.)
194 if (buf
.len
&& buf
.buf
[buf
.len
- 1] == '\n')
195 graph_show_padding(revs
->graph
);
196 putchar(info
->hdr_termination
);
199 * If the message buffer is empty, just show
200 * the rest of the graph output for this
203 if (graph_show_remainder(revs
->graph
))
205 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
208 strbuf_release(&buf
);
210 if (graph_show_remainder(revs
->graph
))
213 maybe_flush_or_die(stdout
, "stdout");
214 finish_commit(commit
);
217 static void finish_commit(struct commit
*commit
)
219 free_commit_list(commit
->parents
);
220 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 void print_disk_usage(off_t size
)
376 struct strbuf sb
= STRBUF_INIT
;
378 strbuf_humanise_bytes(&sb
, size
);
380 strbuf_addf(&sb
, "%"PRIuMAX
, (uintmax_t)size
);
385 static inline int parse_missing_action_value(const char *value
)
387 if (!strcmp(value
, "error")) {
388 arg_missing_action
= MA_ERROR
;
392 if (!strcmp(value
, "allow-any")) {
393 arg_missing_action
= MA_ALLOW_ANY
;
394 fetch_if_missing
= 0;
398 if (!strcmp(value
, "print")) {
399 arg_missing_action
= MA_PRINT
;
400 fetch_if_missing
= 0;
404 if (!strcmp(value
, "allow-promisor")) {
405 arg_missing_action
= MA_ALLOW_PROMISOR
;
406 fetch_if_missing
= 0;
413 static int try_bitmap_count(struct rev_info
*revs
,
414 int filter_provided_objects
)
416 uint32_t commit_count
= 0,
421 struct bitmap_index
*bitmap_git
;
423 /* This function only handles counting, not general traversal. */
428 * A bitmap result can't know left/right, etc, because we don't
431 if (revs
->left_right
|| revs
->cherry_mark
)
435 * If we're counting reachable objects, we can't handle a max count of
436 * commits to traverse, since we don't know which objects go with which
439 if (revs
->max_count
>= 0 &&
440 (revs
->tag_objects
|| revs
->tree_objects
|| revs
->blob_objects
))
444 * This must be saved before doing any walking, since the revision
445 * machinery will count it down to zero while traversing.
447 max_count
= revs
->max_count
;
449 bitmap_git
= prepare_bitmap_walk(revs
, filter_provided_objects
);
453 count_bitmap_commit_list(bitmap_git
, &commit_count
,
454 revs
->tree_objects
? &tree_count
: NULL
,
455 revs
->blob_objects
? &blob_count
: NULL
,
456 revs
->tag_objects
? &tag_count
: NULL
);
457 if (max_count
>= 0 && max_count
< commit_count
)
458 commit_count
= max_count
;
460 printf("%d\n", commit_count
+ tree_count
+ blob_count
+ tag_count
);
461 free_bitmap_index(bitmap_git
);
465 static int try_bitmap_traversal(struct rev_info
*revs
,
466 int filter_provided_objects
)
468 struct bitmap_index
*bitmap_git
;
471 * We can't use a bitmap result with a traversal limit, since the set
472 * of commits we'd get would be essentially random.
474 if (revs
->max_count
>= 0)
477 bitmap_git
= prepare_bitmap_walk(revs
, filter_provided_objects
);
481 traverse_bitmap_commit_list(bitmap_git
, revs
, &show_object_fast
);
482 free_bitmap_index(bitmap_git
);
486 static int try_bitmap_disk_usage(struct rev_info
*revs
,
487 int filter_provided_objects
)
489 struct bitmap_index
*bitmap_git
;
490 off_t size_from_bitmap
;
492 if (!show_disk_usage
)
495 bitmap_git
= prepare_bitmap_walk(revs
, filter_provided_objects
);
499 size_from_bitmap
= get_disk_usage_from_bitmap(bitmap_git
, revs
);
500 print_disk_usage(size_from_bitmap
);
504 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
506 struct rev_info revs
;
507 struct rev_list_info info
;
508 struct setup_revision_opt s_r_opt
= {
509 .allow_exclude_promisor_objects
= 1,
513 int bisect_show_vars
= 0;
514 int bisect_find_all
= 0;
515 int use_bitmap_index
= 0;
516 int filter_provided_objects
= 0;
517 const char *show_progress
= NULL
;
520 if (argc
== 2 && !strcmp(argv
[1], "-h"))
521 usage(rev_list_usage
);
523 git_config(git_default_config
, NULL
);
524 repo_init_revisions(the_repository
, &revs
, prefix
);
525 revs
.abbrev
= DEFAULT_ABBREV
;
526 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
527 revs
.include_header
= 1;
530 * Scan the argument list before invoking setup_revisions(), so that we
531 * know if fetch_if_missing needs to be set to 0.
533 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
534 * by not crossing the boundary from realized objects to promisor
537 * Let "--missing" to conditionally set fetch_if_missing.
539 for (i
= 1; i
< argc
; i
++) {
540 const char *arg
= argv
[i
];
541 if (!strcmp(arg
, "--exclude-promisor-objects")) {
542 fetch_if_missing
= 0;
543 revs
.exclude_promisor_objects
= 1;
547 for (i
= 1; i
< argc
; i
++) {
548 const char *arg
= argv
[i
];
549 if (skip_prefix(arg
, "--missing=", &arg
)) {
550 if (revs
.exclude_promisor_objects
)
551 die(_("options '%s' and '%s' cannot be used together"), "--exclude-promisor-objects", "--missing");
552 if (parse_missing_action_value(arg
))
557 if (arg_missing_action
)
558 revs
.do_not_die_on_missing_tree
= 1;
560 argc
= setup_revisions(argc
, argv
, &revs
, &s_r_opt
);
562 memset(&info
, 0, sizeof(info
));
567 if (revs
.diffopt
.flags
.quick
)
568 info
.flags
|= REV_LIST_QUIET
;
569 for (i
= 1 ; i
< argc
; i
++) {
570 const char *arg
= argv
[i
];
572 if (!strcmp(arg
, "--header")) {
573 revs
.verbose_header
= 1;
576 if (!strcmp(arg
, "--timestamp")) {
577 info
.show_timestamp
= 1;
580 if (!strcmp(arg
, "--bisect")) {
584 if (!strcmp(arg
, "--bisect-all")) {
587 info
.flags
|= BISECT_SHOW_ALL
;
588 revs
.show_decorations
= 1;
591 if (!strcmp(arg
, "--bisect-vars")) {
593 bisect_show_vars
= 1;
596 if (!strcmp(arg
, "--use-bitmap-index")) {
597 use_bitmap_index
= 1;
600 if (!strcmp(arg
, "--test-bitmap")) {
601 test_bitmap_walk(&revs
);
604 if (skip_prefix(arg
, "--progress=", &arg
)) {
608 if (!strcmp(arg
, "--filter-provided-objects")) {
609 filter_provided_objects
= 1;
612 if (!strcmp(arg
, "--filter-print-omitted")) {
613 arg_print_omitted
= 1;
617 if (!strcmp(arg
, "--exclude-promisor-objects"))
618 continue; /* already handled above */
619 if (skip_prefix(arg
, "--missing=", &arg
))
620 continue; /* already handled above */
622 if (!strcmp(arg
, ("--no-object-names"))) {
623 arg_show_object_names
= 0;
627 if (!strcmp(arg
, ("--object-names"))) {
628 arg_show_object_names
= 1;
632 if (!strcmp(arg
, ("--commit-header"))) {
633 revs
.include_header
= 1;
637 if (!strcmp(arg
, ("--no-commit-header"))) {
638 revs
.include_header
= 0;
642 if (skip_prefix(arg
, "--disk-usage", &arg
)) {
644 if (!strcmp(++arg
, "human")) {
647 die(_("invalid value for '%s': '%s', the only allowed format is '%s'"),
648 "--disk-usage=<format>", arg
, "human");
651 * Arguably should goto a label to continue chain of ifs?
652 * Doesn't matter unless we try to add --disk-usage-foo
655 usage(rev_list_usage
);
658 info
.flags
|= REV_LIST_QUIET
;
662 usage(rev_list_usage
);
665 if (revs
.commit_format
!= CMIT_FMT_USERFORMAT
)
666 revs
.include_header
= 1;
667 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
668 /* The command line has a --pretty */
669 info
.hdr_termination
= '\n';
670 if (revs
.commit_format
== CMIT_FMT_ONELINE
|| !revs
.include_header
)
671 info
.header_prefix
= "";
673 info
.header_prefix
= "commit ";
675 else if (revs
.verbose_header
)
676 /* Only --header was specified */
677 revs
.commit_format
= CMIT_FMT_RAW
;
679 if ((!revs
.commits
&& reflog_walk_empty(revs
.reflog_info
) &&
680 (!(revs
.tag_objects
|| revs
.tree_objects
|| revs
.blob_objects
) &&
682 !revs
.rev_input_given
&& !revs
.read_from_stdin
) ||
684 usage(rev_list_usage
);
687 die(_("rev-list does not support display of notes"));
690 (revs
.tag_objects
|| revs
.tree_objects
|| revs
.blob_objects
) &&
691 (revs
.left_right
|| revs
.cherry_mark
))
692 die(_("marked counting and '%s' cannot be used together"), "--objects");
694 save_commit_buffer
= (revs
.verbose_header
||
695 revs
.grep_filter
.pattern_list
||
696 revs
.grep_filter
.header_list
);
701 progress
= start_delayed_progress(show_progress
, 0);
703 if (use_bitmap_index
) {
704 if (!try_bitmap_count(&revs
, filter_provided_objects
))
706 if (!try_bitmap_disk_usage(&revs
, filter_provided_objects
))
708 if (!try_bitmap_traversal(&revs
, filter_provided_objects
))
712 if (prepare_revision_walk(&revs
))
713 die("revision walk setup failed");
714 if (revs
.tree_objects
)
715 mark_edges_uninteresting(&revs
, show_edge
, 0);
719 unsigned bisect_flags
= 0;
722 bisect_flags
|= FIND_BISECTION_ALL
;
724 if (revs
.first_parent_only
)
725 bisect_flags
|= FIND_BISECTION_FIRST_PARENT_ONLY
;
727 find_bisection(&revs
.commits
, &reaches
, &all
, bisect_flags
);
729 if (bisect_show_vars
) {
730 ret
= show_bisect_vars(&info
, reaches
, all
);
735 if (filter_provided_objects
) {
736 struct commit_list
*c
;
737 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
738 struct object_array_entry
*pending
= revs
.pending
.objects
+ i
;
739 pending
->item
->flags
|= NOT_USER_GIVEN
;
741 for (c
= revs
.commits
; c
; c
= c
->next
)
742 c
->item
->object
.flags
|= NOT_USER_GIVEN
;
745 if (arg_print_omitted
)
746 oidset_init(&omitted_objects
, DEFAULT_OIDSET_SIZE
);
747 if (arg_missing_action
== MA_PRINT
)
748 oidset_init(&missing_objects
, DEFAULT_OIDSET_SIZE
);
750 traverse_commit_list_filtered(
751 &revs
, show_commit
, show_object
, &info
,
752 (arg_print_omitted
? &omitted_objects
: NULL
));
754 if (arg_print_omitted
) {
755 struct oidset_iter iter
;
756 struct object_id
*oid
;
757 oidset_iter_init(&omitted_objects
, &iter
);
758 while ((oid
= oidset_iter_next(&iter
)))
759 printf("~%s\n", oid_to_hex(oid
));
760 oidset_clear(&omitted_objects
);
762 if (arg_missing_action
== MA_PRINT
) {
763 struct oidset_iter iter
;
764 struct object_id
*oid
;
765 oidset_iter_init(&missing_objects
, &iter
);
766 while ((oid
= oidset_iter_next(&iter
)))
767 printf("?%s\n", oid_to_hex(oid
));
768 oidset_clear(&missing_objects
);
771 stop_progress(&progress
);
774 if (revs
.left_right
&& revs
.cherry_mark
)
775 printf("%d\t%d\t%d\n", revs
.count_left
, revs
.count_right
, revs
.count_same
);
776 else if (revs
.left_right
)
777 printf("%d\t%d\n", revs
.count_left
, revs
.count_right
);
778 else if (revs
.cherry_mark
)
779 printf("%d\t%d\n", revs
.count_left
+ revs
.count_right
, revs
.count_same
);
781 printf("%d\n", revs
.count_left
+ revs
.count_right
);
785 print_disk_usage(total_disk_usage
);
788 release_revisions(&revs
);