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