]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - revision.c
revisions API: have release_revisions() release "topo_walk_info"
[thirdparty/git.git] / revision.c
... / ...
CommitLineData
1#include "cache.h"
2#include "object-store.h"
3#include "tag.h"
4#include "blob.h"
5#include "tree.h"
6#include "commit.h"
7#include "diff.h"
8#include "diff-merges.h"
9#include "refs.h"
10#include "revision.h"
11#include "repository.h"
12#include "graph.h"
13#include "grep.h"
14#include "reflog-walk.h"
15#include "patch-ids.h"
16#include "decorate.h"
17#include "log-tree.h"
18#include "string-list.h"
19#include "line-log.h"
20#include "mailmap.h"
21#include "commit-slab.h"
22#include "dir.h"
23#include "cache-tree.h"
24#include "bisect.h"
25#include "packfile.h"
26#include "worktree.h"
27#include "strvec.h"
28#include "commit-reach.h"
29#include "commit-graph.h"
30#include "prio-queue.h"
31#include "hashmap.h"
32#include "utf8.h"
33#include "bloom.h"
34#include "json-writer.h"
35#include "list-objects-filter-options.h"
36
37volatile show_early_output_fn_t show_early_output;
38
39static const char *term_bad;
40static const char *term_good;
41
42implement_shared_commit_slab(revision_sources, char *);
43
44static inline int want_ancestry(const struct rev_info *revs);
45
46void show_object_with_name(FILE *out, struct object *obj, const char *name)
47{
48 fprintf(out, "%s ", oid_to_hex(&obj->oid));
49 /*
50 * This "for (const char *p = ..." is made as a first step towards
51 * making use of such declarations elsewhere in our codebase. If
52 * it causes compilation problems on your platform, please report
53 * it to the Git mailing list at git@vger.kernel.org. In the meantime,
54 * adding -std=gnu99 to CFLAGS may help if you are with older GCC.
55 */
56 for (const char *p = name; *p && *p != '\n'; p++)
57 fputc(*p, out);
58 fputc('\n', out);
59}
60
61static void mark_blob_uninteresting(struct blob *blob)
62{
63 if (!blob)
64 return;
65 if (blob->object.flags & UNINTERESTING)
66 return;
67 blob->object.flags |= UNINTERESTING;
68}
69
70static void mark_tree_contents_uninteresting(struct repository *r,
71 struct tree *tree)
72{
73 struct tree_desc desc;
74 struct name_entry entry;
75
76 if (parse_tree_gently(tree, 1) < 0)
77 return;
78
79 init_tree_desc(&desc, tree->buffer, tree->size);
80 while (tree_entry(&desc, &entry)) {
81 switch (object_type(entry.mode)) {
82 case OBJ_TREE:
83 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
84 break;
85 case OBJ_BLOB:
86 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
87 break;
88 default:
89 /* Subproject commit - not in this repository */
90 break;
91 }
92 }
93
94 /*
95 * We don't care about the tree any more
96 * after it has been marked uninteresting.
97 */
98 free_tree_buffer(tree);
99}
100
101void mark_tree_uninteresting(struct repository *r, struct tree *tree)
102{
103 struct object *obj;
104
105 if (!tree)
106 return;
107
108 obj = &tree->object;
109 if (obj->flags & UNINTERESTING)
110 return;
111 obj->flags |= UNINTERESTING;
112 mark_tree_contents_uninteresting(r, tree);
113}
114
115struct path_and_oids_entry {
116 struct hashmap_entry ent;
117 char *path;
118 struct oidset trees;
119};
120
121static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
122 const struct hashmap_entry *eptr,
123 const struct hashmap_entry *entry_or_key,
124 const void *keydata)
125{
126 const struct path_and_oids_entry *e1, *e2;
127
128 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
129 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
130
131 return strcmp(e1->path, e2->path);
132}
133
134static void paths_and_oids_clear(struct hashmap *map)
135{
136 struct hashmap_iter iter;
137 struct path_and_oids_entry *entry;
138
139 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
140 oidset_clear(&entry->trees);
141 free(entry->path);
142 }
143
144 hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
145}
146
147static void paths_and_oids_insert(struct hashmap *map,
148 const char *path,
149 const struct object_id *oid)
150{
151 int hash = strhash(path);
152 struct path_and_oids_entry key;
153 struct path_and_oids_entry *entry;
154
155 hashmap_entry_init(&key.ent, hash);
156
157 /* use a shallow copy for the lookup */
158 key.path = (char *)path;
159 oidset_init(&key.trees, 0);
160
161 entry = hashmap_get_entry(map, &key, ent, NULL);
162 if (!entry) {
163 CALLOC_ARRAY(entry, 1);
164 hashmap_entry_init(&entry->ent, hash);
165 entry->path = xstrdup(key.path);
166 oidset_init(&entry->trees, 16);
167 hashmap_put(map, &entry->ent);
168 }
169
170 oidset_insert(&entry->trees, oid);
171}
172
173static void add_children_by_path(struct repository *r,
174 struct tree *tree,
175 struct hashmap *map)
176{
177 struct tree_desc desc;
178 struct name_entry entry;
179
180 if (!tree)
181 return;
182
183 if (parse_tree_gently(tree, 1) < 0)
184 return;
185
186 init_tree_desc(&desc, tree->buffer, tree->size);
187 while (tree_entry(&desc, &entry)) {
188 switch (object_type(entry.mode)) {
189 case OBJ_TREE:
190 paths_and_oids_insert(map, entry.path, &entry.oid);
191
192 if (tree->object.flags & UNINTERESTING) {
193 struct tree *child = lookup_tree(r, &entry.oid);
194 if (child)
195 child->object.flags |= UNINTERESTING;
196 }
197 break;
198 case OBJ_BLOB:
199 if (tree->object.flags & UNINTERESTING) {
200 struct blob *child = lookup_blob(r, &entry.oid);
201 if (child)
202 child->object.flags |= UNINTERESTING;
203 }
204 break;
205 default:
206 /* Subproject commit - not in this repository */
207 break;
208 }
209 }
210
211 free_tree_buffer(tree);
212}
213
214void mark_trees_uninteresting_sparse(struct repository *r,
215 struct oidset *trees)
216{
217 unsigned has_interesting = 0, has_uninteresting = 0;
218 struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
219 struct hashmap_iter map_iter;
220 struct path_and_oids_entry *entry;
221 struct object_id *oid;
222 struct oidset_iter iter;
223
224 oidset_iter_init(trees, &iter);
225 while ((!has_interesting || !has_uninteresting) &&
226 (oid = oidset_iter_next(&iter))) {
227 struct tree *tree = lookup_tree(r, oid);
228
229 if (!tree)
230 continue;
231
232 if (tree->object.flags & UNINTERESTING)
233 has_uninteresting = 1;
234 else
235 has_interesting = 1;
236 }
237
238 /* Do not walk unless we have both types of trees. */
239 if (!has_uninteresting || !has_interesting)
240 return;
241
242 oidset_iter_init(trees, &iter);
243 while ((oid = oidset_iter_next(&iter))) {
244 struct tree *tree = lookup_tree(r, oid);
245 add_children_by_path(r, tree, &map);
246 }
247
248 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
249 mark_trees_uninteresting_sparse(r, &entry->trees);
250
251 paths_and_oids_clear(&map);
252}
253
254struct commit_stack {
255 struct commit **items;
256 size_t nr, alloc;
257};
258#define COMMIT_STACK_INIT { 0 }
259
260static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
261{
262 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
263 stack->items[stack->nr++] = commit;
264}
265
266static struct commit *commit_stack_pop(struct commit_stack *stack)
267{
268 return stack->nr ? stack->items[--stack->nr] : NULL;
269}
270
271static void commit_stack_clear(struct commit_stack *stack)
272{
273 FREE_AND_NULL(stack->items);
274 stack->nr = stack->alloc = 0;
275}
276
277static void mark_one_parent_uninteresting(struct rev_info *revs, struct commit *commit,
278 struct commit_stack *pending)
279{
280 struct commit_list *l;
281
282 if (commit->object.flags & UNINTERESTING)
283 return;
284 commit->object.flags |= UNINTERESTING;
285
286 /*
287 * Normally we haven't parsed the parent
288 * yet, so we won't have a parent of a parent
289 * here. However, it may turn out that we've
290 * reached this commit some other way (where it
291 * wasn't uninteresting), in which case we need
292 * to mark its parents recursively too..
293 */
294 for (l = commit->parents; l; l = l->next) {
295 commit_stack_push(pending, l->item);
296 if (revs && revs->exclude_first_parent_only)
297 break;
298 }
299}
300
301void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit)
302{
303 struct commit_stack pending = COMMIT_STACK_INIT;
304 struct commit_list *l;
305
306 for (l = commit->parents; l; l = l->next) {
307 mark_one_parent_uninteresting(revs, l->item, &pending);
308 if (revs && revs->exclude_first_parent_only)
309 break;
310 }
311
312 while (pending.nr > 0)
313 mark_one_parent_uninteresting(revs, commit_stack_pop(&pending),
314 &pending);
315
316 commit_stack_clear(&pending);
317}
318
319static void add_pending_object_with_path(struct rev_info *revs,
320 struct object *obj,
321 const char *name, unsigned mode,
322 const char *path)
323{
324 struct interpret_branch_name_options options = { 0 };
325 if (!obj)
326 return;
327 if (revs->no_walk && (obj->flags & UNINTERESTING))
328 revs->no_walk = 0;
329 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
330 struct strbuf buf = STRBUF_INIT;
331 size_t namelen = strlen(name);
332 int len = interpret_branch_name(name, namelen, &buf, &options);
333
334 if (0 < len && len < namelen && buf.len)
335 strbuf_addstr(&buf, name + len);
336 add_reflog_for_walk(revs->reflog_info,
337 (struct commit *)obj,
338 buf.buf[0] ? buf.buf: name);
339 strbuf_release(&buf);
340 return; /* do not add the commit itself */
341 }
342 add_object_array_with_path(obj, name, &revs->pending, mode, path);
343}
344
345static void add_pending_object_with_mode(struct rev_info *revs,
346 struct object *obj,
347 const char *name, unsigned mode)
348{
349 add_pending_object_with_path(revs, obj, name, mode, NULL);
350}
351
352void add_pending_object(struct rev_info *revs,
353 struct object *obj, const char *name)
354{
355 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
356}
357
358void add_head_to_pending(struct rev_info *revs)
359{
360 struct object_id oid;
361 struct object *obj;
362 if (get_oid("HEAD", &oid))
363 return;
364 obj = parse_object(revs->repo, &oid);
365 if (!obj)
366 return;
367 add_pending_object(revs, obj, "HEAD");
368}
369
370static struct object *get_reference(struct rev_info *revs, const char *name,
371 const struct object_id *oid,
372 unsigned int flags)
373{
374 struct object *object;
375 struct commit *commit;
376
377 /*
378 * If the repository has commit graphs, we try to opportunistically
379 * look up the object ID in those graphs. Like this, we can avoid
380 * parsing commit data from disk.
381 */
382 commit = lookup_commit_in_graph(revs->repo, oid);
383 if (commit)
384 object = &commit->object;
385 else
386 object = parse_object(revs->repo, oid);
387
388 if (!object) {
389 if (revs->ignore_missing)
390 return object;
391 if (revs->exclude_promisor_objects && is_promisor_object(oid))
392 return NULL;
393 die("bad object %s", name);
394 }
395 object->flags |= flags;
396 return object;
397}
398
399void add_pending_oid(struct rev_info *revs, const char *name,
400 const struct object_id *oid, unsigned int flags)
401{
402 struct object *object = get_reference(revs, name, oid, flags);
403 add_pending_object(revs, object, name);
404}
405
406static struct commit *handle_commit(struct rev_info *revs,
407 struct object_array_entry *entry)
408{
409 struct object *object = entry->item;
410 const char *name = entry->name;
411 const char *path = entry->path;
412 unsigned int mode = entry->mode;
413 unsigned long flags = object->flags;
414
415 /*
416 * Tag object? Look what it points to..
417 */
418 while (object->type == OBJ_TAG) {
419 struct tag *tag = (struct tag *) object;
420 if (revs->tag_objects && !(flags & UNINTERESTING))
421 add_pending_object(revs, object, tag->tag);
422 object = parse_object(revs->repo, get_tagged_oid(tag));
423 if (!object) {
424 if (revs->ignore_missing_links || (flags & UNINTERESTING))
425 return NULL;
426 if (revs->exclude_promisor_objects &&
427 is_promisor_object(&tag->tagged->oid))
428 return NULL;
429 die("bad object %s", oid_to_hex(&tag->tagged->oid));
430 }
431 object->flags |= flags;
432 /*
433 * We'll handle the tagged object by looping or dropping
434 * through to the non-tag handlers below. Do not
435 * propagate path data from the tag's pending entry.
436 */
437 path = NULL;
438 mode = 0;
439 }
440
441 /*
442 * Commit object? Just return it, we'll do all the complex
443 * reachability crud.
444 */
445 if (object->type == OBJ_COMMIT) {
446 struct commit *commit = (struct commit *)object;
447
448 if (repo_parse_commit(revs->repo, commit) < 0)
449 die("unable to parse commit %s", name);
450 if (flags & UNINTERESTING) {
451 mark_parents_uninteresting(revs, commit);
452
453 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
454 revs->limited = 1;
455 }
456 if (revs->sources) {
457 char **slot = revision_sources_at(revs->sources, commit);
458
459 if (!*slot)
460 *slot = xstrdup(name);
461 }
462 return commit;
463 }
464
465 /*
466 * Tree object? Either mark it uninteresting, or add it
467 * to the list of objects to look at later..
468 */
469 if (object->type == OBJ_TREE) {
470 struct tree *tree = (struct tree *)object;
471 if (!revs->tree_objects)
472 return NULL;
473 if (flags & UNINTERESTING) {
474 mark_tree_contents_uninteresting(revs->repo, tree);
475 return NULL;
476 }
477 add_pending_object_with_path(revs, object, name, mode, path);
478 return NULL;
479 }
480
481 /*
482 * Blob object? You know the drill by now..
483 */
484 if (object->type == OBJ_BLOB) {
485 if (!revs->blob_objects)
486 return NULL;
487 if (flags & UNINTERESTING)
488 return NULL;
489 add_pending_object_with_path(revs, object, name, mode, path);
490 return NULL;
491 }
492 die("%s is unknown object", name);
493}
494
495static int everybody_uninteresting(struct commit_list *orig,
496 struct commit **interesting_cache)
497{
498 struct commit_list *list = orig;
499
500 if (*interesting_cache) {
501 struct commit *commit = *interesting_cache;
502 if (!(commit->object.flags & UNINTERESTING))
503 return 0;
504 }
505
506 while (list) {
507 struct commit *commit = list->item;
508 list = list->next;
509 if (commit->object.flags & UNINTERESTING)
510 continue;
511
512 *interesting_cache = commit;
513 return 0;
514 }
515 return 1;
516}
517
518/*
519 * A definition of "relevant" commit that we can use to simplify limited graphs
520 * by eliminating side branches.
521 *
522 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
523 * in our list), or that is a specified BOTTOM commit. Then after computing
524 * a limited list, during processing we can generally ignore boundary merges
525 * coming from outside the graph, (ie from irrelevant parents), and treat
526 * those merges as if they were single-parent. TREESAME is defined to consider
527 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
528 * we don't care if we were !TREESAME to non-graph parents.
529 *
530 * Treating bottom commits as relevant ensures that a limited graph's
531 * connection to the actual bottom commit is not viewed as a side branch, but
532 * treated as part of the graph. For example:
533 *
534 * ....Z...A---X---o---o---B
535 * . /
536 * W---Y
537 *
538 * When computing "A..B", the A-X connection is at least as important as
539 * Y-X, despite A being flagged UNINTERESTING.
540 *
541 * And when computing --ancestry-path "A..B", the A-X connection is more
542 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
543 */
544static inline int relevant_commit(struct commit *commit)
545{
546 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
547}
548
549/*
550 * Return a single relevant commit from a parent list. If we are a TREESAME
551 * commit, and this selects one of our parents, then we can safely simplify to
552 * that parent.
553 */
554static struct commit *one_relevant_parent(const struct rev_info *revs,
555 struct commit_list *orig)
556{
557 struct commit_list *list = orig;
558 struct commit *relevant = NULL;
559
560 if (!orig)
561 return NULL;
562
563 /*
564 * For 1-parent commits, or if first-parent-only, then return that
565 * first parent (even if not "relevant" by the above definition).
566 * TREESAME will have been set purely on that parent.
567 */
568 if (revs->first_parent_only || !orig->next)
569 return orig->item;
570
571 /*
572 * For multi-parent commits, identify a sole relevant parent, if any.
573 * If we have only one relevant parent, then TREESAME will be set purely
574 * with regard to that parent, and we can simplify accordingly.
575 *
576 * If we have more than one relevant parent, or no relevant parents
577 * (and multiple irrelevant ones), then we can't select a parent here
578 * and return NULL.
579 */
580 while (list) {
581 struct commit *commit = list->item;
582 list = list->next;
583 if (relevant_commit(commit)) {
584 if (relevant)
585 return NULL;
586 relevant = commit;
587 }
588 }
589 return relevant;
590}
591
592/*
593 * The goal is to get REV_TREE_NEW as the result only if the
594 * diff consists of all '+' (and no other changes), REV_TREE_OLD
595 * if the whole diff is removal of old data, and otherwise
596 * REV_TREE_DIFFERENT (of course if the trees are the same we
597 * want REV_TREE_SAME).
598 *
599 * The only time we care about the distinction is when
600 * remove_empty_trees is in effect, in which case we care only about
601 * whether the whole change is REV_TREE_NEW, or if there's another type
602 * of change. Which means we can stop the diff early in either of these
603 * cases:
604 *
605 * 1. We're not using remove_empty_trees at all.
606 *
607 * 2. We saw anything except REV_TREE_NEW.
608 */
609#define REV_TREE_SAME 0
610#define REV_TREE_NEW 1 /* Only new files */
611#define REV_TREE_OLD 2 /* Only files removed */
612#define REV_TREE_DIFFERENT 3 /* Mixed changes */
613static int tree_difference = REV_TREE_SAME;
614
615static void file_add_remove(struct diff_options *options,
616 int addremove, unsigned mode,
617 const struct object_id *oid,
618 int oid_valid,
619 const char *fullpath, unsigned dirty_submodule)
620{
621 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
622 struct rev_info *revs = options->change_fn_data;
623
624 tree_difference |= diff;
625 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
626 options->flags.has_changes = 1;
627}
628
629static void file_change(struct diff_options *options,
630 unsigned old_mode, unsigned new_mode,
631 const struct object_id *old_oid,
632 const struct object_id *new_oid,
633 int old_oid_valid, int new_oid_valid,
634 const char *fullpath,
635 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
636{
637 tree_difference = REV_TREE_DIFFERENT;
638 options->flags.has_changes = 1;
639}
640
641static int bloom_filter_atexit_registered;
642static unsigned int count_bloom_filter_maybe;
643static unsigned int count_bloom_filter_definitely_not;
644static unsigned int count_bloom_filter_false_positive;
645static unsigned int count_bloom_filter_not_present;
646
647static void trace2_bloom_filter_statistics_atexit(void)
648{
649 struct json_writer jw = JSON_WRITER_INIT;
650
651 jw_object_begin(&jw, 0);
652 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
653 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
654 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
655 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
656 jw_end(&jw);
657
658 trace2_data_json("bloom", the_repository, "statistics", &jw);
659
660 jw_release(&jw);
661}
662
663static int forbid_bloom_filters(struct pathspec *spec)
664{
665 if (spec->has_wildcard)
666 return 1;
667 if (spec->nr > 1)
668 return 1;
669 if (spec->magic & ~PATHSPEC_LITERAL)
670 return 1;
671 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
672 return 1;
673
674 return 0;
675}
676
677static void prepare_to_use_bloom_filter(struct rev_info *revs)
678{
679 struct pathspec_item *pi;
680 char *path_alloc = NULL;
681 const char *path, *p;
682 size_t len;
683 int path_component_nr = 1;
684
685 if (!revs->commits)
686 return;
687
688 if (forbid_bloom_filters(&revs->prune_data))
689 return;
690
691 repo_parse_commit(revs->repo, revs->commits->item);
692
693 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
694 if (!revs->bloom_filter_settings)
695 return;
696
697 if (!revs->pruning.pathspec.nr)
698 return;
699
700 pi = &revs->pruning.pathspec.items[0];
701
702 /* remove single trailing slash from path, if needed */
703 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
704 path_alloc = xmemdupz(pi->match, pi->len - 1);
705 path = path_alloc;
706 } else
707 path = pi->match;
708
709 len = strlen(path);
710 if (!len) {
711 revs->bloom_filter_settings = NULL;
712 free(path_alloc);
713 return;
714 }
715
716 p = path;
717 while (*p) {
718 /*
719 * At this point, the path is normalized to use Unix-style
720 * path separators. This is required due to how the
721 * changed-path Bloom filters store the paths.
722 */
723 if (*p == '/')
724 path_component_nr++;
725 p++;
726 }
727
728 revs->bloom_keys_nr = path_component_nr;
729 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
730
731 fill_bloom_key(path, len, &revs->bloom_keys[0],
732 revs->bloom_filter_settings);
733 path_component_nr = 1;
734
735 p = path + len - 1;
736 while (p > path) {
737 if (*p == '/')
738 fill_bloom_key(path, p - path,
739 &revs->bloom_keys[path_component_nr++],
740 revs->bloom_filter_settings);
741 p--;
742 }
743
744 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
745 atexit(trace2_bloom_filter_statistics_atexit);
746 bloom_filter_atexit_registered = 1;
747 }
748
749 free(path_alloc);
750}
751
752static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
753 struct commit *commit)
754{
755 struct bloom_filter *filter;
756 int result = 1, j;
757
758 if (!revs->repo->objects->commit_graph)
759 return -1;
760
761 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
762 return -1;
763
764 filter = get_bloom_filter(revs->repo, commit);
765
766 if (!filter) {
767 count_bloom_filter_not_present++;
768 return -1;
769 }
770
771 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
772 result = bloom_filter_contains(filter,
773 &revs->bloom_keys[j],
774 revs->bloom_filter_settings);
775 }
776
777 if (result)
778 count_bloom_filter_maybe++;
779 else
780 count_bloom_filter_definitely_not++;
781
782 return result;
783}
784
785static int rev_compare_tree(struct rev_info *revs,
786 struct commit *parent, struct commit *commit, int nth_parent)
787{
788 struct tree *t1 = get_commit_tree(parent);
789 struct tree *t2 = get_commit_tree(commit);
790 int bloom_ret = 1;
791
792 if (!t1)
793 return REV_TREE_NEW;
794 if (!t2)
795 return REV_TREE_OLD;
796
797 if (revs->simplify_by_decoration) {
798 /*
799 * If we are simplifying by decoration, then the commit
800 * is worth showing if it has a tag pointing at it.
801 */
802 if (get_name_decoration(&commit->object))
803 return REV_TREE_DIFFERENT;
804 /*
805 * A commit that is not pointed by a tag is uninteresting
806 * if we are not limited by path. This means that you will
807 * see the usual "commits that touch the paths" plus any
808 * tagged commit by specifying both --simplify-by-decoration
809 * and pathspec.
810 */
811 if (!revs->prune_data.nr)
812 return REV_TREE_SAME;
813 }
814
815 if (revs->bloom_keys_nr && !nth_parent) {
816 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
817
818 if (bloom_ret == 0)
819 return REV_TREE_SAME;
820 }
821
822 tree_difference = REV_TREE_SAME;
823 revs->pruning.flags.has_changes = 0;
824 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
825
826 if (!nth_parent)
827 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
828 count_bloom_filter_false_positive++;
829
830 return tree_difference;
831}
832
833static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
834{
835 struct tree *t1 = get_commit_tree(commit);
836
837 if (!t1)
838 return 0;
839
840 tree_difference = REV_TREE_SAME;
841 revs->pruning.flags.has_changes = 0;
842 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
843
844 return tree_difference == REV_TREE_SAME;
845}
846
847struct treesame_state {
848 unsigned int nparents;
849 unsigned char treesame[FLEX_ARRAY];
850};
851
852static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
853{
854 unsigned n = commit_list_count(commit->parents);
855 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
856 st->nparents = n;
857 add_decoration(&revs->treesame, &commit->object, st);
858 return st;
859}
860
861/*
862 * Must be called immediately after removing the nth_parent from a commit's
863 * parent list, if we are maintaining the per-parent treesame[] decoration.
864 * This does not recalculate the master TREESAME flag - update_treesame()
865 * should be called to update it after a sequence of treesame[] modifications
866 * that may have affected it.
867 */
868static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
869{
870 struct treesame_state *st;
871 int old_same;
872
873 if (!commit->parents) {
874 /*
875 * Have just removed the only parent from a non-merge.
876 * Different handling, as we lack decoration.
877 */
878 if (nth_parent != 0)
879 die("compact_treesame %u", nth_parent);
880 old_same = !!(commit->object.flags & TREESAME);
881 if (rev_same_tree_as_empty(revs, commit))
882 commit->object.flags |= TREESAME;
883 else
884 commit->object.flags &= ~TREESAME;
885 return old_same;
886 }
887
888 st = lookup_decoration(&revs->treesame, &commit->object);
889 if (!st || nth_parent >= st->nparents)
890 die("compact_treesame %u", nth_parent);
891
892 old_same = st->treesame[nth_parent];
893 memmove(st->treesame + nth_parent,
894 st->treesame + nth_parent + 1,
895 st->nparents - nth_parent - 1);
896
897 /*
898 * If we've just become a non-merge commit, update TREESAME
899 * immediately, and remove the no-longer-needed decoration.
900 * If still a merge, defer update until update_treesame().
901 */
902 if (--st->nparents == 1) {
903 if (commit->parents->next)
904 die("compact_treesame parents mismatch");
905 if (st->treesame[0] && revs->dense)
906 commit->object.flags |= TREESAME;
907 else
908 commit->object.flags &= ~TREESAME;
909 free(add_decoration(&revs->treesame, &commit->object, NULL));
910 }
911
912 return old_same;
913}
914
915static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
916{
917 if (commit->parents && commit->parents->next) {
918 unsigned n;
919 struct treesame_state *st;
920 struct commit_list *p;
921 unsigned relevant_parents;
922 unsigned relevant_change, irrelevant_change;
923
924 st = lookup_decoration(&revs->treesame, &commit->object);
925 if (!st)
926 die("update_treesame %s", oid_to_hex(&commit->object.oid));
927 relevant_parents = 0;
928 relevant_change = irrelevant_change = 0;
929 for (p = commit->parents, n = 0; p; n++, p = p->next) {
930 if (relevant_commit(p->item)) {
931 relevant_change |= !st->treesame[n];
932 relevant_parents++;
933 } else
934 irrelevant_change |= !st->treesame[n];
935 }
936 if (relevant_parents ? relevant_change : irrelevant_change)
937 commit->object.flags &= ~TREESAME;
938 else
939 commit->object.flags |= TREESAME;
940 }
941
942 return commit->object.flags & TREESAME;
943}
944
945static inline int limiting_can_increase_treesame(const struct rev_info *revs)
946{
947 /*
948 * TREESAME is irrelevant unless prune && dense;
949 * if simplify_history is set, we can't have a mixture of TREESAME and
950 * !TREESAME INTERESTING parents (and we don't have treesame[]
951 * decoration anyway);
952 * if first_parent_only is set, then the TREESAME flag is locked
953 * against the first parent (and again we lack treesame[] decoration).
954 */
955 return revs->prune && revs->dense &&
956 !revs->simplify_history &&
957 !revs->first_parent_only;
958}
959
960static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
961{
962 struct commit_list **pp, *parent;
963 struct treesame_state *ts = NULL;
964 int relevant_change = 0, irrelevant_change = 0;
965 int relevant_parents, nth_parent;
966
967 /*
968 * If we don't do pruning, everything is interesting
969 */
970 if (!revs->prune)
971 return;
972
973 if (!get_commit_tree(commit))
974 return;
975
976 if (!commit->parents) {
977 if (rev_same_tree_as_empty(revs, commit))
978 commit->object.flags |= TREESAME;
979 return;
980 }
981
982 /*
983 * Normal non-merge commit? If we don't want to make the
984 * history dense, we consider it always to be a change..
985 */
986 if (!revs->dense && !commit->parents->next)
987 return;
988
989 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
990 (parent = *pp) != NULL;
991 pp = &parent->next, nth_parent++) {
992 struct commit *p = parent->item;
993 if (relevant_commit(p))
994 relevant_parents++;
995
996 if (nth_parent == 1) {
997 /*
998 * This our second loop iteration - so we now know
999 * we're dealing with a merge.
1000 *
1001 * Do not compare with later parents when we care only about
1002 * the first parent chain, in order to avoid derailing the
1003 * traversal to follow a side branch that brought everything
1004 * in the path we are limited to by the pathspec.
1005 */
1006 if (revs->first_parent_only)
1007 break;
1008 /*
1009 * If this will remain a potentially-simplifiable
1010 * merge, remember per-parent treesame if needed.
1011 * Initialise the array with the comparison from our
1012 * first iteration.
1013 */
1014 if (revs->treesame.name &&
1015 !revs->simplify_history &&
1016 !(commit->object.flags & UNINTERESTING)) {
1017 ts = initialise_treesame(revs, commit);
1018 if (!(irrelevant_change || relevant_change))
1019 ts->treesame[0] = 1;
1020 }
1021 }
1022 if (repo_parse_commit(revs->repo, p) < 0)
1023 die("cannot simplify commit %s (because of %s)",
1024 oid_to_hex(&commit->object.oid),
1025 oid_to_hex(&p->object.oid));
1026 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1027 case REV_TREE_SAME:
1028 if (!revs->simplify_history || !relevant_commit(p)) {
1029 /* Even if a merge with an uninteresting
1030 * side branch brought the entire change
1031 * we are interested in, we do not want
1032 * to lose the other branches of this
1033 * merge, so we just keep going.
1034 */
1035 if (ts)
1036 ts->treesame[nth_parent] = 1;
1037 continue;
1038 }
1039 parent->next = NULL;
1040 commit->parents = parent;
1041
1042 /*
1043 * A merge commit is a "diversion" if it is not
1044 * TREESAME to its first parent but is TREESAME
1045 * to a later parent. In the simplified history,
1046 * we "divert" the history walk to the later
1047 * parent. These commits are shown when "show_pulls"
1048 * is enabled, so do not mark the object as
1049 * TREESAME here.
1050 */
1051 if (!revs->show_pulls || !nth_parent)
1052 commit->object.flags |= TREESAME;
1053
1054 return;
1055
1056 case REV_TREE_NEW:
1057 if (revs->remove_empty_trees &&
1058 rev_same_tree_as_empty(revs, p)) {
1059 /* We are adding all the specified
1060 * paths from this parent, so the
1061 * history beyond this parent is not
1062 * interesting. Remove its parents
1063 * (they are grandparents for us).
1064 * IOW, we pretend this parent is a
1065 * "root" commit.
1066 */
1067 if (repo_parse_commit(revs->repo, p) < 0)
1068 die("cannot simplify commit %s (invalid %s)",
1069 oid_to_hex(&commit->object.oid),
1070 oid_to_hex(&p->object.oid));
1071 p->parents = NULL;
1072 }
1073 /* fallthrough */
1074 case REV_TREE_OLD:
1075 case REV_TREE_DIFFERENT:
1076 if (relevant_commit(p))
1077 relevant_change = 1;
1078 else
1079 irrelevant_change = 1;
1080
1081 if (!nth_parent)
1082 commit->object.flags |= PULL_MERGE;
1083
1084 continue;
1085 }
1086 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1087 }
1088
1089 /*
1090 * TREESAME is straightforward for single-parent commits. For merge
1091 * commits, it is most useful to define it so that "irrelevant"
1092 * parents cannot make us !TREESAME - if we have any relevant
1093 * parents, then we only consider TREESAMEness with respect to them,
1094 * allowing irrelevant merges from uninteresting branches to be
1095 * simplified away. Only if we have only irrelevant parents do we
1096 * base TREESAME on them. Note that this logic is replicated in
1097 * update_treesame, which should be kept in sync.
1098 */
1099 if (relevant_parents ? !relevant_change : !irrelevant_change)
1100 commit->object.flags |= TREESAME;
1101}
1102
1103static int process_parents(struct rev_info *revs, struct commit *commit,
1104 struct commit_list **list, struct prio_queue *queue)
1105{
1106 struct commit_list *parent = commit->parents;
1107 unsigned left_flag;
1108
1109 if (commit->object.flags & ADDED)
1110 return 0;
1111 commit->object.flags |= ADDED;
1112
1113 if (revs->include_check &&
1114 !revs->include_check(commit, revs->include_check_data))
1115 return 0;
1116
1117 /*
1118 * If the commit is uninteresting, don't try to
1119 * prune parents - we want the maximal uninteresting
1120 * set.
1121 *
1122 * Normally we haven't parsed the parent
1123 * yet, so we won't have a parent of a parent
1124 * here. However, it may turn out that we've
1125 * reached this commit some other way (where it
1126 * wasn't uninteresting), in which case we need
1127 * to mark its parents recursively too..
1128 */
1129 if (commit->object.flags & UNINTERESTING) {
1130 while (parent) {
1131 struct commit *p = parent->item;
1132 parent = parent->next;
1133 if (p)
1134 p->object.flags |= UNINTERESTING;
1135 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1136 continue;
1137 if (p->parents)
1138 mark_parents_uninteresting(revs, p);
1139 if (p->object.flags & SEEN)
1140 continue;
1141 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1142 if (list)
1143 commit_list_insert_by_date(p, list);
1144 if (queue)
1145 prio_queue_put(queue, p);
1146 if (revs->exclude_first_parent_only)
1147 break;
1148 }
1149 return 0;
1150 }
1151
1152 /*
1153 * Ok, the commit wasn't uninteresting. Try to
1154 * simplify the commit history and find the parent
1155 * that has no differences in the path set if one exists.
1156 */
1157 try_to_simplify_commit(revs, commit);
1158
1159 if (revs->no_walk)
1160 return 0;
1161
1162 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
1163
1164 for (parent = commit->parents; parent; parent = parent->next) {
1165 struct commit *p = parent->item;
1166 int gently = revs->ignore_missing_links ||
1167 revs->exclude_promisor_objects;
1168 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1169 if (revs->exclude_promisor_objects &&
1170 is_promisor_object(&p->object.oid)) {
1171 if (revs->first_parent_only)
1172 break;
1173 continue;
1174 }
1175 return -1;
1176 }
1177 if (revs->sources) {
1178 char **slot = revision_sources_at(revs->sources, p);
1179
1180 if (!*slot)
1181 *slot = *revision_sources_at(revs->sources, commit);
1182 }
1183 p->object.flags |= left_flag;
1184 if (!(p->object.flags & SEEN)) {
1185 p->object.flags |= (SEEN | NOT_USER_GIVEN);
1186 if (list)
1187 commit_list_insert_by_date(p, list);
1188 if (queue)
1189 prio_queue_put(queue, p);
1190 }
1191 if (revs->first_parent_only)
1192 break;
1193 }
1194 return 0;
1195}
1196
1197static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1198{
1199 struct commit_list *p;
1200 int left_count = 0, right_count = 0;
1201 int left_first;
1202 struct patch_ids ids;
1203 unsigned cherry_flag;
1204
1205 /* First count the commits on the left and on the right */
1206 for (p = list; p; p = p->next) {
1207 struct commit *commit = p->item;
1208 unsigned flags = commit->object.flags;
1209 if (flags & BOUNDARY)
1210 ;
1211 else if (flags & SYMMETRIC_LEFT)
1212 left_count++;
1213 else
1214 right_count++;
1215 }
1216
1217 if (!left_count || !right_count)
1218 return;
1219
1220 left_first = left_count < right_count;
1221 init_patch_ids(revs->repo, &ids);
1222 ids.diffopts.pathspec = revs->diffopt.pathspec;
1223
1224 /* Compute patch-ids for one side */
1225 for (p = list; p; p = p->next) {
1226 struct commit *commit = p->item;
1227 unsigned flags = commit->object.flags;
1228
1229 if (flags & BOUNDARY)
1230 continue;
1231 /*
1232 * If we have fewer left, left_first is set and we omit
1233 * commits on the right branch in this loop. If we have
1234 * fewer right, we skip the left ones.
1235 */
1236 if (left_first != !!(flags & SYMMETRIC_LEFT))
1237 continue;
1238 add_commit_patch_id(commit, &ids);
1239 }
1240
1241 /* either cherry_mark or cherry_pick are true */
1242 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1243
1244 /* Check the other side */
1245 for (p = list; p; p = p->next) {
1246 struct commit *commit = p->item;
1247 struct patch_id *id;
1248 unsigned flags = commit->object.flags;
1249
1250 if (flags & BOUNDARY)
1251 continue;
1252 /*
1253 * If we have fewer left, left_first is set and we omit
1254 * commits on the left branch in this loop.
1255 */
1256 if (left_first == !!(flags & SYMMETRIC_LEFT))
1257 continue;
1258
1259 /*
1260 * Have we seen the same patch id?
1261 */
1262 id = patch_id_iter_first(commit, &ids);
1263 if (!id)
1264 continue;
1265
1266 commit->object.flags |= cherry_flag;
1267 do {
1268 id->commit->object.flags |= cherry_flag;
1269 } while ((id = patch_id_iter_next(id, &ids)));
1270 }
1271
1272 free_patch_ids(&ids);
1273}
1274
1275/* How many extra uninteresting commits we want to see.. */
1276#define SLOP 5
1277
1278static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1279 struct commit **interesting_cache)
1280{
1281 /*
1282 * No source list at all? We're definitely done..
1283 */
1284 if (!src)
1285 return 0;
1286
1287 /*
1288 * Does the destination list contain entries with a date
1289 * before the source list? Definitely _not_ done.
1290 */
1291 if (date <= src->item->date)
1292 return SLOP;
1293
1294 /*
1295 * Does the source list still have interesting commits in
1296 * it? Definitely not done..
1297 */
1298 if (!everybody_uninteresting(src, interesting_cache))
1299 return SLOP;
1300
1301 /* Ok, we're closing in.. */
1302 return slop-1;
1303}
1304
1305/*
1306 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1307 * of B but not ancestors of A but further limits the result to those
1308 * that are descendants of A. This takes the list of bottom commits and
1309 * the result of "A..B" without --ancestry-path, and limits the latter
1310 * further to the ones that can reach one of the commits in "bottom".
1311 */
1312static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
1313{
1314 struct commit_list *p;
1315 struct commit_list *rlist = NULL;
1316 int made_progress;
1317
1318 /*
1319 * Reverse the list so that it will be likely that we would
1320 * process parents before children.
1321 */
1322 for (p = list; p; p = p->next)
1323 commit_list_insert(p->item, &rlist);
1324
1325 for (p = bottom; p; p = p->next)
1326 p->item->object.flags |= TMP_MARK;
1327
1328 /*
1329 * Mark the ones that can reach bottom commits in "list",
1330 * in a bottom-up fashion.
1331 */
1332 do {
1333 made_progress = 0;
1334 for (p = rlist; p; p = p->next) {
1335 struct commit *c = p->item;
1336 struct commit_list *parents;
1337 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1338 continue;
1339 for (parents = c->parents;
1340 parents;
1341 parents = parents->next) {
1342 if (!(parents->item->object.flags & TMP_MARK))
1343 continue;
1344 c->object.flags |= TMP_MARK;
1345 made_progress = 1;
1346 break;
1347 }
1348 }
1349 } while (made_progress);
1350
1351 /*
1352 * NEEDSWORK: decide if we want to remove parents that are
1353 * not marked with TMP_MARK from commit->parents for commits
1354 * in the resulting list. We may not want to do that, though.
1355 */
1356
1357 /*
1358 * The ones that are not marked with TMP_MARK are uninteresting
1359 */
1360 for (p = list; p; p = p->next) {
1361 struct commit *c = p->item;
1362 if (c->object.flags & TMP_MARK)
1363 continue;
1364 c->object.flags |= UNINTERESTING;
1365 }
1366
1367 /* We are done with the TMP_MARK */
1368 for (p = list; p; p = p->next)
1369 p->item->object.flags &= ~TMP_MARK;
1370 for (p = bottom; p; p = p->next)
1371 p->item->object.flags &= ~TMP_MARK;
1372 free_commit_list(rlist);
1373}
1374
1375/*
1376 * Before walking the history, keep the set of "negative" refs the
1377 * caller has asked to exclude.
1378 *
1379 * This is used to compute "rev-list --ancestry-path A..B", as we need
1380 * to filter the result of "A..B" further to the ones that can actually
1381 * reach A.
1382 */
1383static struct commit_list *collect_bottom_commits(struct commit_list *list)
1384{
1385 struct commit_list *elem, *bottom = NULL;
1386 for (elem = list; elem; elem = elem->next)
1387 if (elem->item->object.flags & BOTTOM)
1388 commit_list_insert(elem->item, &bottom);
1389 return bottom;
1390}
1391
1392/* Assumes either left_only or right_only is set */
1393static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1394{
1395 struct commit_list *p;
1396
1397 for (p = list; p; p = p->next) {
1398 struct commit *commit = p->item;
1399
1400 if (revs->right_only) {
1401 if (commit->object.flags & SYMMETRIC_LEFT)
1402 commit->object.flags |= SHOWN;
1403 } else /* revs->left_only is set */
1404 if (!(commit->object.flags & SYMMETRIC_LEFT))
1405 commit->object.flags |= SHOWN;
1406 }
1407}
1408
1409static int limit_list(struct rev_info *revs)
1410{
1411 int slop = SLOP;
1412 timestamp_t date = TIME_MAX;
1413 struct commit_list *original_list = revs->commits;
1414 struct commit_list *newlist = NULL;
1415 struct commit_list **p = &newlist;
1416 struct commit_list *bottom = NULL;
1417 struct commit *interesting_cache = NULL;
1418
1419 if (revs->ancestry_path) {
1420 bottom = collect_bottom_commits(original_list);
1421 if (!bottom)
1422 die("--ancestry-path given but there are no bottom commits");
1423 }
1424
1425 while (original_list) {
1426 struct commit *commit = pop_commit(&original_list);
1427 struct object *obj = &commit->object;
1428 show_early_output_fn_t show;
1429
1430 if (commit == interesting_cache)
1431 interesting_cache = NULL;
1432
1433 if (revs->max_age != -1 && (commit->date < revs->max_age))
1434 obj->flags |= UNINTERESTING;
1435 if (process_parents(revs, commit, &original_list, NULL) < 0)
1436 return -1;
1437 if (obj->flags & UNINTERESTING) {
1438 mark_parents_uninteresting(revs, commit);
1439 slop = still_interesting(original_list, date, slop, &interesting_cache);
1440 if (slop)
1441 continue;
1442 break;
1443 }
1444 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1445 !revs->line_level_traverse)
1446 continue;
1447 date = commit->date;
1448 p = &commit_list_insert(commit, p)->next;
1449
1450 show = show_early_output;
1451 if (!show)
1452 continue;
1453
1454 show(revs, newlist);
1455 show_early_output = NULL;
1456 }
1457 if (revs->cherry_pick || revs->cherry_mark)
1458 cherry_pick_list(newlist, revs);
1459
1460 if (revs->left_only || revs->right_only)
1461 limit_left_right(newlist, revs);
1462
1463 if (bottom)
1464 limit_to_ancestry(bottom, newlist);
1465 free_commit_list(bottom);
1466
1467 /*
1468 * Check if any commits have become TREESAME by some of their parents
1469 * becoming UNINTERESTING.
1470 */
1471 if (limiting_can_increase_treesame(revs)) {
1472 struct commit_list *list = NULL;
1473 for (list = newlist; list; list = list->next) {
1474 struct commit *c = list->item;
1475 if (c->object.flags & (UNINTERESTING | TREESAME))
1476 continue;
1477 update_treesame(revs, c);
1478 }
1479 }
1480
1481 free_commit_list(original_list);
1482 revs->commits = newlist;
1483 return 0;
1484}
1485
1486/*
1487 * Add an entry to refs->cmdline with the specified information.
1488 * *name is copied.
1489 */
1490static void add_rev_cmdline(struct rev_info *revs,
1491 struct object *item,
1492 const char *name,
1493 int whence,
1494 unsigned flags)
1495{
1496 struct rev_cmdline_info *info = &revs->cmdline;
1497 unsigned int nr = info->nr;
1498
1499 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1500 info->rev[nr].item = item;
1501 info->rev[nr].name = xstrdup(name);
1502 info->rev[nr].whence = whence;
1503 info->rev[nr].flags = flags;
1504 info->nr++;
1505}
1506
1507static void add_rev_cmdline_list(struct rev_info *revs,
1508 struct commit_list *commit_list,
1509 int whence,
1510 unsigned flags)
1511{
1512 while (commit_list) {
1513 struct object *object = &commit_list->item->object;
1514 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1515 whence, flags);
1516 commit_list = commit_list->next;
1517 }
1518}
1519
1520struct all_refs_cb {
1521 int all_flags;
1522 int warned_bad_reflog;
1523 struct rev_info *all_revs;
1524 const char *name_for_errormsg;
1525 struct worktree *wt;
1526};
1527
1528int ref_excluded(struct string_list *ref_excludes, const char *path)
1529{
1530 struct string_list_item *item;
1531
1532 if (!ref_excludes)
1533 return 0;
1534 for_each_string_list_item(item, ref_excludes) {
1535 if (!wildmatch(item->string, path, 0))
1536 return 1;
1537 }
1538 return 0;
1539}
1540
1541static int handle_one_ref(const char *path, const struct object_id *oid,
1542 int flag, void *cb_data)
1543{
1544 struct all_refs_cb *cb = cb_data;
1545 struct object *object;
1546
1547 if (ref_excluded(cb->all_revs->ref_excludes, path))
1548 return 0;
1549
1550 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1551 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1552 add_pending_object(cb->all_revs, object, path);
1553 return 0;
1554}
1555
1556static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1557 unsigned flags)
1558{
1559 cb->all_revs = revs;
1560 cb->all_flags = flags;
1561 revs->rev_input_given = 1;
1562 cb->wt = NULL;
1563}
1564
1565void clear_ref_exclusion(struct string_list **ref_excludes_p)
1566{
1567 if (*ref_excludes_p) {
1568 string_list_clear(*ref_excludes_p, 0);
1569 free(*ref_excludes_p);
1570 }
1571 *ref_excludes_p = NULL;
1572}
1573
1574void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1575{
1576 if (!*ref_excludes_p) {
1577 CALLOC_ARRAY(*ref_excludes_p, 1);
1578 (*ref_excludes_p)->strdup_strings = 1;
1579 }
1580 string_list_append(*ref_excludes_p, exclude);
1581}
1582
1583static void handle_refs(struct ref_store *refs,
1584 struct rev_info *revs, unsigned flags,
1585 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1586{
1587 struct all_refs_cb cb;
1588
1589 if (!refs) {
1590 /* this could happen with uninitialized submodules */
1591 return;
1592 }
1593
1594 init_all_refs_cb(&cb, revs, flags);
1595 for_each(refs, handle_one_ref, &cb);
1596}
1597
1598static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1599{
1600 struct all_refs_cb *cb = cb_data;
1601 if (!is_null_oid(oid)) {
1602 struct object *o = parse_object(cb->all_revs->repo, oid);
1603 if (o) {
1604 o->flags |= cb->all_flags;
1605 /* ??? CMDLINEFLAGS ??? */
1606 add_pending_object(cb->all_revs, o, "");
1607 }
1608 else if (!cb->warned_bad_reflog) {
1609 warning("reflog of '%s' references pruned commits",
1610 cb->name_for_errormsg);
1611 cb->warned_bad_reflog = 1;
1612 }
1613 }
1614}
1615
1616static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1617 const char *email, timestamp_t timestamp, int tz,
1618 const char *message, void *cb_data)
1619{
1620 handle_one_reflog_commit(ooid, cb_data);
1621 handle_one_reflog_commit(noid, cb_data);
1622 return 0;
1623}
1624
1625static int handle_one_reflog(const char *refname_in_wt,
1626 const struct object_id *oid,
1627 int flag, void *cb_data)
1628{
1629 struct all_refs_cb *cb = cb_data;
1630 struct strbuf refname = STRBUF_INIT;
1631
1632 cb->warned_bad_reflog = 0;
1633 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1634 cb->name_for_errormsg = refname.buf;
1635 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1636 refname.buf,
1637 handle_one_reflog_ent, cb_data);
1638 strbuf_release(&refname);
1639 return 0;
1640}
1641
1642static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1643{
1644 struct worktree **worktrees, **p;
1645
1646 worktrees = get_worktrees();
1647 for (p = worktrees; *p; p++) {
1648 struct worktree *wt = *p;
1649
1650 if (wt->is_current)
1651 continue;
1652
1653 cb->wt = wt;
1654 refs_for_each_reflog(get_worktree_ref_store(wt),
1655 handle_one_reflog,
1656 cb);
1657 }
1658 free_worktrees(worktrees);
1659}
1660
1661void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1662{
1663 struct all_refs_cb cb;
1664
1665 cb.all_revs = revs;
1666 cb.all_flags = flags;
1667 cb.wt = NULL;
1668 for_each_reflog(handle_one_reflog, &cb);
1669
1670 if (!revs->single_worktree)
1671 add_other_reflogs_to_pending(&cb);
1672}
1673
1674static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1675 struct strbuf *path, unsigned int flags)
1676{
1677 size_t baselen = path->len;
1678 int i;
1679
1680 if (it->entry_count >= 0) {
1681 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1682 tree->object.flags |= flags;
1683 add_pending_object_with_path(revs, &tree->object, "",
1684 040000, path->buf);
1685 }
1686
1687 for (i = 0; i < it->subtree_nr; i++) {
1688 struct cache_tree_sub *sub = it->down[i];
1689 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1690 add_cache_tree(sub->cache_tree, revs, path, flags);
1691 strbuf_setlen(path, baselen);
1692 }
1693
1694}
1695
1696static void do_add_index_objects_to_pending(struct rev_info *revs,
1697 struct index_state *istate,
1698 unsigned int flags)
1699{
1700 int i;
1701
1702 /* TODO: audit for interaction with sparse-index. */
1703 ensure_full_index(istate);
1704 for (i = 0; i < istate->cache_nr; i++) {
1705 struct cache_entry *ce = istate->cache[i];
1706 struct blob *blob;
1707
1708 if (S_ISGITLINK(ce->ce_mode))
1709 continue;
1710
1711 blob = lookup_blob(revs->repo, &ce->oid);
1712 if (!blob)
1713 die("unable to add index blob to traversal");
1714 blob->object.flags |= flags;
1715 add_pending_object_with_path(revs, &blob->object, "",
1716 ce->ce_mode, ce->name);
1717 }
1718
1719 if (istate->cache_tree) {
1720 struct strbuf path = STRBUF_INIT;
1721 add_cache_tree(istate->cache_tree, revs, &path, flags);
1722 strbuf_release(&path);
1723 }
1724}
1725
1726void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1727{
1728 struct worktree **worktrees, **p;
1729
1730 repo_read_index(revs->repo);
1731 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1732
1733 if (revs->single_worktree)
1734 return;
1735
1736 worktrees = get_worktrees();
1737 for (p = worktrees; *p; p++) {
1738 struct worktree *wt = *p;
1739 struct index_state istate = { NULL };
1740
1741 if (wt->is_current)
1742 continue; /* current index already taken care of */
1743
1744 if (read_index_from(&istate,
1745 worktree_git_path(wt, "index"),
1746 get_worktree_git_dir(wt)) > 0)
1747 do_add_index_objects_to_pending(revs, &istate, flags);
1748 discard_index(&istate);
1749 }
1750 free_worktrees(worktrees);
1751}
1752
1753struct add_alternate_refs_data {
1754 struct rev_info *revs;
1755 unsigned int flags;
1756};
1757
1758static void add_one_alternate_ref(const struct object_id *oid,
1759 void *vdata)
1760{
1761 const char *name = ".alternate";
1762 struct add_alternate_refs_data *data = vdata;
1763 struct object *obj;
1764
1765 obj = get_reference(data->revs, name, oid, data->flags);
1766 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1767 add_pending_object(data->revs, obj, name);
1768}
1769
1770static void add_alternate_refs_to_pending(struct rev_info *revs,
1771 unsigned int flags)
1772{
1773 struct add_alternate_refs_data data;
1774 data.revs = revs;
1775 data.flags = flags;
1776 for_each_alternate_ref(add_one_alternate_ref, &data);
1777}
1778
1779static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1780 int exclude_parent)
1781{
1782 struct object_id oid;
1783 struct object *it;
1784 struct commit *commit;
1785 struct commit_list *parents;
1786 int parent_number;
1787 const char *arg = arg_;
1788
1789 if (*arg == '^') {
1790 flags ^= UNINTERESTING | BOTTOM;
1791 arg++;
1792 }
1793 if (get_oid_committish(arg, &oid))
1794 return 0;
1795 while (1) {
1796 it = get_reference(revs, arg, &oid, 0);
1797 if (!it && revs->ignore_missing)
1798 return 0;
1799 if (it->type != OBJ_TAG)
1800 break;
1801 if (!((struct tag*)it)->tagged)
1802 return 0;
1803 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1804 }
1805 if (it->type != OBJ_COMMIT)
1806 return 0;
1807 commit = (struct commit *)it;
1808 if (exclude_parent &&
1809 exclude_parent > commit_list_count(commit->parents))
1810 return 0;
1811 for (parents = commit->parents, parent_number = 1;
1812 parents;
1813 parents = parents->next, parent_number++) {
1814 if (exclude_parent && parent_number != exclude_parent)
1815 continue;
1816
1817 it = &parents->item->object;
1818 it->flags |= flags;
1819 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1820 add_pending_object(revs, it, arg);
1821 }
1822 return 1;
1823}
1824
1825void repo_init_revisions(struct repository *r,
1826 struct rev_info *revs,
1827 const char *prefix)
1828{
1829 memset(revs, 0, sizeof(*revs));
1830
1831 revs->repo = r;
1832 revs->abbrev = DEFAULT_ABBREV;
1833 revs->simplify_history = 1;
1834 revs->pruning.repo = r;
1835 revs->pruning.flags.recursive = 1;
1836 revs->pruning.flags.quick = 1;
1837 revs->pruning.add_remove = file_add_remove;
1838 revs->pruning.change = file_change;
1839 revs->pruning.change_fn_data = revs;
1840 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1841 revs->dense = 1;
1842 revs->prefix = prefix;
1843 revs->max_age = -1;
1844 revs->min_age = -1;
1845 revs->skip_count = -1;
1846 revs->max_count = -1;
1847 revs->max_parents = -1;
1848 revs->expand_tabs_in_log = -1;
1849
1850 revs->commit_format = CMIT_FMT_DEFAULT;
1851 revs->expand_tabs_in_log_default = 8;
1852
1853 grep_init(&revs->grep_filter, revs->repo);
1854 revs->grep_filter.status_only = 1;
1855
1856 repo_diff_setup(revs->repo, &revs->diffopt);
1857 if (prefix && !revs->diffopt.prefix) {
1858 revs->diffopt.prefix = prefix;
1859 revs->diffopt.prefix_length = strlen(prefix);
1860 }
1861
1862 init_display_notes(&revs->notes_opt);
1863}
1864
1865static void add_pending_commit_list(struct rev_info *revs,
1866 struct commit_list *commit_list,
1867 unsigned int flags)
1868{
1869 while (commit_list) {
1870 struct object *object = &commit_list->item->object;
1871 object->flags |= flags;
1872 add_pending_object(revs, object, oid_to_hex(&object->oid));
1873 commit_list = commit_list->next;
1874 }
1875}
1876
1877static void prepare_show_merge(struct rev_info *revs)
1878{
1879 struct commit_list *bases;
1880 struct commit *head, *other;
1881 struct object_id oid;
1882 const char **prune = NULL;
1883 int i, prune_num = 1; /* counting terminating NULL */
1884 struct index_state *istate = revs->repo->index;
1885
1886 if (get_oid("HEAD", &oid))
1887 die("--merge without HEAD?");
1888 head = lookup_commit_or_die(&oid, "HEAD");
1889 if (get_oid("MERGE_HEAD", &oid))
1890 die("--merge without MERGE_HEAD?");
1891 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1892 add_pending_object(revs, &head->object, "HEAD");
1893 add_pending_object(revs, &other->object, "MERGE_HEAD");
1894 bases = get_merge_bases(head, other);
1895 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1896 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1897 free_commit_list(bases);
1898 head->object.flags |= SYMMETRIC_LEFT;
1899
1900 if (!istate->cache_nr)
1901 repo_read_index(revs->repo);
1902 for (i = 0; i < istate->cache_nr; i++) {
1903 const struct cache_entry *ce = istate->cache[i];
1904 if (!ce_stage(ce))
1905 continue;
1906 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1907 prune_num++;
1908 REALLOC_ARRAY(prune, prune_num);
1909 prune[prune_num-2] = ce->name;
1910 prune[prune_num-1] = NULL;
1911 }
1912 while ((i+1 < istate->cache_nr) &&
1913 ce_same_name(ce, istate->cache[i+1]))
1914 i++;
1915 }
1916 clear_pathspec(&revs->prune_data);
1917 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1918 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1919 revs->limited = 1;
1920}
1921
1922static int dotdot_missing(const char *arg, char *dotdot,
1923 struct rev_info *revs, int symmetric)
1924{
1925 if (revs->ignore_missing)
1926 return 0;
1927 /* de-munge so we report the full argument */
1928 *dotdot = '.';
1929 die(symmetric
1930 ? "Invalid symmetric difference expression %s"
1931 : "Invalid revision range %s", arg);
1932}
1933
1934static int handle_dotdot_1(const char *arg, char *dotdot,
1935 struct rev_info *revs, int flags,
1936 int cant_be_filename,
1937 struct object_context *a_oc,
1938 struct object_context *b_oc)
1939{
1940 const char *a_name, *b_name;
1941 struct object_id a_oid, b_oid;
1942 struct object *a_obj, *b_obj;
1943 unsigned int a_flags, b_flags;
1944 int symmetric = 0;
1945 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1946 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1947
1948 a_name = arg;
1949 if (!*a_name)
1950 a_name = "HEAD";
1951
1952 b_name = dotdot + 2;
1953 if (*b_name == '.') {
1954 symmetric = 1;
1955 b_name++;
1956 }
1957 if (!*b_name)
1958 b_name = "HEAD";
1959
1960 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
1961 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
1962 return -1;
1963
1964 if (!cant_be_filename) {
1965 *dotdot = '.';
1966 verify_non_filename(revs->prefix, arg);
1967 *dotdot = '\0';
1968 }
1969
1970 a_obj = parse_object(revs->repo, &a_oid);
1971 b_obj = parse_object(revs->repo, &b_oid);
1972 if (!a_obj || !b_obj)
1973 return dotdot_missing(arg, dotdot, revs, symmetric);
1974
1975 if (!symmetric) {
1976 /* just A..B */
1977 b_flags = flags;
1978 a_flags = flags_exclude;
1979 } else {
1980 /* A...B -- find merge bases between the two */
1981 struct commit *a, *b;
1982 struct commit_list *exclude;
1983
1984 a = lookup_commit_reference(revs->repo, &a_obj->oid);
1985 b = lookup_commit_reference(revs->repo, &b_obj->oid);
1986 if (!a || !b)
1987 return dotdot_missing(arg, dotdot, revs, symmetric);
1988
1989 exclude = get_merge_bases(a, b);
1990 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1991 flags_exclude);
1992 add_pending_commit_list(revs, exclude, flags_exclude);
1993 free_commit_list(exclude);
1994
1995 b_flags = flags;
1996 a_flags = flags | SYMMETRIC_LEFT;
1997 }
1998
1999 a_obj->flags |= a_flags;
2000 b_obj->flags |= b_flags;
2001 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
2002 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
2003 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
2004 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
2005 return 0;
2006}
2007
2008static int handle_dotdot(const char *arg,
2009 struct rev_info *revs, int flags,
2010 int cant_be_filename)
2011{
2012 struct object_context a_oc, b_oc;
2013 char *dotdot = strstr(arg, "..");
2014 int ret;
2015
2016 if (!dotdot)
2017 return -1;
2018
2019 memset(&a_oc, 0, sizeof(a_oc));
2020 memset(&b_oc, 0, sizeof(b_oc));
2021
2022 *dotdot = '\0';
2023 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2024 &a_oc, &b_oc);
2025 *dotdot = '.';
2026
2027 free(a_oc.path);
2028 free(b_oc.path);
2029
2030 return ret;
2031}
2032
2033static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2034{
2035 struct object_context oc;
2036 char *mark;
2037 struct object *object;
2038 struct object_id oid;
2039 int local_flags;
2040 const char *arg = arg_;
2041 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2042 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2043
2044 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2045
2046 if (!cant_be_filename && !strcmp(arg, "..")) {
2047 /*
2048 * Just ".."? That is not a range but the
2049 * pathspec for the parent directory.
2050 */
2051 return -1;
2052 }
2053
2054 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2055 return 0;
2056
2057 mark = strstr(arg, "^@");
2058 if (mark && !mark[2]) {
2059 *mark = 0;
2060 if (add_parents_only(revs, arg, flags, 0))
2061 return 0;
2062 *mark = '^';
2063 }
2064 mark = strstr(arg, "^!");
2065 if (mark && !mark[2]) {
2066 *mark = 0;
2067 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2068 *mark = '^';
2069 }
2070 mark = strstr(arg, "^-");
2071 if (mark) {
2072 int exclude_parent = 1;
2073
2074 if (mark[2]) {
2075 char *end;
2076 exclude_parent = strtoul(mark + 2, &end, 10);
2077 if (*end != '\0' || !exclude_parent)
2078 return -1;
2079 }
2080
2081 *mark = 0;
2082 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2083 *mark = '^';
2084 }
2085
2086 local_flags = 0;
2087 if (*arg == '^') {
2088 local_flags = UNINTERESTING | BOTTOM;
2089 arg++;
2090 }
2091
2092 if (revarg_opt & REVARG_COMMITTISH)
2093 get_sha1_flags |= GET_OID_COMMITTISH;
2094
2095 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2096 return revs->ignore_missing ? 0 : -1;
2097 if (!cant_be_filename)
2098 verify_non_filename(revs->prefix, arg);
2099 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2100 if (!object)
2101 return revs->ignore_missing ? 0 : -1;
2102 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2103 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2104 free(oc.path);
2105 return 0;
2106}
2107
2108int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2109{
2110 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2111 if (!ret)
2112 revs->rev_input_given = 1;
2113 return ret;
2114}
2115
2116static void read_pathspec_from_stdin(struct strbuf *sb,
2117 struct strvec *prune)
2118{
2119 while (strbuf_getline(sb, stdin) != EOF)
2120 strvec_push(prune, sb->buf);
2121}
2122
2123static void read_revisions_from_stdin(struct rev_info *revs,
2124 struct strvec *prune)
2125{
2126 struct strbuf sb;
2127 int seen_dashdash = 0;
2128 int save_warning;
2129
2130 save_warning = warn_on_object_refname_ambiguity;
2131 warn_on_object_refname_ambiguity = 0;
2132
2133 strbuf_init(&sb, 1000);
2134 while (strbuf_getline(&sb, stdin) != EOF) {
2135 int len = sb.len;
2136 if (!len)
2137 break;
2138 if (sb.buf[0] == '-') {
2139 if (len == 2 && sb.buf[1] == '-') {
2140 seen_dashdash = 1;
2141 break;
2142 }
2143 die("options not supported in --stdin mode");
2144 }
2145 if (handle_revision_arg(sb.buf, revs, 0,
2146 REVARG_CANNOT_BE_FILENAME))
2147 die("bad revision '%s'", sb.buf);
2148 }
2149 if (seen_dashdash)
2150 read_pathspec_from_stdin(&sb, prune);
2151
2152 strbuf_release(&sb);
2153 warn_on_object_refname_ambiguity = save_warning;
2154}
2155
2156static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2157{
2158 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2159}
2160
2161static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2162{
2163 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2164}
2165
2166static void add_message_grep(struct rev_info *revs, const char *pattern)
2167{
2168 add_grep(revs, pattern, GREP_PATTERN_BODY);
2169}
2170
2171static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2172 int *unkc, const char **unkv,
2173 const struct setup_revision_opt* opt)
2174{
2175 const char *arg = argv[0];
2176 const char *optarg;
2177 int argcount;
2178 const unsigned hexsz = the_hash_algo->hexsz;
2179
2180 /* pseudo revision arguments */
2181 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2182 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2183 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2184 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2185 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2186 !strcmp(arg, "--indexed-objects") ||
2187 !strcmp(arg, "--alternate-refs") ||
2188 starts_with(arg, "--exclude=") ||
2189 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2190 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2191 {
2192 unkv[(*unkc)++] = arg;
2193 return 1;
2194 }
2195
2196 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2197 revs->max_count = atoi(optarg);
2198 revs->no_walk = 0;
2199 return argcount;
2200 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2201 revs->skip_count = atoi(optarg);
2202 return argcount;
2203 } else if ((*arg == '-') && isdigit(arg[1])) {
2204 /* accept -<digit>, like traditional "head" */
2205 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2206 revs->max_count < 0)
2207 die("'%s': not a non-negative integer", arg + 1);
2208 revs->no_walk = 0;
2209 } else if (!strcmp(arg, "-n")) {
2210 if (argc <= 1)
2211 return error("-n requires an argument");
2212 revs->max_count = atoi(argv[1]);
2213 revs->no_walk = 0;
2214 return 2;
2215 } else if (skip_prefix(arg, "-n", &optarg)) {
2216 revs->max_count = atoi(optarg);
2217 revs->no_walk = 0;
2218 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2219 revs->max_age = atoi(optarg);
2220 return argcount;
2221 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2222 revs->max_age = approxidate(optarg);
2223 return argcount;
2224 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2225 revs->max_age = approxidate(optarg);
2226 return argcount;
2227 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2228 revs->min_age = atoi(optarg);
2229 return argcount;
2230 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2231 revs->min_age = approxidate(optarg);
2232 return argcount;
2233 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2234 revs->min_age = approxidate(optarg);
2235 return argcount;
2236 } else if (!strcmp(arg, "--first-parent")) {
2237 revs->first_parent_only = 1;
2238 } else if (!strcmp(arg, "--exclude-first-parent-only")) {
2239 revs->exclude_first_parent_only = 1;
2240 } else if (!strcmp(arg, "--ancestry-path")) {
2241 revs->ancestry_path = 1;
2242 revs->simplify_history = 0;
2243 revs->limited = 1;
2244 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2245 init_reflog_walk(&revs->reflog_info);
2246 } else if (!strcmp(arg, "--default")) {
2247 if (argc <= 1)
2248 return error("bad --default argument");
2249 revs->def = argv[1];
2250 return 2;
2251 } else if (!strcmp(arg, "--merge")) {
2252 revs->show_merge = 1;
2253 } else if (!strcmp(arg, "--topo-order")) {
2254 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2255 revs->topo_order = 1;
2256 } else if (!strcmp(arg, "--simplify-merges")) {
2257 revs->simplify_merges = 1;
2258 revs->topo_order = 1;
2259 revs->rewrite_parents = 1;
2260 revs->simplify_history = 0;
2261 revs->limited = 1;
2262 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2263 revs->simplify_merges = 1;
2264 revs->topo_order = 1;
2265 revs->rewrite_parents = 1;
2266 revs->simplify_history = 0;
2267 revs->simplify_by_decoration = 1;
2268 revs->limited = 1;
2269 revs->prune = 1;
2270 } else if (!strcmp(arg, "--date-order")) {
2271 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2272 revs->topo_order = 1;
2273 } else if (!strcmp(arg, "--author-date-order")) {
2274 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2275 revs->topo_order = 1;
2276 } else if (!strcmp(arg, "--early-output")) {
2277 revs->early_output = 100;
2278 revs->topo_order = 1;
2279 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2280 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2281 die("'%s': not a non-negative integer", optarg);
2282 revs->topo_order = 1;
2283 } else if (!strcmp(arg, "--parents")) {
2284 revs->rewrite_parents = 1;
2285 revs->print_parents = 1;
2286 } else if (!strcmp(arg, "--dense")) {
2287 revs->dense = 1;
2288 } else if (!strcmp(arg, "--sparse")) {
2289 revs->dense = 0;
2290 } else if (!strcmp(arg, "--in-commit-order")) {
2291 revs->tree_blobs_in_commit_order = 1;
2292 } else if (!strcmp(arg, "--remove-empty")) {
2293 revs->remove_empty_trees = 1;
2294 } else if (!strcmp(arg, "--merges")) {
2295 revs->min_parents = 2;
2296 } else if (!strcmp(arg, "--no-merges")) {
2297 revs->max_parents = 1;
2298 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2299 revs->min_parents = atoi(optarg);
2300 } else if (!strcmp(arg, "--no-min-parents")) {
2301 revs->min_parents = 0;
2302 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2303 revs->max_parents = atoi(optarg);
2304 } else if (!strcmp(arg, "--no-max-parents")) {
2305 revs->max_parents = -1;
2306 } else if (!strcmp(arg, "--boundary")) {
2307 revs->boundary = 1;
2308 } else if (!strcmp(arg, "--left-right")) {
2309 revs->left_right = 1;
2310 } else if (!strcmp(arg, "--left-only")) {
2311 if (revs->right_only)
2312 die("--left-only is incompatible with --right-only"
2313 " or --cherry");
2314 revs->left_only = 1;
2315 } else if (!strcmp(arg, "--right-only")) {
2316 if (revs->left_only)
2317 die(_("options '%s' and '%s' cannot be used together"), "--right-only", "--left-only");
2318 revs->right_only = 1;
2319 } else if (!strcmp(arg, "--cherry")) {
2320 if (revs->left_only)
2321 die(_("options '%s' and '%s' cannot be used together"), "--cherry", "--left-only");
2322 revs->cherry_mark = 1;
2323 revs->right_only = 1;
2324 revs->max_parents = 1;
2325 revs->limited = 1;
2326 } else if (!strcmp(arg, "--count")) {
2327 revs->count = 1;
2328 } else if (!strcmp(arg, "--cherry-mark")) {
2329 if (revs->cherry_pick)
2330 die(_("options '%s' and '%s' cannot be used together"), "--cherry-mark", "--cherry-pick");
2331 revs->cherry_mark = 1;
2332 revs->limited = 1; /* needs limit_list() */
2333 } else if (!strcmp(arg, "--cherry-pick")) {
2334 if (revs->cherry_mark)
2335 die(_("options '%s' and '%s' cannot be used together"), "--cherry-pick", "--cherry-mark");
2336 revs->cherry_pick = 1;
2337 revs->limited = 1;
2338 } else if (!strcmp(arg, "--objects")) {
2339 revs->tag_objects = 1;
2340 revs->tree_objects = 1;
2341 revs->blob_objects = 1;
2342 } else if (!strcmp(arg, "--objects-edge")) {
2343 revs->tag_objects = 1;
2344 revs->tree_objects = 1;
2345 revs->blob_objects = 1;
2346 revs->edge_hint = 1;
2347 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2348 revs->tag_objects = 1;
2349 revs->tree_objects = 1;
2350 revs->blob_objects = 1;
2351 revs->edge_hint = 1;
2352 revs->edge_hint_aggressive = 1;
2353 } else if (!strcmp(arg, "--verify-objects")) {
2354 revs->tag_objects = 1;
2355 revs->tree_objects = 1;
2356 revs->blob_objects = 1;
2357 revs->verify_objects = 1;
2358 } else if (!strcmp(arg, "--unpacked")) {
2359 revs->unpacked = 1;
2360 } else if (starts_with(arg, "--unpacked=")) {
2361 die(_("--unpacked=<packfile> no longer supported"));
2362 } else if (!strcmp(arg, "--no-kept-objects")) {
2363 revs->no_kept_objects = 1;
2364 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2365 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2366 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2367 revs->no_kept_objects = 1;
2368 if (!strcmp(optarg, "in-core"))
2369 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2370 if (!strcmp(optarg, "on-disk"))
2371 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2372 } else if (!strcmp(arg, "-r")) {
2373 revs->diff = 1;
2374 revs->diffopt.flags.recursive = 1;
2375 } else if (!strcmp(arg, "-t")) {
2376 revs->diff = 1;
2377 revs->diffopt.flags.recursive = 1;
2378 revs->diffopt.flags.tree_in_recursive = 1;
2379 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2380 return argcount;
2381 } else if (!strcmp(arg, "-v")) {
2382 revs->verbose_header = 1;
2383 } else if (!strcmp(arg, "--pretty")) {
2384 revs->verbose_header = 1;
2385 revs->pretty_given = 1;
2386 get_commit_format(NULL, revs);
2387 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2388 skip_prefix(arg, "--format=", &optarg)) {
2389 /*
2390 * Detached form ("--pretty X" as opposed to "--pretty=X")
2391 * not allowed, since the argument is optional.
2392 */
2393 revs->verbose_header = 1;
2394 revs->pretty_given = 1;
2395 get_commit_format(optarg, revs);
2396 } else if (!strcmp(arg, "--expand-tabs")) {
2397 revs->expand_tabs_in_log = 8;
2398 } else if (!strcmp(arg, "--no-expand-tabs")) {
2399 revs->expand_tabs_in_log = 0;
2400 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2401 int val;
2402 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2403 die("'%s': not a non-negative integer", arg);
2404 revs->expand_tabs_in_log = val;
2405 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2406 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2407 revs->show_notes_given = 1;
2408 } else if (!strcmp(arg, "--show-signature")) {
2409 revs->show_signature = 1;
2410 } else if (!strcmp(arg, "--no-show-signature")) {
2411 revs->show_signature = 0;
2412 } else if (!strcmp(arg, "--show-linear-break")) {
2413 revs->break_bar = " ..........";
2414 revs->track_linear = 1;
2415 revs->track_first_time = 1;
2416 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2417 revs->break_bar = xstrdup(optarg);
2418 revs->track_linear = 1;
2419 revs->track_first_time = 1;
2420 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2421 skip_prefix(arg, "--notes=", &optarg)) {
2422 if (starts_with(arg, "--show-notes=") &&
2423 revs->notes_opt.use_default_notes < 0)
2424 revs->notes_opt.use_default_notes = 1;
2425 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2426 revs->show_notes_given = 1;
2427 } else if (!strcmp(arg, "--no-notes")) {
2428 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2429 revs->show_notes_given = 1;
2430 } else if (!strcmp(arg, "--standard-notes")) {
2431 revs->show_notes_given = 1;
2432 revs->notes_opt.use_default_notes = 1;
2433 } else if (!strcmp(arg, "--no-standard-notes")) {
2434 revs->notes_opt.use_default_notes = 0;
2435 } else if (!strcmp(arg, "--oneline")) {
2436 revs->verbose_header = 1;
2437 get_commit_format("oneline", revs);
2438 revs->pretty_given = 1;
2439 revs->abbrev_commit = 1;
2440 } else if (!strcmp(arg, "--graph")) {
2441 graph_clear(revs->graph);
2442 revs->graph = graph_init(revs);
2443 } else if (!strcmp(arg, "--no-graph")) {
2444 graph_clear(revs->graph);
2445 revs->graph = NULL;
2446 } else if (!strcmp(arg, "--encode-email-headers")) {
2447 revs->encode_email_headers = 1;
2448 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2449 revs->encode_email_headers = 0;
2450 } else if (!strcmp(arg, "--root")) {
2451 revs->show_root_diff = 1;
2452 } else if (!strcmp(arg, "--no-commit-id")) {
2453 revs->no_commit_id = 1;
2454 } else if (!strcmp(arg, "--always")) {
2455 revs->always_show_header = 1;
2456 } else if (!strcmp(arg, "--no-abbrev")) {
2457 revs->abbrev = 0;
2458 } else if (!strcmp(arg, "--abbrev")) {
2459 revs->abbrev = DEFAULT_ABBREV;
2460 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2461 revs->abbrev = strtoul(optarg, NULL, 10);
2462 if (revs->abbrev < MINIMUM_ABBREV)
2463 revs->abbrev = MINIMUM_ABBREV;
2464 else if (revs->abbrev > hexsz)
2465 revs->abbrev = hexsz;
2466 } else if (!strcmp(arg, "--abbrev-commit")) {
2467 revs->abbrev_commit = 1;
2468 revs->abbrev_commit_given = 1;
2469 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2470 revs->abbrev_commit = 0;
2471 } else if (!strcmp(arg, "--full-diff")) {
2472 revs->diff = 1;
2473 revs->full_diff = 1;
2474 } else if (!strcmp(arg, "--show-pulls")) {
2475 revs->show_pulls = 1;
2476 } else if (!strcmp(arg, "--full-history")) {
2477 revs->simplify_history = 0;
2478 } else if (!strcmp(arg, "--relative-date")) {
2479 revs->date_mode.type = DATE_RELATIVE;
2480 revs->date_mode_explicit = 1;
2481 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2482 parse_date_format(optarg, &revs->date_mode);
2483 revs->date_mode_explicit = 1;
2484 return argcount;
2485 } else if (!strcmp(arg, "--log-size")) {
2486 revs->show_log_size = 1;
2487 }
2488 /*
2489 * Grepping the commit log
2490 */
2491 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2492 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2493 return argcount;
2494 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2495 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2496 return argcount;
2497 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2498 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2499 return argcount;
2500 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2501 add_message_grep(revs, optarg);
2502 return argcount;
2503 } else if (!strcmp(arg, "--basic-regexp")) {
2504 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2505 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2506 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2507 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2508 revs->grep_filter.ignore_case = 1;
2509 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2510 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2511 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2512 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2513 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2514 } else if (!strcmp(arg, "--all-match")) {
2515 revs->grep_filter.all_match = 1;
2516 } else if (!strcmp(arg, "--invert-grep")) {
2517 revs->grep_filter.no_body_match = 1;
2518 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2519 if (strcmp(optarg, "none"))
2520 git_log_output_encoding = xstrdup(optarg);
2521 else
2522 git_log_output_encoding = "";
2523 return argcount;
2524 } else if (!strcmp(arg, "--reverse")) {
2525 revs->reverse ^= 1;
2526 } else if (!strcmp(arg, "--children")) {
2527 revs->children.name = "children";
2528 revs->limited = 1;
2529 } else if (!strcmp(arg, "--ignore-missing")) {
2530 revs->ignore_missing = 1;
2531 } else if (opt && opt->allow_exclude_promisor_objects &&
2532 !strcmp(arg, "--exclude-promisor-objects")) {
2533 if (fetch_if_missing)
2534 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2535 revs->exclude_promisor_objects = 1;
2536 } else {
2537 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2538 if (!opts)
2539 unkv[(*unkc)++] = arg;
2540 return opts;
2541 }
2542
2543 return 1;
2544}
2545
2546void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2547 const struct option *options,
2548 const char * const usagestr[])
2549{
2550 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2551 &ctx->cpidx, ctx->out, NULL);
2552 if (n <= 0) {
2553 error("unknown option `%s'", ctx->argv[0]);
2554 usage_with_options(usagestr, options);
2555 }
2556 ctx->argv += n;
2557 ctx->argc -= n;
2558}
2559
2560void revision_opts_finish(struct rev_info *revs)
2561{
2562 if (revs->graph && revs->track_linear)
2563 die(_("options '%s' and '%s' cannot be used together"), "--show-linear-break", "--graph");
2564
2565 if (revs->graph) {
2566 revs->topo_order = 1;
2567 revs->rewrite_parents = 1;
2568 }
2569}
2570
2571static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2572 void *cb_data, const char *term)
2573{
2574 struct strbuf bisect_refs = STRBUF_INIT;
2575 int status;
2576 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2577 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data);
2578 strbuf_release(&bisect_refs);
2579 return status;
2580}
2581
2582static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2583{
2584 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2585}
2586
2587static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2588{
2589 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2590}
2591
2592static int handle_revision_pseudo_opt(struct rev_info *revs,
2593 const char **argv, int *flags)
2594{
2595 const char *arg = argv[0];
2596 const char *optarg;
2597 struct ref_store *refs;
2598 int argcount;
2599
2600 if (revs->repo != the_repository) {
2601 /*
2602 * We need some something like get_submodule_worktrees()
2603 * before we can go through all worktrees of a submodule,
2604 * .e.g with adding all HEADs from --all, which is not
2605 * supported right now, so stick to single worktree.
2606 */
2607 if (!revs->single_worktree)
2608 BUG("--single-worktree cannot be used together with submodule");
2609 }
2610 refs = get_main_ref_store(revs->repo);
2611
2612 /*
2613 * NOTE!
2614 *
2615 * Commands like "git shortlog" will not accept the options below
2616 * unless parse_revision_opt queues them (as opposed to erroring
2617 * out).
2618 *
2619 * When implementing your new pseudo-option, remember to
2620 * register it in the list at the top of handle_revision_opt.
2621 */
2622 if (!strcmp(arg, "--all")) {
2623 handle_refs(refs, revs, *flags, refs_for_each_ref);
2624 handle_refs(refs, revs, *flags, refs_head_ref);
2625 if (!revs->single_worktree) {
2626 struct all_refs_cb cb;
2627
2628 init_all_refs_cb(&cb, revs, *flags);
2629 other_head_refs(handle_one_ref, &cb);
2630 }
2631 clear_ref_exclusion(&revs->ref_excludes);
2632 } else if (!strcmp(arg, "--branches")) {
2633 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2634 clear_ref_exclusion(&revs->ref_excludes);
2635 } else if (!strcmp(arg, "--bisect")) {
2636 read_bisect_terms(&term_bad, &term_good);
2637 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2638 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2639 for_each_good_bisect_ref);
2640 revs->bisect = 1;
2641 } else if (!strcmp(arg, "--tags")) {
2642 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2643 clear_ref_exclusion(&revs->ref_excludes);
2644 } else if (!strcmp(arg, "--remotes")) {
2645 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2646 clear_ref_exclusion(&revs->ref_excludes);
2647 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2648 struct all_refs_cb cb;
2649 init_all_refs_cb(&cb, revs, *flags);
2650 for_each_glob_ref(handle_one_ref, optarg, &cb);
2651 clear_ref_exclusion(&revs->ref_excludes);
2652 return argcount;
2653 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2654 add_ref_exclusion(&revs->ref_excludes, optarg);
2655 return argcount;
2656 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2657 struct all_refs_cb cb;
2658 init_all_refs_cb(&cb, revs, *flags);
2659 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2660 clear_ref_exclusion(&revs->ref_excludes);
2661 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2662 struct all_refs_cb cb;
2663 init_all_refs_cb(&cb, revs, *flags);
2664 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2665 clear_ref_exclusion(&revs->ref_excludes);
2666 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2667 struct all_refs_cb cb;
2668 init_all_refs_cb(&cb, revs, *flags);
2669 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2670 clear_ref_exclusion(&revs->ref_excludes);
2671 } else if (!strcmp(arg, "--reflog")) {
2672 add_reflogs_to_pending(revs, *flags);
2673 } else if (!strcmp(arg, "--indexed-objects")) {
2674 add_index_objects_to_pending(revs, *flags);
2675 } else if (!strcmp(arg, "--alternate-refs")) {
2676 add_alternate_refs_to_pending(revs, *flags);
2677 } else if (!strcmp(arg, "--not")) {
2678 *flags ^= UNINTERESTING | BOTTOM;
2679 } else if (!strcmp(arg, "--no-walk")) {
2680 revs->no_walk = 1;
2681 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2682 /*
2683 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2684 * not allowed, since the argument is optional.
2685 */
2686 revs->no_walk = 1;
2687 if (!strcmp(optarg, "sorted"))
2688 revs->unsorted_input = 0;
2689 else if (!strcmp(optarg, "unsorted"))
2690 revs->unsorted_input = 1;
2691 else
2692 return error("invalid argument to --no-walk");
2693 } else if (!strcmp(arg, "--do-walk")) {
2694 revs->no_walk = 0;
2695 } else if (!strcmp(arg, "--single-worktree")) {
2696 revs->single_worktree = 1;
2697 } else if (skip_prefix(arg, ("--filter="), &arg)) {
2698 parse_list_objects_filter(&revs->filter, arg);
2699 } else if (!strcmp(arg, ("--no-filter"))) {
2700 list_objects_filter_set_no_filter(&revs->filter);
2701 } else {
2702 return 0;
2703 }
2704
2705 return 1;
2706}
2707
2708static void NORETURN diagnose_missing_default(const char *def)
2709{
2710 int flags;
2711 const char *refname;
2712
2713 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2714 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2715 die(_("your current branch appears to be broken"));
2716
2717 skip_prefix(refname, "refs/heads/", &refname);
2718 die(_("your current branch '%s' does not have any commits yet"),
2719 refname);
2720}
2721
2722/*
2723 * Parse revision information, filling in the "rev_info" structure,
2724 * and removing the used arguments from the argument list.
2725 *
2726 * Returns the number of arguments left that weren't recognized
2727 * (which are also moved to the head of the argument list)
2728 */
2729int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2730{
2731 int i, flags, left, seen_dashdash, revarg_opt;
2732 struct strvec prune_data = STRVEC_INIT;
2733 int seen_end_of_options = 0;
2734
2735 /* First, search for "--" */
2736 if (opt && opt->assume_dashdash) {
2737 seen_dashdash = 1;
2738 } else {
2739 seen_dashdash = 0;
2740 for (i = 1; i < argc; i++) {
2741 const char *arg = argv[i];
2742 if (strcmp(arg, "--"))
2743 continue;
2744 argv[i] = NULL;
2745 argc = i;
2746 if (argv[i + 1])
2747 strvec_pushv(&prune_data, argv + i + 1);
2748 seen_dashdash = 1;
2749 break;
2750 }
2751 }
2752
2753 /* Second, deal with arguments and options */
2754 flags = 0;
2755 revarg_opt = opt ? opt->revarg_opt : 0;
2756 if (seen_dashdash)
2757 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2758 for (left = i = 1; i < argc; i++) {
2759 const char *arg = argv[i];
2760 if (!seen_end_of_options && *arg == '-') {
2761 int opts;
2762
2763 opts = handle_revision_pseudo_opt(
2764 revs, argv + i,
2765 &flags);
2766 if (opts > 0) {
2767 i += opts - 1;
2768 continue;
2769 }
2770
2771 if (!strcmp(arg, "--stdin")) {
2772 if (revs->disable_stdin) {
2773 argv[left++] = arg;
2774 continue;
2775 }
2776 if (revs->read_from_stdin++)
2777 die("--stdin given twice?");
2778 read_revisions_from_stdin(revs, &prune_data);
2779 continue;
2780 }
2781
2782 if (!strcmp(arg, "--end-of-options")) {
2783 seen_end_of_options = 1;
2784 continue;
2785 }
2786
2787 opts = handle_revision_opt(revs, argc - i, argv + i,
2788 &left, argv, opt);
2789 if (opts > 0) {
2790 i += opts - 1;
2791 continue;
2792 }
2793 if (opts < 0)
2794 exit(128);
2795 continue;
2796 }
2797
2798
2799 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2800 int j;
2801 if (seen_dashdash || *arg == '^')
2802 die("bad revision '%s'", arg);
2803
2804 /* If we didn't have a "--":
2805 * (1) all filenames must exist;
2806 * (2) all rev-args must not be interpretable
2807 * as a valid filename.
2808 * but the latter we have checked in the main loop.
2809 */
2810 for (j = i; j < argc; j++)
2811 verify_filename(revs->prefix, argv[j], j == i);
2812
2813 strvec_pushv(&prune_data, argv + i);
2814 break;
2815 }
2816 }
2817 revision_opts_finish(revs);
2818
2819 if (prune_data.nr) {
2820 /*
2821 * If we need to introduce the magic "a lone ':' means no
2822 * pathspec whatsoever", here is the place to do so.
2823 *
2824 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2825 * prune_data.nr = 0;
2826 * prune_data.alloc = 0;
2827 * free(prune_data.path);
2828 * prune_data.path = NULL;
2829 * } else {
2830 * terminate prune_data.alloc with NULL and
2831 * call init_pathspec() to set revs->prune_data here.
2832 * }
2833 */
2834 parse_pathspec(&revs->prune_data, 0, 0,
2835 revs->prefix, prune_data.v);
2836 }
2837 strvec_clear(&prune_data);
2838
2839 if (revs->def == NULL)
2840 revs->def = opt ? opt->def : NULL;
2841 if (opt && opt->tweak)
2842 opt->tweak(revs, opt);
2843 if (revs->show_merge)
2844 prepare_show_merge(revs);
2845 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2846 struct object_id oid;
2847 struct object *object;
2848 struct object_context oc;
2849 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2850 diagnose_missing_default(revs->def);
2851 object = get_reference(revs, revs->def, &oid, 0);
2852 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2853 }
2854
2855 /* Did the user ask for any diff output? Run the diff! */
2856 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2857 revs->diff = 1;
2858
2859 /* Pickaxe, diff-filter and rename following need diffs */
2860 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2861 revs->diffopt.filter ||
2862 revs->diffopt.flags.follow_renames)
2863 revs->diff = 1;
2864
2865 if (revs->diffopt.objfind)
2866 revs->simplify_history = 0;
2867
2868 if (revs->line_level_traverse) {
2869 if (want_ancestry(revs))
2870 revs->limited = 1;
2871 revs->topo_order = 1;
2872 }
2873
2874 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2875 revs->limited = 1;
2876
2877 if (revs->prune_data.nr) {
2878 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2879 /* Can't prune commits with rename following: the paths change.. */
2880 if (!revs->diffopt.flags.follow_renames)
2881 revs->prune = 1;
2882 if (!revs->full_diff)
2883 copy_pathspec(&revs->diffopt.pathspec,
2884 &revs->prune_data);
2885 }
2886
2887 diff_merges_setup_revs(revs);
2888
2889 revs->diffopt.abbrev = revs->abbrev;
2890
2891 diff_setup_done(&revs->diffopt);
2892
2893 if (!is_encoding_utf8(get_log_output_encoding()))
2894 revs->grep_filter.ignore_locale = 1;
2895 compile_grep_patterns(&revs->grep_filter);
2896
2897 if (revs->reverse && revs->reflog_info)
2898 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--walk-reflogs");
2899 if (revs->reflog_info && revs->limited)
2900 die("cannot combine --walk-reflogs with history-limiting options");
2901 if (revs->rewrite_parents && revs->children.name)
2902 die(_("options '%s' and '%s' cannot be used together"), "--parents", "--children");
2903 if (revs->filter.choice && !revs->blob_objects)
2904 die(_("object filtering requires --objects"));
2905
2906 /*
2907 * Limitations on the graph functionality
2908 */
2909 if (revs->reverse && revs->graph)
2910 die(_("options '%s' and '%s' cannot be used together"), "--reverse", "--graph");
2911
2912 if (revs->reflog_info && revs->graph)
2913 die(_("options '%s' and '%s' cannot be used together"), "--walk-reflogs", "--graph");
2914 if (revs->no_walk && revs->graph)
2915 die(_("options '%s' and '%s' cannot be used together"), "--no-walk", "--graph");
2916 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2917 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
2918
2919 if (revs->line_level_traverse &&
2920 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2921 die(_("-L does not yet support diff formats besides -p and -s"));
2922
2923 if (revs->expand_tabs_in_log < 0)
2924 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2925
2926 return left;
2927}
2928
2929static void release_revisions_cmdline(struct rev_cmdline_info *cmdline)
2930{
2931 unsigned int i;
2932
2933 for (i = 0; i < cmdline->nr; i++)
2934 free((char *)cmdline->rev[i].name);
2935 free(cmdline->rev);
2936}
2937
2938static void release_revisions_mailmap(struct string_list *mailmap)
2939{
2940 if (!mailmap)
2941 return;
2942 clear_mailmap(mailmap);
2943 free(mailmap);
2944}
2945
2946static void release_revisions_topo_walk_info(struct topo_walk_info *info);
2947
2948void release_revisions(struct rev_info *revs)
2949{
2950 free_commit_list(revs->commits);
2951 object_array_clear(&revs->pending);
2952 object_array_clear(&revs->boundary_commits);
2953 release_revisions_cmdline(&revs->cmdline);
2954 list_objects_filter_release(&revs->filter);
2955 clear_pathspec(&revs->prune_data);
2956 date_mode_release(&revs->date_mode);
2957 release_revisions_mailmap(revs->mailmap);
2958 free_grep_patterns(&revs->grep_filter);
2959 diff_free(&revs->pruning);
2960 reflog_walk_info_release(revs->reflog_info);
2961 release_revisions_topo_walk_info(revs->topo_walk_info);
2962}
2963
2964static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2965{
2966 struct commit_list *l = xcalloc(1, sizeof(*l));
2967
2968 l->item = child;
2969 l->next = add_decoration(&revs->children, &parent->object, l);
2970}
2971
2972static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2973{
2974 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2975 struct commit_list **pp, *p;
2976 int surviving_parents;
2977
2978 /* Examine existing parents while marking ones we have seen... */
2979 pp = &commit->parents;
2980 surviving_parents = 0;
2981 while ((p = *pp) != NULL) {
2982 struct commit *parent = p->item;
2983 if (parent->object.flags & TMP_MARK) {
2984 *pp = p->next;
2985 if (ts)
2986 compact_treesame(revs, commit, surviving_parents);
2987 continue;
2988 }
2989 parent->object.flags |= TMP_MARK;
2990 surviving_parents++;
2991 pp = &p->next;
2992 }
2993 /* clear the temporary mark */
2994 for (p = commit->parents; p; p = p->next) {
2995 p->item->object.flags &= ~TMP_MARK;
2996 }
2997 /* no update_treesame() - removing duplicates can't affect TREESAME */
2998 return surviving_parents;
2999}
3000
3001struct merge_simplify_state {
3002 struct commit *simplified;
3003};
3004
3005static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
3006{
3007 struct merge_simplify_state *st;
3008
3009 st = lookup_decoration(&revs->merge_simplification, &commit->object);
3010 if (!st) {
3011 CALLOC_ARRAY(st, 1);
3012 add_decoration(&revs->merge_simplification, &commit->object, st);
3013 }
3014 return st;
3015}
3016
3017static int mark_redundant_parents(struct commit *commit)
3018{
3019 struct commit_list *h = reduce_heads(commit->parents);
3020 int i = 0, marked = 0;
3021 struct commit_list *po, *pn;
3022
3023 /* Want these for sanity-checking only */
3024 int orig_cnt = commit_list_count(commit->parents);
3025 int cnt = commit_list_count(h);
3026
3027 /*
3028 * Not ready to remove items yet, just mark them for now, based
3029 * on the output of reduce_heads(). reduce_heads outputs the reduced
3030 * set in its original order, so this isn't too hard.
3031 */
3032 po = commit->parents;
3033 pn = h;
3034 while (po) {
3035 if (pn && po->item == pn->item) {
3036 pn = pn->next;
3037 i++;
3038 } else {
3039 po->item->object.flags |= TMP_MARK;
3040 marked++;
3041 }
3042 po=po->next;
3043 }
3044
3045 if (i != cnt || cnt+marked != orig_cnt)
3046 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
3047
3048 free_commit_list(h);
3049
3050 return marked;
3051}
3052
3053static int mark_treesame_root_parents(struct commit *commit)
3054{
3055 struct commit_list *p;
3056 int marked = 0;
3057
3058 for (p = commit->parents; p; p = p->next) {
3059 struct commit *parent = p->item;
3060 if (!parent->parents && (parent->object.flags & TREESAME)) {
3061 parent->object.flags |= TMP_MARK;
3062 marked++;
3063 }
3064 }
3065
3066 return marked;
3067}
3068
3069/*
3070 * Awkward naming - this means one parent we are TREESAME to.
3071 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3072 * empty tree). Better name suggestions?
3073 */
3074static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3075{
3076 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3077 struct commit *unmarked = NULL, *marked = NULL;
3078 struct commit_list *p;
3079 unsigned n;
3080
3081 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3082 if (ts->treesame[n]) {
3083 if (p->item->object.flags & TMP_MARK) {
3084 if (!marked)
3085 marked = p->item;
3086 } else {
3087 if (!unmarked) {
3088 unmarked = p->item;
3089 break;
3090 }
3091 }
3092 }
3093 }
3094
3095 /*
3096 * If we are TREESAME to a marked-for-deletion parent, but not to any
3097 * unmarked parents, unmark the first TREESAME parent. This is the
3098 * parent that the default simplify_history==1 scan would have followed,
3099 * and it doesn't make sense to omit that path when asking for a
3100 * simplified full history. Retaining it improves the chances of
3101 * understanding odd missed merges that took an old version of a file.
3102 *
3103 * Example:
3104 *
3105 * I--------*X A modified the file, but mainline merge X used
3106 * \ / "-s ours", so took the version from I. X is
3107 * `-*A--' TREESAME to I and !TREESAME to A.
3108 *
3109 * Default log from X would produce "I". Without this check,
3110 * --full-history --simplify-merges would produce "I-A-X", showing
3111 * the merge commit X and that it changed A, but not making clear that
3112 * it had just taken the I version. With this check, the topology above
3113 * is retained.
3114 *
3115 * Note that it is possible that the simplification chooses a different
3116 * TREESAME parent from the default, in which case this test doesn't
3117 * activate, and we _do_ drop the default parent. Example:
3118 *
3119 * I------X A modified the file, but it was reverted in B,
3120 * \ / meaning mainline merge X is TREESAME to both
3121 * *A-*B parents.
3122 *
3123 * Default log would produce "I" by following the first parent;
3124 * --full-history --simplify-merges will produce "I-A-B". But this is a
3125 * reasonable result - it presents a logical full history leading from
3126 * I to X, and X is not an important merge.
3127 */
3128 if (!unmarked && marked) {
3129 marked->object.flags &= ~TMP_MARK;
3130 return 1;
3131 }
3132
3133 return 0;
3134}
3135
3136static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3137{
3138 struct commit_list **pp, *p;
3139 int nth_parent, removed = 0;
3140
3141 pp = &commit->parents;
3142 nth_parent = 0;
3143 while ((p = *pp) != NULL) {
3144 struct commit *parent = p->item;
3145 if (parent->object.flags & TMP_MARK) {
3146 parent->object.flags &= ~TMP_MARK;
3147 *pp = p->next;
3148 free(p);
3149 removed++;
3150 compact_treesame(revs, commit, nth_parent);
3151 continue;
3152 }
3153 pp = &p->next;
3154 nth_parent++;
3155 }
3156
3157 /* Removing parents can only increase TREESAMEness */
3158 if (removed && !(commit->object.flags & TREESAME))
3159 update_treesame(revs, commit);
3160
3161 return nth_parent;
3162}
3163
3164static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3165{
3166 struct commit_list *p;
3167 struct commit *parent;
3168 struct merge_simplify_state *st, *pst;
3169 int cnt;
3170
3171 st = locate_simplify_state(revs, commit);
3172
3173 /*
3174 * Have we handled this one?
3175 */
3176 if (st->simplified)
3177 return tail;
3178
3179 /*
3180 * An UNINTERESTING commit simplifies to itself, so does a
3181 * root commit. We do not rewrite parents of such commit
3182 * anyway.
3183 */
3184 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3185 st->simplified = commit;
3186 return tail;
3187 }
3188
3189 /*
3190 * Do we know what commit all of our parents that matter
3191 * should be rewritten to? Otherwise we are not ready to
3192 * rewrite this one yet.
3193 */
3194 for (cnt = 0, p = commit->parents; p; p = p->next) {
3195 pst = locate_simplify_state(revs, p->item);
3196 if (!pst->simplified) {
3197 tail = &commit_list_insert(p->item, tail)->next;
3198 cnt++;
3199 }
3200 if (revs->first_parent_only)
3201 break;
3202 }
3203 if (cnt) {
3204 tail = &commit_list_insert(commit, tail)->next;
3205 return tail;
3206 }
3207
3208 /*
3209 * Rewrite our list of parents. Note that this cannot
3210 * affect our TREESAME flags in any way - a commit is
3211 * always TREESAME to its simplification.
3212 */
3213 for (p = commit->parents; p; p = p->next) {
3214 pst = locate_simplify_state(revs, p->item);
3215 p->item = pst->simplified;
3216 if (revs->first_parent_only)
3217 break;
3218 }
3219
3220 if (revs->first_parent_only)
3221 cnt = 1;
3222 else
3223 cnt = remove_duplicate_parents(revs, commit);
3224
3225 /*
3226 * It is possible that we are a merge and one side branch
3227 * does not have any commit that touches the given paths;
3228 * in such a case, the immediate parent from that branch
3229 * will be rewritten to be the merge base.
3230 *
3231 * o----X X: the commit we are looking at;
3232 * / / o: a commit that touches the paths;
3233 * ---o----'
3234 *
3235 * Further, a merge of an independent branch that doesn't
3236 * touch the path will reduce to a treesame root parent:
3237 *
3238 * ----o----X X: the commit we are looking at;
3239 * / o: a commit that touches the paths;
3240 * r r: a root commit not touching the paths
3241 *
3242 * Detect and simplify both cases.
3243 */
3244 if (1 < cnt) {
3245 int marked = mark_redundant_parents(commit);
3246 marked += mark_treesame_root_parents(commit);
3247 if (marked)
3248 marked -= leave_one_treesame_to_parent(revs, commit);
3249 if (marked)
3250 cnt = remove_marked_parents(revs, commit);
3251 }
3252
3253 /*
3254 * A commit simplifies to itself if it is a root, if it is
3255 * UNINTERESTING, if it touches the given paths, or if it is a
3256 * merge and its parents don't simplify to one relevant commit
3257 * (the first two cases are already handled at the beginning of
3258 * this function).
3259 *
3260 * Otherwise, it simplifies to what its sole relevant parent
3261 * simplifies to.
3262 */
3263 if (!cnt ||
3264 (commit->object.flags & UNINTERESTING) ||
3265 !(commit->object.flags & TREESAME) ||
3266 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3267 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3268 st->simplified = commit;
3269 else {
3270 pst = locate_simplify_state(revs, parent);
3271 st->simplified = pst->simplified;
3272 }
3273 return tail;
3274}
3275
3276static void simplify_merges(struct rev_info *revs)
3277{
3278 struct commit_list *list, *next;
3279 struct commit_list *yet_to_do, **tail;
3280 struct commit *commit;
3281
3282 if (!revs->prune)
3283 return;
3284
3285 /* feed the list reversed */
3286 yet_to_do = NULL;
3287 for (list = revs->commits; list; list = next) {
3288 commit = list->item;
3289 next = list->next;
3290 /*
3291 * Do not free(list) here yet; the original list
3292 * is used later in this function.
3293 */
3294 commit_list_insert(commit, &yet_to_do);
3295 }
3296 while (yet_to_do) {
3297 list = yet_to_do;
3298 yet_to_do = NULL;
3299 tail = &yet_to_do;
3300 while (list) {
3301 commit = pop_commit(&list);
3302 tail = simplify_one(revs, commit, tail);
3303 }
3304 }
3305
3306 /* clean up the result, removing the simplified ones */
3307 list = revs->commits;
3308 revs->commits = NULL;
3309 tail = &revs->commits;
3310 while (list) {
3311 struct merge_simplify_state *st;
3312
3313 commit = pop_commit(&list);
3314 st = locate_simplify_state(revs, commit);
3315 if (st->simplified == commit)
3316 tail = &commit_list_insert(commit, tail)->next;
3317 }
3318}
3319
3320static void set_children(struct rev_info *revs)
3321{
3322 struct commit_list *l;
3323 for (l = revs->commits; l; l = l->next) {
3324 struct commit *commit = l->item;
3325 struct commit_list *p;
3326
3327 for (p = commit->parents; p; p = p->next)
3328 add_child(revs, p->item, commit);
3329 }
3330}
3331
3332void reset_revision_walk(void)
3333{
3334 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3335}
3336
3337static int mark_uninteresting(const struct object_id *oid,
3338 struct packed_git *pack,
3339 uint32_t pos,
3340 void *cb)
3341{
3342 struct rev_info *revs = cb;
3343 struct object *o = lookup_unknown_object(revs->repo, oid);
3344 o->flags |= UNINTERESTING | SEEN;
3345 return 0;
3346}
3347
3348define_commit_slab(indegree_slab, int);
3349define_commit_slab(author_date_slab, timestamp_t);
3350
3351struct topo_walk_info {
3352 timestamp_t min_generation;
3353 struct prio_queue explore_queue;
3354 struct prio_queue indegree_queue;
3355 struct prio_queue topo_queue;
3356 struct indegree_slab indegree;
3357 struct author_date_slab author_date;
3358};
3359
3360static int topo_walk_atexit_registered;
3361static unsigned int count_explore_walked;
3362static unsigned int count_indegree_walked;
3363static unsigned int count_topo_walked;
3364
3365static void trace2_topo_walk_statistics_atexit(void)
3366{
3367 struct json_writer jw = JSON_WRITER_INIT;
3368
3369 jw_object_begin(&jw, 0);
3370 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3371 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3372 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3373 jw_end(&jw);
3374
3375 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3376
3377 jw_release(&jw);
3378}
3379
3380static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3381{
3382 if (c->object.flags & flag)
3383 return;
3384
3385 c->object.flags |= flag;
3386 prio_queue_put(q, c);
3387}
3388
3389static void explore_walk_step(struct rev_info *revs)
3390{
3391 struct topo_walk_info *info = revs->topo_walk_info;
3392 struct commit_list *p;
3393 struct commit *c = prio_queue_get(&info->explore_queue);
3394
3395 if (!c)
3396 return;
3397
3398 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3399 return;
3400
3401 count_explore_walked++;
3402
3403 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3404 record_author_date(&info->author_date, c);
3405
3406 if (revs->max_age != -1 && (c->date < revs->max_age))
3407 c->object.flags |= UNINTERESTING;
3408
3409 if (process_parents(revs, c, NULL, NULL) < 0)
3410 return;
3411
3412 if (c->object.flags & UNINTERESTING)
3413 mark_parents_uninteresting(revs, c);
3414
3415 for (p = c->parents; p; p = p->next)
3416 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3417}
3418
3419static void explore_to_depth(struct rev_info *revs,
3420 timestamp_t gen_cutoff)
3421{
3422 struct topo_walk_info *info = revs->topo_walk_info;
3423 struct commit *c;
3424 while ((c = prio_queue_peek(&info->explore_queue)) &&
3425 commit_graph_generation(c) >= gen_cutoff)
3426 explore_walk_step(revs);
3427}
3428
3429static void indegree_walk_step(struct rev_info *revs)
3430{
3431 struct commit_list *p;
3432 struct topo_walk_info *info = revs->topo_walk_info;
3433 struct commit *c = prio_queue_get(&info->indegree_queue);
3434
3435 if (!c)
3436 return;
3437
3438 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3439 return;
3440
3441 count_indegree_walked++;
3442
3443 explore_to_depth(revs, commit_graph_generation(c));
3444
3445 for (p = c->parents; p; p = p->next) {
3446 struct commit *parent = p->item;
3447 int *pi = indegree_slab_at(&info->indegree, parent);
3448
3449 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3450 return;
3451
3452 if (*pi)
3453 (*pi)++;
3454 else
3455 *pi = 2;
3456
3457 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3458
3459 if (revs->first_parent_only)
3460 return;
3461 }
3462}
3463
3464static void compute_indegrees_to_depth(struct rev_info *revs,
3465 timestamp_t gen_cutoff)
3466{
3467 struct topo_walk_info *info = revs->topo_walk_info;
3468 struct commit *c;
3469 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3470 commit_graph_generation(c) >= gen_cutoff)
3471 indegree_walk_step(revs);
3472}
3473
3474static void release_revisions_topo_walk_info(struct topo_walk_info *info)
3475{
3476 if (!info)
3477 return;
3478 clear_prio_queue(&info->explore_queue);
3479 clear_prio_queue(&info->indegree_queue);
3480 clear_prio_queue(&info->topo_queue);
3481 clear_indegree_slab(&info->indegree);
3482 clear_author_date_slab(&info->author_date);
3483 free(info);
3484}
3485
3486static void reset_topo_walk(struct rev_info *revs)
3487{
3488 release_revisions_topo_walk_info(revs->topo_walk_info);
3489 revs->topo_walk_info = NULL;
3490}
3491
3492static void init_topo_walk(struct rev_info *revs)
3493{
3494 struct topo_walk_info *info;
3495 struct commit_list *list;
3496 if (revs->topo_walk_info)
3497 reset_topo_walk(revs);
3498
3499 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3500 info = revs->topo_walk_info;
3501 memset(info, 0, sizeof(struct topo_walk_info));
3502
3503 init_indegree_slab(&info->indegree);
3504 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3505 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3506 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3507
3508 switch (revs->sort_order) {
3509 default: /* REV_SORT_IN_GRAPH_ORDER */
3510 info->topo_queue.compare = NULL;
3511 break;
3512 case REV_SORT_BY_COMMIT_DATE:
3513 info->topo_queue.compare = compare_commits_by_commit_date;
3514 break;
3515 case REV_SORT_BY_AUTHOR_DATE:
3516 init_author_date_slab(&info->author_date);
3517 info->topo_queue.compare = compare_commits_by_author_date;
3518 info->topo_queue.cb_data = &info->author_date;
3519 break;
3520 }
3521
3522 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3523 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3524
3525 info->min_generation = GENERATION_NUMBER_INFINITY;
3526 for (list = revs->commits; list; list = list->next) {
3527 struct commit *c = list->item;
3528 timestamp_t generation;
3529
3530 if (repo_parse_commit_gently(revs->repo, c, 1))
3531 continue;
3532
3533 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3534 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3535
3536 generation = commit_graph_generation(c);
3537 if (generation < info->min_generation)
3538 info->min_generation = generation;
3539
3540 *(indegree_slab_at(&info->indegree, c)) = 1;
3541
3542 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3543 record_author_date(&info->author_date, c);
3544 }
3545 compute_indegrees_to_depth(revs, info->min_generation);
3546
3547 for (list = revs->commits; list; list = list->next) {
3548 struct commit *c = list->item;
3549
3550 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3551 prio_queue_put(&info->topo_queue, c);
3552 }
3553
3554 /*
3555 * This is unfortunate; the initial tips need to be shown
3556 * in the order given from the revision traversal machinery.
3557 */
3558 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3559 prio_queue_reverse(&info->topo_queue);
3560
3561 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3562 atexit(trace2_topo_walk_statistics_atexit);
3563 topo_walk_atexit_registered = 1;
3564 }
3565}
3566
3567static struct commit *next_topo_commit(struct rev_info *revs)
3568{
3569 struct commit *c;
3570 struct topo_walk_info *info = revs->topo_walk_info;
3571
3572 /* pop next off of topo_queue */
3573 c = prio_queue_get(&info->topo_queue);
3574
3575 if (c)
3576 *(indegree_slab_at(&info->indegree, c)) = 0;
3577
3578 return c;
3579}
3580
3581static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3582{
3583 struct commit_list *p;
3584 struct topo_walk_info *info = revs->topo_walk_info;
3585 if (process_parents(revs, commit, NULL, NULL) < 0) {
3586 if (!revs->ignore_missing_links)
3587 die("Failed to traverse parents of commit %s",
3588 oid_to_hex(&commit->object.oid));
3589 }
3590
3591 count_topo_walked++;
3592
3593 for (p = commit->parents; p; p = p->next) {
3594 struct commit *parent = p->item;
3595 int *pi;
3596 timestamp_t generation;
3597
3598 if (parent->object.flags & UNINTERESTING)
3599 continue;
3600
3601 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3602 continue;
3603
3604 generation = commit_graph_generation(parent);
3605 if (generation < info->min_generation) {
3606 info->min_generation = generation;
3607 compute_indegrees_to_depth(revs, info->min_generation);
3608 }
3609
3610 pi = indegree_slab_at(&info->indegree, parent);
3611
3612 (*pi)--;
3613 if (*pi == 1)
3614 prio_queue_put(&info->topo_queue, parent);
3615
3616 if (revs->first_parent_only)
3617 return;
3618 }
3619}
3620
3621int prepare_revision_walk(struct rev_info *revs)
3622{
3623 int i;
3624 struct object_array old_pending;
3625 struct commit_list **next = &revs->commits;
3626
3627 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3628 revs->pending.nr = 0;
3629 revs->pending.alloc = 0;
3630 revs->pending.objects = NULL;
3631 for (i = 0; i < old_pending.nr; i++) {
3632 struct object_array_entry *e = old_pending.objects + i;
3633 struct commit *commit = handle_commit(revs, e);
3634 if (commit) {
3635 if (!(commit->object.flags & SEEN)) {
3636 commit->object.flags |= SEEN;
3637 next = commit_list_append(commit, next);
3638 }
3639 }
3640 }
3641 object_array_clear(&old_pending);
3642
3643 /* Signal whether we need per-parent treesame decoration */
3644 if (revs->simplify_merges ||
3645 (revs->limited && limiting_can_increase_treesame(revs)))
3646 revs->treesame.name = "treesame";
3647
3648 if (revs->exclude_promisor_objects) {
3649 for_each_packed_object(mark_uninteresting, revs,
3650 FOR_EACH_OBJECT_PROMISOR_ONLY);
3651 }
3652
3653 if (!revs->reflog_info)
3654 prepare_to_use_bloom_filter(revs);
3655 if (!revs->unsorted_input)
3656 commit_list_sort_by_date(&revs->commits);
3657 if (revs->no_walk)
3658 return 0;
3659 if (revs->limited) {
3660 if (limit_list(revs) < 0)
3661 return -1;
3662 if (revs->topo_order)
3663 sort_in_topological_order(&revs->commits, revs->sort_order);
3664 } else if (revs->topo_order)
3665 init_topo_walk(revs);
3666 if (revs->line_level_traverse && want_ancestry(revs))
3667 /*
3668 * At the moment we can only do line-level log with parent
3669 * rewriting by performing this expensive pre-filtering step.
3670 * If parent rewriting is not requested, then we rather
3671 * perform the line-level log filtering during the regular
3672 * history traversal.
3673 */
3674 line_log_filter(revs);
3675 if (revs->simplify_merges)
3676 simplify_merges(revs);
3677 if (revs->children.name)
3678 set_children(revs);
3679
3680 return 0;
3681}
3682
3683static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3684 struct commit **pp,
3685 struct prio_queue *queue)
3686{
3687 for (;;) {
3688 struct commit *p = *pp;
3689 if (!revs->limited)
3690 if (process_parents(revs, p, NULL, queue) < 0)
3691 return rewrite_one_error;
3692 if (p->object.flags & UNINTERESTING)
3693 return rewrite_one_ok;
3694 if (!(p->object.flags & TREESAME))
3695 return rewrite_one_ok;
3696 if (!p->parents)
3697 return rewrite_one_noparents;
3698 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
3699 return rewrite_one_ok;
3700 *pp = p;
3701 }
3702}
3703
3704static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3705{
3706 while (q->nr) {
3707 struct commit *item = prio_queue_peek(q);
3708 struct commit_list *p = *list;
3709
3710 if (p && p->item->date >= item->date)
3711 list = &p->next;
3712 else {
3713 p = commit_list_insert(item, list);
3714 list = &p->next; /* skip newly added item */
3715 prio_queue_get(q); /* pop item */
3716 }
3717 }
3718}
3719
3720static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3721{
3722 struct prio_queue queue = { compare_commits_by_commit_date };
3723 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3724 merge_queue_into_list(&queue, &revs->commits);
3725 clear_prio_queue(&queue);
3726 return ret;
3727}
3728
3729int rewrite_parents(struct rev_info *revs, struct commit *commit,
3730 rewrite_parent_fn_t rewrite_parent)
3731{
3732 struct commit_list **pp = &commit->parents;
3733 while (*pp) {
3734 struct commit_list *parent = *pp;
3735 switch (rewrite_parent(revs, &parent->item)) {
3736 case rewrite_one_ok:
3737 break;
3738 case rewrite_one_noparents:
3739 *pp = parent->next;
3740 continue;
3741 case rewrite_one_error:
3742 return -1;
3743 }
3744 pp = &parent->next;
3745 }
3746 remove_duplicate_parents(revs, commit);
3747 return 0;
3748}
3749
3750static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
3751{
3752 char *person, *endp;
3753 size_t len, namelen, maillen;
3754 const char *name;
3755 const char *mail;
3756 struct ident_split ident;
3757
3758 person = strstr(buf->buf, what);
3759 if (!person)
3760 return 0;
3761
3762 person += strlen(what);
3763 endp = strchr(person, '\n');
3764 if (!endp)
3765 return 0;
3766
3767 len = endp - person;
3768
3769 if (split_ident_line(&ident, person, len))
3770 return 0;
3771
3772 mail = ident.mail_begin;
3773 maillen = ident.mail_end - ident.mail_begin;
3774 name = ident.name_begin;
3775 namelen = ident.name_end - ident.name_begin;
3776
3777 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3778 struct strbuf namemail = STRBUF_INIT;
3779
3780 strbuf_addf(&namemail, "%.*s <%.*s>",
3781 (int)namelen, name, (int)maillen, mail);
3782
3783 strbuf_splice(buf, ident.name_begin - buf->buf,
3784 ident.mail_end - ident.name_begin + 1,
3785 namemail.buf, namemail.len);
3786
3787 strbuf_release(&namemail);
3788
3789 return 1;
3790 }
3791
3792 return 0;
3793}
3794
3795static int commit_match(struct commit *commit, struct rev_info *opt)
3796{
3797 int retval;
3798 const char *encoding;
3799 const char *message;
3800 struct strbuf buf = STRBUF_INIT;
3801
3802 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3803 return 1;
3804
3805 /* Prepend "fake" headers as needed */
3806 if (opt->grep_filter.use_reflog_filter) {
3807 strbuf_addstr(&buf, "reflog ");
3808 get_reflog_message(&buf, opt->reflog_info);
3809 strbuf_addch(&buf, '\n');
3810 }
3811
3812 /*
3813 * We grep in the user's output encoding, under the assumption that it
3814 * is the encoding they are most likely to write their grep pattern
3815 * for. In addition, it means we will match the "notes" encoding below,
3816 * so we will not end up with a buffer that has two different encodings
3817 * in it.
3818 */
3819 encoding = get_log_output_encoding();
3820 message = logmsg_reencode(commit, NULL, encoding);
3821
3822 /* Copy the commit to temporary if we are using "fake" headers */
3823 if (buf.len)
3824 strbuf_addstr(&buf, message);
3825
3826 if (opt->grep_filter.header_list && opt->mailmap) {
3827 if (!buf.len)
3828 strbuf_addstr(&buf, message);
3829
3830 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3831 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3832 }
3833
3834 /* Append "fake" message parts as needed */
3835 if (opt->show_notes) {
3836 if (!buf.len)
3837 strbuf_addstr(&buf, message);
3838 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3839 }
3840
3841 /*
3842 * Find either in the original commit message, or in the temporary.
3843 * Note that we cast away the constness of "message" here. It is
3844 * const because it may come from the cached commit buffer. That's OK,
3845 * because we know that it is modifiable heap memory, and that while
3846 * grep_buffer may modify it for speed, it will restore any
3847 * changes before returning.
3848 */
3849 if (buf.len)
3850 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3851 else
3852 retval = grep_buffer(&opt->grep_filter,
3853 (char *)message, strlen(message));
3854 strbuf_release(&buf);
3855 unuse_commit_buffer(commit, message);
3856 return retval;
3857}
3858
3859static inline int want_ancestry(const struct rev_info *revs)
3860{
3861 return (revs->rewrite_parents || revs->children.name);
3862}
3863
3864/*
3865 * Return a timestamp to be used for --since/--until comparisons for this
3866 * commit, based on the revision options.
3867 */
3868static timestamp_t comparison_date(const struct rev_info *revs,
3869 struct commit *commit)
3870{
3871 return revs->reflog_info ?
3872 get_reflog_timestamp(revs->reflog_info) :
3873 commit->date;
3874}
3875
3876enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3877{
3878 if (commit->object.flags & SHOWN)
3879 return commit_ignore;
3880 if (revs->unpacked && has_object_pack(&commit->object.oid))
3881 return commit_ignore;
3882 if (revs->no_kept_objects) {
3883 if (has_object_kept_pack(&commit->object.oid,
3884 revs->keep_pack_cache_flags))
3885 return commit_ignore;
3886 }
3887 if (commit->object.flags & UNINTERESTING)
3888 return commit_ignore;
3889 if (revs->line_level_traverse && !want_ancestry(revs)) {
3890 /*
3891 * In case of line-level log with parent rewriting
3892 * prepare_revision_walk() already took care of all line-level
3893 * log filtering, and there is nothing left to do here.
3894 *
3895 * If parent rewriting was not requested, then this is the
3896 * place to perform the line-level log filtering. Notably,
3897 * this check, though expensive, must come before the other,
3898 * cheaper filtering conditions, because the tracked line
3899 * ranges must be adjusted even when the commit will end up
3900 * being ignored based on other conditions.
3901 */
3902 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3903 return commit_ignore;
3904 }
3905 if (revs->min_age != -1 &&
3906 comparison_date(revs, commit) > revs->min_age)
3907 return commit_ignore;
3908 if (revs->min_parents || (revs->max_parents >= 0)) {
3909 int n = commit_list_count(commit->parents);
3910 if ((n < revs->min_parents) ||
3911 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3912 return commit_ignore;
3913 }
3914 if (!commit_match(commit, revs))
3915 return commit_ignore;
3916 if (revs->prune && revs->dense) {
3917 /* Commit without changes? */
3918 if (commit->object.flags & TREESAME) {
3919 int n;
3920 struct commit_list *p;
3921 /* drop merges unless we want parenthood */
3922 if (!want_ancestry(revs))
3923 return commit_ignore;
3924
3925 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3926 return commit_show;
3927
3928 /*
3929 * If we want ancestry, then need to keep any merges
3930 * between relevant commits to tie together topology.
3931 * For consistency with TREESAME and simplification
3932 * use "relevant" here rather than just INTERESTING,
3933 * to treat bottom commit(s) as part of the topology.
3934 */
3935 for (n = 0, p = commit->parents; p; p = p->next)
3936 if (relevant_commit(p->item))
3937 if (++n >= 2)
3938 return commit_show;
3939 return commit_ignore;
3940 }
3941 }
3942 return commit_show;
3943}
3944
3945define_commit_slab(saved_parents, struct commit_list *);
3946
3947#define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3948
3949/*
3950 * You may only call save_parents() once per commit (this is checked
3951 * for non-root commits).
3952 */
3953static void save_parents(struct rev_info *revs, struct commit *commit)
3954{
3955 struct commit_list **pp;
3956
3957 if (!revs->saved_parents_slab) {
3958 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3959 init_saved_parents(revs->saved_parents_slab);
3960 }
3961
3962 pp = saved_parents_at(revs->saved_parents_slab, commit);
3963
3964 /*
3965 * When walking with reflogs, we may visit the same commit
3966 * several times: once for each appearance in the reflog.
3967 *
3968 * In this case, save_parents() will be called multiple times.
3969 * We want to keep only the first set of parents. We need to
3970 * store a sentinel value for an empty (i.e., NULL) parent
3971 * list to distinguish it from a not-yet-saved list, however.
3972 */
3973 if (*pp)
3974 return;
3975 if (commit->parents)
3976 *pp = copy_commit_list(commit->parents);
3977 else
3978 *pp = EMPTY_PARENT_LIST;
3979}
3980
3981static void free_saved_parents(struct rev_info *revs)
3982{
3983 if (revs->saved_parents_slab)
3984 clear_saved_parents(revs->saved_parents_slab);
3985}
3986
3987struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3988{
3989 struct commit_list *parents;
3990
3991 if (!revs->saved_parents_slab)
3992 return commit->parents;
3993
3994 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3995 if (parents == EMPTY_PARENT_LIST)
3996 return NULL;
3997 return parents;
3998}
3999
4000enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
4001{
4002 enum commit_action action = get_commit_action(revs, commit);
4003
4004 if (action == commit_show &&
4005 revs->prune && revs->dense && want_ancestry(revs)) {
4006 /*
4007 * --full-diff on simplified parents is no good: it
4008 * will show spurious changes from the commits that
4009 * were elided. So we save the parents on the side
4010 * when --full-diff is in effect.
4011 */
4012 if (revs->full_diff)
4013 save_parents(revs, commit);
4014 if (rewrite_parents(revs, commit, rewrite_one) < 0)
4015 return commit_error;
4016 }
4017 return action;
4018}
4019
4020static void track_linear(struct rev_info *revs, struct commit *commit)
4021{
4022 if (revs->track_first_time) {
4023 revs->linear = 1;
4024 revs->track_first_time = 0;
4025 } else {
4026 struct commit_list *p;
4027 for (p = revs->previous_parents; p; p = p->next)
4028 if (p->item == NULL || /* first commit */
4029 oideq(&p->item->object.oid, &commit->object.oid))
4030 break;
4031 revs->linear = p != NULL;
4032 }
4033 if (revs->reverse) {
4034 if (revs->linear)
4035 commit->object.flags |= TRACK_LINEAR;
4036 }
4037 free_commit_list(revs->previous_parents);
4038 revs->previous_parents = copy_commit_list(commit->parents);
4039}
4040
4041static struct commit *get_revision_1(struct rev_info *revs)
4042{
4043 while (1) {
4044 struct commit *commit;
4045
4046 if (revs->reflog_info)
4047 commit = next_reflog_entry(revs->reflog_info);
4048 else if (revs->topo_walk_info)
4049 commit = next_topo_commit(revs);
4050 else
4051 commit = pop_commit(&revs->commits);
4052
4053 if (!commit)
4054 return NULL;
4055
4056 if (revs->reflog_info)
4057 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
4058
4059 /*
4060 * If we haven't done the list limiting, we need to look at
4061 * the parents here. We also need to do the date-based limiting
4062 * that we'd otherwise have done in limit_list().
4063 */
4064 if (!revs->limited) {
4065 if (revs->max_age != -1 &&
4066 comparison_date(revs, commit) < revs->max_age)
4067 continue;
4068
4069 if (revs->reflog_info)
4070 try_to_simplify_commit(revs, commit);
4071 else if (revs->topo_walk_info)
4072 expand_topo_walk(revs, commit);
4073 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4074 if (!revs->ignore_missing_links)
4075 die("Failed to traverse parents of commit %s",
4076 oid_to_hex(&commit->object.oid));
4077 }
4078 }
4079
4080 switch (simplify_commit(revs, commit)) {
4081 case commit_ignore:
4082 continue;
4083 case commit_error:
4084 die("Failed to simplify parents of commit %s",
4085 oid_to_hex(&commit->object.oid));
4086 default:
4087 if (revs->track_linear)
4088 track_linear(revs, commit);
4089 return commit;
4090 }
4091 }
4092}
4093
4094/*
4095 * Return true for entries that have not yet been shown. (This is an
4096 * object_array_each_func_t.)
4097 */
4098static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
4099{
4100 return !(entry->item->flags & SHOWN);
4101}
4102
4103/*
4104 * If array is on the verge of a realloc, garbage-collect any entries
4105 * that have already been shown to try to free up some space.
4106 */
4107static void gc_boundary(struct object_array *array)
4108{
4109 if (array->nr == array->alloc)
4110 object_array_filter(array, entry_unshown, NULL);
4111}
4112
4113static void create_boundary_commit_list(struct rev_info *revs)
4114{
4115 unsigned i;
4116 struct commit *c;
4117 struct object_array *array = &revs->boundary_commits;
4118 struct object_array_entry *objects = array->objects;
4119
4120 /*
4121 * If revs->commits is non-NULL at this point, an error occurred in
4122 * get_revision_1(). Ignore the error and continue printing the
4123 * boundary commits anyway. (This is what the code has always
4124 * done.)
4125 */
4126 free_commit_list(revs->commits);
4127 revs->commits = NULL;
4128
4129 /*
4130 * Put all of the actual boundary commits from revs->boundary_commits
4131 * into revs->commits
4132 */
4133 for (i = 0; i < array->nr; i++) {
4134 c = (struct commit *)(objects[i].item);
4135 if (!c)
4136 continue;
4137 if (!(c->object.flags & CHILD_SHOWN))
4138 continue;
4139 if (c->object.flags & (SHOWN | BOUNDARY))
4140 continue;
4141 c->object.flags |= BOUNDARY;
4142 commit_list_insert(c, &revs->commits);
4143 }
4144
4145 /*
4146 * If revs->topo_order is set, sort the boundary commits
4147 * in topological order
4148 */
4149 sort_in_topological_order(&revs->commits, revs->sort_order);
4150}
4151
4152static struct commit *get_revision_internal(struct rev_info *revs)
4153{
4154 struct commit *c = NULL;
4155 struct commit_list *l;
4156
4157 if (revs->boundary == 2) {
4158 /*
4159 * All of the normal commits have already been returned,
4160 * and we are now returning boundary commits.
4161 * create_boundary_commit_list() has populated
4162 * revs->commits with the remaining commits to return.
4163 */
4164 c = pop_commit(&revs->commits);
4165 if (c)
4166 c->object.flags |= SHOWN;
4167 return c;
4168 }
4169
4170 /*
4171 * If our max_count counter has reached zero, then we are done. We
4172 * don't simply return NULL because we still might need to show
4173 * boundary commits. But we want to avoid calling get_revision_1, which
4174 * might do a considerable amount of work finding the next commit only
4175 * for us to throw it away.
4176 *
4177 * If it is non-zero, then either we don't have a max_count at all
4178 * (-1), or it is still counting, in which case we decrement.
4179 */
4180 if (revs->max_count) {
4181 c = get_revision_1(revs);
4182 if (c) {
4183 while (revs->skip_count > 0) {
4184 revs->skip_count--;
4185 c = get_revision_1(revs);
4186 if (!c)
4187 break;
4188 }
4189 }
4190
4191 if (revs->max_count > 0)
4192 revs->max_count--;
4193 }
4194
4195 if (c)
4196 c->object.flags |= SHOWN;
4197
4198 if (!revs->boundary)
4199 return c;
4200
4201 if (!c) {
4202 /*
4203 * get_revision_1() runs out the commits, and
4204 * we are done computing the boundaries.
4205 * switch to boundary commits output mode.
4206 */
4207 revs->boundary = 2;
4208
4209 /*
4210 * Update revs->commits to contain the list of
4211 * boundary commits.
4212 */
4213 create_boundary_commit_list(revs);
4214
4215 return get_revision_internal(revs);
4216 }
4217
4218 /*
4219 * boundary commits are the commits that are parents of the
4220 * ones we got from get_revision_1() but they themselves are
4221 * not returned from get_revision_1(). Before returning
4222 * 'c', we need to mark its parents that they could be boundaries.
4223 */
4224
4225 for (l = c->parents; l; l = l->next) {
4226 struct object *p;
4227 p = &(l->item->object);
4228 if (p->flags & (CHILD_SHOWN | SHOWN))
4229 continue;
4230 p->flags |= CHILD_SHOWN;
4231 gc_boundary(&revs->boundary_commits);
4232 add_object_array(p, NULL, &revs->boundary_commits);
4233 }
4234
4235 return c;
4236}
4237
4238struct commit *get_revision(struct rev_info *revs)
4239{
4240 struct commit *c;
4241 struct commit_list *reversed;
4242
4243 if (revs->reverse) {
4244 reversed = NULL;
4245 while ((c = get_revision_internal(revs)))
4246 commit_list_insert(c, &reversed);
4247 revs->commits = reversed;
4248 revs->reverse = 0;
4249 revs->reverse_output_stage = 1;
4250 }
4251
4252 if (revs->reverse_output_stage) {
4253 c = pop_commit(&revs->commits);
4254 if (revs->track_linear)
4255 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4256 return c;
4257 }
4258
4259 c = get_revision_internal(revs);
4260 if (c && revs->graph)
4261 graph_update(revs->graph, c);
4262 if (!c) {
4263 free_saved_parents(revs);
4264 free_commit_list(revs->previous_parents);
4265 revs->previous_parents = NULL;
4266 }
4267 return c;
4268}
4269
4270const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4271{
4272 if (commit->object.flags & BOUNDARY)
4273 return "-";
4274 else if (commit->object.flags & UNINTERESTING)
4275 return "^";
4276 else if (commit->object.flags & PATCHSAME)
4277 return "=";
4278 else if (!revs || revs->left_right) {
4279 if (commit->object.flags & SYMMETRIC_LEFT)
4280 return "<";
4281 else
4282 return ">";
4283 } else if (revs->graph)
4284 return "*";
4285 else if (revs->cherry_mark)
4286 return "+";
4287 return "";
4288}
4289
4290void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4291{
4292 const char *mark = get_revision_mark(revs, commit);
4293 if (!strlen(mark))
4294 return;
4295 fputs(mark, stdout);
4296 putchar(' ');
4297}