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