6 #include "list-objects.h"
8 #include "pack-bitmap.h"
14 #include "reflog-walk.h"
16 static const char rev_list_usage[] =
17 "git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
20 " --max-age=<epoch>\n"
21 " --min-age=<epoch>\n"
24 " --min-parents=<n>\n"
26 " --max-parents=<n>\n"
39 " formatting output:\n"
42 " --objects | --objects-edge\n"
44 " --header | --pretty\n"
45 " --abbrev=<n> | --no-abbrev\n"
55 static struct progress *progress;
56 static unsigned progress_counter;
58 static void finish_commit(struct commit *commit, void *data);
59 static void show_commit(struct commit *commit, void *data)
61 struct rev_list_info *info = data;
62 struct rev_info *revs = info->revs;
64 display_progress(progress, ++progress_counter);
66 if (info->flags & REV_LIST_QUIET) {
67 finish_commit(commit, data);
71 graph_show_commit(revs->graph);
74 if (commit->object.flags & PATCHSAME)
76 else if (commit->object.flags & SYMMETRIC_LEFT)
80 finish_commit(commit, data);
84 if (info->show_timestamp)
85 printf("%"PRItime" ", commit->date);
86 if (info->header_prefix)
87 fputs(info->header_prefix, stdout);
90 fputs(get_revision_mark(revs, commit), stdout);
91 if (revs->abbrev_commit && revs->abbrev)
92 fputs(find_unique_abbrev(commit->object.oid.hash, revs->abbrev),
95 fputs(oid_to_hex(&commit->object.oid), stdout);
96 if (revs->print_parents) {
97 struct commit_list *parents = commit->parents;
99 printf(" %s", oid_to_hex(&parents->item->object.oid));
100 parents = parents->next;
103 if (revs->children.name) {
104 struct commit_list *children;
106 children = lookup_decoration(&revs->children, &commit->object);
108 printf(" %s", oid_to_hex(&children->item->object.oid));
109 children = children->next;
112 show_decorations(revs, commit);
113 if (revs->commit_format == CMIT_FMT_ONELINE)
118 if (revs->verbose_header && get_cached_commit_buffer(commit, NULL)) {
119 struct strbuf buf = STRBUF_INIT;
120 struct pretty_print_context ctx = {0};
121 ctx.abbrev = revs->abbrev;
122 ctx.date_mode = revs->date_mode;
123 ctx.date_mode_explicit = revs->date_mode_explicit;
124 ctx.fmt = revs->commit_format;
125 ctx.output_encoding = get_log_output_encoding();
126 ctx.color = revs->diffopt.use_color;
127 pretty_print_commit(&ctx, commit, &buf);
129 if (revs->commit_format != CMIT_FMT_ONELINE)
130 graph_show_oneline(revs->graph);
132 graph_show_commit_msg(revs->graph, stdout, &buf);
135 * Add a newline after the commit message.
137 * Usually, this newline produces a blank
138 * padding line between entries, in which case
139 * we need to add graph padding on this line.
141 * However, the commit message may not end in a
142 * newline. In this case the newline simply
143 * ends the last line of the commit message,
144 * and we don't need any graph output. (This
145 * always happens with CMIT_FMT_ONELINE, and it
146 * happens with CMIT_FMT_USERFORMAT when the
147 * format doesn't explicitly end in a newline.)
149 if (buf.len && buf.buf[buf.len - 1] == '\n')
150 graph_show_padding(revs->graph);
151 putchar(info->hdr_termination);
154 * If the message buffer is empty, just show
155 * the rest of the graph output for this
158 if (graph_show_remainder(revs->graph))
160 if (revs->commit_format == CMIT_FMT_ONELINE)
163 strbuf_release(&buf);
165 if (graph_show_remainder(revs->graph))
168 maybe_flush_or_die(stdout, "stdout");
169 finish_commit(commit, data);
172 static void finish_commit(struct commit *commit, void *data)
174 if (commit->parents) {
175 free_commit_list(commit->parents);
176 commit->parents = NULL;
178 free_commit_buffer(commit);
181 static void finish_object(struct object *obj, const char *name, void *cb_data)
183 struct rev_list_info *info = cb_data;
184 if (obj->type == OBJ_BLOB && !has_object_file(&obj->oid))
185 die("missing blob object '%s'", oid_to_hex(&obj->oid));
186 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
187 parse_object(&obj->oid);
190 static void show_object(struct object *obj, const char *name, void *cb_data)
192 struct rev_list_info *info = cb_data;
193 finish_object(obj, name, cb_data);
194 display_progress(progress, ++progress_counter);
195 if (info->flags & REV_LIST_QUIET)
197 show_object_with_name(stdout, obj, name);
200 static void show_edge(struct commit *commit)
202 printf("-%s\n", oid_to_hex(&commit->object.oid));
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;
218 char hex[GIT_MAX_HEXSZ + 1] = "";
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 oid_to_hex_r(hex, &revs->commits->item->object.oid);
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 static int show_object_fast(
261 const unsigned char *sha1,
262 enum object_type type,
265 struct packed_git *found_pack,
268 fprintf(stdout, "%s\n", sha1_to_hex(sha1));
272 int cmd_rev_list(int argc, const char **argv, const char *prefix)
274 struct rev_info revs;
275 struct rev_list_info info;
278 int bisect_show_vars = 0;
279 int bisect_find_all = 0;
280 int use_bitmap_index = 0;
281 const char *show_progress = NULL;
283 if (argc == 2 && !strcmp(argv[1], "-h"))
284 usage(rev_list_usage);
286 git_config(git_default_config, NULL);
287 init_revisions(&revs, prefix);
288 revs.abbrev = DEFAULT_ABBREV;
289 revs.commit_format = CMIT_FMT_UNSPECIFIED;
290 argc = setup_revisions(argc, argv, &revs, NULL);
292 memset(&info, 0, sizeof(info));
297 if (DIFF_OPT_TST(&revs.diffopt, QUICK))
298 info.flags |= REV_LIST_QUIET;
299 for (i = 1 ; i < argc; i++) {
300 const char *arg = argv[i];
302 if (!strcmp(arg, "--header")) {
303 revs.verbose_header = 1;
306 if (!strcmp(arg, "--timestamp")) {
307 info.show_timestamp = 1;
310 if (!strcmp(arg, "--bisect")) {
314 if (!strcmp(arg, "--bisect-all")) {
317 info.flags |= BISECT_SHOW_ALL;
318 revs.show_decorations = 1;
321 if (!strcmp(arg, "--bisect-vars")) {
323 bisect_show_vars = 1;
326 if (!strcmp(arg, "--use-bitmap-index")) {
327 use_bitmap_index = 1;
330 if (!strcmp(arg, "--test-bitmap")) {
331 test_bitmap_walk(&revs);
334 if (skip_prefix(arg, "--progress=", &arg)) {
338 usage(rev_list_usage);
341 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
342 /* The command line has a --pretty */
343 info.hdr_termination = '\n';
344 if (revs.commit_format == CMIT_FMT_ONELINE)
345 info.header_prefix = "";
347 info.header_prefix = "commit ";
349 else if (revs.verbose_header)
350 /* Only --header was specified */
351 revs.commit_format = CMIT_FMT_RAW;
353 if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
354 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
356 !revs.rev_input_given) ||
358 usage(rev_list_usage);
361 die(_("rev-list does not support display of notes"));
363 save_commit_buffer = (revs.verbose_header ||
364 revs.grep_filter.pattern_list ||
365 revs.grep_filter.header_list);
370 progress = start_delayed_progress(show_progress, 0);
372 if (use_bitmap_index && !revs.prune) {
373 if (revs.count && !revs.left_right && !revs.cherry_mark) {
374 uint32_t commit_count;
375 int max_count = revs.max_count;
376 if (!prepare_bitmap_walk(&revs)) {
377 count_bitmap_commit_list(&commit_count, NULL, NULL, NULL);
378 if (max_count >= 0 && max_count < commit_count)
379 commit_count = max_count;
380 printf("%d\n", commit_count);
383 } else if (revs.max_count < 0 &&
384 revs.tag_objects && revs.tree_objects && revs.blob_objects) {
385 if (!prepare_bitmap_walk(&revs)) {
386 traverse_bitmap_commit_list(&show_object_fast);
392 if (prepare_revision_walk(&revs))
393 die("revision walk setup failed");
394 if (revs.tree_objects)
395 mark_edges_uninteresting(&revs, show_edge);
398 int reaches = reaches, all = all;
400 find_bisection(&revs.commits, &reaches, &all, bisect_find_all);
402 if (bisect_show_vars)
403 return show_bisect_vars(&info, reaches, all);
406 traverse_commit_list(&revs, show_commit, show_object, &info);
408 stop_progress(&progress);
411 if (revs.left_right && revs.cherry_mark)
412 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
413 else if (revs.left_right)
414 printf("%d\t%d\n", revs.count_left, revs.count_right);
415 else if (revs.cherry_mark)
416 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
418 printf("%d\n", revs.count_left + revs.count_right);