6 #include "list-objects.h"
7 #include "list-objects-filter.h"
8 #include "list-objects-filter-options.h"
10 #include "pack-bitmap.h"
16 #include "reflog-walk.h"
19 static const char rev_list_usage[] =
20 "git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
23 " --max-age=<epoch>\n"
24 " --min-age=<epoch>\n"
27 " --min-parents=<n>\n"
29 " --max-parents=<n>\n"
42 " formatting output:\n"
45 " --objects | --objects-edge\n"
47 " --header | --pretty\n"
48 " --abbrev=<n> | --no-abbrev\n"
58 static struct progress *progress;
59 static unsigned progress_counter;
61 static struct list_objects_filter_options filter_options;
62 static struct oidset omitted_objects;
63 static int arg_print_omitted; /* print objects omitted by filter */
65 static struct oidset missing_objects;
67 MA_ERROR = 0, /* fail if any missing objects are encountered */
68 MA_ALLOW_ANY, /* silently allow ALL missing objects */
69 MA_PRINT, /* print ALL missing objects in special section */
71 static enum missing_action arg_missing_action;
73 #define DEFAULT_OIDSET_SIZE (16*1024)
75 static void finish_commit(struct commit *commit, void *data);
76 static void show_commit(struct commit *commit, void *data)
78 struct rev_list_info *info = data;
79 struct rev_info *revs = info->revs;
81 display_progress(progress, ++progress_counter);
83 if (info->flags & REV_LIST_QUIET) {
84 finish_commit(commit, data);
88 graph_show_commit(revs->graph);
91 if (commit->object.flags & PATCHSAME)
93 else if (commit->object.flags & SYMMETRIC_LEFT)
97 finish_commit(commit, data);
101 if (info->show_timestamp)
102 printf("%"PRItime" ", commit->date);
103 if (info->header_prefix)
104 fputs(info->header_prefix, stdout);
107 fputs(get_revision_mark(revs, commit), stdout);
108 if (revs->abbrev_commit && revs->abbrev)
109 fputs(find_unique_abbrev(commit->object.oid.hash, revs->abbrev),
112 fputs(oid_to_hex(&commit->object.oid), stdout);
113 if (revs->print_parents) {
114 struct commit_list *parents = commit->parents;
116 printf(" %s", oid_to_hex(&parents->item->object.oid));
117 parents = parents->next;
120 if (revs->children.name) {
121 struct commit_list *children;
123 children = lookup_decoration(&revs->children, &commit->object);
125 printf(" %s", oid_to_hex(&children->item->object.oid));
126 children = children->next;
129 show_decorations(revs, commit);
130 if (revs->commit_format == CMIT_FMT_ONELINE)
135 if (revs->verbose_header && get_cached_commit_buffer(commit, NULL)) {
136 struct strbuf buf = STRBUF_INIT;
137 struct pretty_print_context ctx = {0};
138 ctx.abbrev = revs->abbrev;
139 ctx.date_mode = revs->date_mode;
140 ctx.date_mode_explicit = revs->date_mode_explicit;
141 ctx.fmt = revs->commit_format;
142 ctx.output_encoding = get_log_output_encoding();
143 ctx.color = revs->diffopt.use_color;
144 pretty_print_commit(&ctx, commit, &buf);
146 if (revs->commit_format != CMIT_FMT_ONELINE)
147 graph_show_oneline(revs->graph);
149 graph_show_commit_msg(revs->graph, stdout, &buf);
152 * Add a newline after the commit message.
154 * Usually, this newline produces a blank
155 * padding line between entries, in which case
156 * we need to add graph padding on this line.
158 * However, the commit message may not end in a
159 * newline. In this case the newline simply
160 * ends the last line of the commit message,
161 * and we don't need any graph output. (This
162 * always happens with CMIT_FMT_ONELINE, and it
163 * happens with CMIT_FMT_USERFORMAT when the
164 * format doesn't explicitly end in a newline.)
166 if (buf.len && buf.buf[buf.len - 1] == '\n')
167 graph_show_padding(revs->graph);
168 putchar(info->hdr_termination);
171 * If the message buffer is empty, just show
172 * the rest of the graph output for this
175 if (graph_show_remainder(revs->graph))
177 if (revs->commit_format == CMIT_FMT_ONELINE)
180 strbuf_release(&buf);
182 if (graph_show_remainder(revs->graph))
185 maybe_flush_or_die(stdout, "stdout");
186 finish_commit(commit, data);
189 static void finish_commit(struct commit *commit, void *data)
191 if (commit->parents) {
192 free_commit_list(commit->parents);
193 commit->parents = NULL;
195 free_commit_buffer(commit);
198 static inline void finish_object__ma(struct object *obj)
200 switch (arg_missing_action) {
202 die("missing blob object '%s'", oid_to_hex(&obj->oid));
209 oidset_insert(&missing_objects, &obj->oid);
213 BUG("unhandled missing_action");
218 static void finish_object(struct object *obj, const char *name, void *cb_data)
220 struct rev_list_info *info = cb_data;
221 if (obj->type == OBJ_BLOB && !has_object_file(&obj->oid))
222 finish_object__ma(obj);
223 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
224 parse_object(&obj->oid);
227 static void show_object(struct object *obj, const char *name, void *cb_data)
229 struct rev_list_info *info = cb_data;
230 finish_object(obj, name, cb_data);
231 display_progress(progress, ++progress_counter);
232 if (info->flags & REV_LIST_QUIET)
234 show_object_with_name(stdout, obj, name);
237 static void show_edge(struct commit *commit)
239 printf("-%s\n", oid_to_hex(&commit->object.oid));
242 static void print_var_str(const char *var, const char *val)
244 printf("%s='%s'\n", var, val);
247 static void print_var_int(const char *var, int val)
249 printf("%s=%d\n", var, val);
252 static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
254 int cnt, flags = info->flags;
255 char hex[GIT_MAX_HEXSZ + 1] = "";
256 struct commit_list *tried;
257 struct rev_info *revs = info->revs;
262 revs->commits = filter_skipped(revs->commits, &tried,
263 flags & BISECT_SHOW_ALL,
267 * revs->commits can reach "reaches" commits among
268 * "all" commits. If it is good, then there are
269 * (all-reaches) commits left to be bisected.
270 * On the other hand, if it is bad, then the set
271 * to bisect is "reaches".
272 * A bisect set of size N has (N-1) commits further
273 * to test, as we already know one bad one.
280 oid_to_hex_r(hex, &revs->commits->item->object.oid);
282 if (flags & BISECT_SHOW_ALL) {
283 traverse_commit_list(revs, show_commit, show_object, info);
287 print_var_str("bisect_rev", hex);
288 print_var_int("bisect_nr", cnt - 1);
289 print_var_int("bisect_good", all - reaches - 1);
290 print_var_int("bisect_bad", reaches - 1);
291 print_var_int("bisect_all", all);
292 print_var_int("bisect_steps", estimate_bisect_steps(all));
297 static int show_object_fast(
298 const struct object_id *oid,
299 enum object_type type,
302 struct packed_git *found_pack,
305 fprintf(stdout, "%s\n", oid_to_hex(oid));
309 static inline int parse_missing_action_value(const char *value)
311 if (!strcmp(value, "error")) {
312 arg_missing_action = MA_ERROR;
316 if (!strcmp(value, "allow-any")) {
317 arg_missing_action = MA_ALLOW_ANY;
321 if (!strcmp(value, "print")) {
322 arg_missing_action = MA_PRINT;
329 int cmd_rev_list(int argc, const char **argv, const char *prefix)
331 struct rev_info revs;
332 struct rev_list_info info;
335 int bisect_show_vars = 0;
336 int bisect_find_all = 0;
337 int use_bitmap_index = 0;
338 const char *show_progress = NULL;
340 if (argc == 2 && !strcmp(argv[1], "-h"))
341 usage(rev_list_usage);
343 git_config(git_default_config, NULL);
344 init_revisions(&revs, prefix);
345 revs.abbrev = DEFAULT_ABBREV;
346 revs.commit_format = CMIT_FMT_UNSPECIFIED;
347 argc = setup_revisions(argc, argv, &revs, NULL);
349 memset(&info, 0, sizeof(info));
354 if (revs.diffopt.flags.quick)
355 info.flags |= REV_LIST_QUIET;
356 for (i = 1 ; i < argc; i++) {
357 const char *arg = argv[i];
359 if (!strcmp(arg, "--header")) {
360 revs.verbose_header = 1;
363 if (!strcmp(arg, "--timestamp")) {
364 info.show_timestamp = 1;
367 if (!strcmp(arg, "--bisect")) {
371 if (!strcmp(arg, "--bisect-all")) {
374 info.flags |= BISECT_SHOW_ALL;
375 revs.show_decorations = 1;
378 if (!strcmp(arg, "--bisect-vars")) {
380 bisect_show_vars = 1;
383 if (!strcmp(arg, "--use-bitmap-index")) {
384 use_bitmap_index = 1;
387 if (!strcmp(arg, "--test-bitmap")) {
388 test_bitmap_walk(&revs);
391 if (skip_prefix(arg, "--progress=", &arg)) {
396 if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
397 parse_list_objects_filter(&filter_options, arg);
398 if (filter_options.choice && !revs.blob_objects)
399 die(_("object filtering requires --objects"));
400 if (filter_options.choice == LOFC_SPARSE_OID &&
401 !filter_options.sparse_oid_value)
402 die(_("invalid sparse value '%s'"),
403 filter_options.filter_spec);
406 if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
407 list_objects_filter_release(&filter_options);
410 if (!strcmp(arg, "--filter-print-omitted")) {
411 arg_print_omitted = 1;
415 if (skip_prefix(arg, "--missing=", &arg) &&
416 parse_missing_action_value(arg))
419 usage(rev_list_usage);
422 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
423 /* The command line has a --pretty */
424 info.hdr_termination = '\n';
425 if (revs.commit_format == CMIT_FMT_ONELINE)
426 info.header_prefix = "";
428 info.header_prefix = "commit ";
430 else if (revs.verbose_header)
431 /* Only --header was specified */
432 revs.commit_format = CMIT_FMT_RAW;
434 if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
435 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
437 !revs.rev_input_given) ||
439 usage(rev_list_usage);
442 die(_("rev-list does not support display of notes"));
444 if (filter_options.choice && use_bitmap_index)
445 die(_("cannot combine --use-bitmap-index with object filtering"));
447 save_commit_buffer = (revs.verbose_header ||
448 revs.grep_filter.pattern_list ||
449 revs.grep_filter.header_list);
454 progress = start_delayed_progress(show_progress, 0);
456 if (use_bitmap_index && !revs.prune) {
457 if (revs.count && !revs.left_right && !revs.cherry_mark) {
458 uint32_t commit_count;
459 int max_count = revs.max_count;
460 if (!prepare_bitmap_walk(&revs)) {
461 count_bitmap_commit_list(&commit_count, NULL, NULL, NULL);
462 if (max_count >= 0 && max_count < commit_count)
463 commit_count = max_count;
464 printf("%d\n", commit_count);
467 } else if (revs.max_count < 0 &&
468 revs.tag_objects && revs.tree_objects && revs.blob_objects) {
469 if (!prepare_bitmap_walk(&revs)) {
470 traverse_bitmap_commit_list(&show_object_fast);
476 if (prepare_revision_walk(&revs))
477 die("revision walk setup failed");
478 if (revs.tree_objects)
479 mark_edges_uninteresting(&revs, show_edge);
482 int reaches = reaches, all = all;
484 find_bisection(&revs.commits, &reaches, &all, bisect_find_all);
486 if (bisect_show_vars)
487 return show_bisect_vars(&info, reaches, all);
490 if (arg_print_omitted)
491 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
492 if (arg_missing_action == MA_PRINT)
493 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
495 traverse_commit_list_filtered(
496 &filter_options, &revs, show_commit, show_object, &info,
497 (arg_print_omitted ? &omitted_objects : NULL));
499 if (arg_print_omitted) {
500 struct oidset_iter iter;
501 struct object_id *oid;
502 oidset_iter_init(&omitted_objects, &iter);
503 while ((oid = oidset_iter_next(&iter)))
504 printf("~%s\n", oid_to_hex(oid));
505 oidset_clear(&omitted_objects);
507 if (arg_missing_action == MA_PRINT) {
508 struct oidset_iter iter;
509 struct object_id *oid;
510 oidset_iter_init(&missing_objects, &iter);
511 while ((oid = oidset_iter_next(&iter)))
512 printf("?%s\n", oid_to_hex(oid));
513 oidset_clear(&missing_objects);
516 stop_progress(&progress);
519 if (revs.left_right && revs.cherry_mark)
520 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
521 else if (revs.left_right)
522 printf("%d\t%d\n", revs.count_left, revs.count_right);
523 else if (revs.cherry_mark)
524 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
526 printf("%d\n", revs.count_left + revs.count_right);