Merge branch 'bc/blame-crlf-test' into maint
[git/gitweb.git] / builtin / rev-list.c
blob9f92905379d1801a35bfad87bc05002d76dc23f4
1 #include "cache.h"
2 #include "commit.h"
3 #include "diff.h"
4 #include "revision.h"
5 #include "list-objects.h"
6 #include "pack.h"
7 #include "pack-bitmap.h"
8 #include "builtin.h"
9 #include "log-tree.h"
10 #include "graph.h"
11 #include "bisect.h"
13 static const char rev_list_usage[] =
14 "git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
15 " limiting output:\n"
16 " --max-count=<n>\n"
17 " --max-age=<epoch>\n"
18 " --min-age=<epoch>\n"
19 " --sparse\n"
20 " --no-merges\n"
21 " --min-parents=<n>\n"
22 " --no-min-parents\n"
23 " --max-parents=<n>\n"
24 " --no-max-parents\n"
25 " --remove-empty\n"
26 " --all\n"
27 " --branches\n"
28 " --tags\n"
29 " --remotes\n"
30 " --stdin\n"
31 " --quiet\n"
32 " ordering output:\n"
33 " --topo-order\n"
34 " --date-order\n"
35 " --reverse\n"
36 " formatting output:\n"
37 " --parents\n"
38 " --children\n"
39 " --objects | --objects-edge\n"
40 " --unpacked\n"
41 " --header | --pretty\n"
42 " --abbrev=<n> | --no-abbrev\n"
43 " --abbrev-commit\n"
44 " --left-right\n"
45 " special purpose:\n"
46 " --bisect\n"
47 " --bisect-vars\n"
48 " --bisect-all"
51 static void finish_commit(struct commit *commit, void *data);
52 static void show_commit(struct commit *commit, void *data)
54 struct rev_list_info *info = data;
55 struct rev_info *revs = info->revs;
57 if (info->flags & REV_LIST_QUIET) {
58 finish_commit(commit, data);
59 return;
62 graph_show_commit(revs->graph);
64 if (revs->count) {
65 if (commit->object.flags & PATCHSAME)
66 revs->count_same++;
67 else if (commit->object.flags & SYMMETRIC_LEFT)
68 revs->count_left++;
69 else
70 revs->count_right++;
71 finish_commit(commit, data);
72 return;
75 if (info->show_timestamp)
76 printf("%lu ", commit->date);
77 if (info->header_prefix)
78 fputs(info->header_prefix, stdout);
80 if (!revs->graph)
81 fputs(get_revision_mark(revs, commit), stdout);
82 if (revs->abbrev_commit && revs->abbrev)
83 fputs(find_unique_abbrev(commit->object.sha1, revs->abbrev),
84 stdout);
85 else
86 fputs(sha1_to_hex(commit->object.sha1), stdout);
87 if (revs->print_parents) {
88 struct commit_list *parents = commit->parents;
89 while (parents) {
90 printf(" %s", sha1_to_hex(parents->item->object.sha1));
91 parents = parents->next;
94 if (revs->children.name) {
95 struct commit_list *children;
97 children = lookup_decoration(&revs->children, &commit->object);
98 while (children) {
99 printf(" %s", sha1_to_hex(children->item->object.sha1));
100 children = children->next;
103 show_decorations(revs, commit);
104 if (revs->commit_format == CMIT_FMT_ONELINE)
105 putchar(' ');
106 else
107 putchar('\n');
109 if (revs->verbose_header && commit->buffer) {
110 struct strbuf buf = STRBUF_INIT;
111 struct pretty_print_context ctx = {0};
112 ctx.abbrev = revs->abbrev;
113 ctx.date_mode = revs->date_mode;
114 ctx.date_mode_explicit = revs->date_mode_explicit;
115 ctx.fmt = revs->commit_format;
116 ctx.output_encoding = get_log_output_encoding();
117 pretty_print_commit(&ctx, commit, &buf);
118 if (revs->graph) {
119 if (buf.len) {
120 if (revs->commit_format != CMIT_FMT_ONELINE)
121 graph_show_oneline(revs->graph);
123 graph_show_commit_msg(revs->graph, &buf);
126 * Add a newline after the commit message.
128 * Usually, this newline produces a blank
129 * padding line between entries, in which case
130 * we need to add graph padding on this line.
132 * However, the commit message may not end in a
133 * newline. In this case the newline simply
134 * ends the last line of the commit message,
135 * and we don't need any graph output. (This
136 * always happens with CMIT_FMT_ONELINE, and it
137 * happens with CMIT_FMT_USERFORMAT when the
138 * format doesn't explicitly end in a newline.)
140 if (buf.len && buf.buf[buf.len - 1] == '\n')
141 graph_show_padding(revs->graph);
142 putchar('\n');
143 } else {
145 * If the message buffer is empty, just show
146 * the rest of the graph output for this
147 * commit.
149 if (graph_show_remainder(revs->graph))
150 putchar('\n');
151 if (revs->commit_format == CMIT_FMT_ONELINE)
152 putchar('\n');
154 } else {
155 if (revs->commit_format != CMIT_FMT_USERFORMAT ||
156 buf.len) {
157 fwrite(buf.buf, 1, buf.len, stdout);
158 putchar(info->hdr_termination);
161 strbuf_release(&buf);
162 } else {
163 if (graph_show_remainder(revs->graph))
164 putchar('\n');
166 maybe_flush_or_die(stdout, "stdout");
167 finish_commit(commit, data);
170 static void finish_commit(struct commit *commit, void *data)
172 if (commit->parents) {
173 free_commit_list(commit->parents);
174 commit->parents = NULL;
176 free(commit->buffer);
177 commit->buffer = NULL;
180 static void finish_object(struct object *obj,
181 const struct name_path *path, const char *name,
182 void *cb_data)
184 struct rev_list_info *info = cb_data;
185 if (obj->type == OBJ_BLOB && !has_sha1_file(obj->sha1))
186 die("missing blob object '%s'", sha1_to_hex(obj->sha1));
187 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
188 parse_object(obj->sha1);
191 static void show_object(struct object *obj,
192 const struct name_path *path, const char *component,
193 void *cb_data)
195 struct rev_list_info *info = cb_data;
196 finish_object(obj, path, component, cb_data);
197 if (info->flags & REV_LIST_QUIET)
198 return;
199 show_object_with_name(stdout, obj, path, component);
202 static void show_edge(struct commit *commit)
204 printf("-%s\n", sha1_to_hex(commit->object.sha1));
207 static void print_var_str(const char *var, const char *val)
209 printf("%s='%s'\n", var, val);
212 static void print_var_int(const char *var, int val)
214 printf("%s=%d\n", var, val);
217 static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
219 int cnt, flags = info->flags;
220 char hex[41] = "";
221 struct commit_list *tried;
222 struct rev_info *revs = info->revs;
224 if (!revs->commits)
225 return 1;
227 revs->commits = filter_skipped(revs->commits, &tried,
228 flags & BISECT_SHOW_ALL,
229 NULL, NULL);
232 * revs->commits can reach "reaches" commits among
233 * "all" commits. If it is good, then there are
234 * (all-reaches) commits left to be bisected.
235 * On the other hand, if it is bad, then the set
236 * to bisect is "reaches".
237 * A bisect set of size N has (N-1) commits further
238 * to test, as we already know one bad one.
240 cnt = all - reaches;
241 if (cnt < reaches)
242 cnt = reaches;
244 if (revs->commits)
245 strcpy(hex, sha1_to_hex(revs->commits->item->object.sha1));
247 if (flags & BISECT_SHOW_ALL) {
248 traverse_commit_list(revs, show_commit, show_object, info);
249 printf("------\n");
252 print_var_str("bisect_rev", hex);
253 print_var_int("bisect_nr", cnt - 1);
254 print_var_int("bisect_good", all - reaches - 1);
255 print_var_int("bisect_bad", reaches - 1);
256 print_var_int("bisect_all", all);
257 print_var_int("bisect_steps", estimate_bisect_steps(all));
259 return 0;
262 static int show_object_fast(
263 const unsigned char *sha1,
264 enum object_type type,
265 int exclude,
266 uint32_t name_hash,
267 struct packed_git *found_pack,
268 off_t found_offset)
270 fprintf(stdout, "%s\n", sha1_to_hex(sha1));
271 return 1;
274 int cmd_rev_list(int argc, const char **argv, const char *prefix)
276 struct rev_info revs;
277 struct rev_list_info info;
278 int i;
279 int bisect_list = 0;
280 int bisect_show_vars = 0;
281 int bisect_find_all = 0;
282 int use_bitmap_index = 0;
284 git_config(git_default_config, NULL);
285 init_revisions(&revs, prefix);
286 revs.abbrev = DEFAULT_ABBREV;
287 revs.commit_format = CMIT_FMT_UNSPECIFIED;
288 argc = setup_revisions(argc, argv, &revs, NULL);
290 memset(&info, 0, sizeof(info));
291 info.revs = &revs;
292 if (revs.bisect)
293 bisect_list = 1;
295 if (DIFF_OPT_TST(&revs.diffopt, QUICK))
296 info.flags |= REV_LIST_QUIET;
297 for (i = 1 ; i < argc; i++) {
298 const char *arg = argv[i];
300 if (!strcmp(arg, "--header")) {
301 revs.verbose_header = 1;
302 continue;
304 if (!strcmp(arg, "--timestamp")) {
305 info.show_timestamp = 1;
306 continue;
308 if (!strcmp(arg, "--bisect")) {
309 bisect_list = 1;
310 continue;
312 if (!strcmp(arg, "--bisect-all")) {
313 bisect_list = 1;
314 bisect_find_all = 1;
315 info.flags |= BISECT_SHOW_ALL;
316 revs.show_decorations = 1;
317 continue;
319 if (!strcmp(arg, "--bisect-vars")) {
320 bisect_list = 1;
321 bisect_show_vars = 1;
322 continue;
324 if (!strcmp(arg, "--use-bitmap-index")) {
325 use_bitmap_index = 1;
326 continue;
328 if (!strcmp(arg, "--test-bitmap")) {
329 test_bitmap_walk(&revs);
330 return 0;
332 usage(rev_list_usage);
335 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
336 /* The command line has a --pretty */
337 info.hdr_termination = '\n';
338 if (revs.commit_format == CMIT_FMT_ONELINE)
339 info.header_prefix = "";
340 else
341 info.header_prefix = "commit ";
343 else if (revs.verbose_header)
344 /* Only --header was specified */
345 revs.commit_format = CMIT_FMT_RAW;
347 if ((!revs.commits &&
348 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
349 !revs.pending.nr)) ||
350 revs.diff)
351 usage(rev_list_usage);
353 save_commit_buffer = (revs.verbose_header ||
354 revs.grep_filter.pattern_list ||
355 revs.grep_filter.header_list);
356 if (bisect_list)
357 revs.limited = 1;
359 if (use_bitmap_index) {
360 if (revs.count && !revs.left_right && !revs.cherry_mark) {
361 uint32_t commit_count;
362 if (!prepare_bitmap_walk(&revs)) {
363 count_bitmap_commit_list(&commit_count, NULL, NULL, NULL);
364 printf("%d\n", commit_count);
365 return 0;
367 } else if (revs.tag_objects && revs.tree_objects && revs.blob_objects) {
368 if (!prepare_bitmap_walk(&revs)) {
369 traverse_bitmap_commit_list(&show_object_fast);
370 return 0;
375 if (prepare_revision_walk(&revs))
376 die("revision walk setup failed");
377 if (revs.tree_objects)
378 mark_edges_uninteresting(&revs, show_edge);
380 if (bisect_list) {
381 int reaches = reaches, all = all;
383 revs.commits = find_bisection(revs.commits, &reaches, &all,
384 bisect_find_all);
386 if (bisect_show_vars)
387 return show_bisect_vars(&info, reaches, all);
390 traverse_commit_list(&revs, show_commit, show_object, &info);
392 if (revs.count) {
393 if (revs.left_right && revs.cherry_mark)
394 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
395 else if (revs.left_right)
396 printf("%d\t%d\n", revs.count_left, revs.count_right);
397 else if (revs.cherry_mark)
398 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
399 else
400 printf("%d\n", revs.count_left + revs.count_right);
403 return 0;