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