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