commit: convert commit->buffer to a slab
[git/git.git] / builtin / rev-list.c
CommitLineData
64745109
LT
1#include "cache.h"
2#include "commit.h"
c4e05b1a 3#include "diff.h"
ae563542 4#include "revision.h"
c64ed70d 5#include "list-objects.h"
aa32939f
VM
6#include "pack.h"
7#include "pack-bitmap.h"
5fb61b8d 8#include "builtin.h"
50e62a8e 9#include "log-tree.h"
7fefda5c 10#include "graph.h"
a2ad79ce 11#include "bisect.h"
8906300f 12
a6f68d47 13static const char rev_list_usage[] =
1b1dd23f 14"git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
69e0c256 15" limiting output:\n"
62b4698e
ŠN
16" --max-count=<n>\n"
17" --max-age=<epoch>\n"
18" --min-age=<epoch>\n"
69e0c256
JH
19" --sparse\n"
20" --no-merges\n"
ad5aeede
MG
21" --min-parents=<n>\n"
22" --no-min-parents\n"
23" --max-parents=<n>\n"
24" --no-max-parents\n"
93b74bca 25" --remove-empty\n"
69e0c256 26" --all\n"
a5aa930d
UKK
27" --branches\n"
28" --tags\n"
29" --remotes\n"
42cabc34 30" --stdin\n"
27350891 31" --quiet\n"
69e0c256 32" ordering output:\n"
69e0c256 33" --topo-order\n"
4c8725f1 34" --date-order\n"
7ccd3667 35" --reverse\n"
69e0c256
JH
36" formatting output:\n"
37" --parents\n"
72276a3e 38" --children\n"
c6496575 39" --objects | --objects-edge\n"
69e0c256
JH
40" --unpacked\n"
41" --header | --pretty\n"
62b4698e 42" --abbrev=<n> | --no-abbrev\n"
5c51c985 43" --abbrev-commit\n"
b24bace5 44" --left-right\n"
69e0c256 45" special purpose:\n"
457f08a0 46" --bisect\n"
50e62a8e
CC
47" --bisect-vars\n"
48" --bisect-all"
69e0c256 49;
a6f68d47 50
11c211fa
CC
51static void finish_commit(struct commit *commit, void *data);
52static void show_commit(struct commit *commit, void *data)
81f2bb1f 53{
d797257e
CC
54 struct rev_list_info *info = data;
55 struct rev_info *revs = info->revs;
11c211fa 56
98993722
NTND
57 if (info->flags & REV_LIST_QUIET) {
58 finish_commit(commit, data);
59 return;
60 }
61
11c211fa 62 graph_show_commit(revs->graph);
7fefda5c 63
f69c5018 64 if (revs->count) {
b388e14b
MG
65 if (commit->object.flags & PATCHSAME)
66 revs->count_same++;
67 else if (commit->object.flags & SYMMETRIC_LEFT)
f69c5018
TR
68 revs->count_left++;
69 else
70 revs->count_right++;
71 finish_commit(commit, data);
72 return;
73 }
74
d797257e 75 if (info->show_timestamp)
dc68c4ff 76 printf("%lu ", commit->date);
d797257e
CC
77 if (info->header_prefix)
78 fputs(info->header_prefix, stdout);
7528f27d 79
1df2d656
MG
80 if (!revs->graph)
81 fputs(get_revision_mark(revs, commit), stdout);
11c211fa
CC
82 if (revs->abbrev_commit && revs->abbrev)
83 fputs(find_unique_abbrev(commit->object.sha1, revs->abbrev),
7594c4b2 84 stdout);
5c51c985
JH
85 else
86 fputs(sha1_to_hex(commit->object.sha1), stdout);
11c211fa 87 if (revs->print_parents) {
81f2bb1f
LT
88 struct commit_list *parents = commit->parents;
89 while (parents) {
1ed84157 90 printf(" %s", sha1_to_hex(parents->item->object.sha1));
81f2bb1f
LT
91 parents = parents->next;
92 }
93 }
11c211fa 94 if (revs->children.name) {
72276a3e
JH
95 struct commit_list *children;
96
11c211fa 97 children = lookup_decoration(&revs->children, &commit->object);
72276a3e
JH
98 while (children) {
99 printf(" %s", sha1_to_hex(children->item->object.sha1));
100 children = children->next;
101 }
102 }
11c211fa
CC
103 show_decorations(revs, commit);
104 if (revs->commit_format == CMIT_FMT_ONELINE)
d87449c5
JH
105 putchar(' ');
106 else
107 putchar('\n');
108
a97934d8 109 if (revs->verbose_header && get_cached_commit_buffer(commit)) {
f285a2d7 110 struct strbuf buf = STRBUF_INIT;
dd2e794a
TR
111 struct pretty_print_context ctx = {0};
112 ctx.abbrev = revs->abbrev;
113 ctx.date_mode = revs->date_mode;
f026c756 114 ctx.date_mode_explicit = revs->date_mode_explicit;
6bf13944 115 ctx.fmt = revs->commit_format;
ecaee805 116 ctx.output_encoding = get_log_output_encoding();
6bf13944 117 pretty_print_commit(&ctx, commit, &buf);
11c211fa 118 if (revs->graph) {
7fefda5c 119 if (buf.len) {
11c211fa
CC
120 if (revs->commit_format != CMIT_FMT_ONELINE)
121 graph_show_oneline(revs->graph);
7fefda5c 122
11c211fa 123 graph_show_commit_msg(revs->graph, &buf);
7fefda5c
AS
124
125 /*
126 * Add a newline after the commit message.
127 *
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.
131 *
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.)
139 */
140 if (buf.len && buf.buf[buf.len - 1] == '\n')
11c211fa 141 graph_show_padding(revs->graph);
7fefda5c
AS
142 putchar('\n');
143 } else {
144 /*
145 * If the message buffer is empty, just show
146 * the rest of the graph output for this
147 * commit.
148 */
11c211fa 149 if (graph_show_remainder(revs->graph))
7fefda5c 150 putchar('\n');
1fb5fdd2
EFL
151 if (revs->commit_format == CMIT_FMT_ONELINE)
152 putchar('\n');
7fefda5c
AS
153 }
154 } else {
1fb5fdd2 155 if (revs->commit_format != CMIT_FMT_USERFORMAT ||
9130ac9f
JK
156 buf.len) {
157 fwrite(buf.buf, 1, buf.len, stdout);
158 putchar(info->hdr_termination);
159 }
7fefda5c 160 }
674d1727 161 strbuf_release(&buf);
7fefda5c 162 } else {
11c211fa 163 if (graph_show_remainder(revs->graph))
7fefda5c 164 putchar('\n');
7620d39f 165 }
06f59e9f 166 maybe_flush_or_die(stdout, "stdout");
11c211fa 167 finish_commit(commit, data);
27350891
SP
168}
169
11c211fa 170static void finish_commit(struct commit *commit, void *data)
27350891 171{
cb115748
LT
172 if (commit->parents) {
173 free_commit_list(commit->parents);
174 commit->parents = NULL;
175 }
0fb370da 176 free_commit_buffer(commit);
a3437b8c
JS
177}
178
49473672
JH
179static void finish_object(struct object *obj,
180 const struct name_path *path, const char *name,
181 void *cb_data)
27350891 182{
98993722 183 struct rev_list_info *info = cb_data;
8d2dfc49
LT
184 if (obj->type == OBJ_BLOB && !has_sha1_file(obj->sha1))
185 die("missing blob object '%s'", sha1_to_hex(obj->sha1));
98993722
NTND
186 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
187 parse_object(obj->sha1);
27350891
SP
188}
189
49473672
JH
190static void show_object(struct object *obj,
191 const struct name_path *path, const char *component,
192 void *cb_data)
9de48752 193{
cb8da705 194 struct rev_list_info *info = cb_data;
49473672 195 finish_object(obj, path, component, cb_data);
98993722
NTND
196 if (info->flags & REV_LIST_QUIET)
197 return;
91f17516 198 show_object_with_name(stdout, obj, path, component);
9de48752
LT
199}
200
8d1d8f83
JH
201static void show_edge(struct commit *commit)
202{
203 printf("-%s\n", sha1_to_hex(commit->object.sha1));
204}
205
38ef7507 206static void print_var_str(const char *var, const char *val)
280e65cb 207{
38ef7507 208 printf("%s='%s'\n", var, val);
280e65cb
CC
209}
210
38ef7507 211static void print_var_int(const char *var, int val)
280e65cb 212{
38ef7507 213 printf("%s=%d\n", var, val);
280e65cb
CC
214}
215
f1c92c63 216static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
9996983c 217{
98993722 218 int cnt, flags = info->flags;
280e65cb 219 char hex[41] = "";
95188648 220 struct commit_list *tried;
d797257e 221 struct rev_info *revs = info->revs;
9996983c 222
8ba8fe04 223 if (!revs->commits)
9996983c
CC
224 return 1;
225
9af3589e
CC
226 revs->commits = filter_skipped(revs->commits, &tried,
227 flags & BISECT_SHOW_ALL,
228 NULL, NULL);
95188648 229
9996983c 230 /*
7428d754 231 * revs->commits can reach "reaches" commits among
9996983c
CC
232 * "all" commits. If it is good, then there are
233 * (all-reaches) commits left to be bisected.
234 * On the other hand, if it is bad, then the set
235 * to bisect is "reaches".
236 * A bisect set of size N has (N-1) commits further
237 * to test, as we already know one bad one.
238 */
239 cnt = all - reaches;
240 if (cnt < reaches)
241 cnt = reaches;
6a17fad7 242
95188648
CC
243 if (revs->commits)
244 strcpy(hex, sha1_to_hex(revs->commits->item->object.sha1));
9996983c 245
37c4c38d 246 if (flags & BISECT_SHOW_ALL) {
d797257e 247 traverse_commit_list(revs, show_commit, show_object, info);
9996983c
CC
248 printf("------\n");
249 }
250
38ef7507
CC
251 print_var_str("bisect_rev", hex);
252 print_var_int("bisect_nr", cnt - 1);
253 print_var_int("bisect_good", all - reaches - 1);
254 print_var_int("bisect_bad", reaches - 1);
255 print_var_int("bisect_all", all);
256 print_var_int("bisect_steps", estimate_bisect_steps(all));
9996983c
CC
257
258 return 0;
259}
260
aa32939f
VM
261static int show_object_fast(
262 const unsigned char *sha1,
263 enum object_type type,
264 int exclude,
265 uint32_t name_hash,
266 struct packed_git *found_pack,
267 off_t found_offset)
268{
269 fprintf(stdout, "%s\n", sha1_to_hex(sha1));
270 return 1;
271}
272
a633fca0 273int cmd_rev_list(int argc, const char **argv, const char *prefix)
64745109 274{
11c211fa 275 struct rev_info revs;
d797257e 276 struct rev_list_info info;
d9a83684 277 int i;
ff62d732 278 int bisect_list = 0;
457f08a0 279 int bisect_show_vars = 0;
50e62a8e 280 int bisect_find_all = 0;
aa32939f 281 int use_bitmap_index = 0;
64745109 282
ef90d6d4 283 git_config(git_default_config, NULL);
a633fca0 284 init_revisions(&revs, prefix);
7337b138 285 revs.abbrev = DEFAULT_ABBREV;
7594c4b2 286 revs.commit_format = CMIT_FMT_UNSPECIFIED;
a4a88b2b 287 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 288
d797257e
CC
289 memset(&info, 0, sizeof(info));
290 info.revs = &revs;
ad3f9a71
LT
291 if (revs.bisect)
292 bisect_list = 1;
d797257e 293
98993722
NTND
294 if (DIFF_OPT_TST(&revs.diffopt, QUICK))
295 info.flags |= REV_LIST_QUIET;
fcfda02b 296 for (i = 1 ; i < argc; i++) {
cf484544 297 const char *arg = argv[i];
fcfda02b 298
a6f68d47 299 if (!strcmp(arg, "--header")) {
7594c4b2 300 revs.verbose_header = 1;
9d97aa64
LT
301 continue;
302 }
dc68c4ff 303 if (!strcmp(arg, "--timestamp")) {
d797257e 304 info.show_timestamp = 1;
dc68c4ff
JH
305 continue;
306 }
8b3a1e05
LT
307 if (!strcmp(arg, "--bisect")) {
308 bisect_list = 1;
309 continue;
310 }
50e62a8e
CC
311 if (!strcmp(arg, "--bisect-all")) {
312 bisect_list = 1;
313 bisect_find_all = 1;
98993722 314 info.flags |= BISECT_SHOW_ALL;
6e46cc0d 315 revs.show_decorations = 1;
50e62a8e
CC
316 continue;
317 }
457f08a0
JH
318 if (!strcmp(arg, "--bisect-vars")) {
319 bisect_list = 1;
320 bisect_show_vars = 1;
321 continue;
322 }
aa32939f
VM
323 if (!strcmp(arg, "--use-bitmap-index")) {
324 use_bitmap_index = 1;
325 continue;
326 }
327 if (!strcmp(arg, "--test-bitmap")) {
328 test_bitmap_walk(&revs);
329 return 0;
330 }
ae563542 331 usage(rev_list_usage);
a6f68d47 332
fcfda02b 333 }
7594c4b2
JH
334 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
335 /* The command line has a --pretty */
d797257e 336 info.hdr_termination = '\n';
7594c4b2 337 if (revs.commit_format == CMIT_FMT_ONELINE)
d797257e 338 info.header_prefix = "";
7594c4b2 339 else
d797257e 340 info.header_prefix = "commit ";
7594c4b2 341 }
db89665f
JH
342 else if (revs.verbose_header)
343 /* Only --header was specified */
344 revs.commit_format = CMIT_FMT_RAW;
fcfda02b 345
d797257e 346 if ((!revs.commits &&
c01499ef 347 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
1f1e895f 348 !revs.pending.nr)) ||
8c1f0b44 349 revs.diff)
7b34c2fa
LT
350 usage(rev_list_usage);
351
80235ba7
JH
352 save_commit_buffer = (revs.verbose_header ||
353 revs.grep_filter.pattern_list ||
354 revs.grep_filter.header_list);
4e1dc640
JH
355 if (bisect_list)
356 revs.limited = 1;
9181ca2c 357
aa32939f
VM
358 if (use_bitmap_index) {
359 if (revs.count && !revs.left_right && !revs.cherry_mark) {
360 uint32_t commit_count;
361 if (!prepare_bitmap_walk(&revs)) {
362 count_bitmap_commit_list(&commit_count, NULL, NULL, NULL);
363 printf("%d\n", commit_count);
364 return 0;
365 }
366 } else if (revs.tag_objects && revs.tree_objects && revs.blob_objects) {
367 if (!prepare_bitmap_walk(&revs)) {
368 traverse_bitmap_commit_list(&show_object_fast);
369 return 0;
370 }
371 }
372 }
373
3d51e1b5
MK
374 if (prepare_revision_walk(&revs))
375 die("revision walk setup failed");
a4a88b2b 376 if (revs.tree_objects)
e76a5fb4 377 mark_edges_uninteresting(&revs, show_edge);
a4a88b2b 378
457f08a0
JH
379 if (bisect_list) {
380 int reaches = reaches, all = all;
381
50e62a8e
CC
382 revs.commits = find_bisection(revs.commits, &reaches, &all,
383 bisect_find_all);
95188648 384
9996983c 385 if (bisect_show_vars)
13858e57 386 return show_bisect_vars(&info, reaches, all);
457f08a0 387 }
7b34c2fa 388
98993722 389 traverse_commit_list(&revs, show_commit, show_object, &info);
8906300f 390
f69c5018 391 if (revs.count) {
b388e14b
MG
392 if (revs.left_right && revs.cherry_mark)
393 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
394 else if (revs.left_right)
f69c5018 395 printf("%d\t%d\n", revs.count_left, revs.count_right);
b388e14b
MG
396 else if (revs.cherry_mark)
397 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
f69c5018
TR
398 else
399 printf("%d\n", revs.count_left + revs.count_right);
400 }
401
64745109
LT
402 return 0;
403}