1#include "cache.h"
2#include "config.h"
3#include "commit.h"
4#include "diff.h"
5#include "revision.h"
6#include "list-objects.h"
7#include "pack.h"
8#include "pack-bitmap.h"
9#include "builtin.h"
10#include "log-tree.h"
11#include "graph.h"
12#include "bisect.h"
13#include "progress.h"
14
15static const char rev_list_usage[] =
16"git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
17" limiting output:\n"
18" --max-count=<n>\n"
19" --max-age=<epoch>\n"
20" --min-age=<epoch>\n"
21" --sparse\n"
22" --no-merges\n"
23" --min-parents=<n>\n"
24" --no-min-parents\n"
25" --max-parents=<n>\n"
26" --no-max-parents\n"
27" --remove-empty\n"
28" --all\n"
29" --branches\n"
30" --tags\n"
31" --remotes\n"
32" --stdin\n"
33" --quiet\n"
34" ordering output:\n"
35" --topo-order\n"
36" --date-order\n"
37" --reverse\n"
38" formatting output:\n"
39" --parents\n"
40" --children\n"
41" --objects | --objects-edge\n"
42" --unpacked\n"
43" --header | --pretty\n"
44" --abbrev=<n> | --no-abbrev\n"
45" --abbrev-commit\n"
46" --left-right\n"
47" --count\n"
48" special purpose:\n"
49" --bisect\n"
50" --bisect-vars\n"
51" --bisect-all"
52;
53
54static struct progress *progress;
55static unsigned progress_counter;
56
57static void finish_commit(struct commit *commit, void *data);
58static void show_commit(struct commit *commit, void *data)
59{
60 struct rev_list_info *info = data;
61 struct rev_info *revs = info->revs;
62
63 display_progress(progress, ++progress_counter);
64
65 if (info->flags & REV_LIST_QUIET) {
66 finish_commit(commit, data);
67 return;
68 }
69
70 graph_show_commit(revs->graph);
71
72 if (revs->count) {
73 if (commit->object.flags & PATCHSAME)
74 revs->count_same++;
75 else if (commit->object.flags & SYMMETRIC_LEFT)
76 revs->count_left++;
77 else
78 revs->count_right++;
79 finish_commit(commit, data);
80 return;
81 }
82
83 if (info->show_timestamp)
84 printf("%"PRItime" ", commit->date);
85 if (info->header_prefix)
86 fputs(info->header_prefix, stdout);
87
88 if (!revs->graph)
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),
92 stdout);
93 else
94 fputs(oid_to_hex(&commit->object.oid), stdout);
95 if (revs->print_parents) {
96 struct commit_list *parents = commit->parents;
97 while (parents) {
98 printf(" %s", oid_to_hex(&parents->item->object.oid));
99 parents = parents->next;
100 }
101 }
102 if (revs->children.name) {
103 struct commit_list *children;
104
105 children = lookup_decoration(&revs->children, &commit->object);
106 while (children) {
107 printf(" %s", oid_to_hex(&children->item->object.oid));
108 children = children->next;
109 }
110 }
111 show_decorations(revs, commit);
112 if (revs->commit_format == CMIT_FMT_ONELINE)
113 putchar(' ');
114 else
115 putchar('\n');
116
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 ctx.color = revs->diffopt.use_color;
126 pretty_print_commit(&ctx, commit, &buf);
127 if (buf.len) {
128 if (revs->commit_format != CMIT_FMT_ONELINE)
129 graph_show_oneline(revs->graph);
130
131 graph_show_commit_msg(revs->graph, stdout, &buf);
132
133 /*
134 * Add a newline after the commit message.
135 *
136 * Usually, this newline produces a blank
137 * padding line between entries, in which case
138 * we need to add graph padding on this line.
139 *
140 * However, the commit message may not end in a
141 * newline. In this case the newline simply
142 * ends the last line of the commit message,
143 * and we don't need any graph output. (This
144 * always happens with CMIT_FMT_ONELINE, and it
145 * happens with CMIT_FMT_USERFORMAT when the
146 * format doesn't explicitly end in a newline.)
147 */
148 if (buf.len && buf.buf[buf.len - 1] == '\n')
149 graph_show_padding(revs->graph);
150 putchar(info->hdr_termination);
151 } else {
152 /*
153 * If the message buffer is empty, just show
154 * the rest of the graph output for this
155 * commit.
156 */
157 if (graph_show_remainder(revs->graph))
158 putchar('\n');
159 if (revs->commit_format == CMIT_FMT_ONELINE)
160 putchar('\n');
161 }
162 strbuf_release(&buf);
163 } else {
164 if (graph_show_remainder(revs->graph))
165 putchar('\n');
166 }
167 maybe_flush_or_die(stdout, "stdout");
168 finish_commit(commit, data);
169}
170
171static void finish_commit(struct commit *commit, void *data)
172{
173 if (commit->parents) {
174 free_commit_list(commit->parents);
175 commit->parents = NULL;
176 }
177 free_commit_buffer(commit);
178}
179
180static void finish_object(struct object *obj, const char *name, void *cb_data)
181{
182 struct rev_list_info *info = cb_data;
183 if (obj->type == OBJ_BLOB && !has_object_file(&obj->oid))
184 die("missing blob object '%s'", oid_to_hex(&obj->oid));
185 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
186 parse_object(&obj->oid);
187}
188
189static void show_object(struct object *obj, const char *name, void *cb_data)
190{
191 struct rev_list_info *info = cb_data;
192 finish_object(obj, name, cb_data);
193 display_progress(progress, ++progress_counter);
194 if (info->flags & REV_LIST_QUIET)
195 return;
196 show_object_with_name(stdout, obj, name);
197}
198
199static void show_edge(struct commit *commit)
200{
201 printf("-%s\n", oid_to_hex(&commit->object.oid));
202}
203
204static void print_var_str(const char *var, const char *val)
205{
206 printf("%s='%s'\n", var, val);
207}
208
209static void print_var_int(const char *var, int val)
210{
211 printf("%s=%d\n", var, val);
212}
213
214static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
215{
216 int cnt, flags = info->flags;
217 char hex[GIT_MAX_HEXSZ + 1] = "";
218 struct commit_list *tried;
219 struct rev_info *revs = info->revs;
220
221 if (!revs->commits)
222 return 1;
223
224 revs->commits = filter_skipped(revs->commits, &tried,
225 flags & BISECT_SHOW_ALL,
226 NULL, NULL);
227
228 /*
229 * revs->commits can reach "reaches" commits among
230 * "all" commits. If it is good, then there are
231 * (all-reaches) commits left to be bisected.
232 * On the other hand, if it is bad, then the set
233 * to bisect is "reaches".
234 * A bisect set of size N has (N-1) commits further
235 * to test, as we already know one bad one.
236 */
237 cnt = all - reaches;
238 if (cnt < reaches)
239 cnt = reaches;
240
241 if (revs->commits)
242 oid_to_hex_r(hex, &revs->commits->item->object.oid);
243
244 if (flags & BISECT_SHOW_ALL) {
245 traverse_commit_list(revs, show_commit, show_object, info);
246 printf("------\n");
247 }
248
249 print_var_str("bisect_rev", hex);
250 print_var_int("bisect_nr", cnt - 1);
251 print_var_int("bisect_good", all - reaches - 1);
252 print_var_int("bisect_bad", reaches - 1);
253 print_var_int("bisect_all", all);
254 print_var_int("bisect_steps", estimate_bisect_steps(all));
255
256 return 0;
257}
258
259static int show_object_fast(
260 const unsigned char *sha1,
261 enum object_type type,
262 int exclude,
263 uint32_t name_hash,
264 struct packed_git *found_pack,
265 off_t found_offset)
266{
267 fprintf(stdout, "%s\n", sha1_to_hex(sha1));
268 return 1;
269}
270
271int cmd_rev_list(int argc, const char **argv, const char *prefix)
272{
273 struct rev_info revs;
274 struct rev_list_info info;
275 int i;
276 int bisect_list = 0;
277 int bisect_show_vars = 0;
278 int bisect_find_all = 0;
279 int use_bitmap_index = 0;
280 const char *show_progress = NULL;
281
282 if (argc == 2 && !strcmp(argv[1], "-h"))
283 usage(rev_list_usage);
284
285 git_config(git_default_config, NULL);
286 init_revisions(&revs, prefix);
287 revs.abbrev = DEFAULT_ABBREV;
288 revs.commit_format = CMIT_FMT_UNSPECIFIED;
289 argc = setup_revisions(argc, argv, &revs, NULL);
290
291 memset(&info, 0, sizeof(info));
292 info.revs = &revs;
293 if (revs.bisect)
294 bisect_list = 1;
295
296 if (DIFF_OPT_TST(&revs.diffopt, QUICK))
297 info.flags |= REV_LIST_QUIET;
298 for (i = 1 ; i < argc; i++) {
299 const char *arg = argv[i];
300
301 if (!strcmp(arg, "--header")) {
302 revs.verbose_header = 1;
303 continue;
304 }
305 if (!strcmp(arg, "--timestamp")) {
306 info.show_timestamp = 1;
307 continue;
308 }
309 if (!strcmp(arg, "--bisect")) {
310 bisect_list = 1;
311 continue;
312 }
313 if (!strcmp(arg, "--bisect-all")) {
314 bisect_list = 1;
315 bisect_find_all = 1;
316 info.flags |= BISECT_SHOW_ALL;
317 revs.show_decorations = 1;
318 continue;
319 }
320 if (!strcmp(arg, "--bisect-vars")) {
321 bisect_list = 1;
322 bisect_show_vars = 1;
323 continue;
324 }
325 if (!strcmp(arg, "--use-bitmap-index")) {
326 use_bitmap_index = 1;
327 continue;
328 }
329 if (!strcmp(arg, "--test-bitmap")) {
330 test_bitmap_walk(&revs);
331 return 0;
332 }
333 if (skip_prefix(arg, "--progress=", &arg)) {
334 show_progress = arg;
335 continue;
336 }
337 usage(rev_list_usage);
338
339 }
340 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
341 /* The command line has a --pretty */
342 info.hdr_termination = '\n';
343 if (revs.commit_format == CMIT_FMT_ONELINE)
344 info.header_prefix = "";
345 else
346 info.header_prefix = "commit ";
347 }
348 else if (revs.verbose_header)
349 /* Only --header was specified */
350 revs.commit_format = CMIT_FMT_RAW;
351
352 if ((!revs.commits &&
353 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
354 !revs.pending.nr)) ||
355 revs.diff)
356 usage(rev_list_usage);
357
358 if (revs.show_notes)
359 die(_("rev-list does not support display of notes"));
360
361 save_commit_buffer = (revs.verbose_header ||
362 revs.grep_filter.pattern_list ||
363 revs.grep_filter.header_list);
364 if (bisect_list)
365 revs.limited = 1;
366
367 if (show_progress)
368 progress = start_progress_delay(show_progress, 0, 0, 2);
369
370 if (use_bitmap_index && !revs.prune) {
371 if (revs.count && !revs.left_right && !revs.cherry_mark) {
372 uint32_t commit_count;
373 int max_count = revs.max_count;
374 if (!prepare_bitmap_walk(&revs)) {
375 count_bitmap_commit_list(&commit_count, NULL, NULL, NULL);
376 if (max_count >= 0 && max_count < commit_count)
377 commit_count = max_count;
378 printf("%d\n", commit_count);
379 return 0;
380 }
381 } else if (revs.max_count < 0 &&
382 revs.tag_objects && revs.tree_objects && revs.blob_objects) {
383 if (!prepare_bitmap_walk(&revs)) {
384 traverse_bitmap_commit_list(&show_object_fast);
385 return 0;
386 }
387 }
388 }
389
390 if (prepare_revision_walk(&revs))
391 die("revision walk setup failed");
392 if (revs.tree_objects)
393 mark_edges_uninteresting(&revs, show_edge);
394
395 if (bisect_list) {
396 int reaches = reaches, all = all;
397
398 revs.commits = find_bisection(revs.commits, &reaches, &all,
399 bisect_find_all);
400
401 if (bisect_show_vars)
402 return show_bisect_vars(&info, reaches, all);
403 }
404
405 traverse_commit_list(&revs, show_commit, show_object, &info);
406
407 stop_progress(&progress);
408
409 if (revs.count) {
410 if (revs.left_right && revs.cherry_mark)
411 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
412 else if (revs.left_right)
413 printf("%d\t%d\n", revs.count_left, revs.count_right);
414 else if (revs.cherry_mark)
415 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
416 else
417 printf("%d\n", revs.count_left + revs.count_right);
418 }
419
420 return 0;
421}