6 #include "list-objects.h"
8 #include "pack-bitmap.h"
15 static const char rev_list_usage
[] =
16 "git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
19 " --max-age=<epoch>\n"
20 " --min-age=<epoch>\n"
23 " --min-parents=<n>\n"
25 " --max-parents=<n>\n"
38 " formatting output:\n"
41 " --objects | --objects-edge\n"
43 " --header | --pretty\n"
44 " --abbrev=<n> | --no-abbrev\n"
54 static struct progress
*progress
;
55 static unsigned progress_counter
;
57 static void finish_commit(struct commit
*commit
, void *data
);
58 static void show_commit(struct commit
*commit
, void *data
)
60 struct rev_list_info
*info
= data
;
61 struct rev_info
*revs
= info
->revs
;
63 display_progress(progress
, ++progress_counter
);
65 if (info
->flags
& REV_LIST_QUIET
) {
66 finish_commit(commit
, data
);
70 graph_show_commit(revs
->graph
);
73 if (commit
->object
.flags
& PATCHSAME
)
75 else if (commit
->object
.flags
& SYMMETRIC_LEFT
)
79 finish_commit(commit
, data
);
83 if (info
->show_timestamp
)
84 printf("%"PRItime
" ", commit
->date
);
85 if (info
->header_prefix
)
86 fputs(info
->header_prefix
, stdout
);
89 fputs(get_revision_mark(revs
, commit
), stdout
);
90 if (revs
->abbrev_commit
&& revs
->abbrev
)
91 fputs(find_unique_abbrev(commit
->object
.oid
.hash
, revs
->abbrev
),
94 fputs(oid_to_hex(&commit
->object
.oid
), stdout
);
95 if (revs
->print_parents
) {
96 struct commit_list
*parents
= commit
->parents
;
98 printf(" %s", oid_to_hex(&parents
->item
->object
.oid
));
99 parents
= parents
->next
;
102 if (revs
->children
.name
) {
103 struct commit_list
*children
;
105 children
= lookup_decoration(&revs
->children
, &commit
->object
);
107 printf(" %s", oid_to_hex(&children
->item
->object
.oid
));
108 children
= children
->next
;
111 show_decorations(revs
, commit
);
112 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
117 if (revs
->verbose_header
&& get_cached_commit_buffer(commit
, NULL
)) {
118 struct strbuf buf
= STRBUF_INIT
;
119 struct pretty_print_context ctx
= {0};
120 ctx
.abbrev
= revs
->abbrev
;
121 ctx
.date_mode
= revs
->date_mode
;
122 ctx
.date_mode_explicit
= revs
->date_mode_explicit
;
123 ctx
.fmt
= revs
->commit_format
;
124 ctx
.output_encoding
= get_log_output_encoding();
125 pretty_print_commit(&ctx
, commit
, &buf
);
127 if (revs
->commit_format
!= CMIT_FMT_ONELINE
)
128 graph_show_oneline(revs
->graph
);
130 graph_show_commit_msg(revs
->graph
, stdout
, &buf
);
133 * Add a newline after the commit message.
135 * Usually, this newline produces a blank
136 * padding line between entries, in which case
137 * we need to add graph padding on this line.
139 * However, the commit message may not end in a
140 * newline. In this case the newline simply
141 * ends the last line of the commit message,
142 * and we don't need any graph output. (This
143 * always happens with CMIT_FMT_ONELINE, and it
144 * happens with CMIT_FMT_USERFORMAT when the
145 * format doesn't explicitly end in a newline.)
147 if (buf
.len
&& buf
.buf
[buf
.len
- 1] == '\n')
148 graph_show_padding(revs
->graph
);
149 putchar(info
->hdr_termination
);
152 * If the message buffer is empty, just show
153 * the rest of the graph output for this
156 if (graph_show_remainder(revs
->graph
))
158 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
161 strbuf_release(&buf
);
163 if (graph_show_remainder(revs
->graph
))
166 maybe_flush_or_die(stdout
, "stdout");
167 finish_commit(commit
, data
);
170 static void finish_commit(struct commit
*commit
, void *data
)
172 if (commit
->parents
) {
173 free_commit_list(commit
->parents
);
174 commit
->parents
= NULL
;
176 free_commit_buffer(commit
);
179 static void finish_object(struct object
*obj
, const char *name
, void *cb_data
)
181 struct rev_list_info
*info
= cb_data
;
182 if (obj
->type
== OBJ_BLOB
&& !has_object_file(&obj
->oid
))
183 die("missing blob object '%s'", oid_to_hex(&obj
->oid
));
184 if (info
->revs
->verify_objects
&& !obj
->parsed
&& obj
->type
!= OBJ_COMMIT
)
185 parse_object(&obj
->oid
);
188 static void show_object(struct object
*obj
, const char *name
, void *cb_data
)
190 struct rev_list_info
*info
= cb_data
;
191 finish_object(obj
, name
, cb_data
);
192 display_progress(progress
, ++progress_counter
);
193 if (info
->flags
& REV_LIST_QUIET
)
195 show_object_with_name(stdout
, obj
, name
);
198 static void show_edge(struct commit
*commit
)
200 printf("-%s\n", oid_to_hex(&commit
->object
.oid
));
203 static void print_var_str(const char *var
, const char *val
)
205 printf("%s='%s'\n", var
, val
);
208 static void print_var_int(const char *var
, int val
)
210 printf("%s=%d\n", var
, val
);
213 static int show_bisect_vars(struct rev_list_info
*info
, int reaches
, int all
)
215 int cnt
, flags
= info
->flags
;
216 char hex
[GIT_MAX_HEXSZ
+ 1] = "";
217 struct commit_list
*tried
;
218 struct rev_info
*revs
= info
->revs
;
223 revs
->commits
= filter_skipped(revs
->commits
, &tried
,
224 flags
& BISECT_SHOW_ALL
,
228 * revs->commits can reach "reaches" commits among
229 * "all" commits. If it is good, then there are
230 * (all-reaches) commits left to be bisected.
231 * On the other hand, if it is bad, then the set
232 * to bisect is "reaches".
233 * A bisect set of size N has (N-1) commits further
234 * to test, as we already know one bad one.
241 oid_to_hex_r(hex
, &revs
->commits
->item
->object
.oid
);
243 if (flags
& BISECT_SHOW_ALL
) {
244 traverse_commit_list(revs
, show_commit
, show_object
, info
);
248 print_var_str("bisect_rev", hex
);
249 print_var_int("bisect_nr", cnt
- 1);
250 print_var_int("bisect_good", all
- reaches
- 1);
251 print_var_int("bisect_bad", reaches
- 1);
252 print_var_int("bisect_all", all
);
253 print_var_int("bisect_steps", estimate_bisect_steps(all
));
258 static int show_object_fast(
259 const unsigned char *sha1
,
260 enum object_type type
,
263 struct packed_git
*found_pack
,
266 fprintf(stdout
, "%s\n", sha1_to_hex(sha1
));
270 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
272 struct rev_info revs
;
273 struct rev_list_info info
;
276 int bisect_show_vars
= 0;
277 int bisect_find_all
= 0;
278 int use_bitmap_index
= 0;
279 const char *show_progress
= NULL
;
281 if (argc
== 2 && !strcmp(argv
[1], "-h"))
282 usage(rev_list_usage
);
284 git_config(git_default_config
, NULL
);
285 init_revisions(&revs
, prefix
);
286 revs
.abbrev
= DEFAULT_ABBREV
;
287 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
288 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
290 memset(&info
, 0, sizeof(info
));
295 if (DIFF_OPT_TST(&revs
.diffopt
, QUICK
))
296 info
.flags
|= REV_LIST_QUIET
;
297 for (i
= 1 ; i
< argc
; i
++) {
298 const char *arg
= argv
[i
];
300 if (!strcmp(arg
, "--header")) {
301 revs
.verbose_header
= 1;
304 if (!strcmp(arg
, "--timestamp")) {
305 info
.show_timestamp
= 1;
308 if (!strcmp(arg
, "--bisect")) {
312 if (!strcmp(arg
, "--bisect-all")) {
315 info
.flags
|= BISECT_SHOW_ALL
;
316 revs
.show_decorations
= 1;
319 if (!strcmp(arg
, "--bisect-vars")) {
321 bisect_show_vars
= 1;
324 if (!strcmp(arg
, "--use-bitmap-index")) {
325 use_bitmap_index
= 1;
328 if (!strcmp(arg
, "--test-bitmap")) {
329 test_bitmap_walk(&revs
);
332 if (skip_prefix(arg
, "--progress=", &arg
)) {
336 usage(rev_list_usage
);
339 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
340 /* The command line has a --pretty */
341 info
.hdr_termination
= '\n';
342 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
343 info
.header_prefix
= "";
345 info
.header_prefix
= "commit ";
347 else if (revs
.verbose_header
)
348 /* Only --header was specified */
349 revs
.commit_format
= CMIT_FMT_RAW
;
351 if ((!revs
.commits
&&
352 (!(revs
.tag_objects
|| revs
.tree_objects
|| revs
.blob_objects
) &&
353 !revs
.pending
.nr
)) ||
355 usage(rev_list_usage
);
358 die(_("rev-list does not support display of notes"));
360 save_commit_buffer
= (revs
.verbose_header
||
361 revs
.grep_filter
.pattern_list
||
362 revs
.grep_filter
.header_list
);
367 progress
= start_progress_delay(show_progress
, 0, 0, 2);
369 if (use_bitmap_index
&& !revs
.prune
) {
370 if (revs
.count
&& !revs
.left_right
&& !revs
.cherry_mark
) {
371 uint32_t commit_count
;
372 int max_count
= revs
.max_count
;
373 if (!prepare_bitmap_walk(&revs
)) {
374 count_bitmap_commit_list(&commit_count
, NULL
, NULL
, NULL
);
375 if (max_count
>= 0 && max_count
< commit_count
)
376 commit_count
= max_count
;
377 printf("%d\n", commit_count
);
380 } else if (revs
.max_count
< 0 &&
381 revs
.tag_objects
&& revs
.tree_objects
&& revs
.blob_objects
) {
382 if (!prepare_bitmap_walk(&revs
)) {
383 traverse_bitmap_commit_list(&show_object_fast
);
389 if (prepare_revision_walk(&revs
))
390 die("revision walk setup failed");
391 if (revs
.tree_objects
)
392 mark_edges_uninteresting(&revs
, show_edge
);
395 int reaches
= reaches
, all
= all
;
397 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
400 if (bisect_show_vars
)
401 return show_bisect_vars(&info
, reaches
, all
);
404 traverse_commit_list(&revs
, show_commit
, show_object
, &info
);
406 stop_progress(&progress
);
409 if (revs
.left_right
&& revs
.cherry_mark
)
410 printf("%d\t%d\t%d\n", revs
.count_left
, revs
.count_right
, revs
.count_same
);
411 else if (revs
.left_right
)
412 printf("%d\t%d\n", revs
.count_left
, revs
.count_right
);
413 else if (revs
.cherry_mark
)
414 printf("%d\t%d\n", revs
.count_left
+ revs
.count_right
, revs
.count_same
);
416 printf("%d\n", revs
.count_left
+ revs
.count_right
);