5 #include "list-objects.h"
11 static const char rev_list_usage
[] =
12 "git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
15 " --max-age=<epoch>\n"
16 " --min-age=<epoch>\n"
19 " --min-parents=<n>\n"
21 " --max-parents=<n>\n"
34 " formatting output:\n"
37 " --objects | --objects-edge\n"
39 " --header | --pretty\n"
40 " --abbrev=<n> | --no-abbrev\n"
49 static void finish_commit(struct commit
*commit
, void *data
);
50 static void show_commit(struct commit
*commit
, void *data
)
52 struct rev_list_info
*info
= data
;
53 struct rev_info
*revs
= info
->revs
;
55 if (info
->flags
& REV_LIST_QUIET
) {
56 finish_commit(commit
, data
);
60 graph_show_commit(revs
->graph
);
63 if (commit
->object
.flags
& PATCHSAME
)
65 else if (commit
->object
.flags
& SYMMETRIC_LEFT
)
69 finish_commit(commit
, data
);
73 if (info
->show_timestamp
)
74 printf("%lu ", commit
->date
);
75 if (info
->header_prefix
)
76 fputs(info
->header_prefix
, stdout
);
79 fputs(get_revision_mark(revs
, commit
), stdout
);
80 if (revs
->abbrev_commit
&& revs
->abbrev
)
81 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
->abbrev
),
84 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
85 if (revs
->print_parents
) {
86 struct commit_list
*parents
= commit
->parents
;
88 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
89 parents
= parents
->next
;
92 if (revs
->children
.name
) {
93 struct commit_list
*children
;
95 children
= lookup_decoration(&revs
->children
, &commit
->object
);
97 printf(" %s", sha1_to_hex(children
->item
->object
.sha1
));
98 children
= children
->next
;
101 show_decorations(revs
, commit
);
102 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
107 if (revs
->verbose_header
&& commit
->buffer
) {
108 struct strbuf buf
= STRBUF_INIT
;
109 struct pretty_print_context ctx
= {0};
110 ctx
.abbrev
= revs
->abbrev
;
111 ctx
.date_mode
= revs
->date_mode
;
112 ctx
.date_mode_explicit
= revs
->date_mode_explicit
;
113 ctx
.fmt
= revs
->commit_format
;
114 ctx
.output_encoding
= get_log_output_encoding();
115 pretty_print_commit(&ctx
, commit
, &buf
);
118 if (revs
->commit_format
!= CMIT_FMT_ONELINE
)
119 graph_show_oneline(revs
->graph
);
121 graph_show_commit_msg(revs
->graph
, &buf
);
124 * Add a newline after the commit message.
126 * Usually, this newline produces a blank
127 * padding line between entries, in which case
128 * we need to add graph padding on this line.
130 * However, the commit message may not end in a
131 * newline. In this case the newline simply
132 * ends the last line of the commit message,
133 * and we don't need any graph output. (This
134 * always happens with CMIT_FMT_ONELINE, and it
135 * happens with CMIT_FMT_USERFORMAT when the
136 * format doesn't explicitly end in a newline.)
138 if (buf
.len
&& buf
.buf
[buf
.len
- 1] == '\n')
139 graph_show_padding(revs
->graph
);
143 * If the message buffer is empty, just show
144 * the rest of the graph output for this
147 if (graph_show_remainder(revs
->graph
))
149 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
153 if (revs
->commit_format
!= CMIT_FMT_USERFORMAT
||
155 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
156 putchar(info
->hdr_termination
);
159 strbuf_release(&buf
);
161 if (graph_show_remainder(revs
->graph
))
164 maybe_flush_or_die(stdout
, "stdout");
165 finish_commit(commit
, data
);
168 static void finish_commit(struct commit
*commit
, void *data
)
170 if (commit
->parents
) {
171 free_commit_list(commit
->parents
);
172 commit
->parents
= NULL
;
174 free(commit
->buffer
);
175 commit
->buffer
= NULL
;
178 static void finish_object(struct object
*obj
,
179 const struct name_path
*path
, const char *name
,
182 struct rev_list_info
*info
= cb_data
;
183 if (obj
->type
== OBJ_BLOB
&& !has_sha1_file(obj
->sha1
))
184 die("missing blob object '%s'", sha1_to_hex(obj
->sha1
));
185 if (info
->revs
->verify_objects
&& !obj
->parsed
&& obj
->type
!= OBJ_COMMIT
)
186 parse_object(obj
->sha1
);
189 static void show_object(struct object
*obj
,
190 const struct name_path
*path
, const char *component
,
193 struct rev_list_info
*info
= cb_data
;
194 finish_object(obj
, path
, component
, cb_data
);
195 if (info
->flags
& REV_LIST_QUIET
)
197 show_object_with_name(stdout
, obj
, path
, component
);
200 static void show_edge(struct commit
*commit
)
202 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
205 static void print_var_str(const char *var
, const char *val
)
207 printf("%s='%s'\n", var
, val
);
210 static void print_var_int(const char *var
, int val
)
212 printf("%s=%d\n", var
, val
);
215 static int show_bisect_vars(struct rev_list_info
*info
, int reaches
, int all
)
217 int cnt
, flags
= info
->flags
;
219 struct commit_list
*tried
;
220 struct rev_info
*revs
= info
->revs
;
225 revs
->commits
= filter_skipped(revs
->commits
, &tried
,
226 flags
& BISECT_SHOW_ALL
,
230 * revs->commits can reach "reaches" commits among
231 * "all" commits. If it is good, then there are
232 * (all-reaches) commits left to be bisected.
233 * On the other hand, if it is bad, then the set
234 * to bisect is "reaches".
235 * A bisect set of size N has (N-1) commits further
236 * to test, as we already know one bad one.
243 strcpy(hex
, sha1_to_hex(revs
->commits
->item
->object
.sha1
));
245 if (flags
& BISECT_SHOW_ALL
) {
246 traverse_commit_list(revs
, show_commit
, show_object
, info
);
250 print_var_str("bisect_rev", hex
);
251 print_var_int("bisect_nr", cnt
- 1);
252 print_var_int("bisect_good", all
- reaches
- 1);
253 print_var_int("bisect_bad", reaches
- 1);
254 print_var_int("bisect_all", all
);
255 print_var_int("bisect_steps", estimate_bisect_steps(all
));
260 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
262 struct rev_info revs
;
263 struct rev_list_info info
;
266 int bisect_show_vars
= 0;
267 int bisect_find_all
= 0;
269 git_config(git_default_config
, NULL
);
270 init_revisions(&revs
, prefix
);
271 revs
.abbrev
= DEFAULT_ABBREV
;
272 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
273 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
275 memset(&info
, 0, sizeof(info
));
280 if (DIFF_OPT_TST(&revs
.diffopt
, QUICK
))
281 info
.flags
|= REV_LIST_QUIET
;
282 for (i
= 1 ; i
< argc
; i
++) {
283 const char *arg
= argv
[i
];
285 if (!strcmp(arg
, "--header")) {
286 revs
.verbose_header
= 1;
289 if (!strcmp(arg
, "--timestamp")) {
290 info
.show_timestamp
= 1;
293 if (!strcmp(arg
, "--bisect")) {
297 if (!strcmp(arg
, "--bisect-all")) {
300 info
.flags
|= BISECT_SHOW_ALL
;
301 revs
.show_decorations
= 1;
304 if (!strcmp(arg
, "--bisect-vars")) {
306 bisect_show_vars
= 1;
309 usage(rev_list_usage
);
312 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
313 /* The command line has a --pretty */
314 info
.hdr_termination
= '\n';
315 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
316 info
.header_prefix
= "";
318 info
.header_prefix
= "commit ";
320 else if (revs
.verbose_header
)
321 /* Only --header was specified */
322 revs
.commit_format
= CMIT_FMT_RAW
;
324 if ((!revs
.commits
&&
325 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
326 !revs
.pending
.nr
)) ||
328 usage(rev_list_usage
);
330 save_commit_buffer
= (revs
.verbose_header
||
331 revs
.grep_filter
.pattern_list
||
332 revs
.grep_filter
.header_list
);
336 if (prepare_revision_walk(&revs
))
337 die("revision walk setup failed");
338 if (revs
.tree_objects
)
339 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
342 int reaches
= reaches
, all
= all
;
344 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
347 if (bisect_show_vars
)
348 return show_bisect_vars(&info
, reaches
, all
);
351 traverse_commit_list(&revs
, show_commit
, show_object
, &info
);
354 if (revs
.left_right
&& revs
.cherry_mark
)
355 printf("%d\t%d\t%d\n", revs
.count_left
, revs
.count_right
, revs
.count_same
);
356 else if (revs
.left_right
)
357 printf("%d\t%d\n", revs
.count_left
, revs
.count_right
);
358 else if (revs
.cherry_mark
)
359 printf("%d\t%d\n", revs
.count_left
+ revs
.count_right
, revs
.count_same
);
361 printf("%d\n", revs
.count_left
+ revs
.count_right
);