builtin/submodule--helper: fix usage string for 'update-clone'
[git/debian.git] / builtin / rev-list.c
blob301ccb970bb36340bc6b4a136a3029811f252aa2
1 #include "cache.h"
2 #include "config.h"
3 #include "commit.h"
4 #include "diff.h"
5 #include "revision.h"
6 #include "list-objects.h"
7 #include "list-objects-filter.h"
8 #include "list-objects-filter-options.h"
9 #include "object.h"
10 #include "object-store.h"
11 #include "pack.h"
12 #include "pack-bitmap.h"
13 #include "builtin.h"
14 #include "log-tree.h"
15 #include "graph.h"
16 #include "bisect.h"
17 #include "progress.h"
18 #include "reflog-walk.h"
19 #include "oidset.h"
20 #include "packfile.h"
21 #include "object-store.h"
23 static const char rev_list_usage[] =
24 "git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
25 " limiting output:\n"
26 " --max-count=<n>\n"
27 " --max-age=<epoch>\n"
28 " --min-age=<epoch>\n"
29 " --sparse\n"
30 " --no-merges\n"
31 " --min-parents=<n>\n"
32 " --no-min-parents\n"
33 " --max-parents=<n>\n"
34 " --no-max-parents\n"
35 " --remove-empty\n"
36 " --all\n"
37 " --branches\n"
38 " --tags\n"
39 " --remotes\n"
40 " --stdin\n"
41 " --quiet\n"
42 " ordering output:\n"
43 " --topo-order\n"
44 " --date-order\n"
45 " --reverse\n"
46 " formatting output:\n"
47 " --parents\n"
48 " --children\n"
49 " --objects | --objects-edge\n"
50 " --unpacked\n"
51 " --header | --pretty\n"
52 " --[no-]object-names\n"
53 " --abbrev=<n> | --no-abbrev\n"
54 " --abbrev-commit\n"
55 " --left-right\n"
56 " --count\n"
57 " special purpose:\n"
58 " --bisect\n"
59 " --bisect-vars\n"
60 " --bisect-all"
63 static struct progress *progress;
64 static unsigned progress_counter;
66 static struct list_objects_filter_options filter_options;
67 static struct oidset omitted_objects;
68 static int arg_print_omitted; /* print objects omitted by filter */
70 static struct oidset missing_objects;
71 enum missing_action {
72 MA_ERROR = 0, /* fail if any missing objects are encountered */
73 MA_ALLOW_ANY, /* silently allow ALL missing objects */
74 MA_PRINT, /* print ALL missing objects in special section */
75 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
77 static enum missing_action arg_missing_action;
79 /* display only the oid of each object encountered */
80 static int arg_show_object_names = 1;
82 #define DEFAULT_OIDSET_SIZE (16*1024)
84 static void finish_commit(struct commit *commit);
85 static void show_commit(struct commit *commit, void *data)
87 struct rev_list_info *info = data;
88 struct rev_info *revs = info->revs;
90 display_progress(progress, ++progress_counter);
92 if (info->flags & REV_LIST_QUIET) {
93 finish_commit(commit);
94 return;
97 graph_show_commit(revs->graph);
99 if (revs->count) {
100 if (commit->object.flags & PATCHSAME)
101 revs->count_same++;
102 else if (commit->object.flags & SYMMETRIC_LEFT)
103 revs->count_left++;
104 else
105 revs->count_right++;
106 finish_commit(commit);
107 return;
110 if (info->show_timestamp)
111 printf("%"PRItime" ", commit->date);
112 if (info->header_prefix)
113 fputs(info->header_prefix, stdout);
115 if (!revs->graph)
116 fputs(get_revision_mark(revs, commit), stdout);
117 if (revs->abbrev_commit && revs->abbrev)
118 fputs(find_unique_abbrev(&commit->object.oid, revs->abbrev),
119 stdout);
120 else
121 fputs(oid_to_hex(&commit->object.oid), stdout);
122 if (revs->print_parents) {
123 struct commit_list *parents = commit->parents;
124 while (parents) {
125 printf(" %s", oid_to_hex(&parents->item->object.oid));
126 parents = parents->next;
129 if (revs->children.name) {
130 struct commit_list *children;
132 children = lookup_decoration(&revs->children, &commit->object);
133 while (children) {
134 printf(" %s", oid_to_hex(&children->item->object.oid));
135 children = children->next;
138 show_decorations(revs, commit);
139 if (revs->commit_format == CMIT_FMT_ONELINE)
140 putchar(' ');
141 else
142 putchar('\n');
144 if (revs->verbose_header) {
145 struct strbuf buf = STRBUF_INIT;
146 struct pretty_print_context ctx = {0};
147 ctx.abbrev = revs->abbrev;
148 ctx.date_mode = revs->date_mode;
149 ctx.date_mode_explicit = revs->date_mode_explicit;
150 ctx.fmt = revs->commit_format;
151 ctx.output_encoding = get_log_output_encoding();
152 ctx.color = revs->diffopt.use_color;
153 pretty_print_commit(&ctx, commit, &buf);
154 if (buf.len) {
155 if (revs->commit_format != CMIT_FMT_ONELINE)
156 graph_show_oneline(revs->graph);
158 graph_show_commit_msg(revs->graph, stdout, &buf);
161 * Add a newline after the commit message.
163 * Usually, this newline produces a blank
164 * padding line between entries, in which case
165 * we need to add graph padding on this line.
167 * However, the commit message may not end in a
168 * newline. In this case the newline simply
169 * ends the last line of the commit message,
170 * and we don't need any graph output. (This
171 * always happens with CMIT_FMT_ONELINE, and it
172 * happens with CMIT_FMT_USERFORMAT when the
173 * format doesn't explicitly end in a newline.)
175 if (buf.len && buf.buf[buf.len - 1] == '\n')
176 graph_show_padding(revs->graph);
177 putchar(info->hdr_termination);
178 } else {
180 * If the message buffer is empty, just show
181 * the rest of the graph output for this
182 * commit.
184 if (graph_show_remainder(revs->graph))
185 putchar('\n');
186 if (revs->commit_format == CMIT_FMT_ONELINE)
187 putchar('\n');
189 strbuf_release(&buf);
190 } else {
191 if (graph_show_remainder(revs->graph))
192 putchar('\n');
194 maybe_flush_or_die(stdout, "stdout");
195 finish_commit(commit);
198 static void finish_commit(struct commit *commit)
200 if (commit->parents) {
201 free_commit_list(commit->parents);
202 commit->parents = NULL;
204 free_commit_buffer(the_repository->parsed_objects,
205 commit);
208 static inline void finish_object__ma(struct object *obj)
211 * Whether or not we try to dynamically fetch missing objects
212 * from the server, we currently DO NOT have the object. We
213 * can either print, allow (ignore), or conditionally allow
214 * (ignore) them.
216 switch (arg_missing_action) {
217 case MA_ERROR:
218 die("missing %s object '%s'",
219 type_name(obj->type), oid_to_hex(&obj->oid));
220 return;
222 case MA_ALLOW_ANY:
223 return;
225 case MA_PRINT:
226 oidset_insert(&missing_objects, &obj->oid);
227 return;
229 case MA_ALLOW_PROMISOR:
230 if (is_promisor_object(&obj->oid))
231 return;
232 die("unexpected missing %s object '%s'",
233 type_name(obj->type), oid_to_hex(&obj->oid));
234 return;
236 default:
237 BUG("unhandled missing_action");
238 return;
242 static int finish_object(struct object *obj, const char *name, void *cb_data)
244 struct rev_list_info *info = cb_data;
245 if (oid_object_info_extended(the_repository, &obj->oid, NULL, 0) < 0) {
246 finish_object__ma(obj);
247 return 1;
249 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
250 parse_object(the_repository, &obj->oid);
251 return 0;
254 static void show_object(struct object *obj, const char *name, void *cb_data)
256 struct rev_list_info *info = cb_data;
257 if (finish_object(obj, name, cb_data))
258 return;
259 display_progress(progress, ++progress_counter);
260 if (info->flags & REV_LIST_QUIET)
261 return;
262 if (arg_show_object_names)
263 show_object_with_name(stdout, obj, name);
264 else
265 printf("%s\n", oid_to_hex(&obj->oid));
268 static void show_edge(struct commit *commit)
270 printf("-%s\n", oid_to_hex(&commit->object.oid));
273 static void print_var_str(const char *var, const char *val)
275 printf("%s='%s'\n", var, val);
278 static void print_var_int(const char *var, int val)
280 printf("%s=%d\n", var, val);
283 static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
285 int cnt, flags = info->flags;
286 char hex[GIT_MAX_HEXSZ + 1] = "";
287 struct commit_list *tried;
288 struct rev_info *revs = info->revs;
290 if (!revs->commits)
291 return 1;
293 revs->commits = filter_skipped(revs->commits, &tried,
294 flags & BISECT_SHOW_ALL,
295 NULL, NULL);
298 * revs->commits can reach "reaches" commits among
299 * "all" commits. If it is good, then there are
300 * (all-reaches) commits left to be bisected.
301 * On the other hand, if it is bad, then the set
302 * to bisect is "reaches".
303 * A bisect set of size N has (N-1) commits further
304 * to test, as we already know one bad one.
306 cnt = all - reaches;
307 if (cnt < reaches)
308 cnt = reaches;
310 if (revs->commits)
311 oid_to_hex_r(hex, &revs->commits->item->object.oid);
313 if (flags & BISECT_SHOW_ALL) {
314 traverse_commit_list(revs, show_commit, show_object, info);
315 printf("------\n");
318 print_var_str("bisect_rev", hex);
319 print_var_int("bisect_nr", cnt - 1);
320 print_var_int("bisect_good", all - reaches - 1);
321 print_var_int("bisect_bad", reaches - 1);
322 print_var_int("bisect_all", all);
323 print_var_int("bisect_steps", estimate_bisect_steps(all));
325 return 0;
328 static int show_object_fast(
329 const struct object_id *oid,
330 enum object_type type,
331 int exclude,
332 uint32_t name_hash,
333 struct packed_git *found_pack,
334 off_t found_offset)
336 fprintf(stdout, "%s\n", oid_to_hex(oid));
337 return 1;
340 static inline int parse_missing_action_value(const char *value)
342 if (!strcmp(value, "error")) {
343 arg_missing_action = MA_ERROR;
344 return 1;
347 if (!strcmp(value, "allow-any")) {
348 arg_missing_action = MA_ALLOW_ANY;
349 fetch_if_missing = 0;
350 return 1;
353 if (!strcmp(value, "print")) {
354 arg_missing_action = MA_PRINT;
355 fetch_if_missing = 0;
356 return 1;
359 if (!strcmp(value, "allow-promisor")) {
360 arg_missing_action = MA_ALLOW_PROMISOR;
361 fetch_if_missing = 0;
362 return 1;
365 return 0;
368 int cmd_rev_list(int argc, const char **argv, const char *prefix)
370 struct rev_info revs;
371 struct rev_list_info info;
372 struct setup_revision_opt s_r_opt = {
373 .allow_exclude_promisor_objects = 1,
375 int i;
376 int bisect_list = 0;
377 int bisect_show_vars = 0;
378 int bisect_find_all = 0;
379 int use_bitmap_index = 0;
380 const char *show_progress = NULL;
382 if (argc == 2 && !strcmp(argv[1], "-h"))
383 usage(rev_list_usage);
385 git_config(git_default_config, NULL);
386 repo_init_revisions(the_repository, &revs, prefix);
387 revs.abbrev = DEFAULT_ABBREV;
388 revs.commit_format = CMIT_FMT_UNSPECIFIED;
391 * Scan the argument list before invoking setup_revisions(), so that we
392 * know if fetch_if_missing needs to be set to 0.
394 * "--exclude-promisor-objects" acts as a pre-filter on missing objects
395 * by not crossing the boundary from realized objects to promisor
396 * objects.
398 * Let "--missing" to conditionally set fetch_if_missing.
400 for (i = 1; i < argc; i++) {
401 const char *arg = argv[i];
402 if (!strcmp(arg, "--exclude-promisor-objects")) {
403 fetch_if_missing = 0;
404 revs.exclude_promisor_objects = 1;
405 break;
408 for (i = 1; i < argc; i++) {
409 const char *arg = argv[i];
410 if (skip_prefix(arg, "--missing=", &arg)) {
411 if (revs.exclude_promisor_objects)
412 die(_("cannot combine --exclude-promisor-objects and --missing"));
413 if (parse_missing_action_value(arg))
414 break;
418 if (arg_missing_action)
419 revs.do_not_die_on_missing_tree = 1;
421 argc = setup_revisions(argc, argv, &revs, &s_r_opt);
423 memset(&info, 0, sizeof(info));
424 info.revs = &revs;
425 if (revs.bisect)
426 bisect_list = 1;
428 if (revs.diffopt.flags.quick)
429 info.flags |= REV_LIST_QUIET;
430 for (i = 1 ; i < argc; i++) {
431 const char *arg = argv[i];
433 if (!strcmp(arg, "--header")) {
434 revs.verbose_header = 1;
435 continue;
437 if (!strcmp(arg, "--timestamp")) {
438 info.show_timestamp = 1;
439 continue;
441 if (!strcmp(arg, "--bisect")) {
442 bisect_list = 1;
443 continue;
445 if (!strcmp(arg, "--bisect-all")) {
446 bisect_list = 1;
447 bisect_find_all = 1;
448 info.flags |= BISECT_SHOW_ALL;
449 revs.show_decorations = 1;
450 continue;
452 if (!strcmp(arg, "--bisect-vars")) {
453 bisect_list = 1;
454 bisect_show_vars = 1;
455 continue;
457 if (!strcmp(arg, "--use-bitmap-index")) {
458 use_bitmap_index = 1;
459 continue;
461 if (!strcmp(arg, "--test-bitmap")) {
462 test_bitmap_walk(&revs);
463 return 0;
465 if (skip_prefix(arg, "--progress=", &arg)) {
466 show_progress = arg;
467 continue;
470 if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
471 parse_list_objects_filter(&filter_options, arg);
472 if (filter_options.choice && !revs.blob_objects)
473 die(_("object filtering requires --objects"));
474 if (filter_options.choice == LOFC_SPARSE_OID &&
475 !filter_options.sparse_oid_value)
476 die(_("invalid sparse value '%s'"),
477 filter_options.filter_spec);
478 continue;
480 if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
481 list_objects_filter_set_no_filter(&filter_options);
482 continue;
484 if (!strcmp(arg, "--filter-print-omitted")) {
485 arg_print_omitted = 1;
486 continue;
489 if (!strcmp(arg, "--exclude-promisor-objects"))
490 continue; /* already handled above */
491 if (skip_prefix(arg, "--missing=", &arg))
492 continue; /* already handled above */
494 if (!strcmp(arg, ("--no-object-names"))) {
495 arg_show_object_names = 0;
496 continue;
499 if (!strcmp(arg, ("--object-names"))) {
500 arg_show_object_names = 1;
501 continue;
504 usage(rev_list_usage);
507 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
508 /* The command line has a --pretty */
509 info.hdr_termination = '\n';
510 if (revs.commit_format == CMIT_FMT_ONELINE)
511 info.header_prefix = "";
512 else
513 info.header_prefix = "commit ";
515 else if (revs.verbose_header)
516 /* Only --header was specified */
517 revs.commit_format = CMIT_FMT_RAW;
519 if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
520 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
521 !revs.pending.nr) &&
522 !revs.rev_input_given && !revs.read_from_stdin) ||
523 revs.diff)
524 usage(rev_list_usage);
526 if (revs.show_notes)
527 die(_("rev-list does not support display of notes"));
529 if (filter_options.choice && use_bitmap_index)
530 die(_("cannot combine --use-bitmap-index with object filtering"));
532 save_commit_buffer = (revs.verbose_header ||
533 revs.grep_filter.pattern_list ||
534 revs.grep_filter.header_list);
535 if (bisect_list)
536 revs.limited = 1;
538 if (show_progress)
539 progress = start_delayed_progress(show_progress, 0);
541 if (use_bitmap_index && !revs.prune) {
542 if (revs.count && !revs.left_right && !revs.cherry_mark) {
543 uint32_t commit_count;
544 int max_count = revs.max_count;
545 struct bitmap_index *bitmap_git;
546 if ((bitmap_git = prepare_bitmap_walk(&revs))) {
547 count_bitmap_commit_list(bitmap_git, &commit_count, NULL, NULL, NULL);
548 if (max_count >= 0 && max_count < commit_count)
549 commit_count = max_count;
550 printf("%d\n", commit_count);
551 free_bitmap_index(bitmap_git);
552 return 0;
554 } else if (revs.max_count < 0 &&
555 revs.tag_objects && revs.tree_objects && revs.blob_objects) {
556 struct bitmap_index *bitmap_git;
557 if ((bitmap_git = prepare_bitmap_walk(&revs))) {
558 traverse_bitmap_commit_list(bitmap_git, &show_object_fast);
559 free_bitmap_index(bitmap_git);
560 return 0;
565 if (prepare_revision_walk(&revs))
566 die("revision walk setup failed");
567 if (revs.tree_objects)
568 mark_edges_uninteresting(&revs, show_edge, 0);
570 if (bisect_list) {
571 int reaches, all;
573 find_bisection(&revs.commits, &reaches, &all, bisect_find_all);
575 if (bisect_show_vars)
576 return show_bisect_vars(&info, reaches, all);
579 if (arg_print_omitted)
580 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
581 if (arg_missing_action == MA_PRINT)
582 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
584 traverse_commit_list_filtered(
585 &filter_options, &revs, show_commit, show_object, &info,
586 (arg_print_omitted ? &omitted_objects : NULL));
588 if (arg_print_omitted) {
589 struct oidset_iter iter;
590 struct object_id *oid;
591 oidset_iter_init(&omitted_objects, &iter);
592 while ((oid = oidset_iter_next(&iter)))
593 printf("~%s\n", oid_to_hex(oid));
594 oidset_clear(&omitted_objects);
596 if (arg_missing_action == MA_PRINT) {
597 struct oidset_iter iter;
598 struct object_id *oid;
599 oidset_iter_init(&missing_objects, &iter);
600 while ((oid = oidset_iter_next(&iter)))
601 printf("?%s\n", oid_to_hex(oid));
602 oidset_clear(&missing_objects);
605 stop_progress(&progress);
607 if (revs.count) {
608 if (revs.left_right && revs.cherry_mark)
609 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
610 else if (revs.left_right)
611 printf("%d\t%d\n", revs.count_left, revs.count_right);
612 else if (revs.cherry_mark)
613 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
614 else
615 printf("%d\n", revs.count_left + revs.count_right);
618 return 0;