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-id>... [-- <path>...]\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 oidset omitted_objects
;
66 static int arg_print_omitted
; /* print objects omitted by filter */
68 static struct oidset missing_objects
;
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 */
75 static enum missing_action arg_missing_action
;
77 /* display only the oid of each object encountered */
78 static int arg_show_object_names
= 1;
80 #define DEFAULT_OIDSET_SIZE (16*1024)
82 static int show_disk_usage
;
83 static off_t total_disk_usage
;
85 static off_t
get_object_disk_usage(struct object
*obj
)
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
));
95 static void finish_commit(struct commit
*commit
);
96 static void show_commit(struct commit
*commit
, void *data
)
98 struct rev_list_info
*info
= data
;
99 struct rev_info
*revs
= info
->revs
;
101 display_progress(progress
, ++progress_counter
);
104 total_disk_usage
+= get_object_disk_usage(&commit
->object
);
106 if (info
->flags
& REV_LIST_QUIET
) {
107 finish_commit(commit
);
111 graph_show_commit(revs
->graph
);
114 if (commit
->object
.flags
& PATCHSAME
)
116 else if (commit
->object
.flags
& SYMMETRIC_LEFT
)
120 finish_commit(commit
);
124 if (info
->show_timestamp
)
125 printf("%"PRItime
" ", commit
->date
);
126 if (info
->header_prefix
)
127 fputs(info
->header_prefix
, stdout
);
129 if (revs
->include_header
) {
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
),
136 fputs(oid_to_hex(&commit
->object
.oid
), stdout
);
138 if (revs
->print_parents
) {
139 struct commit_list
*parents
= commit
->parents
;
141 printf(" %s", oid_to_hex(&parents
->item
->object
.oid
));
142 parents
= parents
->next
;
145 if (revs
->children
.name
) {
146 struct commit_list
*children
;
148 children
= lookup_decoration(&revs
->children
, &commit
->object
);
150 printf(" %s", oid_to_hex(&children
->item
->object
.oid
));
151 children
= children
->next
;
154 show_decorations(revs
, commit
);
155 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
157 else if (revs
->include_header
)
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
);
171 if (revs
->commit_format
!= CMIT_FMT_ONELINE
)
172 graph_show_oneline(revs
->graph
);
174 graph_show_commit_msg(revs
->graph
, stdout
, &buf
);
177 * Add a newline after the commit message.
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.
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.)
191 if (buf
.len
&& buf
.buf
[buf
.len
- 1] == '\n')
192 graph_show_padding(revs
->graph
);
193 putchar(info
->hdr_termination
);
196 * If the message buffer is empty, just show
197 * the rest of the graph output for this
200 if (graph_show_remainder(revs
->graph
))
202 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
205 strbuf_release(&buf
);
207 if (graph_show_remainder(revs
->graph
))
210 maybe_flush_or_die(stdout
, "stdout");
211 finish_commit(commit
);
214 static void finish_commit(struct commit
*commit
)
216 free_commit_list(commit
->parents
);
217 commit
->parents
= NULL
;
218 free_commit_buffer(the_repository
->parsed_objects
,
222 static inline void finish_object__ma(struct object
*obj
)
225 * Whether or not we try to dynamically fetch missing objects
226 * from the server, we currently DO NOT have the object. We
227 * can either print, allow (ignore), or conditionally allow
230 switch (arg_missing_action
) {
232 die("missing %s object '%s'",
233 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
240 oidset_insert(&missing_objects
, &obj
->oid
);
243 case MA_ALLOW_PROMISOR
:
244 if (is_promisor_object(&obj
->oid
))
246 die("unexpected missing %s object '%s'",
247 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
251 BUG("unhandled missing_action");
256 static int finish_object(struct object
*obj
, const char *name
, void *cb_data
)
258 struct rev_list_info
*info
= cb_data
;
259 if (oid_object_info_extended(the_repository
, &obj
->oid
, NULL
, 0) < 0) {
260 finish_object__ma(obj
);
263 if (info
->revs
->verify_objects
&& !obj
->parsed
&& obj
->type
!= OBJ_COMMIT
)
264 parse_object(the_repository
, &obj
->oid
);
268 static void show_object(struct object
*obj
, const char *name
, void *cb_data
)
270 struct rev_list_info
*info
= cb_data
;
271 struct rev_info
*revs
= info
->revs
;
273 if (finish_object(obj
, name
, cb_data
))
275 display_progress(progress
, ++progress_counter
);
277 total_disk_usage
+= get_object_disk_usage(obj
);
278 if (info
->flags
& REV_LIST_QUIET
)
283 * The object count is always accumulated in the .count_right
284 * field for traversal that is not a left-right traversal,
285 * and cmd_rev_list() made sure that a .count request that
286 * wants to count non-commit objects, which is handled by
287 * the show_object() callback, does not ask for .left_right.
293 if (arg_show_object_names
)
294 show_object_with_name(stdout
, obj
, name
);
296 printf("%s\n", oid_to_hex(&obj
->oid
));
299 static void show_edge(struct commit
*commit
)
301 printf("-%s\n", oid_to_hex(&commit
->object
.oid
));
304 static void print_var_str(const char *var
, const char *val
)
306 printf("%s='%s'\n", var
, val
);
309 static void print_var_int(const char *var
, int val
)
311 printf("%s=%d\n", var
, val
);
314 static int show_bisect_vars(struct rev_list_info
*info
, int reaches
, int all
)
316 int cnt
, flags
= info
->flags
;
317 char hex
[GIT_MAX_HEXSZ
+ 1] = "";
318 struct commit_list
*tried
;
319 struct rev_info
*revs
= info
->revs
;
324 revs
->commits
= filter_skipped(revs
->commits
, &tried
,
325 flags
& BISECT_SHOW_ALL
,
329 * revs->commits can reach "reaches" commits among
330 * "all" commits. If it is good, then there are
331 * (all-reaches) commits left to be bisected.
332 * On the other hand, if it is bad, then the set
333 * to bisect is "reaches".
334 * A bisect set of size N has (N-1) commits further
335 * to test, as we already know one bad one.
342 oid_to_hex_r(hex
, &revs
->commits
->item
->object
.oid
);
344 if (flags
& BISECT_SHOW_ALL
) {
345 traverse_commit_list(revs
, show_commit
, show_object
, info
);
349 print_var_str("bisect_rev", hex
);
350 print_var_int("bisect_nr", cnt
- 1);
351 print_var_int("bisect_good", all
- reaches
- 1);
352 print_var_int("bisect_bad", reaches
- 1);
353 print_var_int("bisect_all", all
);
354 print_var_int("bisect_steps", estimate_bisect_steps(all
));
359 static int show_object_fast(
360 const struct object_id
*oid
,
361 enum object_type type
,
364 struct packed_git
*found_pack
,
367 fprintf(stdout
, "%s\n", oid_to_hex(oid
));
371 static inline int parse_missing_action_value(const char *value
)
373 if (!strcmp(value
, "error")) {
374 arg_missing_action
= MA_ERROR
;
378 if (!strcmp(value
, "allow-any")) {
379 arg_missing_action
= MA_ALLOW_ANY
;
380 fetch_if_missing
= 0;
384 if (!strcmp(value
, "print")) {
385 arg_missing_action
= MA_PRINT
;
386 fetch_if_missing
= 0;
390 if (!strcmp(value
, "allow-promisor")) {
391 arg_missing_action
= MA_ALLOW_PROMISOR
;
392 fetch_if_missing
= 0;
399 static int try_bitmap_count(struct rev_info
*revs
,
400 int filter_provided_objects
)
402 uint32_t commit_count
= 0,
407 struct bitmap_index
*bitmap_git
;
409 /* This function only handles counting, not general traversal. */
414 * A bitmap result can't know left/right, etc, because we don't
417 if (revs
->left_right
|| revs
->cherry_mark
)
421 * If we're counting reachable objects, we can't handle a max count of
422 * commits to traverse, since we don't know which objects go with which
425 if (revs
->max_count
>= 0 &&
426 (revs
->tag_objects
|| revs
->tree_objects
|| revs
->blob_objects
))
430 * This must be saved before doing any walking, since the revision
431 * machinery will count it down to zero while traversing.
433 max_count
= revs
->max_count
;
435 bitmap_git
= prepare_bitmap_walk(revs
, filter_provided_objects
);
439 count_bitmap_commit_list(bitmap_git
, &commit_count
,
440 revs
->tree_objects
? &tree_count
: NULL
,
441 revs
->blob_objects
? &blob_count
: NULL
,
442 revs
->tag_objects
? &tag_count
: NULL
);
443 if (max_count
>= 0 && max_count
< commit_count
)
444 commit_count
= max_count
;
446 printf("%d\n", commit_count
+ tree_count
+ blob_count
+ tag_count
);
447 free_bitmap_index(bitmap_git
);
451 static int try_bitmap_traversal(struct rev_info
*revs
,
452 int filter_provided_objects
)
454 struct bitmap_index
*bitmap_git
;
457 * We can't use a bitmap result with a traversal limit, since the set
458 * of commits we'd get would be essentially random.
460 if (revs
->max_count
>= 0)
463 bitmap_git
= prepare_bitmap_walk(revs
, filter_provided_objects
);
467 traverse_bitmap_commit_list(bitmap_git
, revs
, &show_object_fast
);
468 free_bitmap_index(bitmap_git
);
472 static int try_bitmap_disk_usage(struct rev_info
*revs
,
473 int filter_provided_objects
)
475 struct bitmap_index
*bitmap_git
;
477 if (!show_disk_usage
)
480 bitmap_git
= prepare_bitmap_walk(revs
, filter_provided_objects
);
484 printf("%"PRIuMAX
"\n",
485 (uintmax_t)get_disk_usage_from_bitmap(bitmap_git
, revs
));
489 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
491 struct rev_info revs
;
492 struct rev_list_info info
;
493 struct setup_revision_opt s_r_opt
= {
494 .allow_exclude_promisor_objects
= 1,
498 int bisect_show_vars
= 0;
499 int bisect_find_all
= 0;
500 int use_bitmap_index
= 0;
501 int filter_provided_objects
= 0;
502 const char *show_progress
= NULL
;
505 if (argc
== 2 && !strcmp(argv
[1], "-h"))
506 usage(rev_list_usage
);
508 git_config(git_default_config
, NULL
);
509 repo_init_revisions(the_repository
, &revs
, prefix
);
510 revs
.abbrev
= DEFAULT_ABBREV
;
511 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
512 revs
.include_header
= 1;
515 * Scan the argument list before invoking setup_revisions(), so that we
516 * know if fetch_if_missing needs to be set to 0.
518 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
519 * by not crossing the boundary from realized objects to promisor
522 * Let "--missing" to conditionally set fetch_if_missing.
524 for (i
= 1; i
< argc
; i
++) {
525 const char *arg
= argv
[i
];
526 if (!strcmp(arg
, "--exclude-promisor-objects")) {
527 fetch_if_missing
= 0;
528 revs
.exclude_promisor_objects
= 1;
532 for (i
= 1; i
< argc
; i
++) {
533 const char *arg
= argv
[i
];
534 if (skip_prefix(arg
, "--missing=", &arg
)) {
535 if (revs
.exclude_promisor_objects
)
536 die(_("options '%s' and '%s' cannot be used together"), "--exclude-promisor-objects", "--missing");
537 if (parse_missing_action_value(arg
))
542 if (arg_missing_action
)
543 revs
.do_not_die_on_missing_tree
= 1;
545 argc
= setup_revisions(argc
, argv
, &revs
, &s_r_opt
);
547 memset(&info
, 0, sizeof(info
));
552 if (revs
.diffopt
.flags
.quick
)
553 info
.flags
|= REV_LIST_QUIET
;
554 for (i
= 1 ; i
< argc
; i
++) {
555 const char *arg
= argv
[i
];
557 if (!strcmp(arg
, "--header")) {
558 revs
.verbose_header
= 1;
561 if (!strcmp(arg
, "--timestamp")) {
562 info
.show_timestamp
= 1;
565 if (!strcmp(arg
, "--bisect")) {
569 if (!strcmp(arg
, "--bisect-all")) {
572 info
.flags
|= BISECT_SHOW_ALL
;
573 revs
.show_decorations
= 1;
576 if (!strcmp(arg
, "--bisect-vars")) {
578 bisect_show_vars
= 1;
581 if (!strcmp(arg
, "--use-bitmap-index")) {
582 use_bitmap_index
= 1;
585 if (!strcmp(arg
, "--test-bitmap")) {
586 test_bitmap_walk(&revs
);
589 if (skip_prefix(arg
, "--progress=", &arg
)) {
593 if (!strcmp(arg
, "--filter-provided-objects")) {
594 filter_provided_objects
= 1;
597 if (!strcmp(arg
, "--filter-print-omitted")) {
598 arg_print_omitted
= 1;
602 if (!strcmp(arg
, "--exclude-promisor-objects"))
603 continue; /* already handled above */
604 if (skip_prefix(arg
, "--missing=", &arg
))
605 continue; /* already handled above */
607 if (!strcmp(arg
, ("--no-object-names"))) {
608 arg_show_object_names
= 0;
612 if (!strcmp(arg
, ("--object-names"))) {
613 arg_show_object_names
= 1;
617 if (!strcmp(arg
, ("--commit-header"))) {
618 revs
.include_header
= 1;
622 if (!strcmp(arg
, ("--no-commit-header"))) {
623 revs
.include_header
= 0;
627 if (!strcmp(arg
, "--disk-usage")) {
629 info
.flags
|= REV_LIST_QUIET
;
633 usage(rev_list_usage
);
636 if (revs
.commit_format
!= CMIT_FMT_USERFORMAT
)
637 revs
.include_header
= 1;
638 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
639 /* The command line has a --pretty */
640 info
.hdr_termination
= '\n';
641 if (revs
.commit_format
== CMIT_FMT_ONELINE
|| !revs
.include_header
)
642 info
.header_prefix
= "";
644 info
.header_prefix
= "commit ";
646 else if (revs
.verbose_header
)
647 /* Only --header was specified */
648 revs
.commit_format
= CMIT_FMT_RAW
;
650 if ((!revs
.commits
&& reflog_walk_empty(revs
.reflog_info
) &&
651 (!(revs
.tag_objects
|| revs
.tree_objects
|| revs
.blob_objects
) &&
653 !revs
.rev_input_given
&& !revs
.read_from_stdin
) ||
655 usage(rev_list_usage
);
658 die(_("rev-list does not support display of notes"));
661 (revs
.tag_objects
|| revs
.tree_objects
|| revs
.blob_objects
) &&
662 (revs
.left_right
|| revs
.cherry_mark
))
663 die(_("marked counting and '%s' cannot be used together"), "--objects");
665 save_commit_buffer
= (revs
.verbose_header
||
666 revs
.grep_filter
.pattern_list
||
667 revs
.grep_filter
.header_list
);
672 progress
= start_delayed_progress(show_progress
, 0);
674 if (use_bitmap_index
) {
675 if (!try_bitmap_count(&revs
, filter_provided_objects
))
677 if (!try_bitmap_disk_usage(&revs
, filter_provided_objects
))
679 if (!try_bitmap_traversal(&revs
, filter_provided_objects
))
683 if (prepare_revision_walk(&revs
))
684 die("revision walk setup failed");
685 if (revs
.tree_objects
)
686 mark_edges_uninteresting(&revs
, show_edge
, 0);
690 unsigned bisect_flags
= 0;
693 bisect_flags
|= FIND_BISECTION_ALL
;
695 if (revs
.first_parent_only
)
696 bisect_flags
|= FIND_BISECTION_FIRST_PARENT_ONLY
;
698 find_bisection(&revs
.commits
, &reaches
, &all
, bisect_flags
);
700 if (bisect_show_vars
) {
701 ret
= show_bisect_vars(&info
, reaches
, all
);
706 if (filter_provided_objects
) {
707 struct commit_list
*c
;
708 for (i
= 0; i
< revs
.pending
.nr
; i
++) {
709 struct object_array_entry
*pending
= revs
.pending
.objects
+ i
;
710 pending
->item
->flags
|= NOT_USER_GIVEN
;
712 for (c
= revs
.commits
; c
; c
= c
->next
)
713 c
->item
->object
.flags
|= NOT_USER_GIVEN
;
716 if (arg_print_omitted
)
717 oidset_init(&omitted_objects
, DEFAULT_OIDSET_SIZE
);
718 if (arg_missing_action
== MA_PRINT
)
719 oidset_init(&missing_objects
, DEFAULT_OIDSET_SIZE
);
721 traverse_commit_list_filtered(
722 &revs
, show_commit
, show_object
, &info
,
723 (arg_print_omitted
? &omitted_objects
: NULL
));
725 if (arg_print_omitted
) {
726 struct oidset_iter iter
;
727 struct object_id
*oid
;
728 oidset_iter_init(&omitted_objects
, &iter
);
729 while ((oid
= oidset_iter_next(&iter
)))
730 printf("~%s\n", oid_to_hex(oid
));
731 oidset_clear(&omitted_objects
);
733 if (arg_missing_action
== MA_PRINT
) {
734 struct oidset_iter iter
;
735 struct object_id
*oid
;
736 oidset_iter_init(&missing_objects
, &iter
);
737 while ((oid
= oidset_iter_next(&iter
)))
738 printf("?%s\n", oid_to_hex(oid
));
739 oidset_clear(&missing_objects
);
742 stop_progress(&progress
);
745 if (revs
.left_right
&& revs
.cherry_mark
)
746 printf("%d\t%d\t%d\n", revs
.count_left
, revs
.count_right
, revs
.count_same
);
747 else if (revs
.left_right
)
748 printf("%d\t%d\n", revs
.count_left
, revs
.count_right
);
749 else if (revs
.cherry_mark
)
750 printf("%d\t%d\n", revs
.count_left
+ revs
.count_right
, revs
.count_same
);
752 printf("%d\n", revs
.count_left
+ revs
.count_right
);
756 printf("%"PRIuMAX
"\n", (uintmax_t)total_disk_usage
);
759 release_revisions(&revs
);