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"
21 #include "object-store.h"
23 static const char rev_list_usage
[] =
24 "git rev-list [OPTION] <commit-id>... [ -- paths... ]\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"
51 " --header | --pretty\n"
52 " --[no-]object-names\n"
53 " --abbrev=<n> | --no-abbrev\n"
63 static struct progress
*progress
;
64 static unsigned progress_counter
;
66 static struct list_objects_filter_options filter_options
;
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 void finish_commit(struct commit
*commit
);
85 static void show_commit(struct commit
*commit
, void *data
)
87 struct rev_list_info
*info
= data
;
88 struct rev_info
*revs
= info
->revs
;
90 display_progress(progress
, ++progress_counter
);
92 if (info
->flags
& REV_LIST_QUIET
) {
93 finish_commit(commit
);
97 graph_show_commit(revs
->graph
);
100 if (commit
->object
.flags
& PATCHSAME
)
102 else if (commit
->object
.flags
& SYMMETRIC_LEFT
)
106 finish_commit(commit
);
110 if (info
->show_timestamp
)
111 printf("%"PRItime
" ", commit
->date
);
112 if (info
->header_prefix
)
113 fputs(info
->header_prefix
, stdout
);
116 fputs(get_revision_mark(revs
, commit
), stdout
);
117 if (revs
->abbrev_commit
&& revs
->abbrev
)
118 fputs(find_unique_abbrev(&commit
->object
.oid
, revs
->abbrev
),
121 fputs(oid_to_hex(&commit
->object
.oid
), stdout
);
122 if (revs
->print_parents
) {
123 struct commit_list
*parents
= commit
->parents
;
125 printf(" %s", oid_to_hex(&parents
->item
->object
.oid
));
126 parents
= parents
->next
;
129 if (revs
->children
.name
) {
130 struct commit_list
*children
;
132 children
= lookup_decoration(&revs
->children
, &commit
->object
);
134 printf(" %s", oid_to_hex(&children
->item
->object
.oid
));
135 children
= children
->next
;
138 show_decorations(revs
, commit
);
139 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
144 if (revs
->verbose_header
) {
145 struct strbuf buf
= STRBUF_INIT
;
146 struct pretty_print_context ctx
= {0};
147 ctx
.abbrev
= revs
->abbrev
;
148 ctx
.date_mode
= revs
->date_mode
;
149 ctx
.date_mode_explicit
= revs
->date_mode_explicit
;
150 ctx
.fmt
= revs
->commit_format
;
151 ctx
.output_encoding
= get_log_output_encoding();
152 ctx
.color
= revs
->diffopt
.use_color
;
153 pretty_print_commit(&ctx
, commit
, &buf
);
155 if (revs
->commit_format
!= CMIT_FMT_ONELINE
)
156 graph_show_oneline(revs
->graph
);
158 graph_show_commit_msg(revs
->graph
, stdout
, &buf
);
161 * Add a newline after the commit message.
163 * Usually, this newline produces a blank
164 * padding line between entries, in which case
165 * we need to add graph padding on this line.
167 * However, the commit message may not end in a
168 * newline. In this case the newline simply
169 * ends the last line of the commit message,
170 * and we don't need any graph output. (This
171 * always happens with CMIT_FMT_ONELINE, and it
172 * happens with CMIT_FMT_USERFORMAT when the
173 * format doesn't explicitly end in a newline.)
175 if (buf
.len
&& buf
.buf
[buf
.len
- 1] == '\n')
176 graph_show_padding(revs
->graph
);
177 putchar(info
->hdr_termination
);
180 * If the message buffer is empty, just show
181 * the rest of the graph output for this
184 if (graph_show_remainder(revs
->graph
))
186 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
189 strbuf_release(&buf
);
191 if (graph_show_remainder(revs
->graph
))
194 maybe_flush_or_die(stdout
, "stdout");
195 finish_commit(commit
);
198 static void finish_commit(struct commit
*commit
)
200 if (commit
->parents
) {
201 free_commit_list(commit
->parents
);
202 commit
->parents
= NULL
;
204 free_commit_buffer(the_repository
->parsed_objects
,
208 static inline void finish_object__ma(struct object
*obj
)
211 * Whether or not we try to dynamically fetch missing objects
212 * from the server, we currently DO NOT have the object. We
213 * can either print, allow (ignore), or conditionally allow
216 switch (arg_missing_action
) {
218 die("missing %s object '%s'",
219 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
226 oidset_insert(&missing_objects
, &obj
->oid
);
229 case MA_ALLOW_PROMISOR
:
230 if (is_promisor_object(&obj
->oid
))
232 die("unexpected missing %s object '%s'",
233 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
237 BUG("unhandled missing_action");
242 static int finish_object(struct object
*obj
, const char *name
, void *cb_data
)
244 struct rev_list_info
*info
= cb_data
;
245 if (oid_object_info_extended(the_repository
, &obj
->oid
, NULL
, 0) < 0) {
246 finish_object__ma(obj
);
249 if (info
->revs
->verify_objects
&& !obj
->parsed
&& obj
->type
!= OBJ_COMMIT
)
250 parse_object(the_repository
, &obj
->oid
);
254 static void show_object(struct object
*obj
, const char *name
, void *cb_data
)
256 struct rev_list_info
*info
= cb_data
;
257 if (finish_object(obj
, name
, cb_data
))
259 display_progress(progress
, ++progress_counter
);
260 if (info
->flags
& REV_LIST_QUIET
)
262 if (arg_show_object_names
)
263 show_object_with_name(stdout
, obj
, name
);
265 printf("%s\n", oid_to_hex(&obj
->oid
));
268 static void show_edge(struct commit
*commit
)
270 printf("-%s\n", oid_to_hex(&commit
->object
.oid
));
273 static void print_var_str(const char *var
, const char *val
)
275 printf("%s='%s'\n", var
, val
);
278 static void print_var_int(const char *var
, int val
)
280 printf("%s=%d\n", var
, val
);
283 static int show_bisect_vars(struct rev_list_info
*info
, int reaches
, int all
)
285 int cnt
, flags
= info
->flags
;
286 char hex
[GIT_MAX_HEXSZ
+ 1] = "";
287 struct commit_list
*tried
;
288 struct rev_info
*revs
= info
->revs
;
293 revs
->commits
= filter_skipped(revs
->commits
, &tried
,
294 flags
& BISECT_SHOW_ALL
,
298 * revs->commits can reach "reaches" commits among
299 * "all" commits. If it is good, then there are
300 * (all-reaches) commits left to be bisected.
301 * On the other hand, if it is bad, then the set
302 * to bisect is "reaches".
303 * A bisect set of size N has (N-1) commits further
304 * to test, as we already know one bad one.
311 oid_to_hex_r(hex
, &revs
->commits
->item
->object
.oid
);
313 if (flags
& BISECT_SHOW_ALL
) {
314 traverse_commit_list(revs
, show_commit
, show_object
, info
);
318 print_var_str("bisect_rev", hex
);
319 print_var_int("bisect_nr", cnt
- 1);
320 print_var_int("bisect_good", all
- reaches
- 1);
321 print_var_int("bisect_bad", reaches
- 1);
322 print_var_int("bisect_all", all
);
323 print_var_int("bisect_steps", estimate_bisect_steps(all
));
328 static int show_object_fast(
329 const struct object_id
*oid
,
330 enum object_type type
,
333 struct packed_git
*found_pack
,
336 fprintf(stdout
, "%s\n", oid_to_hex(oid
));
340 static inline int parse_missing_action_value(const char *value
)
342 if (!strcmp(value
, "error")) {
343 arg_missing_action
= MA_ERROR
;
347 if (!strcmp(value
, "allow-any")) {
348 arg_missing_action
= MA_ALLOW_ANY
;
349 fetch_if_missing
= 0;
353 if (!strcmp(value
, "print")) {
354 arg_missing_action
= MA_PRINT
;
355 fetch_if_missing
= 0;
359 if (!strcmp(value
, "allow-promisor")) {
360 arg_missing_action
= MA_ALLOW_PROMISOR
;
361 fetch_if_missing
= 0;
368 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
370 struct rev_info revs
;
371 struct rev_list_info info
;
372 struct setup_revision_opt s_r_opt
= {
373 .allow_exclude_promisor_objects
= 1,
377 int bisect_show_vars
= 0;
378 int bisect_find_all
= 0;
379 int use_bitmap_index
= 0;
380 const char *show_progress
= NULL
;
382 if (argc
== 2 && !strcmp(argv
[1], "-h"))
383 usage(rev_list_usage
);
385 git_config(git_default_config
, NULL
);
386 repo_init_revisions(the_repository
, &revs
, prefix
);
387 revs
.abbrev
= DEFAULT_ABBREV
;
388 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
391 * Scan the argument list before invoking setup_revisions(), so that we
392 * know if fetch_if_missing needs to be set to 0.
394 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
395 * by not crossing the boundary from realized objects to promisor
398 * Let "--missing" to conditionally set fetch_if_missing.
400 for (i
= 1; i
< argc
; i
++) {
401 const char *arg
= argv
[i
];
402 if (!strcmp(arg
, "--exclude-promisor-objects")) {
403 fetch_if_missing
= 0;
404 revs
.exclude_promisor_objects
= 1;
408 for (i
= 1; i
< argc
; i
++) {
409 const char *arg
= argv
[i
];
410 if (skip_prefix(arg
, "--missing=", &arg
)) {
411 if (revs
.exclude_promisor_objects
)
412 die(_("cannot combine --exclude-promisor-objects and --missing"));
413 if (parse_missing_action_value(arg
))
418 if (arg_missing_action
)
419 revs
.do_not_die_on_missing_tree
= 1;
421 argc
= setup_revisions(argc
, argv
, &revs
, &s_r_opt
);
423 memset(&info
, 0, sizeof(info
));
428 if (revs
.diffopt
.flags
.quick
)
429 info
.flags
|= REV_LIST_QUIET
;
430 for (i
= 1 ; i
< argc
; i
++) {
431 const char *arg
= argv
[i
];
433 if (!strcmp(arg
, "--header")) {
434 revs
.verbose_header
= 1;
437 if (!strcmp(arg
, "--timestamp")) {
438 info
.show_timestamp
= 1;
441 if (!strcmp(arg
, "--bisect")) {
445 if (!strcmp(arg
, "--bisect-all")) {
448 info
.flags
|= BISECT_SHOW_ALL
;
449 revs
.show_decorations
= 1;
452 if (!strcmp(arg
, "--bisect-vars")) {
454 bisect_show_vars
= 1;
457 if (!strcmp(arg
, "--use-bitmap-index")) {
458 use_bitmap_index
= 1;
461 if (!strcmp(arg
, "--test-bitmap")) {
462 test_bitmap_walk(&revs
);
465 if (skip_prefix(arg
, "--progress=", &arg
)) {
470 if (skip_prefix(arg
, ("--" CL_ARG__FILTER
"="), &arg
)) {
471 parse_list_objects_filter(&filter_options
, arg
);
472 if (filter_options
.choice
&& !revs
.blob_objects
)
473 die(_("object filtering requires --objects"));
474 if (filter_options
.choice
== LOFC_SPARSE_OID
&&
475 !filter_options
.sparse_oid_value
)
476 die(_("invalid sparse value '%s'"),
477 filter_options
.filter_spec
);
480 if (!strcmp(arg
, ("--no-" CL_ARG__FILTER
))) {
481 list_objects_filter_set_no_filter(&filter_options
);
484 if (!strcmp(arg
, "--filter-print-omitted")) {
485 arg_print_omitted
= 1;
489 if (!strcmp(arg
, "--exclude-promisor-objects"))
490 continue; /* already handled above */
491 if (skip_prefix(arg
, "--missing=", &arg
))
492 continue; /* already handled above */
494 if (!strcmp(arg
, ("--no-object-names"))) {
495 arg_show_object_names
= 0;
499 if (!strcmp(arg
, ("--object-names"))) {
500 arg_show_object_names
= 1;
504 usage(rev_list_usage
);
507 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
508 /* The command line has a --pretty */
509 info
.hdr_termination
= '\n';
510 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
511 info
.header_prefix
= "";
513 info
.header_prefix
= "commit ";
515 else if (revs
.verbose_header
)
516 /* Only --header was specified */
517 revs
.commit_format
= CMIT_FMT_RAW
;
519 if ((!revs
.commits
&& reflog_walk_empty(revs
.reflog_info
) &&
520 (!(revs
.tag_objects
|| revs
.tree_objects
|| revs
.blob_objects
) &&
522 !revs
.rev_input_given
&& !revs
.read_from_stdin
) ||
524 usage(rev_list_usage
);
527 die(_("rev-list does not support display of notes"));
529 if (filter_options
.choice
&& use_bitmap_index
)
530 die(_("cannot combine --use-bitmap-index with object filtering"));
532 save_commit_buffer
= (revs
.verbose_header
||
533 revs
.grep_filter
.pattern_list
||
534 revs
.grep_filter
.header_list
);
539 progress
= start_delayed_progress(show_progress
, 0);
541 if (use_bitmap_index
&& !revs
.prune
) {
542 if (revs
.count
&& !revs
.left_right
&& !revs
.cherry_mark
) {
543 uint32_t commit_count
;
544 int max_count
= revs
.max_count
;
545 struct bitmap_index
*bitmap_git
;
546 if ((bitmap_git
= prepare_bitmap_walk(&revs
))) {
547 count_bitmap_commit_list(bitmap_git
, &commit_count
, NULL
, NULL
, NULL
);
548 if (max_count
>= 0 && max_count
< commit_count
)
549 commit_count
= max_count
;
550 printf("%d\n", commit_count
);
551 free_bitmap_index(bitmap_git
);
554 } else if (revs
.max_count
< 0 &&
555 revs
.tag_objects
&& revs
.tree_objects
&& revs
.blob_objects
) {
556 struct bitmap_index
*bitmap_git
;
557 if ((bitmap_git
= prepare_bitmap_walk(&revs
))) {
558 traverse_bitmap_commit_list(bitmap_git
, &show_object_fast
);
559 free_bitmap_index(bitmap_git
);
565 if (prepare_revision_walk(&revs
))
566 die("revision walk setup failed");
567 if (revs
.tree_objects
)
568 mark_edges_uninteresting(&revs
, show_edge
, 0);
573 find_bisection(&revs
.commits
, &reaches
, &all
, bisect_find_all
);
575 if (bisect_show_vars
)
576 return show_bisect_vars(&info
, reaches
, all
);
579 if (arg_print_omitted
)
580 oidset_init(&omitted_objects
, DEFAULT_OIDSET_SIZE
);
581 if (arg_missing_action
== MA_PRINT
)
582 oidset_init(&missing_objects
, DEFAULT_OIDSET_SIZE
);
584 traverse_commit_list_filtered(
585 &filter_options
, &revs
, show_commit
, show_object
, &info
,
586 (arg_print_omitted
? &omitted_objects
: NULL
));
588 if (arg_print_omitted
) {
589 struct oidset_iter iter
;
590 struct object_id
*oid
;
591 oidset_iter_init(&omitted_objects
, &iter
);
592 while ((oid
= oidset_iter_next(&iter
)))
593 printf("~%s\n", oid_to_hex(oid
));
594 oidset_clear(&omitted_objects
);
596 if (arg_missing_action
== MA_PRINT
) {
597 struct oidset_iter iter
;
598 struct object_id
*oid
;
599 oidset_iter_init(&missing_objects
, &iter
);
600 while ((oid
= oidset_iter_next(&iter
)))
601 printf("?%s\n", oid_to_hex(oid
));
602 oidset_clear(&missing_objects
);
605 stop_progress(&progress
);
608 if (revs
.left_right
&& revs
.cherry_mark
)
609 printf("%d\t%d\t%d\n", revs
.count_left
, revs
.count_right
, revs
.count_same
);
610 else if (revs
.left_right
)
611 printf("%d\t%d\n", revs
.count_left
, revs
.count_right
);
612 else if (revs
.cherry_mark
)
613 printf("%d\t%d\n", revs
.count_left
+ revs
.count_right
, revs
.count_same
);
615 printf("%d\n", revs
.count_left
+ revs
.count_right
);