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 graph_show_commit(revs
->graph
);
58 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
62 finish_commit(commit
, data
);
66 if (info
->show_timestamp
)
67 printf("%lu ", commit
->date
);
68 if (info
->header_prefix
)
69 fputs(info
->header_prefix
, stdout
);
72 fputs(get_revision_mark(revs
, commit
), stdout
);
73 if (revs
->abbrev_commit
&& revs
->abbrev
)
74 fputs(find_unique_abbrev(commit
->object
.sha1
, revs
->abbrev
),
77 fputs(sha1_to_hex(commit
->object
.sha1
), stdout
);
78 if (revs
->print_parents
) {
79 struct commit_list
*parents
= commit
->parents
;
81 printf(" %s", sha1_to_hex(parents
->item
->object
.sha1
));
82 parents
= parents
->next
;
85 if (revs
->children
.name
) {
86 struct commit_list
*children
;
88 children
= lookup_decoration(&revs
->children
, &commit
->object
);
90 printf(" %s", sha1_to_hex(children
->item
->object
.sha1
));
91 children
= children
->next
;
94 show_decorations(revs
, commit
);
95 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
100 if (revs
->verbose_header
&& commit
->buffer
) {
101 struct strbuf buf
= STRBUF_INIT
;
102 struct pretty_print_context ctx
= {0};
103 ctx
.abbrev
= revs
->abbrev
;
104 ctx
.date_mode
= revs
->date_mode
;
105 pretty_print_commit(revs
->commit_format
, commit
, &buf
, &ctx
);
108 if (revs
->commit_format
!= CMIT_FMT_ONELINE
)
109 graph_show_oneline(revs
->graph
);
111 graph_show_commit_msg(revs
->graph
, &buf
);
114 * Add a newline after the commit message.
116 * Usually, this newline produces a blank
117 * padding line between entries, in which case
118 * we need to add graph padding on this line.
120 * However, the commit message may not end in a
121 * newline. In this case the newline simply
122 * ends the last line of the commit message,
123 * and we don't need any graph output. (This
124 * always happens with CMIT_FMT_ONELINE, and it
125 * happens with CMIT_FMT_USERFORMAT when the
126 * format doesn't explicitly end in a newline.)
128 if (buf
.len
&& buf
.buf
[buf
.len
- 1] == '\n')
129 graph_show_padding(revs
->graph
);
133 * If the message buffer is empty, just show
134 * the rest of the graph output for this
137 if (graph_show_remainder(revs
->graph
))
139 if (revs
->commit_format
== CMIT_FMT_ONELINE
)
143 if (revs
->commit_format
!= CMIT_FMT_USERFORMAT
||
145 fwrite(buf
.buf
, 1, buf
.len
, stdout
);
146 putchar(info
->hdr_termination
);
149 strbuf_release(&buf
);
151 if (graph_show_remainder(revs
->graph
))
154 maybe_flush_or_die(stdout
, "stdout");
155 finish_commit(commit
, data
);
158 static void finish_commit(struct commit
*commit
, void *data
)
160 if (commit
->parents
) {
161 free_commit_list(commit
->parents
);
162 commit
->parents
= NULL
;
164 free(commit
->buffer
);
165 commit
->buffer
= NULL
;
168 static void finish_object(struct object
*obj
, const struct name_path
*path
, const char *name
)
170 if (obj
->type
== OBJ_BLOB
&& !has_sha1_file(obj
->sha1
))
171 die("missing blob object '%s'", sha1_to_hex(obj
->sha1
));
174 static void show_object(struct object
*obj
, const struct name_path
*path
, const char *component
)
176 char *name
= path_name(path
, component
);
177 /* An object with name "foo\n0000000..." can be used to
178 * confuse downstream "git pack-objects" very badly.
180 const char *ep
= strchr(name
, '\n');
182 finish_object(obj
, path
, name
);
184 printf("%s %.*s\n", sha1_to_hex(obj
->sha1
),
189 printf("%s %s\n", sha1_to_hex(obj
->sha1
), name
);
193 static void show_edge(struct commit
*commit
)
195 printf("-%s\n", sha1_to_hex(commit
->object
.sha1
));
198 static inline int log2i(int n
)
202 for (; n
> 1; n
>>= 1)
208 static inline int exp2i(int n
)
214 * Estimate the number of bisect steps left (after the current step)
216 * For any x between 0 included and 2^n excluded, the probability for
217 * n - 1 steps left looks like:
219 * P(2^n + x) == (2^n - x) / (2^n + x)
221 * and P(2^n + x) < 0.5 means 2^n < 3x
223 int estimate_bisect_steps(int all
)
234 return (e
< 3 * x
) ? n
: n
- 1;
237 void print_commit_list(struct commit_list
*list
,
238 const char *format_cur
,
239 const char *format_last
)
241 for ( ; list
; list
= list
->next
) {
242 const char *format
= list
->next
? format_cur
: format_last
;
243 printf(format
, sha1_to_hex(list
->item
->object
.sha1
));
247 static void show_tried_revs(struct commit_list
*tried
)
249 printf("bisect_tried='");
250 print_commit_list(tried
, "%s|", "%s");
254 static void print_var_str(const char *var
, const char *val
)
256 printf("%s='%s'\n", var
, val
);
259 static void print_var_int(const char *var
, int val
)
261 printf("%s=%d\n", var
, val
);
264 static int show_bisect_vars(struct rev_list_info
*info
, int reaches
, int all
)
266 int cnt
, flags
= info
->bisect_show_flags
;
268 struct commit_list
*tried
;
269 struct rev_info
*revs
= info
->revs
;
271 if (!revs
->commits
&& !(flags
& BISECT_SHOW_TRIED
))
274 revs
->commits
= filter_skipped(revs
->commits
, &tried
,
275 flags
& BISECT_SHOW_ALL
,
279 * revs->commits can reach "reaches" commits among
280 * "all" commits. If it is good, then there are
281 * (all-reaches) commits left to be bisected.
282 * On the other hand, if it is bad, then the set
283 * to bisect is "reaches".
284 * A bisect set of size N has (N-1) commits further
285 * to test, as we already know one bad one.
292 strcpy(hex
, sha1_to_hex(revs
->commits
->item
->object
.sha1
));
294 if (flags
& BISECT_SHOW_ALL
) {
295 traverse_commit_list(revs
, show_commit
, show_object
, info
);
299 if (flags
& BISECT_SHOW_TRIED
)
300 show_tried_revs(tried
);
302 print_var_str("bisect_rev", hex
);
303 print_var_int("bisect_nr", cnt
- 1);
304 print_var_int("bisect_good", all
- reaches
- 1);
305 print_var_int("bisect_bad", reaches
- 1);
306 print_var_int("bisect_all", all
);
307 print_var_int("bisect_steps", estimate_bisect_steps(all
));
312 int cmd_rev_list(int argc
, const char **argv
, const char *prefix
)
314 struct rev_info revs
;
315 struct rev_list_info info
;
318 int bisect_show_vars
= 0;
319 int bisect_find_all
= 0;
322 git_config(git_default_config
, NULL
);
323 init_revisions(&revs
, prefix
);
324 revs
.abbrev
= DEFAULT_ABBREV
;
325 revs
.commit_format
= CMIT_FMT_UNSPECIFIED
;
326 argc
= setup_revisions(argc
, argv
, &revs
, NULL
);
328 memset(&info
, 0, sizeof(info
));
333 quiet
= DIFF_OPT_TST(&revs
.diffopt
, QUICK
);
334 for (i
= 1 ; i
< argc
; i
++) {
335 const char *arg
= argv
[i
];
337 if (!strcmp(arg
, "--header")) {
338 revs
.verbose_header
= 1;
341 if (!strcmp(arg
, "--timestamp")) {
342 info
.show_timestamp
= 1;
345 if (!strcmp(arg
, "--bisect")) {
349 if (!strcmp(arg
, "--bisect-all")) {
352 info
.bisect_show_flags
= BISECT_SHOW_ALL
;
353 revs
.show_decorations
= 1;
356 if (!strcmp(arg
, "--bisect-vars")) {
358 bisect_show_vars
= 1;
361 usage(rev_list_usage
);
364 if (revs
.commit_format
!= CMIT_FMT_UNSPECIFIED
) {
365 /* The command line has a --pretty */
366 info
.hdr_termination
= '\n';
367 if (revs
.commit_format
== CMIT_FMT_ONELINE
)
368 info
.header_prefix
= "";
370 info
.header_prefix
= "commit ";
372 else if (revs
.verbose_header
)
373 /* Only --header was specified */
374 revs
.commit_format
= CMIT_FMT_RAW
;
376 if ((!revs
.commits
&&
377 (!(revs
.tag_objects
||revs
.tree_objects
||revs
.blob_objects
) &&
378 !revs
.pending
.nr
)) ||
380 usage(rev_list_usage
);
382 save_commit_buffer
= (revs
.verbose_header
||
383 revs
.grep_filter
.pattern_list
||
384 revs
.grep_filter
.header_list
);
388 if (prepare_revision_walk(&revs
))
389 die("revision walk setup failed");
390 if (revs
.tree_objects
)
391 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
394 int reaches
= reaches
, all
= all
;
396 revs
.commits
= find_bisection(revs
.commits
, &reaches
, &all
,
399 if (bisect_show_vars
)
400 return show_bisect_vars(&info
, reaches
, all
);
403 traverse_commit_list(&revs
,
404 quiet
? finish_commit
: show_commit
,
405 quiet
? finish_object
: show_object
,
410 printf("%d\t%d\n", revs
.count_left
, revs
.count_right
);
412 printf("%d\n", revs
.count_left
+ revs
.count_right
);