rev-list: allow commit-only bitmap traversals
[git] / builtin / rev-list.c
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
22 static const char rev_list_usage[] =
23 "git rev-list [OPTION] <commit-id>... [ -- paths... ]\n"
24 "  limiting output:\n"
25 "    --max-count=<n>\n"
26 "    --max-age=<epoch>\n"
27 "    --min-age=<epoch>\n"
28 "    --sparse\n"
29 "    --no-merges\n"
30 "    --min-parents=<n>\n"
31 "    --no-min-parents\n"
32 "    --max-parents=<n>\n"
33 "    --no-max-parents\n"
34 "    --remove-empty\n"
35 "    --all\n"
36 "    --branches\n"
37 "    --tags\n"
38 "    --remotes\n"
39 "    --stdin\n"
40 "    --quiet\n"
41 "  ordering output:\n"
42 "    --topo-order\n"
43 "    --date-order\n"
44 "    --reverse\n"
45 "  formatting output:\n"
46 "    --parents\n"
47 "    --children\n"
48 "    --objects | --objects-edge\n"
49 "    --unpacked\n"
50 "    --header | --pretty\n"
51 "    --[no-]object-names\n"
52 "    --abbrev=<n> | --no-abbrev\n"
53 "    --abbrev-commit\n"
54 "    --left-right\n"
55 "    --count\n"
56 "  special purpose:\n"
57 "    --bisect\n"
58 "    --bisect-vars\n"
59 "    --bisect-all"
60 ;
61
62 static struct progress *progress;
63 static unsigned progress_counter;
64
65 static struct list_objects_filter_options filter_options;
66 static struct oidset omitted_objects;
67 static int arg_print_omitted; /* print objects omitted by filter */
68
69 static struct oidset missing_objects;
70 enum missing_action {
71         MA_ERROR = 0,    /* fail if any missing objects are encountered */
72         MA_ALLOW_ANY,    /* silently allow ALL missing objects */
73         MA_PRINT,        /* print ALL missing objects in special section */
74         MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */
75 };
76 static enum missing_action arg_missing_action;
77
78 /* display only the oid of each object encountered */
79 static int arg_show_object_names = 1;
80
81 #define DEFAULT_OIDSET_SIZE     (16*1024)
82
83 static void finish_commit(struct commit *commit);
84 static void show_commit(struct commit *commit, void *data)
85 {
86         struct rev_list_info *info = data;
87         struct rev_info *revs = info->revs;
88
89         display_progress(progress, ++progress_counter);
90
91         if (info->flags & REV_LIST_QUIET) {
92                 finish_commit(commit);
93                 return;
94         }
95
96         graph_show_commit(revs->graph);
97
98         if (revs->count) {
99                 if (commit->object.flags & PATCHSAME)
100                         revs->count_same++;
101                 else if (commit->object.flags & SYMMETRIC_LEFT)
102                         revs->count_left++;
103                 else
104                         revs->count_right++;
105                 finish_commit(commit);
106                 return;
107         }
108
109         if (info->show_timestamp)
110                 printf("%"PRItime" ", commit->date);
111         if (info->header_prefix)
112                 fputs(info->header_prefix, stdout);
113
114         if (!revs->graph)
115                 fputs(get_revision_mark(revs, commit), stdout);
116         if (revs->abbrev_commit && revs->abbrev)
117                 fputs(find_unique_abbrev(&commit->object.oid, revs->abbrev),
118                       stdout);
119         else
120                 fputs(oid_to_hex(&commit->object.oid), stdout);
121         if (revs->print_parents) {
122                 struct commit_list *parents = commit->parents;
123                 while (parents) {
124                         printf(" %s", oid_to_hex(&parents->item->object.oid));
125                         parents = parents->next;
126                 }
127         }
128         if (revs->children.name) {
129                 struct commit_list *children;
130
131                 children = lookup_decoration(&revs->children, &commit->object);
132                 while (children) {
133                         printf(" %s", oid_to_hex(&children->item->object.oid));
134                         children = children->next;
135                 }
136         }
137         show_decorations(revs, commit);
138         if (revs->commit_format == CMIT_FMT_ONELINE)
139                 putchar(' ');
140         else
141                 putchar('\n');
142
143         if (revs->verbose_header) {
144                 struct strbuf buf = STRBUF_INIT;
145                 struct pretty_print_context ctx = {0};
146                 ctx.abbrev = revs->abbrev;
147                 ctx.date_mode = revs->date_mode;
148                 ctx.date_mode_explicit = revs->date_mode_explicit;
149                 ctx.fmt = revs->commit_format;
150                 ctx.output_encoding = get_log_output_encoding();
151                 ctx.color = revs->diffopt.use_color;
152                 pretty_print_commit(&ctx, commit, &buf);
153                 if (buf.len) {
154                         if (revs->commit_format != CMIT_FMT_ONELINE)
155                                 graph_show_oneline(revs->graph);
156
157                         graph_show_commit_msg(revs->graph, stdout, &buf);
158
159                         /*
160                          * Add a newline after the commit message.
161                          *
162                          * Usually, this newline produces a blank
163                          * padding line between entries, in which case
164                          * we need to add graph padding on this line.
165                          *
166                          * However, the commit message may not end in a
167                          * newline.  In this case the newline simply
168                          * ends the last line of the commit message,
169                          * and we don't need any graph output.  (This
170                          * always happens with CMIT_FMT_ONELINE, and it
171                          * happens with CMIT_FMT_USERFORMAT when the
172                          * format doesn't explicitly end in a newline.)
173                          */
174                         if (buf.len && buf.buf[buf.len - 1] == '\n')
175                                 graph_show_padding(revs->graph);
176                         putchar(info->hdr_termination);
177                 } else {
178                         /*
179                          * If the message buffer is empty, just show
180                          * the rest of the graph output for this
181                          * commit.
182                          */
183                         if (graph_show_remainder(revs->graph))
184                                 putchar('\n');
185                         if (revs->commit_format == CMIT_FMT_ONELINE)
186                                 putchar('\n');
187                 }
188                 strbuf_release(&buf);
189         } else {
190                 if (graph_show_remainder(revs->graph))
191                         putchar('\n');
192         }
193         maybe_flush_or_die(stdout, "stdout");
194         finish_commit(commit);
195 }
196
197 static void finish_commit(struct commit *commit)
198 {
199         if (commit->parents) {
200                 free_commit_list(commit->parents);
201                 commit->parents = NULL;
202         }
203         free_commit_buffer(the_repository->parsed_objects,
204                            commit);
205 }
206
207 static inline void finish_object__ma(struct object *obj)
208 {
209         /*
210          * Whether or not we try to dynamically fetch missing objects
211          * from the server, we currently DO NOT have the object.  We
212          * can either print, allow (ignore), or conditionally allow
213          * (ignore) them.
214          */
215         switch (arg_missing_action) {
216         case MA_ERROR:
217                 die("missing %s object '%s'",
218                     type_name(obj->type), oid_to_hex(&obj->oid));
219                 return;
220
221         case MA_ALLOW_ANY:
222                 return;
223
224         case MA_PRINT:
225                 oidset_insert(&missing_objects, &obj->oid);
226                 return;
227
228         case MA_ALLOW_PROMISOR:
229                 if (is_promisor_object(&obj->oid))
230                         return;
231                 die("unexpected missing %s object '%s'",
232                     type_name(obj->type), oid_to_hex(&obj->oid));
233                 return;
234
235         default:
236                 BUG("unhandled missing_action");
237                 return;
238         }
239 }
240
241 static int finish_object(struct object *obj, const char *name, void *cb_data)
242 {
243         struct rev_list_info *info = cb_data;
244         if (oid_object_info_extended(the_repository, &obj->oid, NULL, 0) < 0) {
245                 finish_object__ma(obj);
246                 return 1;
247         }
248         if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
249                 parse_object(the_repository, &obj->oid);
250         return 0;
251 }
252
253 static void show_object(struct object *obj, const char *name, void *cb_data)
254 {
255         struct rev_list_info *info = cb_data;
256         struct rev_info *revs = info->revs;
257
258         if (finish_object(obj, name, cb_data))
259                 return;
260         display_progress(progress, ++progress_counter);
261         if (info->flags & REV_LIST_QUIET)
262                 return;
263
264         if (revs->count) {
265                 revs->count_right++;
266                 return;
267         }
268
269         if (arg_show_object_names)
270                 show_object_with_name(stdout, obj, name);
271         else
272                 printf("%s\n", oid_to_hex(&obj->oid));
273 }
274
275 static void show_edge(struct commit *commit)
276 {
277         printf("-%s\n", oid_to_hex(&commit->object.oid));
278 }
279
280 static void print_var_str(const char *var, const char *val)
281 {
282         printf("%s='%s'\n", var, val);
283 }
284
285 static void print_var_int(const char *var, int val)
286 {
287         printf("%s=%d\n", var, val);
288 }
289
290 static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
291 {
292         int cnt, flags = info->flags;
293         char hex[GIT_MAX_HEXSZ + 1] = "";
294         struct commit_list *tried;
295         struct rev_info *revs = info->revs;
296
297         if (!revs->commits)
298                 return 1;
299
300         revs->commits = filter_skipped(revs->commits, &tried,
301                                        flags & BISECT_SHOW_ALL,
302                                        NULL, NULL);
303
304         /*
305          * revs->commits can reach "reaches" commits among
306          * "all" commits.  If it is good, then there are
307          * (all-reaches) commits left to be bisected.
308          * On the other hand, if it is bad, then the set
309          * to bisect is "reaches".
310          * A bisect set of size N has (N-1) commits further
311          * to test, as we already know one bad one.
312          */
313         cnt = all - reaches;
314         if (cnt < reaches)
315                 cnt = reaches;
316
317         if (revs->commits)
318                 oid_to_hex_r(hex, &revs->commits->item->object.oid);
319
320         if (flags & BISECT_SHOW_ALL) {
321                 traverse_commit_list(revs, show_commit, show_object, info);
322                 printf("------\n");
323         }
324
325         print_var_str("bisect_rev", hex);
326         print_var_int("bisect_nr", cnt - 1);
327         print_var_int("bisect_good", all - reaches - 1);
328         print_var_int("bisect_bad", reaches - 1);
329         print_var_int("bisect_all", all);
330         print_var_int("bisect_steps", estimate_bisect_steps(all));
331
332         return 0;
333 }
334
335 static int show_object_fast(
336         const struct object_id *oid,
337         enum object_type type,
338         int exclude,
339         uint32_t name_hash,
340         struct packed_git *found_pack,
341         off_t found_offset)
342 {
343         fprintf(stdout, "%s\n", oid_to_hex(oid));
344         return 1;
345 }
346
347 static inline int parse_missing_action_value(const char *value)
348 {
349         if (!strcmp(value, "error")) {
350                 arg_missing_action = MA_ERROR;
351                 return 1;
352         }
353
354         if (!strcmp(value, "allow-any")) {
355                 arg_missing_action = MA_ALLOW_ANY;
356                 fetch_if_missing = 0;
357                 return 1;
358         }
359
360         if (!strcmp(value, "print")) {
361                 arg_missing_action = MA_PRINT;
362                 fetch_if_missing = 0;
363                 return 1;
364         }
365
366         if (!strcmp(value, "allow-promisor")) {
367                 arg_missing_action = MA_ALLOW_PROMISOR;
368                 fetch_if_missing = 0;
369                 return 1;
370         }
371
372         return 0;
373 }
374
375 static int try_bitmap_count(struct rev_info *revs)
376 {
377         uint32_t commit_count = 0,
378                  tag_count = 0,
379                  tree_count = 0,
380                  blob_count = 0;
381         int max_count;
382         struct bitmap_index *bitmap_git;
383
384         /* This function only handles counting, not general traversal. */
385         if (!revs->count)
386                 return -1;
387
388         /*
389          * A bitmap result can't know left/right, etc, because we don't
390          * actually traverse.
391          */
392         if (revs->left_right || revs->cherry_mark)
393                 return -1;
394
395         /*
396          * If we're counting reachable objects, we can't handle a max count of
397          * commits to traverse, since we don't know which objects go with which
398          * commit.
399          */
400         if (revs->max_count >= 0 &&
401             (revs->tag_objects || revs->tree_objects || revs->blob_objects))
402                 return -1;
403
404         /*
405          * This must be saved before doing any walking, since the revision
406          * machinery will count it down to zero while traversing.
407          */
408         max_count = revs->max_count;
409
410         bitmap_git = prepare_bitmap_walk(revs);
411         if (!bitmap_git)
412                 return -1;
413
414         count_bitmap_commit_list(bitmap_git, &commit_count,
415                                  revs->tree_objects ? &tree_count : NULL,
416                                  revs->blob_objects ? &blob_count : NULL,
417                                  revs->tag_objects ? &tag_count : NULL);
418         if (max_count >= 0 && max_count < commit_count)
419                 commit_count = max_count;
420
421         printf("%d\n", commit_count + tree_count + blob_count + tag_count);
422         free_bitmap_index(bitmap_git);
423         return 0;
424 }
425
426 static int try_bitmap_traversal(struct rev_info *revs)
427 {
428         struct bitmap_index *bitmap_git;
429
430         /*
431          * We can't use a bitmap result with a traversal limit, since the set
432          * of commits we'd get would be essentially random.
433          */
434         if (revs->max_count >= 0)
435                 return -1;
436
437         bitmap_git = prepare_bitmap_walk(revs);
438         if (!bitmap_git)
439                 return -1;
440
441         traverse_bitmap_commit_list(bitmap_git, revs, &show_object_fast);
442         free_bitmap_index(bitmap_git);
443         return 0;
444 }
445
446 int cmd_rev_list(int argc, const char **argv, const char *prefix)
447 {
448         struct rev_info revs;
449         struct rev_list_info info;
450         struct setup_revision_opt s_r_opt = {
451                 .allow_exclude_promisor_objects = 1,
452         };
453         int i;
454         int bisect_list = 0;
455         int bisect_show_vars = 0;
456         int bisect_find_all = 0;
457         int use_bitmap_index = 0;
458         const char *show_progress = NULL;
459
460         if (argc == 2 && !strcmp(argv[1], "-h"))
461                 usage(rev_list_usage);
462
463         git_config(git_default_config, NULL);
464         repo_init_revisions(the_repository, &revs, prefix);
465         revs.abbrev = DEFAULT_ABBREV;
466         revs.commit_format = CMIT_FMT_UNSPECIFIED;
467
468         /*
469          * Scan the argument list before invoking setup_revisions(), so that we
470          * know if fetch_if_missing needs to be set to 0.
471          *
472          * "--exclude-promisor-objects" acts as a pre-filter on missing objects
473          * by not crossing the boundary from realized objects to promisor
474          * objects.
475          *
476          * Let "--missing" to conditionally set fetch_if_missing.
477          */
478         for (i = 1; i < argc; i++) {
479                 const char *arg = argv[i];
480                 if (!strcmp(arg, "--exclude-promisor-objects")) {
481                         fetch_if_missing = 0;
482                         revs.exclude_promisor_objects = 1;
483                         break;
484                 }
485         }
486         for (i = 1; i < argc; i++) {
487                 const char *arg = argv[i];
488                 if (skip_prefix(arg, "--missing=", &arg)) {
489                         if (revs.exclude_promisor_objects)
490                                 die(_("cannot combine --exclude-promisor-objects and --missing"));
491                         if (parse_missing_action_value(arg))
492                                 break;
493                 }
494         }
495
496         if (arg_missing_action)
497                 revs.do_not_die_on_missing_tree = 1;
498
499         argc = setup_revisions(argc, argv, &revs, &s_r_opt);
500
501         memset(&info, 0, sizeof(info));
502         info.revs = &revs;
503         if (revs.bisect)
504                 bisect_list = 1;
505
506         if (revs.diffopt.flags.quick)
507                 info.flags |= REV_LIST_QUIET;
508         for (i = 1 ; i < argc; i++) {
509                 const char *arg = argv[i];
510
511                 if (!strcmp(arg, "--header")) {
512                         revs.verbose_header = 1;
513                         continue;
514                 }
515                 if (!strcmp(arg, "--timestamp")) {
516                         info.show_timestamp = 1;
517                         continue;
518                 }
519                 if (!strcmp(arg, "--bisect")) {
520                         bisect_list = 1;
521                         continue;
522                 }
523                 if (!strcmp(arg, "--bisect-all")) {
524                         bisect_list = 1;
525                         bisect_find_all = 1;
526                         info.flags |= BISECT_SHOW_ALL;
527                         revs.show_decorations = 1;
528                         continue;
529                 }
530                 if (!strcmp(arg, "--bisect-vars")) {
531                         bisect_list = 1;
532                         bisect_show_vars = 1;
533                         continue;
534                 }
535                 if (!strcmp(arg, "--use-bitmap-index")) {
536                         use_bitmap_index = 1;
537                         continue;
538                 }
539                 if (!strcmp(arg, "--test-bitmap")) {
540                         test_bitmap_walk(&revs);
541                         return 0;
542                 }
543                 if (skip_prefix(arg, "--progress=", &arg)) {
544                         show_progress = arg;
545                         continue;
546                 }
547
548                 if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
549                         parse_list_objects_filter(&filter_options, arg);
550                         if (filter_options.choice && !revs.blob_objects)
551                                 die(_("object filtering requires --objects"));
552                         continue;
553                 }
554                 if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
555                         list_objects_filter_set_no_filter(&filter_options);
556                         continue;
557                 }
558                 if (!strcmp(arg, "--filter-print-omitted")) {
559                         arg_print_omitted = 1;
560                         continue;
561                 }
562
563                 if (!strcmp(arg, "--exclude-promisor-objects"))
564                         continue; /* already handled above */
565                 if (skip_prefix(arg, "--missing=", &arg))
566                         continue; /* already handled above */
567
568                 if (!strcmp(arg, ("--no-object-names"))) {
569                         arg_show_object_names = 0;
570                         continue;
571                 }
572
573                 if (!strcmp(arg, ("--object-names"))) {
574                         arg_show_object_names = 1;
575                         continue;
576                 }
577
578                 usage(rev_list_usage);
579
580         }
581         if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
582                 /* The command line has a --pretty  */
583                 info.hdr_termination = '\n';
584                 if (revs.commit_format == CMIT_FMT_ONELINE)
585                         info.header_prefix = "";
586                 else
587                         info.header_prefix = "commit ";
588         }
589         else if (revs.verbose_header)
590                 /* Only --header was specified */
591                 revs.commit_format = CMIT_FMT_RAW;
592
593         if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
594              (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
595               !revs.pending.nr) &&
596              !revs.rev_input_given && !revs.read_from_stdin) ||
597             revs.diff)
598                 usage(rev_list_usage);
599
600         if (revs.show_notes)
601                 die(_("rev-list does not support display of notes"));
602
603         if (revs.count &&
604             (revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
605             (revs.left_right || revs.cherry_mark))
606                 die(_("marked counting is incompatible with --objects"));
607
608         if (filter_options.choice)
609                 use_bitmap_index = 0;
610
611         save_commit_buffer = (revs.verbose_header ||
612                               revs.grep_filter.pattern_list ||
613                               revs.grep_filter.header_list);
614         if (bisect_list)
615                 revs.limited = 1;
616
617         if (show_progress)
618                 progress = start_delayed_progress(show_progress, 0);
619
620         if (use_bitmap_index) {
621                 if (!try_bitmap_count(&revs))
622                         return 0;
623                 if (!try_bitmap_traversal(&revs))
624                         return 0;
625         }
626
627         if (prepare_revision_walk(&revs))
628                 die("revision walk setup failed");
629         if (revs.tree_objects)
630                 mark_edges_uninteresting(&revs, show_edge, 0);
631
632         if (bisect_list) {
633                 int reaches, all;
634
635                 find_bisection(&revs.commits, &reaches, &all, bisect_find_all);
636
637                 if (bisect_show_vars)
638                         return show_bisect_vars(&info, reaches, all);
639         }
640
641         if (arg_print_omitted)
642                 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
643         if (arg_missing_action == MA_PRINT)
644                 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
645
646         traverse_commit_list_filtered(
647                 &filter_options, &revs, show_commit, show_object, &info,
648                 (arg_print_omitted ? &omitted_objects : NULL));
649
650         if (arg_print_omitted) {
651                 struct oidset_iter iter;
652                 struct object_id *oid;
653                 oidset_iter_init(&omitted_objects, &iter);
654                 while ((oid = oidset_iter_next(&iter)))
655                         printf("~%s\n", oid_to_hex(oid));
656                 oidset_clear(&omitted_objects);
657         }
658         if (arg_missing_action == MA_PRINT) {
659                 struct oidset_iter iter;
660                 struct object_id *oid;
661                 oidset_iter_init(&missing_objects, &iter);
662                 while ((oid = oidset_iter_next(&iter)))
663                         printf("?%s\n", oid_to_hex(oid));
664                 oidset_clear(&missing_objects);
665         }
666
667         stop_progress(&progress);
668
669         if (revs.count) {
670                 if (revs.left_right && revs.cherry_mark)
671                         printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
672                 else if (revs.left_right)
673                         printf("%d\t%d\n", revs.count_left, revs.count_right);
674                 else if (revs.cherry_mark)
675                         printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
676                 else
677                         printf("%d\n", revs.count_left + revs.count_right);
678         }
679
680         return 0;
681 }