rev-list: factor out bitmap-optimized routines
[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         if (finish_object(obj, name, cb_data))
257                 return;
258         display_progress(progress, ++progress_counter);
259         if (info->flags & REV_LIST_QUIET)
260                 return;
261         if (arg_show_object_names)
262                 show_object_with_name(stdout, obj, name);
263         else
264                 printf("%s\n", oid_to_hex(&obj->oid));
265 }
266
267 static void show_edge(struct commit *commit)
268 {
269         printf("-%s\n", oid_to_hex(&commit->object.oid));
270 }
271
272 static void print_var_str(const char *var, const char *val)
273 {
274         printf("%s='%s'\n", var, val);
275 }
276
277 static void print_var_int(const char *var, int val)
278 {
279         printf("%s=%d\n", var, val);
280 }
281
282 static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
283 {
284         int cnt, flags = info->flags;
285         char hex[GIT_MAX_HEXSZ + 1] = "";
286         struct commit_list *tried;
287         struct rev_info *revs = info->revs;
288
289         if (!revs->commits)
290                 return 1;
291
292         revs->commits = filter_skipped(revs->commits, &tried,
293                                        flags & BISECT_SHOW_ALL,
294                                        NULL, NULL);
295
296         /*
297          * revs->commits can reach "reaches" commits among
298          * "all" commits.  If it is good, then there are
299          * (all-reaches) commits left to be bisected.
300          * On the other hand, if it is bad, then the set
301          * to bisect is "reaches".
302          * A bisect set of size N has (N-1) commits further
303          * to test, as we already know one bad one.
304          */
305         cnt = all - reaches;
306         if (cnt < reaches)
307                 cnt = reaches;
308
309         if (revs->commits)
310                 oid_to_hex_r(hex, &revs->commits->item->object.oid);
311
312         if (flags & BISECT_SHOW_ALL) {
313                 traverse_commit_list(revs, show_commit, show_object, info);
314                 printf("------\n");
315         }
316
317         print_var_str("bisect_rev", hex);
318         print_var_int("bisect_nr", cnt - 1);
319         print_var_int("bisect_good", all - reaches - 1);
320         print_var_int("bisect_bad", reaches - 1);
321         print_var_int("bisect_all", all);
322         print_var_int("bisect_steps", estimate_bisect_steps(all));
323
324         return 0;
325 }
326
327 static int show_object_fast(
328         const struct object_id *oid,
329         enum object_type type,
330         int exclude,
331         uint32_t name_hash,
332         struct packed_git *found_pack,
333         off_t found_offset)
334 {
335         fprintf(stdout, "%s\n", oid_to_hex(oid));
336         return 1;
337 }
338
339 static inline int parse_missing_action_value(const char *value)
340 {
341         if (!strcmp(value, "error")) {
342                 arg_missing_action = MA_ERROR;
343                 return 1;
344         }
345
346         if (!strcmp(value, "allow-any")) {
347                 arg_missing_action = MA_ALLOW_ANY;
348                 fetch_if_missing = 0;
349                 return 1;
350         }
351
352         if (!strcmp(value, "print")) {
353                 arg_missing_action = MA_PRINT;
354                 fetch_if_missing = 0;
355                 return 1;
356         }
357
358         if (!strcmp(value, "allow-promisor")) {
359                 arg_missing_action = MA_ALLOW_PROMISOR;
360                 fetch_if_missing = 0;
361                 return 1;
362         }
363
364         return 0;
365 }
366
367 static int try_bitmap_count(struct rev_info *revs)
368 {
369         uint32_t commit_count;
370         int max_count;
371         struct bitmap_index *bitmap_git;
372
373         /* This function only handles counting, not general traversal. */
374         if (!revs->count)
375                 return -1;
376
377         /*
378          * A bitmap result can't know left/right, etc, because we don't
379          * actually traverse.
380          */
381         if (revs->left_right || revs->cherry_mark)
382                 return -1;
383
384         /*
385          * This must be saved before doing any walking, since the revision
386          * machinery will count it down to zero while traversing.
387          */
388         max_count = revs->max_count;
389
390         bitmap_git = prepare_bitmap_walk(revs);
391         if (!bitmap_git)
392                 return -1;
393
394         count_bitmap_commit_list(bitmap_git, &commit_count, NULL, NULL, NULL);
395         if (max_count >= 0 && max_count < commit_count)
396                 commit_count = max_count;
397
398         printf("%d\n", commit_count);
399         free_bitmap_index(bitmap_git);
400         return 0;
401 }
402
403 static int try_bitmap_traversal(struct rev_info *revs)
404 {
405         struct bitmap_index *bitmap_git;
406
407         /*
408          * We can't use a bitmap result with a traversal limit, since the set
409          * of commits we'd get would be essentially random.
410          */
411         if (revs->max_count >= 0)
412                 return -1;
413
414         /*
415          * Our bitmap result will return all objects, and we're not
416          * yet prepared to show only particular types.
417          */
418         if (!revs->tag_objects || !revs->tree_objects || !revs->blob_objects)
419                 return -1;
420
421         bitmap_git = prepare_bitmap_walk(revs);
422         if (!bitmap_git)
423                 return -1;
424
425         traverse_bitmap_commit_list(bitmap_git, &show_object_fast);
426         free_bitmap_index(bitmap_git);
427         return 0;
428 }
429
430 int cmd_rev_list(int argc, const char **argv, const char *prefix)
431 {
432         struct rev_info revs;
433         struct rev_list_info info;
434         struct setup_revision_opt s_r_opt = {
435                 .allow_exclude_promisor_objects = 1,
436         };
437         int i;
438         int bisect_list = 0;
439         int bisect_show_vars = 0;
440         int bisect_find_all = 0;
441         int use_bitmap_index = 0;
442         const char *show_progress = NULL;
443
444         if (argc == 2 && !strcmp(argv[1], "-h"))
445                 usage(rev_list_usage);
446
447         git_config(git_default_config, NULL);
448         repo_init_revisions(the_repository, &revs, prefix);
449         revs.abbrev = DEFAULT_ABBREV;
450         revs.commit_format = CMIT_FMT_UNSPECIFIED;
451
452         /*
453          * Scan the argument list before invoking setup_revisions(), so that we
454          * know if fetch_if_missing needs to be set to 0.
455          *
456          * "--exclude-promisor-objects" acts as a pre-filter on missing objects
457          * by not crossing the boundary from realized objects to promisor
458          * objects.
459          *
460          * Let "--missing" to conditionally set fetch_if_missing.
461          */
462         for (i = 1; i < argc; i++) {
463                 const char *arg = argv[i];
464                 if (!strcmp(arg, "--exclude-promisor-objects")) {
465                         fetch_if_missing = 0;
466                         revs.exclude_promisor_objects = 1;
467                         break;
468                 }
469         }
470         for (i = 1; i < argc; i++) {
471                 const char *arg = argv[i];
472                 if (skip_prefix(arg, "--missing=", &arg)) {
473                         if (revs.exclude_promisor_objects)
474                                 die(_("cannot combine --exclude-promisor-objects and --missing"));
475                         if (parse_missing_action_value(arg))
476                                 break;
477                 }
478         }
479
480         if (arg_missing_action)
481                 revs.do_not_die_on_missing_tree = 1;
482
483         argc = setup_revisions(argc, argv, &revs, &s_r_opt);
484
485         memset(&info, 0, sizeof(info));
486         info.revs = &revs;
487         if (revs.bisect)
488                 bisect_list = 1;
489
490         if (revs.diffopt.flags.quick)
491                 info.flags |= REV_LIST_QUIET;
492         for (i = 1 ; i < argc; i++) {
493                 const char *arg = argv[i];
494
495                 if (!strcmp(arg, "--header")) {
496                         revs.verbose_header = 1;
497                         continue;
498                 }
499                 if (!strcmp(arg, "--timestamp")) {
500                         info.show_timestamp = 1;
501                         continue;
502                 }
503                 if (!strcmp(arg, "--bisect")) {
504                         bisect_list = 1;
505                         continue;
506                 }
507                 if (!strcmp(arg, "--bisect-all")) {
508                         bisect_list = 1;
509                         bisect_find_all = 1;
510                         info.flags |= BISECT_SHOW_ALL;
511                         revs.show_decorations = 1;
512                         continue;
513                 }
514                 if (!strcmp(arg, "--bisect-vars")) {
515                         bisect_list = 1;
516                         bisect_show_vars = 1;
517                         continue;
518                 }
519                 if (!strcmp(arg, "--use-bitmap-index")) {
520                         use_bitmap_index = 1;
521                         continue;
522                 }
523                 if (!strcmp(arg, "--test-bitmap")) {
524                         test_bitmap_walk(&revs);
525                         return 0;
526                 }
527                 if (skip_prefix(arg, "--progress=", &arg)) {
528                         show_progress = arg;
529                         continue;
530                 }
531
532                 if (skip_prefix(arg, ("--" CL_ARG__FILTER "="), &arg)) {
533                         parse_list_objects_filter(&filter_options, arg);
534                         if (filter_options.choice && !revs.blob_objects)
535                                 die(_("object filtering requires --objects"));
536                         continue;
537                 }
538                 if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
539                         list_objects_filter_set_no_filter(&filter_options);
540                         continue;
541                 }
542                 if (!strcmp(arg, "--filter-print-omitted")) {
543                         arg_print_omitted = 1;
544                         continue;
545                 }
546
547                 if (!strcmp(arg, "--exclude-promisor-objects"))
548                         continue; /* already handled above */
549                 if (skip_prefix(arg, "--missing=", &arg))
550                         continue; /* already handled above */
551
552                 if (!strcmp(arg, ("--no-object-names"))) {
553                         arg_show_object_names = 0;
554                         continue;
555                 }
556
557                 if (!strcmp(arg, ("--object-names"))) {
558                         arg_show_object_names = 1;
559                         continue;
560                 }
561
562                 usage(rev_list_usage);
563
564         }
565         if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
566                 /* The command line has a --pretty  */
567                 info.hdr_termination = '\n';
568                 if (revs.commit_format == CMIT_FMT_ONELINE)
569                         info.header_prefix = "";
570                 else
571                         info.header_prefix = "commit ";
572         }
573         else if (revs.verbose_header)
574                 /* Only --header was specified */
575                 revs.commit_format = CMIT_FMT_RAW;
576
577         if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
578              (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
579               !revs.pending.nr) &&
580              !revs.rev_input_given && !revs.read_from_stdin) ||
581             revs.diff)
582                 usage(rev_list_usage);
583
584         if (revs.show_notes)
585                 die(_("rev-list does not support display of notes"));
586
587         if (filter_options.choice)
588                 use_bitmap_index = 0;
589
590         save_commit_buffer = (revs.verbose_header ||
591                               revs.grep_filter.pattern_list ||
592                               revs.grep_filter.header_list);
593         if (bisect_list)
594                 revs.limited = 1;
595
596         if (show_progress)
597                 progress = start_delayed_progress(show_progress, 0);
598
599         if (use_bitmap_index) {
600                 if (!try_bitmap_count(&revs))
601                         return 0;
602                 if (!try_bitmap_traversal(&revs))
603                         return 0;
604         }
605
606         if (prepare_revision_walk(&revs))
607                 die("revision walk setup failed");
608         if (revs.tree_objects)
609                 mark_edges_uninteresting(&revs, show_edge, 0);
610
611         if (bisect_list) {
612                 int reaches, all;
613
614                 find_bisection(&revs.commits, &reaches, &all, bisect_find_all);
615
616                 if (bisect_show_vars)
617                         return show_bisect_vars(&info, reaches, all);
618         }
619
620         if (arg_print_omitted)
621                 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
622         if (arg_missing_action == MA_PRINT)
623                 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
624
625         traverse_commit_list_filtered(
626                 &filter_options, &revs, show_commit, show_object, &info,
627                 (arg_print_omitted ? &omitted_objects : NULL));
628
629         if (arg_print_omitted) {
630                 struct oidset_iter iter;
631                 struct object_id *oid;
632                 oidset_iter_init(&omitted_objects, &iter);
633                 while ((oid = oidset_iter_next(&iter)))
634                         printf("~%s\n", oid_to_hex(oid));
635                 oidset_clear(&omitted_objects);
636         }
637         if (arg_missing_action == MA_PRINT) {
638                 struct oidset_iter iter;
639                 struct object_id *oid;
640                 oidset_iter_init(&missing_objects, &iter);
641                 while ((oid = oidset_iter_next(&iter)))
642                         printf("?%s\n", oid_to_hex(oid));
643                 oidset_clear(&missing_objects);
644         }
645
646         stop_progress(&progress);
647
648         if (revs.count) {
649                 if (revs.left_right && revs.cherry_mark)
650                         printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
651                 else if (revs.left_right)
652                         printf("%d\t%d\n", revs.count_left, revs.count_right);
653                 else if (revs.cherry_mark)
654                         printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
655                 else
656                         printf("%d\n", revs.count_left + revs.count_right);
657         }
658
659         return 0;
660 }