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 void finish_commit(struct commit
*commit
);
84 static void show_commit(struct commit
*commit
, void *data
)
86 struct rev_list_info
*info
= data
;
87 struct rev_info
*revs
= info
->revs
;
89 display_progress(progress
, ++progress_counter
);
91 if (info
->flags
& REV_LIST_QUIET
) {
92 finish_commit(commit
);
96 graph_show_commit(revs
->graph
);
99 if (commit
->object
.flags
& PATCHSAME
)
101 else if (commit
->object
.flags
& SYMMETRIC_LEFT
)
105 finish_commit(commit
);
109 if (info
->show_timestamp
)
110 printf("%"PRItime
" ", commit
->date
);
111 if (info
->header_prefix
)
112 fputs(info
->header_prefix
, stdout
);
115 fputs(get_revision_mark(revs
, commit
), stdout
);
116 if (revs
->abbrev_commit
&& revs
->abbrev
)
117 fputs(find_unique_abbrev(&commit
->object
.oid
, revs
->abbrev
),
120 fputs(oid_to_hex(&commit
->object
.oid
), stdout
);
121 if (revs
->print_parents
) {
122 struct commit_list
*parents
= commit
->parents
;
124 printf(" %s", oid_to_hex(&parents
->item
->object
.oid
));
125 parents
= parents
->next
;
128 if (revs
->children
.name
) {
129 struct commit_list
*children
;
131 children
= lookup_decoration(&revs
->children
, &commit
->object
);
133 printf(" %s", oid_to_hex(&children
->item
->object
.oid
));
134 children
= children
->next
;
137 show_decorations(revs
, commit
);
138 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
143 if (revs
->verbose_header
) {
144 struct strbuf buf
= STRBUF_INIT
;
145 struct pretty_print_context ctx
= {0};
146 ctx
.abbrev
= revs
->abbrev
;
147 ctx
.date_mode
= revs
->date_mode
;
148 ctx
.date_mode_explicit
= revs
->date_mode_explicit
;
149 ctx
.fmt
= revs
->commit_format
;
150 ctx
.output_encoding
= get_log_output_encoding();
151 ctx
.color
= revs
->diffopt
.use_color
;
152 pretty_print_commit(&ctx
, commit
, &buf
);
154 if (revs
->commit_format
!= CMIT_FMT_ONELINE
)
155 graph_show_oneline(revs
->graph
);
157 graph_show_commit_msg(revs
->graph
, stdout
, &buf
);
160 * Add a newline after the commit message.
162 * Usually, this newline produces a blank
163 * padding line between entries, in which case
164 * we need to add graph padding on this line.
166 * However, the commit message may not end in a
167 * newline. In this case the newline simply
168 * ends the last line of the commit message,
169 * and we don't need any graph output. (This
170 * always happens with CMIT_FMT_ONELINE, and it
171 * happens with CMIT_FMT_USERFORMAT when the
172 * format doesn't explicitly end in a newline.)
174 if (buf
.len
&& buf
.buf
[buf
.len
- 1] == '\n')
175 graph_show_padding(revs
->graph
);
176 putchar(info
->hdr_termination
);
179 * If the message buffer is empty, just show
180 * the rest of the graph output for this
183 if (graph_show_remainder(revs
->graph
))
185 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
188 strbuf_release(&buf
);
190 if (graph_show_remainder(revs
->graph
))
193 maybe_flush_or_die(stdout
, "stdout");
194 finish_commit(commit
);
197 static void finish_commit(struct commit
*commit
)
199 if (commit
->parents
) {
200 free_commit_list(commit
->parents
);
201 commit
->parents
= NULL
;
203 free_commit_buffer(the_repository
->parsed_objects
,
207 static inline void finish_object__ma(struct object
*obj
)
210 * Whether or not we try to dynamically fetch missing objects
211 * from the server, we currently DO NOT have the object. We
212 * can either print, allow (ignore), or conditionally allow
215 switch (arg_missing_action
) {
217 die("missing %s object '%s'",
218 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
225 oidset_insert(&missing_objects
, &obj
->oid
);
228 case MA_ALLOW_PROMISOR
:
229 if (is_promisor_object(&obj
->oid
))
231 die("unexpected missing %s object '%s'",
232 type_name(obj
->type
), oid_to_hex(&obj
->oid
));
236 BUG("unhandled missing_action");
241 static int finish_object(struct object
*obj
, const char *name
, void *cb_data
)
243 struct rev_list_info
*info
= cb_data
;
244 if (oid_object_info_extended(the_repository
, &obj
->oid
, NULL
, 0) < 0) {
245 finish_object__ma(obj
);
248 if (info
->revs
->verify_objects
&& !obj
->parsed
&& obj
->type
!= OBJ_COMMIT
)
249 parse_object(the_repository
, &obj
->oid
);
253 static void show_object(struct object
*obj
, const char *name
, void *cb_data
)
255 struct rev_list_info
*info
= cb_data
;
256 if (finish_object(obj
, name
, cb_data
))
258 display_progress(progress
, ++progress_counter
);
259 if (info
->flags
& REV_LIST_QUIET
)
261 if (arg_show_object_names
)
262 show_object_with_name(stdout
, obj
, name
);
264 printf("%s\n", oid_to_hex(&obj
->oid
));
267 static void show_edge(struct commit
*commit
)
269 printf("-%s\n", oid_to_hex(&commit
->object
.oid
));
272 static void print_var_str(const char *var
, const char *val
)
274 printf("%s='%s'\n", var
, val
);
277 static void print_var_int(const char *var
, int val
)
279 printf("%s=%d\n", var
, val
);
282 static int show_bisect_vars(struct rev_list_info
*info
, int reaches
, int all
)
284 int cnt
, flags
= info
->flags
;
285 char hex
[GIT_MAX_HEXSZ
+ 1] = "";
286 struct commit_list
*tried
;
287 struct rev_info
*revs
= info
->revs
;
292 revs
->commits
= filter_skipped(revs
->commits
, &tried
,
293 flags
& BISECT_SHOW_ALL
,
297 * revs->commits can reach "reaches" commits among
298 * "all" commits. If it is good, then there are
299 * (all-reaches) commits left to be bisected.
300 * On the other hand, if it is bad, then the set
301 * to bisect is "reaches".
302 * A bisect set of size N has (N-1) commits further
303 * to test, as we already know one bad one.
310 oid_to_hex_r(hex
, &revs
->commits
->item
->object
.oid
);
312 if (flags
& BISECT_SHOW_ALL
) {
313 traverse_commit_list(revs
, show_commit
, show_object
, info
);
317 print_var_str("bisect_rev", hex
);
318 print_var_int("bisect_nr", cnt
- 1);
319 print_var_int("bisect_good", all
- reaches
- 1);
320 print_var_int("bisect_bad", reaches
- 1);
321 print_var_int("bisect_all", all
);
322 print_var_int("bisect_steps", estimate_bisect_steps(all
));
327 static int show_object_fast(
328 const struct object_id
*oid
,
329 enum object_type type
,
332 struct packed_git
*found_pack
,
335 fprintf(stdout
, "%s\n", oid_to_hex(oid
));
339 static inline int parse_missing_action_value(const char *value
)
341 if (!strcmp(value
, "error")) {
342 arg_missing_action
= MA_ERROR
;
346 if (!strcmp(value
, "allow-any")) {
347 arg_missing_action
= MA_ALLOW_ANY
;
348 fetch_if_missing
= 0;
352 if (!strcmp(value
, "print")) {
353 arg_missing_action
= MA_PRINT
;
354 fetch_if_missing
= 0;
358 if (!strcmp(value
, "allow-promisor")) {
359 arg_missing_action
= MA_ALLOW_PROMISOR
;
360 fetch_if_missing
= 0;
367 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
369 struct rev_info revs
;
370 struct rev_list_info info
;
371 struct setup_revision_opt s_r_opt
= {
372 .allow_exclude_promisor_objects
= 1,
376 int bisect_show_vars
= 0;
377 int bisect_find_all
= 0;
378 int use_bitmap_index
= 0;
379 const char *show_progress
= NULL
;
381 if (argc
== 2 && !strcmp(argv
[1], "-h"))
382 usage(rev_list_usage
);
384 git_config(git_default_config
, NULL
);
385 repo_init_revisions(the_repository
, &revs
, prefix
);
386 revs
.abbrev
= DEFAULT_ABBREV
;
387 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
390 * Scan the argument list before invoking setup_revisions(), so that we
391 * know if fetch_if_missing needs to be set to 0.
393 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
394 * by not crossing the boundary from realized objects to promisor
397 * Let "--missing" to conditionally set fetch_if_missing.
399 for (i
= 1; i
< argc
; i
++) {
400 const char *arg
= argv
[i
];
401 if (!strcmp(arg
, "--exclude-promisor-objects")) {
402 fetch_if_missing
= 0;
403 revs
.exclude_promisor_objects
= 1;
407 for (i
= 1; i
< argc
; i
++) {
408 const char *arg
= argv
[i
];
409 if (skip_prefix(arg
, "--missing=", &arg
)) {
410 if (revs
.exclude_promisor_objects
)
411 die(_("cannot combine --exclude-promisor-objects and --missing"));
412 if (parse_missing_action_value(arg
))
417 if (arg_missing_action
)
418 revs
.do_not_die_on_missing_tree
= 1;
420 argc
= setup_revisions(argc
, argv
, &revs
, &s_r_opt
);
422 memset(&info
, 0, sizeof(info
));
427 if (revs
.diffopt
.flags
.quick
)
428 info
.flags
|= REV_LIST_QUIET
;
429 for (i
= 1 ; i
< argc
; i
++) {
430 const char *arg
= argv
[i
];
432 if (!strcmp(arg
, "--header")) {
433 revs
.verbose_header
= 1;
436 if (!strcmp(arg
, "--timestamp")) {
437 info
.show_timestamp
= 1;
440 if (!strcmp(arg
, "--bisect")) {
444 if (!strcmp(arg
, "--bisect-all")) {
447 info
.flags
|= BISECT_SHOW_ALL
;
448 revs
.show_decorations
= 1;
451 if (!strcmp(arg
, "--bisect-vars")) {
453 bisect_show_vars
= 1;
456 if (!strcmp(arg
, "--use-bitmap-index")) {
457 use_bitmap_index
= 1;
460 if (!strcmp(arg
, "--test-bitmap")) {
461 test_bitmap_walk(&revs
);
464 if (skip_prefix(arg
, "--progress=", &arg
)) {
469 if (skip_prefix(arg
, ("--" CL_ARG__FILTER
"="), &arg
)) {
470 parse_list_objects_filter(&filter_options
, arg
);
471 if (filter_options
.choice
&& !revs
.blob_objects
)
472 die(_("object filtering requires --objects"));
475 if (!strcmp(arg
, ("--no-" CL_ARG__FILTER
))) {
476 list_objects_filter_set_no_filter(&filter_options
);
479 if (!strcmp(arg
, "--filter-print-omitted")) {
480 arg_print_omitted
= 1;
484 if (!strcmp(arg
, "--exclude-promisor-objects"))
485 continue; /* already handled above */
486 if (skip_prefix(arg
, "--missing=", &arg
))
487 continue; /* already handled above */
489 if (!strcmp(arg
, ("--no-object-names"))) {
490 arg_show_object_names
= 0;
494 if (!strcmp(arg
, ("--object-names"))) {
495 arg_show_object_names
= 1;
499 usage(rev_list_usage
);
502 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
503 /* The command line has a --pretty */
504 info
.hdr_termination
= '\n';
505 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
506 info
.header_prefix
= "";
508 info
.header_prefix
= "commit ";
510 else if (revs
.verbose_header
)
511 /* Only --header was specified */
512 revs
.commit_format
= CMIT_FMT_RAW
;
514 if ((!revs
.commits
&& reflog_walk_empty(revs
.reflog_info
) &&
515 (!(revs
.tag_objects
|| revs
.tree_objects
|| revs
.blob_objects
) &&
517 !revs
.rev_input_given
&& !revs
.read_from_stdin
) ||
519 usage(rev_list_usage
);
522 die(_("rev-list does not support display of notes"));
524 if (filter_options
.choice
&& use_bitmap_index
)
525 die(_("cannot combine --use-bitmap-index with object filtering"));
527 save_commit_buffer
= (revs
.verbose_header
||
528 revs
.grep_filter
.pattern_list
||
529 revs
.grep_filter
.header_list
);
534 progress
= start_delayed_progress(show_progress
, 0);
536 if (use_bitmap_index
&& !revs
.prune
) {
537 if (revs
.count
&& !revs
.left_right
&& !revs
.cherry_mark
) {
538 uint32_t commit_count
;
539 int max_count
= revs
.max_count
;
540 struct bitmap_index
*bitmap_git
;
541 if ((bitmap_git
= prepare_bitmap_walk(&revs
))) {
542 count_bitmap_commit_list(bitmap_git
, &commit_count
, NULL
, NULL
, NULL
);
543 if (max_count
>= 0 && max_count
< commit_count
)
544 commit_count
= max_count
;
545 printf("%d\n", commit_count
);
546 free_bitmap_index(bitmap_git
);
549 } else if (revs
.max_count
< 0 &&
550 revs
.tag_objects
&& revs
.tree_objects
&& revs
.blob_objects
) {
551 struct bitmap_index
*bitmap_git
;
552 if ((bitmap_git
= prepare_bitmap_walk(&revs
))) {
553 traverse_bitmap_commit_list(bitmap_git
, &show_object_fast
);
554 free_bitmap_index(bitmap_git
);
560 if (prepare_revision_walk(&revs
))
561 die("revision walk setup failed");
562 if (revs
.tree_objects
)
563 mark_edges_uninteresting(&revs
, show_edge
, 0);
568 find_bisection(&revs
.commits
, &reaches
, &all
, bisect_find_all
);
570 if (bisect_show_vars
)
571 return show_bisect_vars(&info
, reaches
, all
);
574 if (arg_print_omitted
)
575 oidset_init(&omitted_objects
, DEFAULT_OIDSET_SIZE
);
576 if (arg_missing_action
== MA_PRINT
)
577 oidset_init(&missing_objects
, DEFAULT_OIDSET_SIZE
);
579 traverse_commit_list_filtered(
580 &filter_options
, &revs
, show_commit
, show_object
, &info
,
581 (arg_print_omitted
? &omitted_objects
: NULL
));
583 if (arg_print_omitted
) {
584 struct oidset_iter iter
;
585 struct object_id
*oid
;
586 oidset_iter_init(&omitted_objects
, &iter
);
587 while ((oid
= oidset_iter_next(&iter
)))
588 printf("~%s\n", oid_to_hex(oid
));
589 oidset_clear(&omitted_objects
);
591 if (arg_missing_action
== MA_PRINT
) {
592 struct oidset_iter iter
;
593 struct object_id
*oid
;
594 oidset_iter_init(&missing_objects
, &iter
);
595 while ((oid
= oidset_iter_next(&iter
)))
596 printf("?%s\n", oid_to_hex(oid
));
597 oidset_clear(&missing_objects
);
600 stop_progress(&progress
);
603 if (revs
.left_right
&& revs
.cherry_mark
)
604 printf("%d\t%d\t%d\n", revs
.count_left
, revs
.count_right
, revs
.count_same
);
605 else if (revs
.left_right
)
606 printf("%d\t%d\n", revs
.count_left
, revs
.count_right
);
607 else if (revs
.cherry_mark
)
608 printf("%d\t%d\n", revs
.count_left
+ revs
.count_right
, revs
.count_same
);
610 printf("%d\n", revs
.count_left
+ revs
.count_right
);