5 #include "environment.h"
9 #include "list-objects.h"
10 #include "list-objects-filter.h"
11 #include "list-objects-filter-options.h"
13 #include "object-store.h"
15 #include "pack-bitmap.h"
21 #include "reflog-walk.h"
25 static const char rev_list_usage
[] =
26 "git rev-list [<options>] <commit>... [--] [<path>...]\n"
30 " --max-age=<epoch>\n"
31 " --min-age=<epoch>\n"
34 " --min-parents=<n>\n"
36 " --max-parents=<n>\n"
44 " --exclude-hidden=[fetch|receive|uploadpack]\n"
50 " formatting output:\n"
53 " --objects | --objects-edge\n"
54 " --disk-usage[=human]\n"
56 " --header | --pretty\n"
57 " --[no-]object-names\n"
58 " --abbrev=<n> | --no-abbrev\n"
68 static struct progress
*progress
;
69 static unsigned progress_counter
;
71 static struct oidset omitted_objects
;
72 static int arg_print_omitted
; /* print objects omitted by filter */
74 static struct oidset missing_objects
;
76 MA_ERROR
= 0, /* fail if any missing objects are encountered */
77 MA_ALLOW_ANY
, /* silently allow ALL missing objects */
78 MA_PRINT
, /* print ALL missing objects in special section */
79 MA_ALLOW_PROMISOR
, /* silently allow all missing PROMISOR objects */
81 static enum missing_action arg_missing_action
;
83 /* display only the oid of each object encountered */
84 static int arg_show_object_names
= 1;
86 #define DEFAULT_OIDSET_SIZE (16*1024)
88 static int show_disk_usage
;
89 static off_t total_disk_usage
;
90 static int human_readable
;
92 static off_t
get_object_disk_usage(struct object
*obj
)
95 struct object_info oi
= OBJECT_INFO_INIT
;
96 oi
.disk_sizep
= &size
;
97 if (oid_object_info_extended(the_repository
, &obj
->oid
, &oi
, 0) < 0)
98 die(_("unable to get disk usage of %s"), oid_to_hex(&obj
->oid
));
102 static void finish_commit(struct commit
*commit
);
103 static void show_commit(struct commit
*commit
, void *data
)
105 struct rev_list_info
*info
= data
;
106 struct rev_info
*revs
= info
->revs
;
108 display_progress(progress
, ++progress_counter
);
111 total_disk_usage
+= get_object_disk_usage(&commit
->object
);
113 if (info
->flags
& REV_LIST_QUIET
) {
114 finish_commit(commit
);
118 graph_show_commit(revs
->graph
);
121 if (commit
->object
.flags
& PATCHSAME
)
123 else if (commit
->object
.flags
& SYMMETRIC_LEFT
)
127 finish_commit(commit
);
131 if (info
->show_timestamp
)
132 printf("%"PRItime
" ", commit
->date
);
133 if (info
->header_prefix
)
134 fputs(info
->header_prefix
, stdout
);
136 if (revs
->include_header
) {
138 fputs(get_revision_mark(revs
, commit
), stdout
);
139 if (revs
->abbrev_commit
&& revs
->abbrev
)
140 fputs(repo_find_unique_abbrev(the_repository
, &commit
->object
.oid
, revs
->abbrev
),
143 fputs(oid_to_hex(&commit
->object
.oid
), stdout
);
145 if (revs
->print_parents
) {
146 struct commit_list
*parents
= commit
->parents
;
148 printf(" %s", oid_to_hex(&parents
->item
->object
.oid
));
149 parents
= parents
->next
;
152 if (revs
->children
.name
) {
153 struct commit_list
*children
;
155 children
= lookup_decoration(&revs
->children
, &commit
->object
);
157 printf(" %s", oid_to_hex(&children
->item
->object
.oid
));
158 children
= children
->next
;
161 show_decorations(revs
, commit
);
162 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
164 else if (revs
->include_header
)
167 if (revs
->verbose_header
) {
168 struct strbuf buf
= STRBUF_INIT
;
169 struct pretty_print_context ctx
= {0};
170 ctx
.abbrev
= revs
->abbrev
;
171 ctx
.date_mode
= revs
->date_mode
;
172 ctx
.date_mode_explicit
= revs
->date_mode_explicit
;
173 ctx
.fmt
= revs
->commit_format
;
174 ctx
.output_encoding
= get_log_output_encoding();
175 ctx
.color
= revs
->diffopt
.use_color
;
176 pretty_print_commit(&ctx
, commit
, &buf
);
178 if (revs
->commit_format
!= CMIT_FMT_ONELINE
)
179 graph_show_oneline(revs
->graph
);
181 graph_show_commit_msg(revs
->graph
, stdout
, &buf
);
184 * Add a newline after the commit message.
186 * Usually, this newline produces a blank
187 * padding line between entries, in which case
188 * we need to add graph padding on this line.
190 * However, the commit message may not end in a
191 * newline. In this case the newline simply
192 * ends the last line of the commit message,
193 * and we don't need any graph output. (This
194 * always happens with CMIT_FMT_ONELINE, and it
195 * happens with CMIT_FMT_USERFORMAT when the
196 * format doesn't explicitly end in a newline.)
198 if (buf
.len
&& buf
.buf
[buf
.len
- 1] == '\n')
199 graph_show_padding(revs
->graph
);
200 putchar(info
->hdr_termination
);
203 * If the message buffer is empty, just show
204 * the rest of the graph output for this
207 if (graph_show_remainder(revs
->graph
))
209 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
212 strbuf_release(&buf
);
214 if (graph_show_remainder(revs
->graph
))
217 maybe_flush_or_die(stdout
, "stdout");
218 finish_commit(commit
);
221 static void finish_commit(struct commit
*commit
)
223 free_commit_list(commit
->parents
);
224 commit
->parents
= NULL
;
225 free_commit_buffer(the_repository
->parsed_objects
,
229 static inline void finish_object__ma(struct object
*obj
)
232 * Whether or not we try to dynamically fetch missing objects
233 * from the server, we currently DO NOT have the object. We
234 * can either print, allow (ignore), or conditionally allow
237 switch (arg_missing_action
) {
239 die("missing %s object '%s'",
240 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
247 oidset_insert(&missing_objects
, &obj
->oid
);
250 case MA_ALLOW_PROMISOR
:
251 if (is_promisor_object(&obj
->oid
))
253 die("unexpected missing %s object '%s'",
254 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
258 BUG("unhandled missing_action");
263 static int finish_object(struct object
*obj
, const char *name UNUSED
,
266 struct rev_list_info
*info
= cb_data
;
267 if (oid_object_info_extended(the_repository
, &obj
->oid
, NULL
, 0) < 0) {
268 finish_object__ma(obj
);
271 if (info
->revs
->verify_objects
&& !obj
->parsed
&& obj
->type
!= OBJ_COMMIT
)
272 parse_object(the_repository
, &obj
->oid
);
276 static void show_object(struct object
*obj
, const char *name
, void *cb_data
)
278 struct rev_list_info
*info
= cb_data
;
279 struct rev_info
*revs
= info
->revs
;
281 if (finish_object(obj
, name
, cb_data
))
283 display_progress(progress
, ++progress_counter
);
285 total_disk_usage
+= get_object_disk_usage(obj
);
286 if (info
->flags
& REV_LIST_QUIET
)
291 * The object count is always accumulated in the .count_right
292 * field for traversal that is not a left-right traversal,
293 * and cmd_rev_list() made sure that a .count request that
294 * wants to count non-commit objects, which is handled by
295 * the show_object() callback, does not ask for .left_right.
301 if (arg_show_object_names
)
302 show_object_with_name(stdout
, obj
, name
);
304 printf("%s\n", oid_to_hex(&obj
->oid
));
307 static void show_edge(struct commit
*commit
)
309 printf("-%s\n", oid_to_hex(&commit
->object
.oid
));
312 static void print_var_str(const char *var
, const char *val
)
314 printf("%s='%s'\n", var
, val
);
317 static void print_var_int(const char *var
, int val
)
319 printf("%s=%d\n", var
, val
);
322 static int show_bisect_vars(struct rev_list_info
*info
, int reaches
, int all
)
324 int cnt
, flags
= info
->flags
;
325 char hex
[GIT_MAX_HEXSZ
+ 1] = "";
326 struct commit_list
*tried
;
327 struct rev_info
*revs
= info
->revs
;
332 revs
->commits
= filter_skipped(revs
->commits
, &tried
,
333 flags
& BISECT_SHOW_ALL
,
337 * revs->commits can reach "reaches" commits among
338 * "all" commits. If it is good, then there are
339 * (all-reaches) commits left to be bisected.
340 * On the other hand, if it is bad, then the set
341 * to bisect is "reaches".
342 * A bisect set of size N has (N-1) commits further
343 * to test, as we already know one bad one.
350 oid_to_hex_r(hex
, &revs
->commits
->item
->object
.oid
);
352 if (flags
& BISECT_SHOW_ALL
) {
353 traverse_commit_list(revs
, show_commit
, show_object
, info
);
357 print_var_str("bisect_rev", hex
);
358 print_var_int("bisect_nr", cnt
- 1);
359 print_var_int("bisect_good", all
- reaches
- 1);
360 print_var_int("bisect_bad", reaches
- 1);
361 print_var_int("bisect_all", all
);
362 print_var_int("bisect_steps", estimate_bisect_steps(all
));
367 static int show_object_fast(
368 const struct object_id
*oid
,
369 enum object_type type UNUSED
,
371 uint32_t name_hash UNUSED
,
372 struct packed_git
*found_pack UNUSED
,
373 off_t found_offset UNUSED
)
375 fprintf(stdout
, "%s\n", oid_to_hex(oid
));
379 static void print_disk_usage(off_t size
)
381 struct strbuf sb
= STRBUF_INIT
;
383 strbuf_humanise_bytes(&sb
, size
);
385 strbuf_addf(&sb
, "%"PRIuMAX
, (uintmax_t)size
);
390 static inline int parse_missing_action_value(const char *value
)
392 if (!strcmp(value
, "error")) {
393 arg_missing_action
= MA_ERROR
;
397 if (!strcmp(value
, "allow-any")) {
398 arg_missing_action
= MA_ALLOW_ANY
;
399 fetch_if_missing
= 0;
403 if (!strcmp(value
, "print")) {
404 arg_missing_action
= MA_PRINT
;
405 fetch_if_missing
= 0;
409 if (!strcmp(value
, "allow-promisor")) {
410 arg_missing_action
= MA_ALLOW_PROMISOR
;
411 fetch_if_missing
= 0;
418 static int try_bitmap_count(struct rev_info
*revs
,
419 int filter_provided_objects
)
421 uint32_t commit_count
= 0,
426 struct bitmap_index
*bitmap_git
;
428 /* This function only handles counting, not general traversal. */
433 * A bitmap result can't know left/right, etc, because we don't
436 if (revs
->left_right
|| revs
->cherry_mark
)
440 * If we're counting reachable objects, we can't handle a max count of
441 * commits to traverse, since we don't know which objects go with which
444 if (revs
->max_count
>= 0 &&
445 (revs
->tag_objects
|| revs
->tree_objects
|| revs
->blob_objects
))
449 * This must be saved before doing any walking, since the revision
450 * machinery will count it down to zero while traversing.
452 max_count
= revs
->max_count
;
454 bitmap_git
= prepare_bitmap_walk(revs
, filter_provided_objects
);
458 count_bitmap_commit_list(bitmap_git
, &commit_count
,
459 revs
->tree_objects
? &tree_count
: NULL
,
460 revs
->blob_objects
? &blob_count
: NULL
,
461 revs
->tag_objects
? &tag_count
: NULL
);
462 if (max_count
>= 0 && max_count
< commit_count
)
463 commit_count
= max_count
;
465 printf("%d\n", commit_count
+ tree_count
+ blob_count
+ tag_count
);
466 free_bitmap_index(bitmap_git
);
470 static int try_bitmap_traversal(struct rev_info
*revs
,
471 int filter_provided_objects
)
473 struct bitmap_index
*bitmap_git
;
476 * We can't use a bitmap result with a traversal limit, since the set
477 * of commits we'd get would be essentially random.
479 if (revs
->max_count
>= 0)
482 bitmap_git
= prepare_bitmap_walk(revs
, filter_provided_objects
);
486 traverse_bitmap_commit_list(bitmap_git
, revs
, &show_object_fast
);
487 free_bitmap_index(bitmap_git
);
491 static int try_bitmap_disk_usage(struct rev_info
*revs
,
492 int filter_provided_objects
)
494 struct bitmap_index
*bitmap_git
;
495 off_t size_from_bitmap
;
497 if (!show_disk_usage
)
500 bitmap_git
= prepare_bitmap_walk(revs
, filter_provided_objects
);
504 size_from_bitmap
= get_disk_usage_from_bitmap(bitmap_git
, revs
);
505 print_disk_usage(size_from_bitmap
);
509 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
511 struct rev_info revs
;
512 struct rev_list_info info
;
513 struct setup_revision_opt s_r_opt
= {
514 .allow_exclude_promisor_objects
= 1,
518 int bisect_show_vars
= 0;
519 int bisect_find_all
= 0;
520 int use_bitmap_index
= 0;
521 int filter_provided_objects
= 0;
522 const char *show_progress
= NULL
;
525 if (argc
== 2 && !strcmp(argv
[1], "-h"))
526 usage(rev_list_usage
);
528 git_config(git_default_config
, NULL
);
529 repo_init_revisions(the_repository
, &revs
, prefix
);
530 revs
.abbrev
= DEFAULT_ABBREV
;
531 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
532 revs
.include_header
= 1;
535 * Scan the argument list before invoking setup_revisions(), so that we
536 * know if fetch_if_missing needs to be set to 0.
538 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
539 * by not crossing the boundary from realized objects to promisor
542 * Let "--missing" to conditionally set fetch_if_missing.
544 for (i
= 1; i
< argc
; i
++) {
545 const char *arg
= argv
[i
];
546 if (!strcmp(arg
, "--exclude-promisor-objects")) {
547 fetch_if_missing
= 0;
548 revs
.exclude_promisor_objects
= 1;
552 for (i
= 1; i
< argc
; i
++) {
553 const char *arg
= argv
[i
];
554 if (skip_prefix(arg
, "--missing=", &arg
)) {
555 if (revs
.exclude_promisor_objects
)
556 die(_("options '%s' and '%s' cannot be used together"), "--exclude-promisor-objects", "--missing");
557 if (parse_missing_action_value(arg
))
562 if (arg_missing_action
)
563 revs
.do_not_die_on_missing_tree
= 1;
565 argc
= setup_revisions(argc
, argv
, &revs
, &s_r_opt
);
567 memset(&info
, 0, sizeof(info
));
572 if (revs
.diffopt
.flags
.quick
)
573 info
.flags
|= REV_LIST_QUIET
;
574 for (i
= 1 ; i
< argc
; i
++) {
575 const char *arg
= argv
[i
];
577 if (!strcmp(arg
, "--header")) {
578 revs
.verbose_header
= 1;
581 if (!strcmp(arg
, "--timestamp")) {
582 info
.show_timestamp
= 1;
585 if (!strcmp(arg
, "--bisect")) {
589 if (!strcmp(arg
, "--bisect-all")) {
592 info
.flags
|= BISECT_SHOW_ALL
;
593 revs
.show_decorations
= 1;
596 if (!strcmp(arg
, "--bisect-vars")) {
598 bisect_show_vars
= 1;
601 if (!strcmp(arg
, "--use-bitmap-index")) {
602 use_bitmap_index
= 1;
605 if (!strcmp(arg
, "--test-bitmap")) {
606 test_bitmap_walk(&revs
);
609 if (skip_prefix(arg
, "--progress=", &arg
)) {
613 if (!strcmp(arg
, "--filter-provided-objects")) {
614 filter_provided_objects
= 1;
617 if (!strcmp(arg
, "--filter-print-omitted")) {
618 arg_print_omitted
= 1;
622 if (!strcmp(arg
, "--exclude-promisor-objects"))
623 continue; /* already handled above */
624 if (skip_prefix(arg
, "--missing=", &arg
))
625 continue; /* already handled above */
627 if (!strcmp(arg
, ("--no-object-names"))) {
628 arg_show_object_names
= 0;
632 if (!strcmp(arg
, ("--object-names"))) {
633 arg_show_object_names
= 1;
637 if (!strcmp(arg
, ("--commit-header"))) {
638 revs
.include_header
= 1;
642 if (!strcmp(arg
, ("--no-commit-header"))) {
643 revs
.include_header
= 0;
647 if (skip_prefix(arg
, "--disk-usage", &arg
)) {
649 if (!strcmp(++arg
, "human")) {
652 die(_("invalid value for '%s': '%s', the only allowed format is '%s'"),
653 "--disk-usage=<format>", arg
, "human");
656 * Arguably should goto a label to continue chain of ifs?
657 * Doesn't matter unless we try to add --disk-usage-foo
660 usage(rev_list_usage
);
663 info
.flags
|= REV_LIST_QUIET
;
667 usage(rev_list_usage
);
670 if (revs
.commit_format
!= CMIT_FMT_USERFORMAT
)
671 revs
.include_header
= 1;
672 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
673 /* The command line has a --pretty */
674 info
.hdr_termination
= '\n';
675 if (revs
.commit_format
== CMIT_FMT_ONELINE
|| !revs
.include_header
)
676 info
.header_prefix
= "";
678 info
.header_prefix
= "commit ";
680 else if (revs
.verbose_header
)
681 /* Only --header was specified */
682 revs
.commit_format
= CMIT_FMT_RAW
;
684 if ((!revs
.commits
&& reflog_walk_empty(revs
.reflog_info
) &&
685 (!(revs
.tag_objects
|| revs
.tree_objects
|| revs
.blob_objects
) &&
687 !revs
.rev_input_given
&& !revs
.read_from_stdin
) ||
689 usage(rev_list_usage
);
692 die(_("rev-list does not support display of notes"));
695 (revs
.tag_objects
|| revs
.tree_objects
|| revs
.blob_objects
) &&
696 (revs
.left_right
|| revs
.cherry_mark
))
697 die(_("marked counting and '%s' cannot be used together"), "--objects");
699 save_commit_buffer
= (revs
.verbose_header
||
700 revs
.grep_filter
.pattern_list
||
701 revs
.grep_filter
.header_list
);
706 progress
= start_delayed_progress(show_progress
, 0);
708 if (use_bitmap_index
) {
709 if (!try_bitmap_count(&revs
, filter_provided_objects
))
711 if (!try_bitmap_disk_usage(&revs
, filter_provided_objects
))
713 if (!try_bitmap_traversal(&revs
, filter_provided_objects
))
717 if (prepare_revision_walk(&revs
))
718 die("revision walk setup failed");
719 if (revs
.tree_objects
)
720 mark_edges_uninteresting(&revs
, show_edge
, 0);
724 unsigned bisect_flags
= 0;
727 bisect_flags
|= FIND_BISECTION_ALL
;
729 if (revs
.first_parent_only
)
730 bisect_flags
|= FIND_BISECTION_FIRST_PARENT_ONLY
;
732 find_bisection(&revs
.commits
, &reaches
, &all
, bisect_flags
);
734 if (bisect_show_vars
) {
735 ret
= show_bisect_vars(&info
, reaches
, all
);
740 if (filter_provided_objects
) {
741 struct commit_list
*c
;
742 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
743 struct object_array_entry
*pending
= revs
.pending
.objects
+ i
;
744 pending
->item
->flags
|= NOT_USER_GIVEN
;
746 for (c
= revs
.commits
; c
; c
= c
->next
)
747 c
->item
->object
.flags
|= NOT_USER_GIVEN
;
750 if (arg_print_omitted
)
751 oidset_init(&omitted_objects
, DEFAULT_OIDSET_SIZE
);
752 if (arg_missing_action
== MA_PRINT
)
753 oidset_init(&missing_objects
, DEFAULT_OIDSET_SIZE
);
755 traverse_commit_list_filtered(
756 &revs
, show_commit
, show_object
, &info
,
757 (arg_print_omitted
? &omitted_objects
: NULL
));
759 if (arg_print_omitted
) {
760 struct oidset_iter iter
;
761 struct object_id
*oid
;
762 oidset_iter_init(&omitted_objects
, &iter
);
763 while ((oid
= oidset_iter_next(&iter
)))
764 printf("~%s\n", oid_to_hex(oid
));
765 oidset_clear(&omitted_objects
);
767 if (arg_missing_action
== MA_PRINT
) {
768 struct oidset_iter iter
;
769 struct object_id
*oid
;
770 oidset_iter_init(&missing_objects
, &iter
);
771 while ((oid
= oidset_iter_next(&iter
)))
772 printf("?%s\n", oid_to_hex(oid
));
773 oidset_clear(&missing_objects
);
776 stop_progress(&progress
);
779 if (revs
.left_right
&& revs
.cherry_mark
)
780 printf("%d\t%d\t%d\n", revs
.count_left
, revs
.count_right
, revs
.count_same
);
781 else if (revs
.left_right
)
782 printf("%d\t%d\n", revs
.count_left
, revs
.count_right
);
783 else if (revs
.cherry_mark
)
784 printf("%d\t%d\n", revs
.count_left
+ revs
.count_right
, revs
.count_same
);
786 printf("%d\n", revs
.count_left
+ revs
.count_right
);
790 print_disk_usage(total_disk_usage
);
793 release_revisions(&revs
);