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"
22
23static 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"
61;
62
63static struct progress *progress;
64static unsigned progress_counter;
65
66static struct list_objects_filter_options filter_options;
67static struct oidset omitted_objects;
68static int arg_print_omitted; /* print objects omitted by filter */
69
70static struct oidset missing_objects;
71enum 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 */
76};
77static enum missing_action arg_missing_action;
78
79/* display only the oid of each object encountered */
80static int arg_show_object_names = 1;
81
82#define DEFAULT_OIDSET_SIZE (16*1024)
83
84static void finish_commit(struct commit *commit);
85static void show_commit(struct commit *commit, void *data)
86{
87 struct rev_list_info *info = data;
88 struct rev_info *revs = info->revs;
89
90 display_progress(progress, ++progress_counter);
91
92 if (info->flags & REV_LIST_QUIET) {
93 finish_commit(commit);
94 return;
95 }
96
97 graph_show_commit(revs->graph);
98
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;
108 }
109
110 if (info->show_timestamp)
111 printf("%"PRItime" ", commit->date);
112 if (info->header_prefix)
113 fputs(info->header_prefix, stdout);
114
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;
127 }
128 }
129 if (revs->children.name) {
130 struct commit_list *children;
131
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;
136 }
137 }
138 show_decorations(revs, commit);
139 if (revs->commit_format == CMIT_FMT_ONELINE)
140 putchar(' ');
141 else
142 putchar('\n');
143
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);
157
158 graph_show_commit_msg(revs->graph, stdout, &buf);
159
160 /*
161 * Add a newline after the commit message.
162 *
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.
166 *
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.)
174 */
175 if (buf.len && buf.buf[buf.len - 1] == '\n')
176 graph_show_padding(revs->graph);
177 putchar(info->hdr_termination);
178 } else {
179 /*
180 * If the message buffer is empty, just show
181 * the rest of the graph output for this
182 * commit.
183 */
184 if (graph_show_remainder(revs->graph))
185 putchar('\n');
186 if (revs->commit_format == CMIT_FMT_ONELINE)
187 putchar('\n');
188 }
189 strbuf_release(&buf);
190 } else {
191 if (graph_show_remainder(revs->graph))
192 putchar('\n');
193 }
194 maybe_flush_or_die(stdout, "stdout");
195 finish_commit(commit);
196}
197
198static void finish_commit(struct commit *commit)
199{
200 if (commit->parents) {
201 free_commit_list(commit->parents);
202 commit->parents = NULL;
203 }
204 free_commit_buffer(the_repository->parsed_objects,
205 commit);
206}
207
208static inline void finish_object__ma(struct object *obj)
209{
210 /*
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.
215 */
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;
221
222 case MA_ALLOW_ANY:
223 return;
224
225 case MA_PRINT:
226 oidset_insert(&missing_objects, &obj->oid);
227 return;
228
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;
235
236 default:
237 BUG("unhandled missing_action");
238 return;
239 }
240}
241
242static int finish_object(struct object *obj, const char *name, void *cb_data)
243{
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;
248 }
249 if (info->revs->verify_objects && !obj->parsed && obj->type != OBJ_COMMIT)
250 parse_object(the_repository, &obj->oid);
251 return 0;
252}
253
254static void show_object(struct object *obj, const char *name, void *cb_data)
255{
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));
266}
267
268static void show_edge(struct commit *commit)
269{
270 printf("-%s\n", oid_to_hex(&commit->object.oid));
271}
272
273static void print_var_str(const char *var, const char *val)
274{
275 printf("%s='%s'\n", var, val);
276}
277
278static void print_var_int(const char *var, int val)
279{
280 printf("%s=%d\n", var, val);
281}
282
283static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)
284{
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;
289
290 if (!revs->commits)
291 return 1;
292
293 revs->commits = filter_skipped(revs->commits, &tried,
294 flags & BISECT_SHOW_ALL,
295 NULL, NULL);
296
297 /*
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.
305 */
306 cnt = all - reaches;
307 if (cnt < reaches)
308 cnt = reaches;
309
310 if (revs->commits)
311 oid_to_hex_r(hex, &revs->commits->item->object.oid);
312
313 if (flags & BISECT_SHOW_ALL) {
314 traverse_commit_list(revs, show_commit, show_object, info);
315 printf("------\n");
316 }
317
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));
324
325 return 0;
326}
327
328static 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)
335{
336 fprintf(stdout, "%s\n", oid_to_hex(oid));
337 return 1;
338}
339
340static inline int parse_missing_action_value(const char *value)
341{
342 if (!strcmp(value, "error")) {
343 arg_missing_action = MA_ERROR;
344 return 1;
345 }
346
347 if (!strcmp(value, "allow-any")) {
348 arg_missing_action = MA_ALLOW_ANY;
349 fetch_if_missing = 0;
350 return 1;
351 }
352
353 if (!strcmp(value, "print")) {
354 arg_missing_action = MA_PRINT;
355 fetch_if_missing = 0;
356 return 1;
357 }
358
359 if (!strcmp(value, "allow-promisor")) {
360 arg_missing_action = MA_ALLOW_PROMISOR;
361 fetch_if_missing = 0;
362 return 1;
363 }
364
365 return 0;
366}
367
368int cmd_rev_list(int argc, const char **argv, const char *prefix)
369{
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,
374 };
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;
381
382 if (argc == 2 && !strcmp(argv[1], "-h"))
383 usage(rev_list_usage);
384
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;
389
390 /*
391 * Scan the argument list before invoking setup_revisions(), so that we
392 * know if fetch_if_missing needs to be set to 0.
393 *
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.
397 *
398 * Let "--missing" to conditionally set fetch_if_missing.
399 */
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;
406 }
407 }
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;
415 }
416 }
417
418 if (arg_missing_action)
419 revs.do_not_die_on_missing_tree = 1;
420
421 argc = setup_revisions(argc, argv, &revs, &s_r_opt);
422
423 memset(&info, 0, sizeof(info));
424 info.revs = &revs;
425 if (revs.bisect)
426 bisect_list = 1;
427
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];
432
433 if (!strcmp(arg, "--header")) {
434 revs.verbose_header = 1;
435 continue;
436 }
437 if (!strcmp(arg, "--timestamp")) {
438 info.show_timestamp = 1;
439 continue;
440 }
441 if (!strcmp(arg, "--bisect")) {
442 bisect_list = 1;
443 continue;
444 }
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;
451 }
452 if (!strcmp(arg, "--bisect-vars")) {
453 bisect_list = 1;
454 bisect_show_vars = 1;
455 continue;
456 }
457 if (!strcmp(arg, "--use-bitmap-index")) {
458 use_bitmap_index = 1;
459 continue;
460 }
461 if (!strcmp(arg, "--test-bitmap")) {
462 test_bitmap_walk(&revs);
463 return 0;
464 }
465 if (skip_prefix(arg, "--progress=", &arg)) {
466 show_progress = arg;
467 continue;
468 }
469
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(
477 _("invalid sparse value '%s'"),
478 list_objects_filter_spec(
479 &filter_options));
480 continue;
481 }
482 if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
483 list_objects_filter_set_no_filter(&filter_options);
484 continue;
485 }
486 if (!strcmp(arg, "--filter-print-omitted")) {
487 arg_print_omitted = 1;
488 continue;
489 }
490
491 if (!strcmp(arg, "--exclude-promisor-objects"))
492 continue; /* already handled above */
493 if (skip_prefix(arg, "--missing=", &arg))
494 continue; /* already handled above */
495
496 if (!strcmp(arg, ("--no-object-names"))) {
497 arg_show_object_names = 0;
498 continue;
499 }
500
501 if (!strcmp(arg, ("--object-names"))) {
502 arg_show_object_names = 1;
503 continue;
504 }
505
506 usage(rev_list_usage);
507
508 }
509 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
510 /* The command line has a --pretty */
511 info.hdr_termination = '\n';
512 if (revs.commit_format == CMIT_FMT_ONELINE)
513 info.header_prefix = "";
514 else
515 info.header_prefix = "commit ";
516 }
517 else if (revs.verbose_header)
518 /* Only --header was specified */
519 revs.commit_format = CMIT_FMT_RAW;
520
521 if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
522 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
523 !revs.pending.nr) &&
524 !revs.rev_input_given && !revs.read_from_stdin) ||
525 revs.diff)
526 usage(rev_list_usage);
527
528 if (revs.show_notes)
529 die(_("rev-list does not support display of notes"));
530
531 if (filter_options.choice && use_bitmap_index)
532 die(_("cannot combine --use-bitmap-index with object filtering"));
533
534 save_commit_buffer = (revs.verbose_header ||
535 revs.grep_filter.pattern_list ||
536 revs.grep_filter.header_list);
537 if (bisect_list)
538 revs.limited = 1;
539
540 if (show_progress)
541 progress = start_delayed_progress(show_progress, 0);
542
543 if (use_bitmap_index && !revs.prune) {
544 if (revs.count && !revs.left_right && !revs.cherry_mark) {
545 uint32_t commit_count;
546 int max_count = revs.max_count;
547 struct bitmap_index *bitmap_git;
548 if ((bitmap_git = prepare_bitmap_walk(&revs))) {
549 count_bitmap_commit_list(bitmap_git, &commit_count, NULL, NULL, NULL);
550 if (max_count >= 0 && max_count < commit_count)
551 commit_count = max_count;
552 printf("%d\n", commit_count);
553 free_bitmap_index(bitmap_git);
554 return 0;
555 }
556 } else if (revs.max_count < 0 &&
557 revs.tag_objects && revs.tree_objects && revs.blob_objects) {
558 struct bitmap_index *bitmap_git;
559 if ((bitmap_git = prepare_bitmap_walk(&revs))) {
560 traverse_bitmap_commit_list(bitmap_git, &show_object_fast);
561 free_bitmap_index(bitmap_git);
562 return 0;
563 }
564 }
565 }
566
567 if (prepare_revision_walk(&revs))
568 die("revision walk setup failed");
569 if (revs.tree_objects)
570 mark_edges_uninteresting(&revs, show_edge, 0);
571
572 if (bisect_list) {
573 int reaches, all;
574
575 find_bisection(&revs.commits, &reaches, &all, bisect_find_all);
576
577 if (bisect_show_vars)
578 return show_bisect_vars(&info, reaches, all);
579 }
580
581 if (arg_print_omitted)
582 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
583 if (arg_missing_action == MA_PRINT)
584 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
585
586 traverse_commit_list_filtered(
587 &filter_options, &revs, show_commit, show_object, &info,
588 (arg_print_omitted ? &omitted_objects : NULL));
589
590 if (arg_print_omitted) {
591 struct oidset_iter iter;
592 struct object_id *oid;
593 oidset_iter_init(&omitted_objects, &iter);
594 while ((oid = oidset_iter_next(&iter)))
595 printf("~%s\n", oid_to_hex(oid));
596 oidset_clear(&omitted_objects);
597 }
598 if (arg_missing_action == MA_PRINT) {
599 struct oidset_iter iter;
600 struct object_id *oid;
601 oidset_iter_init(&missing_objects, &iter);
602 while ((oid = oidset_iter_next(&iter)))
603 printf("?%s\n", oid_to_hex(oid));
604 oidset_clear(&missing_objects);
605 }
606
607 stop_progress(&progress);
608
609 if (revs.count) {
610 if (revs.left_right && revs.cherry_mark)
611 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
612 else if (revs.left_right)
613 printf("%d\t%d\n", revs.count_left, revs.count_right);
614 else if (revs.cherry_mark)
615 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
616 else
617 printf("%d\n", revs.count_left + revs.count_right);
618 }
619
620 return 0;
621}