Merge branch 'jk/cached-commit-buffer'
[git/git.git] / builtin / rev-list.c
CommitLineData
64745109 1#include "cache.h"
b2141fc1 2#include "config.h"
64745109 3#include "commit.h"
c4e05b1a 4#include "diff.h"
ae563542 5#include "revision.h"
c64ed70d 6#include "list-objects.h"
caf3827e
JH
7#include "list-objects-filter.h"
8#include "list-objects-filter-options.h"
aa32939f
VM
9#include "pack.h"
10#include "pack-bitmap.h"
5fb61b8d 11#include "builtin.h"
50e62a8e 12#include "log-tree.h"
7fefda5c 13#include "graph.h"
a2ad79ce 14#include "bisect.h"
434ea3cd 15#include "progress.h"
7f97de5e 16#include "reflog-walk.h"
caf3827e 17#include "oidset.h"
df11e196 18#include "packfile.h"
8906300f 19
a6f68d47 20static const char rev_list_usage[] =
1b1dd23f 21"git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
69e0c256 22" limiting output:\n"
62b4698e
ŠN
23" --max-count=<n>\n"
24" --max-age=<epoch>\n"
25" --min-age=<epoch>\n"
69e0c256
JH
26" --sparse\n"
27" --no-merges\n"
ad5aeede
MG
28" --min-parents=<n>\n"
29" --no-min-parents\n"
30" --max-parents=<n>\n"
31" --no-max-parents\n"
93b74bca 32" --remove-empty\n"
69e0c256 33" --all\n"
a5aa930d
UKK
34" --branches\n"
35" --tags\n"
36" --remotes\n"
42cabc34 37" --stdin\n"
27350891 38" --quiet\n"
69e0c256 39" ordering output:\n"
69e0c256 40" --topo-order\n"
4c8725f1 41" --date-order\n"
7ccd3667 42" --reverse\n"
69e0c256
JH
43" formatting output:\n"
44" --parents\n"
72276a3e 45" --children\n"
c6496575 46" --objects | --objects-edge\n"
69e0c256
JH
47" --unpacked\n"
48" --header | --pretty\n"
62b4698e 49" --abbrev=<n> | --no-abbrev\n"
5c51c985 50" --abbrev-commit\n"
b24bace5 51" --left-right\n"
75d2e5a7 52" --count\n"
69e0c256 53" special purpose:\n"
457f08a0 54" --bisect\n"
50e62a8e
CC
55" --bisect-vars\n"
56" --bisect-all"
69e0c256 57;
a6f68d47 58
434ea3cd
JK
59static struct progress *progress;
60static unsigned progress_counter;
61
caf3827e
JH
62static struct list_objects_filter_options filter_options;
63static struct oidset omitted_objects;
64static int arg_print_omitted; /* print objects omitted by filter */
65
66static struct oidset missing_objects;
67enum missing_action {
68 MA_ERROR = 0, /* fail if any missing objects are encountered */
69 MA_ALLOW_ANY, /* silently allow ALL missing objects */
70 MA_PRINT, /* print ALL missing objects in special section */
df11e196 71 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
caf3827e
JH
72};
73static enum missing_action arg_missing_action;
74
75#define DEFAULT_OIDSET_SIZE (16*1024)
76
11c211fa
CC
77static void finish_commit(struct commit *commit, void *data);
78static void show_commit(struct commit *commit, void *data)
81f2bb1f 79{
d797257e
CC
80 struct rev_list_info *info = data;
81 struct rev_info *revs = info->revs;
11c211fa 82
434ea3cd
JK
83 display_progress(progress, ++progress_counter);
84
98993722
NTND
85 if (info->flags & REV_LIST_QUIET) {
86 finish_commit(commit, data);
87 return;
88 }
89
11c211fa 90 graph_show_commit(revs->graph);
7fefda5c 91
f69c5018 92 if (revs->count) {
b388e14b
MG
93 if (commit->object.flags & PATCHSAME)
94 revs->count_same++;
95 else if (commit->object.flags & SYMMETRIC_LEFT)
f69c5018
TR
96 revs->count_left++;
97 else
98 revs->count_right++;
99 finish_commit(commit, data);
100 return;
101 }
102
d797257e 103 if (info->show_timestamp)
cb71f8bd 104 printf("%"PRItime" ", commit->date);
d797257e
CC
105 if (info->header_prefix)
106 fputs(info->header_prefix, stdout);
7528f27d 107
1df2d656
MG
108 if (!revs->graph)
109 fputs(get_revision_mark(revs, commit), stdout);
11c211fa 110 if (revs->abbrev_commit && revs->abbrev)
ed1c9977 111 fputs(find_unique_abbrev(commit->object.oid.hash, revs->abbrev),
7594c4b2 112 stdout);
5c51c985 113 else
f2fd0760 114 fputs(oid_to_hex(&commit->object.oid), stdout);
11c211fa 115 if (revs->print_parents) {
81f2bb1f
LT
116 struct commit_list *parents = commit->parents;
117 while (parents) {
f2fd0760 118 printf(" %s", oid_to_hex(&parents->item->object.oid));
81f2bb1f
LT
119 parents = parents->next;
120 }
121 }
11c211fa 122 if (revs->children.name) {
72276a3e
JH
123 struct commit_list *children;
124
11c211fa 125 children = lookup_decoration(&revs->children, &commit->object);
72276a3e 126 while (children) {
f2fd0760 127 printf(" %s", oid_to_hex(&children->item->object.oid));
72276a3e
JH
128 children = children->next;
129 }
130 }
11c211fa
CC
131 show_decorations(revs, commit);
132 if (revs->commit_format == CMIT_FMT_ONELINE)
d87449c5
JH
133 putchar(' ');
134 else
135 putchar('\n');
136
7fa31b64 137 if (revs->verbose_header) {
f285a2d7 138 struct strbuf buf = STRBUF_INIT;
dd2e794a
TR
139 struct pretty_print_context ctx = {0};
140 ctx.abbrev = revs->abbrev;
141 ctx.date_mode = revs->date_mode;
f026c756 142 ctx.date_mode_explicit = revs->date_mode_explicit;
6bf13944 143 ctx.fmt = revs->commit_format;
ecaee805 144 ctx.output_encoding = get_log_output_encoding();
d75dfb10 145 ctx.color = revs->diffopt.use_color;
6bf13944 146 pretty_print_commit(&ctx, commit, &buf);
660e113c
JK
147 if (buf.len) {
148 if (revs->commit_format != CMIT_FMT_ONELINE)
149 graph_show_oneline(revs->graph);
7fefda5c 150
660e113c 151 graph_show_commit_msg(revs->graph, stdout, &buf);
7fefda5c 152
660e113c
JK
153 /*
154 * Add a newline after the commit message.
155 *
156 * Usually, this newline produces a blank
157 * padding line between entries, in which case
158 * we need to add graph padding on this line.
159 *
160 * However, the commit message may not end in a
161 * newline. In this case the newline simply
162 * ends the last line of the commit message,
163 * and we don't need any graph output. (This
164 * always happens with CMIT_FMT_ONELINE, and it
165 * happens with CMIT_FMT_USERFORMAT when the
166 * format doesn't explicitly end in a newline.)
167 */
168 if (buf.len && buf.buf[buf.len - 1] == '\n')
169 graph_show_padding(revs->graph);
98985c69 170 putchar(info->hdr_termination);
7fefda5c 171 } else {
660e113c
JK
172 /*
173 * If the message buffer is empty, just show
174 * the rest of the graph output for this
175 * commit.
176 */
177 if (graph_show_remainder(revs->graph))
178 putchar('\n');
179 if (revs->commit_format == CMIT_FMT_ONELINE)
180 putchar('\n');
7fefda5c 181 }
674d1727 182 strbuf_release(&buf);
7fefda5c 183 } else {
11c211fa 184 if (graph_show_remainder(revs->graph))
7fefda5c 185 putchar('\n');
7620d39f 186 }
06f59e9f 187 maybe_flush_or_die(stdout, "stdout");
11c211fa 188 finish_commit(commit, data);
27350891
SP
189}
190
11c211fa 191static void finish_commit(struct commit *commit, void *data)
27350891 192{
cb115748
LT
193 if (commit->parents) {
194 free_commit_list(commit->parents);
195 commit->parents = NULL;
196 }
0fb370da 197 free_commit_buffer(commit);
a3437b8c
JS
198}
199
caf3827e
JH
200static inline void finish_object__ma(struct object *obj)
201{
df11e196
JT
202 /*
203 * Whether or not we try to dynamically fetch missing objects
204 * from the server, we currently DO NOT have the object. We
205 * can either print, allow (ignore), or conditionally allow
206 * (ignore) them.
207 */
caf3827e
JH
208 switch (arg_missing_action) {
209 case MA_ERROR:
210 die("missing blob object '%s'", oid_to_hex(&obj->oid));
211 return;
212
213 case MA_ALLOW_ANY:
214 return;
215
216 case MA_PRINT:
217 oidset_insert(&missing_objects, &obj->oid);
218 return;
219
df11e196
JT
220 case MA_ALLOW_PROMISOR:
221 if (is_promisor_object(&obj->oid))
222 return;
223 die("unexpected missing blob object '%s'",
224 oid_to_hex(&obj->oid));
225 return;
226
caf3827e
JH
227 default:
228 BUG("unhandled missing_action");
229 return;
230 }
231}
232
df11e196 233static int finish_object(struct object *obj, const char *name, void *cb_data)
27350891 234{
98993722 235 struct rev_list_info *info = cb_data;
df11e196 236 if (obj->type == OBJ_BLOB && !has_object_file(&obj->oid)) {
caf3827e 237 finish_object__ma(obj);
df11e196
JT
238 return 1;
239 }
98993722 240 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
c251c83d 241 parse_object(&obj->oid);
df11e196 242 return 0;
27350891
SP
243}
244
de1e67d0 245static void show_object(struct object *obj, const char *name, void *cb_data)
9de48752 246{
cb8da705 247 struct rev_list_info *info = cb_data;
df11e196
JT
248 if (finish_object(obj, name, cb_data))
249 return;
434ea3cd 250 display_progress(progress, ++progress_counter);
98993722
NTND
251 if (info->flags & REV_LIST_QUIET)
252 return;
de1e67d0 253 show_object_with_name(stdout, obj, name);
9de48752
LT
254}
255
8d1d8f83
JH
256static void show_edge(struct commit *commit)
257{
f2fd0760 258 printf("-%s\n", oid_to_hex(&commit->object.oid));
8d1d8f83
JH
259}
260
38ef7507 261static void print_var_str(const char *var, const char *val)
280e65cb 262{
38ef7507 263 printf("%s='%s'\n", var, val);
280e65cb
CC
264}
265
38ef7507 266static void print_var_int(const char *var, int val)
280e65cb 267{
38ef7507 268 printf("%s=%d\n", var, val);
280e65cb
CC
269}
270
f1c92c63 271static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
9996983c 272{
98993722 273 int cnt, flags = info->flags;
dc01505f 274 char hex[GIT_MAX_HEXSZ + 1] = "";
95188648 275 struct commit_list *tried;
d797257e 276 struct rev_info *revs = info->revs;
9996983c 277
8ba8fe04 278 if (!revs->commits)
9996983c
CC
279 return 1;
280
9af3589e
CC
281 revs->commits = filter_skipped(revs->commits, &tried,
282 flags & BISECT_SHOW_ALL,
283 NULL, NULL);
95188648 284
9996983c 285 /*
7428d754 286 * revs->commits can reach "reaches" commits among
9996983c
CC
287 * "all" commits. If it is good, then there are
288 * (all-reaches) commits left to be bisected.
289 * On the other hand, if it is bad, then the set
290 * to bisect is "reaches".
291 * A bisect set of size N has (N-1) commits further
292 * to test, as we already know one bad one.
293 */
294 cnt = all - reaches;
295 if (cnt < reaches)
296 cnt = reaches;
6a17fad7 297
95188648 298 if (revs->commits)
2490574d 299 oid_to_hex_r(hex, &revs->commits->item->object.oid);
9996983c 300
37c4c38d 301 if (flags & BISECT_SHOW_ALL) {
d797257e 302 traverse_commit_list(revs, show_commit, show_object, info);
9996983c
CC
303 printf("------\n");
304 }
305
38ef7507
CC
306 print_var_str("bisect_rev", hex);
307 print_var_int("bisect_nr", cnt - 1);
308 print_var_int("bisect_good", all - reaches - 1);
309 print_var_int("bisect_bad", reaches - 1);
310 print_var_int("bisect_all", all);
311 print_var_int("bisect_steps", estimate_bisect_steps(all));
9996983c
CC
312
313 return 0;
314}
315
aa32939f 316static int show_object_fast(
20664967 317 const struct object_id *oid,
aa32939f
VM
318 enum object_type type,
319 int exclude,
320 uint32_t name_hash,
321 struct packed_git *found_pack,
322 off_t found_offset)
323{
20664967 324 fprintf(stdout, "%s\n", oid_to_hex(oid));
aa32939f
VM
325 return 1;
326}
327
caf3827e
JH
328static inline int parse_missing_action_value(const char *value)
329{
330 if (!strcmp(value, "error")) {
331 arg_missing_action = MA_ERROR;
332 return 1;
333 }
334
335 if (!strcmp(value, "allow-any")) {
336 arg_missing_action = MA_ALLOW_ANY;
df11e196 337 fetch_if_missing = 0;
caf3827e
JH
338 return 1;
339 }
340
341 if (!strcmp(value, "print")) {
342 arg_missing_action = MA_PRINT;
df11e196
JT
343 fetch_if_missing = 0;
344 return 1;
345 }
346
347 if (!strcmp(value, "allow-promisor")) {
348 arg_missing_action = MA_ALLOW_PROMISOR;
349 fetch_if_missing = 0;
caf3827e
JH
350 return 1;
351 }
352
353 return 0;
354}
355
a633fca0 356int cmd_rev_list(int argc, const char **argv, const char *prefix)
64745109 357{
11c211fa 358 struct rev_info revs;
d797257e 359 struct rev_list_info info;
d9a83684 360 int i;
ff62d732 361 int bisect_list = 0;
457f08a0 362 int bisect_show_vars = 0;
50e62a8e 363 int bisect_find_all = 0;
aa32939f 364 int use_bitmap_index = 0;
434ea3cd 365 const char *show_progress = NULL;
64745109 366
5a88f97c
JH
367 if (argc == 2 && !strcmp(argv[1], "-h"))
368 usage(rev_list_usage);
369
ef90d6d4 370 git_config(git_default_config, NULL);
a633fca0 371 init_revisions(&revs, prefix);
7337b138 372 revs.abbrev = DEFAULT_ABBREV;
7594c4b2 373 revs.commit_format = CMIT_FMT_UNSPECIFIED;
df11e196
JT
374
375 /*
376 * Scan the argument list before invoking setup_revisions(), so that we
377 * know if fetch_if_missing needs to be set to 0.
378 *
379 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
380 * by not crossing the boundary from realized objects to promisor
381 * objects.
382 *
383 * Let "--missing" to conditionally set fetch_if_missing.
384 */
385 for (i = 1; i < argc; i++) {
386 const char *arg = argv[i];
387 if (!strcmp(arg, "--exclude-promisor-objects")) {
388 fetch_if_missing = 0;
389 revs.exclude_promisor_objects = 1;
390 break;
391 }
392 }
393 for (i = 1; i < argc; i++) {
394 const char *arg = argv[i];
395 if (skip_prefix(arg, "--missing=", &arg)) {
396 if (revs.exclude_promisor_objects)
397 die(_("cannot combine --exclude-promisor-objects and --missing"));
398 if (parse_missing_action_value(arg))
399 break;
400 }
401 }
402
a4a88b2b 403 argc = setup_revisions(argc, argv, &revs, NULL);
ae563542 404
d797257e
CC
405 memset(&info, 0, sizeof(info));
406 info.revs = &revs;
ad3f9a71
LT
407 if (revs.bisect)
408 bisect_list = 1;
d797257e 409
0d1e0e78 410 if (revs.diffopt.flags.quick)
98993722 411 info.flags |= REV_LIST_QUIET;
fcfda02b 412 for (i = 1 ; i < argc; i++) {
cf484544 413 const char *arg = argv[i];
fcfda02b 414
a6f68d47 415 if (!strcmp(arg, "--header")) {
7594c4b2 416 revs.verbose_header = 1;
9d97aa64
LT
417 continue;
418 }
dc68c4ff 419 if (!strcmp(arg, "--timestamp")) {
d797257e 420 info.show_timestamp = 1;
dc68c4ff
JH
421 continue;
422 }
8b3a1e05
LT
423 if (!strcmp(arg, "--bisect")) {
424 bisect_list = 1;
425 continue;
426 }
50e62a8e
CC
427 if (!strcmp(arg, "--bisect-all")) {
428 bisect_list = 1;
429 bisect_find_all = 1;
98993722 430 info.flags |= BISECT_SHOW_ALL;
6e46cc0d 431 revs.show_decorations = 1;
50e62a8e
CC
432 continue;
433 }
457f08a0
JH
434 if (!strcmp(arg, "--bisect-vars")) {
435 bisect_list = 1;
436 bisect_show_vars = 1;
437 continue;
438 }
aa32939f
VM
439 if (!strcmp(arg, "--use-bitmap-index")) {
440 use_bitmap_index = 1;
441 continue;
442 }
443 if (!strcmp(arg, "--test-bitmap")) {
444 test_bitmap_walk(&revs);
445 return 0;
446 }
434ea3cd
JK
447 if (skip_prefix(arg, "--progress=", &arg)) {
448 show_progress = arg;
449 continue;
450 }
caf3827e
JH
451
452 if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
453 parse_list_objects_filter(&filter_options, arg);
454 if (filter_options.choice && !revs.blob_objects)
455 die(_("object filtering requires --objects"));
456 if (filter_options.choice == LOFC_SPARSE_OID &&
457 !filter_options.sparse_oid_value)
458 die(_("invalid sparse value '%s'"),
459 filter_options.filter_spec);
460 continue;
461 }
f4371a88 462 if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
aa57b871 463 list_objects_filter_set_no_filter(&filter_options);
f4371a88
JH
464 continue;
465 }
caf3827e
JH
466 if (!strcmp(arg, "--filter-print-omitted")) {
467 arg_print_omitted = 1;
468 continue;
469 }
470
df11e196
JT
471 if (!strcmp(arg, "--exclude-promisor-objects"))
472 continue; /* already handled above */
473 if (skip_prefix(arg, "--missing=", &arg))
474 continue; /* already handled above */
caf3827e 475
ae563542 476 usage(rev_list_usage);
a6f68d47 477
fcfda02b 478 }
7594c4b2
JH
479 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
480 /* The command line has a --pretty */
d797257e 481 info.hdr_termination = '\n';
7594c4b2 482 if (revs.commit_format == CMIT_FMT_ONELINE)
d797257e 483 info.header_prefix = "";
7594c4b2 484 else
d797257e 485 info.header_prefix = "commit ";
7594c4b2 486 }
db89665f
JH
487 else if (revs.verbose_header)
488 /* Only --header was specified */
489 revs.commit_format = CMIT_FMT_RAW;
fcfda02b 490
7f97de5e 491 if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
c01499ef 492 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
0159ba32
JK
493 !revs.pending.nr) &&
494 !revs.rev_input_given) ||
8c1f0b44 495 revs.diff)
7b34c2fa
LT
496 usage(rev_list_usage);
497
2aea7a51
JK
498 if (revs.show_notes)
499 die(_("rev-list does not support display of notes"));
500
caf3827e
JH
501 if (filter_options.choice && use_bitmap_index)
502 die(_("cannot combine --use-bitmap-index with object filtering"));
503
80235ba7
JH
504 save_commit_buffer = (revs.verbose_header ||
505 revs.grep_filter.pattern_list ||
506 revs.grep_filter.header_list);
4e1dc640
JH
507 if (bisect_list)
508 revs.limited = 1;
9181ca2c 509
434ea3cd 510 if (show_progress)
8aade107 511 progress = start_delayed_progress(show_progress, 0);
434ea3cd 512
c8a70d35 513 if (use_bitmap_index && !revs.prune) {
aa32939f
VM
514 if (revs.count && !revs.left_right && !revs.cherry_mark) {
515 uint32_t commit_count;
5c9f9bf3 516 int max_count = revs.max_count;
aa32939f
VM
517 if (!prepare_bitmap_walk(&revs)) {
518 count_bitmap_commit_list(&commit_count, NULL, NULL, NULL);
5c9f9bf3
JK
519 if (max_count >= 0 && max_count < commit_count)
520 commit_count = max_count;
aa32939f
VM
521 printf("%d\n", commit_count);
522 return 0;
523 }
fb85db84
JK
524 } else if (revs.max_count < 0 &&
525 revs.tag_objects && revs.tree_objects && revs.blob_objects) {
aa32939f
VM
526 if (!prepare_bitmap_walk(&revs)) {
527 traverse_bitmap_commit_list(&show_object_fast);
528 return 0;
529 }
530 }
531 }
532
3d51e1b5
MK
533 if (prepare_revision_walk(&revs))
534 die("revision walk setup failed");
a4a88b2b 535 if (revs.tree_objects)
e76a5fb4 536 mark_edges_uninteresting(&revs, show_edge);
a4a88b2b 537
457f08a0
JH
538 if (bisect_list) {
539 int reaches = reaches, all = all;
540
24d707f6 541 find_bisection(&revs.commits, &reaches, &all, bisect_find_all);
95188648 542
9996983c 543 if (bisect_show_vars)
13858e57 544 return show_bisect_vars(&info, reaches, all);
457f08a0 545 }
7b34c2fa 546
caf3827e
JH
547 if (arg_print_omitted)
548 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
549 if (arg_missing_action == MA_PRINT)
550 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
551
552 traverse_commit_list_filtered(
553 &filter_options, &revs, show_commit, show_object, &info,
554 (arg_print_omitted ? &omitted_objects : NULL));
555
556 if (arg_print_omitted) {
557 struct oidset_iter iter;
558 struct object_id *oid;
559 oidset_iter_init(&omitted_objects, &iter);
560 while ((oid = oidset_iter_next(&iter)))
561 printf("~%s\n", oid_to_hex(oid));
562 oidset_clear(&omitted_objects);
563 }
564 if (arg_missing_action == MA_PRINT) {
565 struct oidset_iter iter;
566 struct object_id *oid;
567 oidset_iter_init(&missing_objects, &iter);
568 while ((oid = oidset_iter_next(&iter)))
569 printf("?%s\n", oid_to_hex(oid));
570 oidset_clear(&missing_objects);
571 }
8906300f 572
434ea3cd
JK
573 stop_progress(&progress);
574
f69c5018 575 if (revs.count) {
b388e14b
MG
576 if (revs.left_right && revs.cherry_mark)
577 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
578 else if (revs.left_right)
f69c5018 579 printf("%d\t%d\n", revs.count_left, revs.count_right);
b388e14b
MG
580 else if (revs.cherry_mark)
581 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
f69c5018
TR
582 else
583 printf("%d\n", revs.count_left + revs.count_right);
584 }
585
64745109
LT
586 return 0;
587}