5 #include "list-objects.h"
7 #include "pack-bitmap.h"
13 static const char rev_list_usage[] =
14 "git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
17 " --max-age=<epoch>\n"
18 " --min-age=<epoch>\n"
21 " --min-parents=<n>\n"
23 " --max-parents=<n>\n"
36 " formatting output:\n"
39 " --objects | --objects-edge\n"
41 " --header | --pretty\n"
42 " --abbrev=<n> | --no-abbrev\n"
52 static void finish_commit(struct commit *commit, void *data);
53 static void show_commit(struct commit *commit, void *data)
55 struct rev_list_info *info = data;
56 struct rev_info *revs = info->revs;
58 if (info->flags & REV_LIST_QUIET) {
59 finish_commit(commit, data);
63 graph_show_commit(revs->graph);
66 if (commit->object.flags & PATCHSAME)
68 else if (commit->object.flags & SYMMETRIC_LEFT)
72 finish_commit(commit, data);
76 if (info->show_timestamp)
77 printf("%lu ", commit->date);
78 if (info->header_prefix)
79 fputs(info->header_prefix, stdout);
82 fputs(get_revision_mark(revs, commit), stdout);
83 if (revs->abbrev_commit && revs->abbrev)
84 fputs(find_unique_abbrev(commit->object.oid.hash, revs->abbrev),
87 fputs(oid_to_hex(&commit->object.oid), stdout);
88 if (revs->print_parents) {
89 struct commit_list *parents = commit->parents;
91 printf(" %s", oid_to_hex(&parents->item->object.oid));
92 parents = parents->next;
95 if (revs->children.name) {
96 struct commit_list *children;
98 children = lookup_decoration(&revs->children, &commit->object);
100 printf(" %s", oid_to_hex(&children->item->object.oid));
101 children = children->next;
104 show_decorations(revs, commit);
105 if (revs->commit_format == CMIT_FMT_ONELINE)
110 if (revs->verbose_header && get_cached_commit_buffer(commit, NULL)) {
111 struct strbuf buf = STRBUF_INIT;
112 struct pretty_print_context ctx = {0};
113 ctx.abbrev = revs->abbrev;
114 ctx.date_mode = revs->date_mode;
115 ctx.date_mode_explicit = revs->date_mode_explicit;
116 ctx.fmt = revs->commit_format;
117 ctx.output_encoding = get_log_output_encoding();
118 pretty_print_commit(&ctx, commit, &buf);
121 if (revs->commit_format != CMIT_FMT_ONELINE)
122 graph_show_oneline(revs->graph);
124 graph_show_commit_msg(revs->graph, &buf);
127 * Add a newline after the commit message.
129 * Usually, this newline produces a blank
130 * padding line between entries, in which case
131 * we need to add graph padding on this line.
133 * However, the commit message may not end in a
134 * newline. In this case the newline simply
135 * ends the last line of the commit message,
136 * and we don't need any graph output. (This
137 * always happens with CMIT_FMT_ONELINE, and it
138 * happens with CMIT_FMT_USERFORMAT when the
139 * format doesn't explicitly end in a newline.)
141 if (buf.len && buf.buf[buf.len - 1] == '\n')
142 graph_show_padding(revs->graph);
146 * If the message buffer is empty, just show
147 * the rest of the graph output for this
150 if (graph_show_remainder(revs->graph))
152 if (revs->commit_format == CMIT_FMT_ONELINE)
156 if (revs->commit_format != CMIT_FMT_USERFORMAT ||
158 fwrite(buf.buf, 1, buf.len, stdout);
159 putchar(info->hdr_termination);
162 strbuf_release(&buf);
164 if (graph_show_remainder(revs->graph))
167 maybe_flush_or_die(stdout, "stdout");
168 finish_commit(commit, data);
171 static void finish_commit(struct commit *commit, void *data)
173 if (commit->parents) {
174 free_commit_list(commit->parents);
175 commit->parents = NULL;
177 free_commit_buffer(commit);
180 static void finish_object(struct object *obj, const char *name, void *cb_data)
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.hash);
189 static void show_object(struct object *obj, const char *name, void *cb_data)
191 struct rev_list_info *info = cb_data;
192 finish_object(obj, name, cb_data);
193 if (info->flags & REV_LIST_QUIET)
195 show_object_with_name(stdout, obj, name);
198 static void show_edge(struct commit *commit)
200 printf("-%s\n", oid_to_hex(&commit->object.oid));
203 static void print_var_str(const char *var, const char *val)
205 printf("%s='%s'\n", var, val);
208 static void print_var_int(const char *var, int val)
210 printf("%s=%d\n", var, val);
213 static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
215 int cnt, flags = info->flags;
216 char hex[GIT_SHA1_HEXSZ + 1] = "";
217 struct commit_list *tried;
218 struct rev_info *revs = info->revs;
223 revs->commits = filter_skipped(revs->commits, &tried,
224 flags & BISECT_SHOW_ALL,
228 * revs->commits can reach "reaches" commits among
229 * "all" commits. If it is good, then there are
230 * (all-reaches) commits left to be bisected.
231 * On the other hand, if it is bad, then the set
232 * to bisect is "reaches".
233 * A bisect set of size N has (N-1) commits further
234 * to test, as we already know one bad one.
241 sha1_to_hex_r(hex, revs->commits->item->object.oid.hash);
243 if (flags & BISECT_SHOW_ALL) {
244 traverse_commit_list(revs, show_commit, show_object, info);
248 print_var_str("bisect_rev", hex);
249 print_var_int("bisect_nr", cnt - 1);
250 print_var_int("bisect_good", all - reaches - 1);
251 print_var_int("bisect_bad", reaches - 1);
252 print_var_int("bisect_all", all);
253 print_var_int("bisect_steps", estimate_bisect_steps(all));
258 static int show_object_fast(
259 const unsigned char *sha1,
260 enum object_type type,
263 struct packed_git *found_pack,
266 fprintf(stdout, "%s\n", sha1_to_hex(sha1));
270 int cmd_rev_list(int argc, const char **argv, const char *prefix)
272 struct rev_info revs;
273 struct rev_list_info info;
276 int bisect_show_vars = 0;
277 int bisect_find_all = 0;
278 int use_bitmap_index = 0;
280 git_config(git_default_config, NULL);
281 init_revisions(&revs, prefix);
282 revs.abbrev = DEFAULT_ABBREV;
283 revs.commit_format = CMIT_FMT_UNSPECIFIED;
284 argc = setup_revisions(argc, argv, &revs, NULL);
286 memset(&info, 0, sizeof(info));
291 if (DIFF_OPT_TST(&revs.diffopt, QUICK))
292 info.flags |= REV_LIST_QUIET;
293 for (i = 1 ; i < argc; i++) {
294 const char *arg = argv[i];
296 if (!strcmp(arg, "--header")) {
297 revs.verbose_header = 1;
300 if (!strcmp(arg, "--timestamp")) {
301 info.show_timestamp = 1;
304 if (!strcmp(arg, "--bisect")) {
308 if (!strcmp(arg, "--bisect-all")) {
311 info.flags |= BISECT_SHOW_ALL;
312 revs.show_decorations = 1;
315 if (!strcmp(arg, "--bisect-vars")) {
317 bisect_show_vars = 1;
320 if (!strcmp(arg, "--use-bitmap-index")) {
321 use_bitmap_index = 1;
324 if (!strcmp(arg, "--test-bitmap")) {
325 test_bitmap_walk(&revs);
328 usage(rev_list_usage);
331 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
332 /* The command line has a --pretty */
333 info.hdr_termination = '\n';
334 if (revs.commit_format == CMIT_FMT_ONELINE)
335 info.header_prefix = "";
337 info.header_prefix = "commit ";
339 else if (revs.verbose_header)
340 /* Only --header was specified */
341 revs.commit_format = CMIT_FMT_RAW;
343 if ((!revs.commits &&
344 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
345 !revs.pending.nr)) ||
347 usage(rev_list_usage);
350 die(_("rev-list does not support display of notes"));
352 save_commit_buffer = (revs.verbose_header ||
353 revs.grep_filter.pattern_list ||
354 revs.grep_filter.header_list);
358 if (use_bitmap_index && !revs.prune) {
359 if (revs.count && !revs.left_right && !revs.cherry_mark) {
360 uint32_t commit_count;
361 int max_count = revs.max_count;
362 if (!prepare_bitmap_walk(&revs)) {
363 count_bitmap_commit_list(&commit_count, NULL, NULL, NULL);
364 if (max_count >= 0 && max_count < commit_count)
365 commit_count = max_count;
366 printf("%d\n", commit_count);
369 } else if (revs.max_count < 0 &&
370 revs.tag_objects && revs.tree_objects && revs.blob_objects) {
371 if (!prepare_bitmap_walk(&revs)) {
372 traverse_bitmap_commit_list(&show_object_fast);
378 if (prepare_revision_walk(&revs))
379 die("revision walk setup failed");
380 if (revs.tree_objects)
381 mark_edges_uninteresting(&revs, show_edge);
384 int reaches = reaches, all = all;
386 revs.commits = find_bisection(revs.commits, &reaches, &all,
389 if (bisect_show_vars)
390 return show_bisect_vars(&info, reaches, all);
393 traverse_commit_list(&revs, show_commit, show_object, &info);
396 if (revs.left_right && revs.cherry_mark)
397 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
398 else if (revs.left_right)
399 printf("%d\t%d\n", revs.count_left, revs.count_right);
400 else if (revs.cherry_mark)
401 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
403 printf("%d\n", revs.count_left + revs.count_right);