5 #include "list-objects.h"
7 #include "pack-bitmap.h"
13 static const char rev_list_usage
[] =
14 "git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
17 " --max-age=<epoch>\n"
18 " --min-age=<epoch>\n"
21 " --min-parents=<n>\n"
23 " --max-parents=<n>\n"
36 " formatting output:\n"
39 " --objects | --objects-edge\n"
41 " --header | --pretty\n"
42 " --abbrev=<n> | --no-abbrev\n"
51 static void finish_commit(struct commit
*commit
, void *data
);
52 static void show_commit(struct commit
*commit
, void *data
)
54 struct rev_list_info
*info
= data
;
55 struct rev_info
*revs
= info
->revs
;
57 if (info
->flags
& REV_LIST_QUIET
) {
58 finish_commit(commit
, data
);
62 graph_show_commit(revs
->graph
);
65 if (commit
->object
.flags
& PATCHSAME
)
67 else if (commit
->object
.flags
& SYMMETRIC_LEFT
)
71 finish_commit(commit
, data
);
75 if (info
->show_timestamp
)
76 printf("%lu ", commit
->date
);
77 if (info
->header_prefix
)
78 fputs(info
->header_prefix
, stdout
);
81 fputs(get_revision_mark(revs
, commit
), stdout
);
82 if (revs
->abbrev_commit
&& revs
->abbrev
)
83 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
->abbrev
),
86 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
87 if (revs
->print_parents
) {
88 struct commit_list
*parents
= commit
->parents
;
90 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
91 parents
= parents
->next
;
94 if (revs
->children
.name
) {
95 struct commit_list
*children
;
97 children
= lookup_decoration(&revs
->children
, &commit
->object
);
99 printf(" %s", sha1_to_hex(children
->item
->object
.sha1
));
100 children
= children
->next
;
103 show_decorations(revs
, commit
);
104 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
109 if (revs
->verbose_header
&& get_cached_commit_buffer(commit
, NULL
)) {
110 struct strbuf buf
= STRBUF_INIT
;
111 struct pretty_print_context ctx
= {0};
112 ctx
.abbrev
= revs
->abbrev
;
113 ctx
.date_mode
= revs
->date_mode
;
114 ctx
.date_mode_explicit
= revs
->date_mode_explicit
;
115 ctx
.fmt
= revs
->commit_format
;
116 ctx
.output_encoding
= get_log_output_encoding();
117 pretty_print_commit(&ctx
, commit
, &buf
);
120 if (revs
->commit_format
!= CMIT_FMT_ONELINE
)
121 graph_show_oneline(revs
->graph
);
123 graph_show_commit_msg(revs
->graph
, &buf
);
126 * Add a newline after the commit message.
128 * Usually, this newline produces a blank
129 * padding line between entries, in which case
130 * we need to add graph padding on this line.
132 * However, the commit message may not end in a
133 * newline. In this case the newline simply
134 * ends the last line of the commit message,
135 * and we don't need any graph output. (This
136 * always happens with CMIT_FMT_ONELINE, and it
137 * happens with CMIT_FMT_USERFORMAT when the
138 * format doesn't explicitly end in a newline.)
140 if (buf
.len
&& buf
.buf
[buf
.len
- 1] == '\n')
141 graph_show_padding(revs
->graph
);
145 * If the message buffer is empty, just show
146 * the rest of the graph output for this
149 if (graph_show_remainder(revs
->graph
))
151 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
155 if (revs
->commit_format
!= CMIT_FMT_USERFORMAT
||
157 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
158 putchar(info
->hdr_termination
);
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
,
180 const struct name_path
*path
, const char *name
,
183 struct rev_list_info
*info
= cb_data
;
184 if (obj
->type
== OBJ_BLOB
&& !has_sha1_file(obj
->sha1
))
185 die("missing blob object '%s'", sha1_to_hex(obj
->sha1
));
186 if (info
->revs
->verify_objects
&& !obj
->parsed
&& obj
->type
!= OBJ_COMMIT
)
187 parse_object(obj
->sha1
);
190 static void show_object(struct object
*obj
,
191 const struct name_path
*path
, const char *component
,
194 struct rev_list_info
*info
= cb_data
;
195 finish_object(obj
, path
, component
, cb_data
);
196 if (info
->flags
& REV_LIST_QUIET
)
198 show_object_with_name(stdout
, obj
, path
, component
);
201 static void show_edge(struct commit
*commit
)
203 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
206 static void print_var_str(const char *var
, const char *val
)
208 printf("%s='%s'\n", var
, val
);
211 static void print_var_int(const char *var
, int val
)
213 printf("%s=%d\n", var
, val
);
216 static int show_bisect_vars(struct rev_list_info
*info
, int reaches
, int all
)
218 int cnt
, flags
= info
->flags
;
220 struct commit_list
*tried
;
221 struct rev_info
*revs
= info
->revs
;
226 revs
->commits
= filter_skipped(revs
->commits
, &tried
,
227 flags
& BISECT_SHOW_ALL
,
231 * revs->commits can reach "reaches" commits among
232 * "all" commits. If it is good, then there are
233 * (all-reaches) commits left to be bisected.
234 * On the other hand, if it is bad, then the set
235 * to bisect is "reaches".
236 * A bisect set of size N has (N-1) commits further
237 * to test, as we already know one bad one.
244 strcpy(hex
, sha1_to_hex(revs
->commits
->item
->object
.sha1
));
246 if (flags
& BISECT_SHOW_ALL
) {
247 traverse_commit_list(revs
, show_commit
, show_object
, info
);
251 print_var_str("bisect_rev", hex
);
252 print_var_int("bisect_nr", cnt
- 1);
253 print_var_int("bisect_good", all
- reaches
- 1);
254 print_var_int("bisect_bad", reaches
- 1);
255 print_var_int("bisect_all", all
);
256 print_var_int("bisect_steps", estimate_bisect_steps(all
));
261 static int show_object_fast(
262 const unsigned char *sha1
,
263 enum object_type type
,
266 struct packed_git
*found_pack
,
269 fprintf(stdout
, "%s\n", sha1_to_hex(sha1
));
273 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
275 struct rev_info revs
;
276 struct rev_list_info info
;
279 int bisect_show_vars
= 0;
280 int bisect_find_all
= 0;
281 int use_bitmap_index
= 0;
283 git_config(git_default_config
, NULL
);
284 init_revisions(&revs
, prefix
);
285 revs
.abbrev
= DEFAULT_ABBREV
;
286 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
287 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
289 memset(&info
, 0, sizeof(info
));
294 if (DIFF_OPT_TST(&revs
.diffopt
, QUICK
))
295 info
.flags
|= REV_LIST_QUIET
;
296 for (i
= 1 ; i
< argc
; i
++) {
297 const char *arg
= argv
[i
];
299 if (!strcmp(arg
, "--header")) {
300 revs
.verbose_header
= 1;
303 if (!strcmp(arg
, "--timestamp")) {
304 info
.show_timestamp
= 1;
307 if (!strcmp(arg
, "--bisect")) {
311 if (!strcmp(arg
, "--bisect-all")) {
314 info
.flags
|= BISECT_SHOW_ALL
;
315 revs
.show_decorations
= 1;
318 if (!strcmp(arg
, "--bisect-vars")) {
320 bisect_show_vars
= 1;
323 if (!strcmp(arg
, "--use-bitmap-index")) {
324 use_bitmap_index
= 1;
327 if (!strcmp(arg
, "--test-bitmap")) {
328 test_bitmap_walk(&revs
);
331 usage(rev_list_usage
);
334 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
335 /* The command line has a --pretty */
336 info
.hdr_termination
= '\n';
337 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
338 info
.header_prefix
= "";
340 info
.header_prefix
= "commit ";
342 else if (revs
.verbose_header
)
343 /* Only --header was specified */
344 revs
.commit_format
= CMIT_FMT_RAW
;
346 if ((!revs
.commits
&&
347 (!(revs
.tag_objects
|| revs
.tree_objects
|| revs
.blob_objects
) &&
348 !revs
.pending
.nr
)) ||
350 usage(rev_list_usage
);
352 save_commit_buffer
= (revs
.verbose_header
||
353 revs
.grep_filter
.pattern_list
||
354 revs
.grep_filter
.header_list
);
358 if (use_bitmap_index
) {
359 if (revs
.count
&& !revs
.left_right
&& !revs
.cherry_mark
) {
360 uint32_t commit_count
;
361 if (!prepare_bitmap_walk(&revs
)) {
362 count_bitmap_commit_list(&commit_count
, NULL
, NULL
, NULL
);
363 printf("%d\n", commit_count
);
366 } else if (revs
.tag_objects
&& revs
.tree_objects
&& revs
.blob_objects
) {
367 if (!prepare_bitmap_walk(&revs
)) {
368 traverse_bitmap_commit_list(&show_object_fast
);
374 if (prepare_revision_walk(&revs
))
375 die("revision walk setup failed");
376 if (revs
.tree_objects
)
377 mark_edges_uninteresting(&revs
, show_edge
);
380 int reaches
= reaches
, all
= all
;
382 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
385 if (bisect_show_vars
)
386 return show_bisect_vars(&info
, reaches
, all
);
389 traverse_commit_list(&revs
, show_commit
, show_object
, &info
);
392 if (revs
.left_right
&& revs
.cherry_mark
)
393 printf("%d\t%d\t%d\n", revs
.count_left
, revs
.count_right
, revs
.count_same
);
394 else if (revs
.left_right
)
395 printf("%d\t%d\n", revs
.count_left
, revs
.count_right
);
396 else if (revs
.cherry_mark
)
397 printf("%d\t%d\n", revs
.count_left
+ revs
.count_right
, revs
.count_same
);
399 printf("%d\n", revs
.count_left
+ revs
.count_right
);