]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/rev-list.c
Merge branch 'sg/name-rev-cutoff-underflow-fix'
[thirdparty/git.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 #include "object-store.h"
22
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"
61 ;
62
63 static struct progress *progress;
64 static unsigned progress_counter;
65
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 */
69
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 */
76 };
77 static enum missing_action arg_missing_action;
78
79 /* display only the oid of each object encountered */
80 static int arg_show_object_names = 1;
81
82 #define DEFAULT_OIDSET_SIZE (16*1024)
83
84 static void finish_commit(struct commit *commit);
85 static 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
198 static 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
208 static 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
242 static 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
254 static 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
268 static void show_edge(struct commit *commit)
269 {
270 printf("-%s\n", oid_to_hex(&commit->object.oid));
271 }
272
273 static void print_var_str(const char *var, const char *val)
274 {
275 printf("%s='%s'\n", var, val);
276 }
277
278 static void print_var_int(const char *var, int val)
279 {
280 printf("%s=%d\n", var, val);
281 }
282
283 static 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
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)
335 {
336 fprintf(stdout, "%s\n", oid_to_hex(oid));
337 return 1;
338 }
339
340 static 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
368 int 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 continue;
475 }
476 if (!strcmp(arg, ("--no-" CL_ARG__FILTER))) {
477 list_objects_filter_set_no_filter(&filter_options);
478 continue;
479 }
480 if (!strcmp(arg, "--filter-print-omitted")) {
481 arg_print_omitted = 1;
482 continue;
483 }
484
485 if (!strcmp(arg, "--exclude-promisor-objects"))
486 continue; /* already handled above */
487 if (skip_prefix(arg, "--missing=", &arg))
488 continue; /* already handled above */
489
490 if (!strcmp(arg, ("--no-object-names"))) {
491 arg_show_object_names = 0;
492 continue;
493 }
494
495 if (!strcmp(arg, ("--object-names"))) {
496 arg_show_object_names = 1;
497 continue;
498 }
499
500 usage(rev_list_usage);
501
502 }
503 if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {
504 /* The command line has a --pretty */
505 info.hdr_termination = '\n';
506 if (revs.commit_format == CMIT_FMT_ONELINE)
507 info.header_prefix = "";
508 else
509 info.header_prefix = "commit ";
510 }
511 else if (revs.verbose_header)
512 /* Only --header was specified */
513 revs.commit_format = CMIT_FMT_RAW;
514
515 if ((!revs.commits && reflog_walk_empty(revs.reflog_info) &&
516 (!(revs.tag_objects || revs.tree_objects || revs.blob_objects) &&
517 !revs.pending.nr) &&
518 !revs.rev_input_given && !revs.read_from_stdin) ||
519 revs.diff)
520 usage(rev_list_usage);
521
522 if (revs.show_notes)
523 die(_("rev-list does not support display of notes"));
524
525 if (filter_options.choice && use_bitmap_index)
526 die(_("cannot combine --use-bitmap-index with object filtering"));
527
528 save_commit_buffer = (revs.verbose_header ||
529 revs.grep_filter.pattern_list ||
530 revs.grep_filter.header_list);
531 if (bisect_list)
532 revs.limited = 1;
533
534 if (show_progress)
535 progress = start_delayed_progress(show_progress, 0);
536
537 if (use_bitmap_index && !revs.prune) {
538 if (revs.count && !revs.left_right && !revs.cherry_mark) {
539 uint32_t commit_count;
540 int max_count = revs.max_count;
541 struct bitmap_index *bitmap_git;
542 if ((bitmap_git = prepare_bitmap_walk(&revs))) {
543 count_bitmap_commit_list(bitmap_git, &commit_count, NULL, NULL, NULL);
544 if (max_count >= 0 && max_count < commit_count)
545 commit_count = max_count;
546 printf("%d\n", commit_count);
547 free_bitmap_index(bitmap_git);
548 return 0;
549 }
550 } else if (revs.max_count < 0 &&
551 revs.tag_objects && revs.tree_objects && revs.blob_objects) {
552 struct bitmap_index *bitmap_git;
553 if ((bitmap_git = prepare_bitmap_walk(&revs))) {
554 traverse_bitmap_commit_list(bitmap_git, &show_object_fast);
555 free_bitmap_index(bitmap_git);
556 return 0;
557 }
558 }
559 }
560
561 if (prepare_revision_walk(&revs))
562 die("revision walk setup failed");
563 if (revs.tree_objects)
564 mark_edges_uninteresting(&revs, show_edge, 0);
565
566 if (bisect_list) {
567 int reaches, all;
568
569 find_bisection(&revs.commits, &reaches, &all, bisect_find_all);
570
571 if (bisect_show_vars)
572 return show_bisect_vars(&info, reaches, all);
573 }
574
575 if (arg_print_omitted)
576 oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
577 if (arg_missing_action == MA_PRINT)
578 oidset_init(&missing_objects, DEFAULT_OIDSET_SIZE);
579
580 traverse_commit_list_filtered(
581 &filter_options, &revs, show_commit, show_object, &info,
582 (arg_print_omitted ? &omitted_objects : NULL));
583
584 if (arg_print_omitted) {
585 struct oidset_iter iter;
586 struct object_id *oid;
587 oidset_iter_init(&omitted_objects, &iter);
588 while ((oid = oidset_iter_next(&iter)))
589 printf("~%s\n", oid_to_hex(oid));
590 oidset_clear(&omitted_objects);
591 }
592 if (arg_missing_action == MA_PRINT) {
593 struct oidset_iter iter;
594 struct object_id *oid;
595 oidset_iter_init(&missing_objects, &iter);
596 while ((oid = oidset_iter_next(&iter)))
597 printf("?%s\n", oid_to_hex(oid));
598 oidset_clear(&missing_objects);
599 }
600
601 stop_progress(&progress);
602
603 if (revs.count) {
604 if (revs.left_right && revs.cherry_mark)
605 printf("%d\t%d\t%d\n", revs.count_left, revs.count_right, revs.count_same);
606 else if (revs.left_right)
607 printf("%d\t%d\n", revs.count_left, revs.count_right);
608 else if (revs.cherry_mark)
609 printf("%d\t%d\n", revs.count_left + revs.count_right, revs.count_same);
610 else
611 printf("%d\n", revs.count_left + revs.count_right);
612 }
613
614 return 0;
615 }