2 #include "object-store.h"
8 #include "diff-merges.h"
11 #include "repository.h"
14 #include "reflog-walk.h"
15 #include "patch-ids.h"
18 #include "string-list.h"
21 #include "commit-slab.h"
23 #include "cache-tree.h"
28 #include "commit-reach.h"
29 #include "commit-graph.h"
30 #include "prio-queue.h"
34 #include "json-writer.h"
36 volatile show_early_output_fn_t show_early_output;
38 static const char *term_bad;
39 static const char *term_good;
41 implement_shared_commit_slab(revision_sources, char *);
43 static inline int want_ancestry(const struct rev_info *revs);
45 void show_object_with_name(FILE *out, struct object *obj, const char *name)
49 fprintf(out, "%s ", oid_to_hex(&obj->oid));
50 for (p = name; *p && *p != '\n'; p++)
55 static void mark_blob_uninteresting(struct blob *blob)
59 if (blob->object.flags & UNINTERESTING)
61 blob->object.flags |= UNINTERESTING;
64 static void mark_tree_contents_uninteresting(struct repository *r,
67 struct tree_desc desc;
68 struct name_entry entry;
70 if (parse_tree_gently(tree, 1) < 0)
73 init_tree_desc(&desc, tree->buffer, tree->size);
74 while (tree_entry(&desc, &entry)) {
75 switch (object_type(entry.mode)) {
77 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
80 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
83 /* Subproject commit - not in this repository */
89 * We don't care about the tree any more
90 * after it has been marked uninteresting.
92 free_tree_buffer(tree);
95 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
103 if (obj->flags & UNINTERESTING)
105 obj->flags |= UNINTERESTING;
106 mark_tree_contents_uninteresting(r, tree);
109 struct path_and_oids_entry {
110 struct hashmap_entry ent;
115 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
116 const struct hashmap_entry *eptr,
117 const struct hashmap_entry *entry_or_key,
120 const struct path_and_oids_entry *e1, *e2;
122 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
123 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
125 return strcmp(e1->path, e2->path);
128 static void paths_and_oids_clear(struct hashmap *map)
130 struct hashmap_iter iter;
131 struct path_and_oids_entry *entry;
133 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
134 oidset_clear(&entry->trees);
138 hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
141 static void paths_and_oids_insert(struct hashmap *map,
143 const struct object_id *oid)
145 int hash = strhash(path);
146 struct path_and_oids_entry key;
147 struct path_and_oids_entry *entry;
149 hashmap_entry_init(&key.ent, hash);
151 /* use a shallow copy for the lookup */
152 key.path = (char *)path;
153 oidset_init(&key.trees, 0);
155 entry = hashmap_get_entry(map, &key, ent, NULL);
157 CALLOC_ARRAY(entry, 1);
158 hashmap_entry_init(&entry->ent, hash);
159 entry->path = xstrdup(key.path);
160 oidset_init(&entry->trees, 16);
161 hashmap_put(map, &entry->ent);
164 oidset_insert(&entry->trees, oid);
167 static void add_children_by_path(struct repository *r,
171 struct tree_desc desc;
172 struct name_entry entry;
177 if (parse_tree_gently(tree, 1) < 0)
180 init_tree_desc(&desc, tree->buffer, tree->size);
181 while (tree_entry(&desc, &entry)) {
182 switch (object_type(entry.mode)) {
184 paths_and_oids_insert(map, entry.path, &entry.oid);
186 if (tree->object.flags & UNINTERESTING) {
187 struct tree *child = lookup_tree(r, &entry.oid);
189 child->object.flags |= UNINTERESTING;
193 if (tree->object.flags & UNINTERESTING) {
194 struct blob *child = lookup_blob(r, &entry.oid);
196 child->object.flags |= UNINTERESTING;
200 /* Subproject commit - not in this repository */
205 free_tree_buffer(tree);
208 void mark_trees_uninteresting_sparse(struct repository *r,
209 struct oidset *trees)
211 unsigned has_interesting = 0, has_uninteresting = 0;
212 struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
213 struct hashmap_iter map_iter;
214 struct path_and_oids_entry *entry;
215 struct object_id *oid;
216 struct oidset_iter iter;
218 oidset_iter_init(trees, &iter);
219 while ((!has_interesting || !has_uninteresting) &&
220 (oid = oidset_iter_next(&iter))) {
221 struct tree *tree = lookup_tree(r, oid);
226 if (tree->object.flags & UNINTERESTING)
227 has_uninteresting = 1;
232 /* Do not walk unless we have both types of trees. */
233 if (!has_uninteresting || !has_interesting)
236 oidset_iter_init(trees, &iter);
237 while ((oid = oidset_iter_next(&iter))) {
238 struct tree *tree = lookup_tree(r, oid);
239 add_children_by_path(r, tree, &map);
242 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
243 mark_trees_uninteresting_sparse(r, &entry->trees);
245 paths_and_oids_clear(&map);
248 struct commit_stack {
249 struct commit **items;
252 #define COMMIT_STACK_INIT { NULL, 0, 0 }
254 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
256 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
257 stack->items[stack->nr++] = commit;
260 static struct commit *commit_stack_pop(struct commit_stack *stack)
262 return stack->nr ? stack->items[--stack->nr] : NULL;
265 static void commit_stack_clear(struct commit_stack *stack)
267 FREE_AND_NULL(stack->items);
268 stack->nr = stack->alloc = 0;
271 static void mark_one_parent_uninteresting(struct commit *commit,
272 struct commit_stack *pending)
274 struct commit_list *l;
276 if (commit->object.flags & UNINTERESTING)
278 commit->object.flags |= UNINTERESTING;
281 * Normally we haven't parsed the parent
282 * yet, so we won't have a parent of a parent
283 * here. However, it may turn out that we've
284 * reached this commit some other way (where it
285 * wasn't uninteresting), in which case we need
286 * to mark its parents recursively too..
288 for (l = commit->parents; l; l = l->next)
289 commit_stack_push(pending, l->item);
292 void mark_parents_uninteresting(struct commit *commit)
294 struct commit_stack pending = COMMIT_STACK_INIT;
295 struct commit_list *l;
297 for (l = commit->parents; l; l = l->next)
298 mark_one_parent_uninteresting(l->item, &pending);
300 while (pending.nr > 0)
301 mark_one_parent_uninteresting(commit_stack_pop(&pending),
304 commit_stack_clear(&pending);
307 static void add_pending_object_with_path(struct rev_info *revs,
309 const char *name, unsigned mode,
312 struct interpret_branch_name_options options = { 0 };
315 if (revs->no_walk && (obj->flags & UNINTERESTING))
317 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
318 struct strbuf buf = STRBUF_INIT;
319 int len = interpret_branch_name(name, 0, &buf, &options);
321 if (0 < len && name[len] && buf.len)
322 strbuf_addstr(&buf, name + len);
323 add_reflog_for_walk(revs->reflog_info,
324 (struct commit *)obj,
325 buf.buf[0] ? buf.buf: name);
326 strbuf_release(&buf);
327 return; /* do not add the commit itself */
329 add_object_array_with_path(obj, name, &revs->pending, mode, path);
332 static void add_pending_object_with_mode(struct rev_info *revs,
334 const char *name, unsigned mode)
336 add_pending_object_with_path(revs, obj, name, mode, NULL);
339 void add_pending_object(struct rev_info *revs,
340 struct object *obj, const char *name)
342 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
345 void add_head_to_pending(struct rev_info *revs)
347 struct object_id oid;
349 if (get_oid("HEAD", &oid))
351 obj = parse_object(revs->repo, &oid);
354 add_pending_object(revs, obj, "HEAD");
357 static struct object *get_reference(struct rev_info *revs, const char *name,
358 const struct object_id *oid,
361 struct object *object;
364 * If the repository has commit graphs, repo_parse_commit() avoids
365 * reading the object buffer, so use it whenever possible.
367 if (oid_object_info(revs->repo, oid, NULL) == OBJ_COMMIT) {
368 struct commit *c = lookup_commit(revs->repo, oid);
369 if (!repo_parse_commit(revs->repo, c))
370 object = (struct object *) c;
374 object = parse_object(revs->repo, oid);
378 if (revs->ignore_missing)
380 if (revs->exclude_promisor_objects && is_promisor_object(oid))
382 die("bad object %s", name);
384 object->flags |= flags;
388 void add_pending_oid(struct rev_info *revs, const char *name,
389 const struct object_id *oid, unsigned int flags)
391 struct object *object = get_reference(revs, name, oid, flags);
392 add_pending_object(revs, object, name);
395 static struct commit *handle_commit(struct rev_info *revs,
396 struct object_array_entry *entry)
398 struct object *object = entry->item;
399 const char *name = entry->name;
400 const char *path = entry->path;
401 unsigned int mode = entry->mode;
402 unsigned long flags = object->flags;
405 * Tag object? Look what it points to..
407 while (object->type == OBJ_TAG) {
408 struct tag *tag = (struct tag *) object;
409 if (revs->tag_objects && !(flags & UNINTERESTING))
410 add_pending_object(revs, object, tag->tag);
411 object = parse_object(revs->repo, get_tagged_oid(tag));
413 if (revs->ignore_missing_links || (flags & UNINTERESTING))
415 if (revs->exclude_promisor_objects &&
416 is_promisor_object(&tag->tagged->oid))
418 die("bad object %s", oid_to_hex(&tag->tagged->oid));
420 object->flags |= flags;
422 * We'll handle the tagged object by looping or dropping
423 * through to the non-tag handlers below. Do not
424 * propagate path data from the tag's pending entry.
431 * Commit object? Just return it, we'll do all the complex
434 if (object->type == OBJ_COMMIT) {
435 struct commit *commit = (struct commit *)object;
437 if (repo_parse_commit(revs->repo, commit) < 0)
438 die("unable to parse commit %s", name);
439 if (flags & UNINTERESTING) {
440 mark_parents_uninteresting(commit);
442 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
446 char **slot = revision_sources_at(revs->sources, commit);
449 *slot = xstrdup(name);
455 * Tree object? Either mark it uninteresting, or add it
456 * to the list of objects to look at later..
458 if (object->type == OBJ_TREE) {
459 struct tree *tree = (struct tree *)object;
460 if (!revs->tree_objects)
462 if (flags & UNINTERESTING) {
463 mark_tree_contents_uninteresting(revs->repo, tree);
466 add_pending_object_with_path(revs, object, name, mode, path);
471 * Blob object? You know the drill by now..
473 if (object->type == OBJ_BLOB) {
474 if (!revs->blob_objects)
476 if (flags & UNINTERESTING)
478 add_pending_object_with_path(revs, object, name, mode, path);
481 die("%s is unknown object", name);
484 static int everybody_uninteresting(struct commit_list *orig,
485 struct commit **interesting_cache)
487 struct commit_list *list = orig;
489 if (*interesting_cache) {
490 struct commit *commit = *interesting_cache;
491 if (!(commit->object.flags & UNINTERESTING))
496 struct commit *commit = list->item;
498 if (commit->object.flags & UNINTERESTING)
501 *interesting_cache = commit;
508 * A definition of "relevant" commit that we can use to simplify limited graphs
509 * by eliminating side branches.
511 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
512 * in our list), or that is a specified BOTTOM commit. Then after computing
513 * a limited list, during processing we can generally ignore boundary merges
514 * coming from outside the graph, (ie from irrelevant parents), and treat
515 * those merges as if they were single-parent. TREESAME is defined to consider
516 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
517 * we don't care if we were !TREESAME to non-graph parents.
519 * Treating bottom commits as relevant ensures that a limited graph's
520 * connection to the actual bottom commit is not viewed as a side branch, but
521 * treated as part of the graph. For example:
523 * ....Z...A---X---o---o---B
527 * When computing "A..B", the A-X connection is at least as important as
528 * Y-X, despite A being flagged UNINTERESTING.
530 * And when computing --ancestry-path "A..B", the A-X connection is more
531 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
533 static inline int relevant_commit(struct commit *commit)
535 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
539 * Return a single relevant commit from a parent list. If we are a TREESAME
540 * commit, and this selects one of our parents, then we can safely simplify to
543 static struct commit *one_relevant_parent(const struct rev_info *revs,
544 struct commit_list *orig)
546 struct commit_list *list = orig;
547 struct commit *relevant = NULL;
553 * For 1-parent commits, or if first-parent-only, then return that
554 * first parent (even if not "relevant" by the above definition).
555 * TREESAME will have been set purely on that parent.
557 if (revs->first_parent_only || !orig->next)
561 * For multi-parent commits, identify a sole relevant parent, if any.
562 * If we have only one relevant parent, then TREESAME will be set purely
563 * with regard to that parent, and we can simplify accordingly.
565 * If we have more than one relevant parent, or no relevant parents
566 * (and multiple irrelevant ones), then we can't select a parent here
570 struct commit *commit = list->item;
572 if (relevant_commit(commit)) {
582 * The goal is to get REV_TREE_NEW as the result only if the
583 * diff consists of all '+' (and no other changes), REV_TREE_OLD
584 * if the whole diff is removal of old data, and otherwise
585 * REV_TREE_DIFFERENT (of course if the trees are the same we
586 * want REV_TREE_SAME).
588 * The only time we care about the distinction is when
589 * remove_empty_trees is in effect, in which case we care only about
590 * whether the whole change is REV_TREE_NEW, or if there's another type
591 * of change. Which means we can stop the diff early in either of these
594 * 1. We're not using remove_empty_trees at all.
596 * 2. We saw anything except REV_TREE_NEW.
598 static int tree_difference = REV_TREE_SAME;
600 static void file_add_remove(struct diff_options *options,
601 int addremove, unsigned mode,
602 const struct object_id *oid,
604 const char *fullpath, unsigned dirty_submodule)
606 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
607 struct rev_info *revs = options->change_fn_data;
609 tree_difference |= diff;
610 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
611 options->flags.has_changes = 1;
614 static void file_change(struct diff_options *options,
615 unsigned old_mode, unsigned new_mode,
616 const struct object_id *old_oid,
617 const struct object_id *new_oid,
618 int old_oid_valid, int new_oid_valid,
619 const char *fullpath,
620 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
622 tree_difference = REV_TREE_DIFFERENT;
623 options->flags.has_changes = 1;
626 static int bloom_filter_atexit_registered;
627 static unsigned int count_bloom_filter_maybe;
628 static unsigned int count_bloom_filter_definitely_not;
629 static unsigned int count_bloom_filter_false_positive;
630 static unsigned int count_bloom_filter_not_present;
632 static void trace2_bloom_filter_statistics_atexit(void)
634 struct json_writer jw = JSON_WRITER_INIT;
636 jw_object_begin(&jw, 0);
637 jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
638 jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
639 jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
640 jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
643 trace2_data_json("bloom", the_repository, "statistics", &jw);
648 static int forbid_bloom_filters(struct pathspec *spec)
650 if (spec->has_wildcard)
654 if (spec->magic & ~PATHSPEC_LITERAL)
656 if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
662 static void prepare_to_use_bloom_filter(struct rev_info *revs)
664 struct pathspec_item *pi;
665 char *path_alloc = NULL;
666 const char *path, *p;
668 int path_component_nr = 1;
673 if (forbid_bloom_filters(&revs->prune_data))
676 repo_parse_commit(revs->repo, revs->commits->item);
678 revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
679 if (!revs->bloom_filter_settings)
682 if (!revs->pruning.pathspec.nr)
685 pi = &revs->pruning.pathspec.items[0];
687 /* remove single trailing slash from path, if needed */
688 if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
689 path_alloc = xmemdupz(pi->match, pi->len - 1);
696 revs->bloom_filter_settings = NULL;
704 * At this point, the path is normalized to use Unix-style
705 * path separators. This is required due to how the
706 * changed-path Bloom filters store the paths.
713 revs->bloom_keys_nr = path_component_nr;
714 ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
716 fill_bloom_key(path, len, &revs->bloom_keys[0],
717 revs->bloom_filter_settings);
718 path_component_nr = 1;
723 fill_bloom_key(path, p - path,
724 &revs->bloom_keys[path_component_nr++],
725 revs->bloom_filter_settings);
729 if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
730 atexit(trace2_bloom_filter_statistics_atexit);
731 bloom_filter_atexit_registered = 1;
737 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
738 struct commit *commit)
740 struct bloom_filter *filter;
743 if (!revs->repo->objects->commit_graph)
746 if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
749 filter = get_bloom_filter(revs->repo, commit);
752 count_bloom_filter_not_present++;
756 for (j = 0; result && j < revs->bloom_keys_nr; j++) {
757 result = bloom_filter_contains(filter,
758 &revs->bloom_keys[j],
759 revs->bloom_filter_settings);
763 count_bloom_filter_maybe++;
765 count_bloom_filter_definitely_not++;
770 static int rev_compare_tree(struct rev_info *revs,
771 struct commit *parent, struct commit *commit, int nth_parent)
773 struct tree *t1 = get_commit_tree(parent);
774 struct tree *t2 = get_commit_tree(commit);
782 if (revs->simplify_by_decoration) {
784 * If we are simplifying by decoration, then the commit
785 * is worth showing if it has a tag pointing at it.
787 if (get_name_decoration(&commit->object))
788 return REV_TREE_DIFFERENT;
790 * A commit that is not pointed by a tag is uninteresting
791 * if we are not limited by path. This means that you will
792 * see the usual "commits that touch the paths" plus any
793 * tagged commit by specifying both --simplify-by-decoration
796 if (!revs->prune_data.nr)
797 return REV_TREE_SAME;
800 if (revs->bloom_keys_nr && !nth_parent) {
801 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
804 return REV_TREE_SAME;
807 tree_difference = REV_TREE_SAME;
808 revs->pruning.flags.has_changes = 0;
809 diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
812 if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
813 count_bloom_filter_false_positive++;
815 return tree_difference;
818 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
820 struct tree *t1 = get_commit_tree(commit);
825 tree_difference = REV_TREE_SAME;
826 revs->pruning.flags.has_changes = 0;
827 diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
829 return tree_difference == REV_TREE_SAME;
832 struct treesame_state {
833 unsigned int nparents;
834 unsigned char treesame[FLEX_ARRAY];
837 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
839 unsigned n = commit_list_count(commit->parents);
840 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
842 add_decoration(&revs->treesame, &commit->object, st);
847 * Must be called immediately after removing the nth_parent from a commit's
848 * parent list, if we are maintaining the per-parent treesame[] decoration.
849 * This does not recalculate the master TREESAME flag - update_treesame()
850 * should be called to update it after a sequence of treesame[] modifications
851 * that may have affected it.
853 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
855 struct treesame_state *st;
858 if (!commit->parents) {
860 * Have just removed the only parent from a non-merge.
861 * Different handling, as we lack decoration.
864 die("compact_treesame %u", nth_parent);
865 old_same = !!(commit->object.flags & TREESAME);
866 if (rev_same_tree_as_empty(revs, commit))
867 commit->object.flags |= TREESAME;
869 commit->object.flags &= ~TREESAME;
873 st = lookup_decoration(&revs->treesame, &commit->object);
874 if (!st || nth_parent >= st->nparents)
875 die("compact_treesame %u", nth_parent);
877 old_same = st->treesame[nth_parent];
878 memmove(st->treesame + nth_parent,
879 st->treesame + nth_parent + 1,
880 st->nparents - nth_parent - 1);
883 * If we've just become a non-merge commit, update TREESAME
884 * immediately, and remove the no-longer-needed decoration.
885 * If still a merge, defer update until update_treesame().
887 if (--st->nparents == 1) {
888 if (commit->parents->next)
889 die("compact_treesame parents mismatch");
890 if (st->treesame[0] && revs->dense)
891 commit->object.flags |= TREESAME;
893 commit->object.flags &= ~TREESAME;
894 free(add_decoration(&revs->treesame, &commit->object, NULL));
900 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
902 if (commit->parents && commit->parents->next) {
904 struct treesame_state *st;
905 struct commit_list *p;
906 unsigned relevant_parents;
907 unsigned relevant_change, irrelevant_change;
909 st = lookup_decoration(&revs->treesame, &commit->object);
911 die("update_treesame %s", oid_to_hex(&commit->object.oid));
912 relevant_parents = 0;
913 relevant_change = irrelevant_change = 0;
914 for (p = commit->parents, n = 0; p; n++, p = p->next) {
915 if (relevant_commit(p->item)) {
916 relevant_change |= !st->treesame[n];
919 irrelevant_change |= !st->treesame[n];
921 if (relevant_parents ? relevant_change : irrelevant_change)
922 commit->object.flags &= ~TREESAME;
924 commit->object.flags |= TREESAME;
927 return commit->object.flags & TREESAME;
930 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
933 * TREESAME is irrelevant unless prune && dense;
934 * if simplify_history is set, we can't have a mixture of TREESAME and
935 * !TREESAME INTERESTING parents (and we don't have treesame[]
936 * decoration anyway);
937 * if first_parent_only is set, then the TREESAME flag is locked
938 * against the first parent (and again we lack treesame[] decoration).
940 return revs->prune && revs->dense &&
941 !revs->simplify_history &&
942 !revs->first_parent_only;
945 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
947 struct commit_list **pp, *parent;
948 struct treesame_state *ts = NULL;
949 int relevant_change = 0, irrelevant_change = 0;
950 int relevant_parents, nth_parent;
953 * If we don't do pruning, everything is interesting
958 if (!get_commit_tree(commit))
961 if (!commit->parents) {
962 if (rev_same_tree_as_empty(revs, commit))
963 commit->object.flags |= TREESAME;
968 * Normal non-merge commit? If we don't want to make the
969 * history dense, we consider it always to be a change..
971 if (!revs->dense && !commit->parents->next)
974 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
975 (parent = *pp) != NULL;
976 pp = &parent->next, nth_parent++) {
977 struct commit *p = parent->item;
978 if (relevant_commit(p))
981 if (nth_parent == 1) {
983 * This our second loop iteration - so we now know
984 * we're dealing with a merge.
986 * Do not compare with later parents when we care only about
987 * the first parent chain, in order to avoid derailing the
988 * traversal to follow a side branch that brought everything
989 * in the path we are limited to by the pathspec.
991 if (revs->first_parent_only)
994 * If this will remain a potentially-simplifiable
995 * merge, remember per-parent treesame if needed.
996 * Initialise the array with the comparison from our
999 if (revs->treesame.name &&
1000 !revs->simplify_history &&
1001 !(commit->object.flags & UNINTERESTING)) {
1002 ts = initialise_treesame(revs, commit);
1003 if (!(irrelevant_change || relevant_change))
1004 ts->treesame[0] = 1;
1007 if (repo_parse_commit(revs->repo, p) < 0)
1008 die("cannot simplify commit %s (because of %s)",
1009 oid_to_hex(&commit->object.oid),
1010 oid_to_hex(&p->object.oid));
1011 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
1013 if (!revs->simplify_history || !relevant_commit(p)) {
1014 /* Even if a merge with an uninteresting
1015 * side branch brought the entire change
1016 * we are interested in, we do not want
1017 * to lose the other branches of this
1018 * merge, so we just keep going.
1021 ts->treesame[nth_parent] = 1;
1024 parent->next = NULL;
1025 commit->parents = parent;
1028 * A merge commit is a "diversion" if it is not
1029 * TREESAME to its first parent but is TREESAME
1030 * to a later parent. In the simplified history,
1031 * we "divert" the history walk to the later
1032 * parent. These commits are shown when "show_pulls"
1033 * is enabled, so do not mark the object as
1036 if (!revs->show_pulls || !nth_parent)
1037 commit->object.flags |= TREESAME;
1042 if (revs->remove_empty_trees &&
1043 rev_same_tree_as_empty(revs, p)) {
1044 /* We are adding all the specified
1045 * paths from this parent, so the
1046 * history beyond this parent is not
1047 * interesting. Remove its parents
1048 * (they are grandparents for us).
1049 * IOW, we pretend this parent is a
1052 if (repo_parse_commit(revs->repo, p) < 0)
1053 die("cannot simplify commit %s (invalid %s)",
1054 oid_to_hex(&commit->object.oid),
1055 oid_to_hex(&p->object.oid));
1060 case REV_TREE_DIFFERENT:
1061 if (relevant_commit(p))
1062 relevant_change = 1;
1064 irrelevant_change = 1;
1067 commit->object.flags |= PULL_MERGE;
1071 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
1075 * TREESAME is straightforward for single-parent commits. For merge
1076 * commits, it is most useful to define it so that "irrelevant"
1077 * parents cannot make us !TREESAME - if we have any relevant
1078 * parents, then we only consider TREESAMEness with respect to them,
1079 * allowing irrelevant merges from uninteresting branches to be
1080 * simplified away. Only if we have only irrelevant parents do we
1081 * base TREESAME on them. Note that this logic is replicated in
1082 * update_treesame, which should be kept in sync.
1084 if (relevant_parents ? !relevant_change : !irrelevant_change)
1085 commit->object.flags |= TREESAME;
1088 static int process_parents(struct rev_info *revs, struct commit *commit,
1089 struct commit_list **list, struct prio_queue *queue)
1091 struct commit_list *parent = commit->parents;
1094 if (commit->object.flags & ADDED)
1096 commit->object.flags |= ADDED;
1098 if (revs->include_check &&
1099 !revs->include_check(commit, revs->include_check_data))
1103 * If the commit is uninteresting, don't try to
1104 * prune parents - we want the maximal uninteresting
1107 * Normally we haven't parsed the parent
1108 * yet, so we won't have a parent of a parent
1109 * here. However, it may turn out that we've
1110 * reached this commit some other way (where it
1111 * wasn't uninteresting), in which case we need
1112 * to mark its parents recursively too..
1114 if (commit->object.flags & UNINTERESTING) {
1116 struct commit *p = parent->item;
1117 parent = parent->next;
1119 p->object.flags |= UNINTERESTING;
1120 if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
1123 mark_parents_uninteresting(p);
1124 if (p->object.flags & SEEN)
1126 p->object.flags |= SEEN;
1128 commit_list_insert_by_date(p, list);
1130 prio_queue_put(queue, p);
1136 * Ok, the commit wasn't uninteresting. Try to
1137 * simplify the commit history and find the parent
1138 * that has no differences in the path set if one exists.
1140 try_to_simplify_commit(revs, commit);
1145 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
1147 for (parent = commit->parents; parent; parent = parent->next) {
1148 struct commit *p = parent->item;
1149 int gently = revs->ignore_missing_links ||
1150 revs->exclude_promisor_objects;
1151 if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
1152 if (revs->exclude_promisor_objects &&
1153 is_promisor_object(&p->object.oid)) {
1154 if (revs->first_parent_only)
1160 if (revs->sources) {
1161 char **slot = revision_sources_at(revs->sources, p);
1164 *slot = *revision_sources_at(revs->sources, commit);
1166 p->object.flags |= left_flag;
1167 if (!(p->object.flags & SEEN)) {
1168 p->object.flags |= SEEN;
1170 commit_list_insert_by_date(p, list);
1172 prio_queue_put(queue, p);
1174 if (revs->first_parent_only)
1180 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1182 struct commit_list *p;
1183 int left_count = 0, right_count = 0;
1185 struct patch_ids ids;
1186 unsigned cherry_flag;
1188 /* First count the commits on the left and on the right */
1189 for (p = list; p; p = p->next) {
1190 struct commit *commit = p->item;
1191 unsigned flags = commit->object.flags;
1192 if (flags & BOUNDARY)
1194 else if (flags & SYMMETRIC_LEFT)
1200 if (!left_count || !right_count)
1203 left_first = left_count < right_count;
1204 init_patch_ids(revs->repo, &ids);
1205 ids.diffopts.pathspec = revs->diffopt.pathspec;
1207 /* Compute patch-ids for one side */
1208 for (p = list; p; p = p->next) {
1209 struct commit *commit = p->item;
1210 unsigned flags = commit->object.flags;
1212 if (flags & BOUNDARY)
1215 * If we have fewer left, left_first is set and we omit
1216 * commits on the right branch in this loop. If we have
1217 * fewer right, we skip the left ones.
1219 if (left_first != !!(flags & SYMMETRIC_LEFT))
1221 add_commit_patch_id(commit, &ids);
1224 /* either cherry_mark or cherry_pick are true */
1225 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1227 /* Check the other side */
1228 for (p = list; p; p = p->next) {
1229 struct commit *commit = p->item;
1230 struct patch_id *id;
1231 unsigned flags = commit->object.flags;
1233 if (flags & BOUNDARY)
1236 * If we have fewer left, left_first is set and we omit
1237 * commits on the left branch in this loop.
1239 if (left_first == !!(flags & SYMMETRIC_LEFT))
1243 * Have we seen the same patch id?
1245 id = patch_id_iter_first(commit, &ids);
1249 commit->object.flags |= cherry_flag;
1251 id->commit->object.flags |= cherry_flag;
1252 } while ((id = patch_id_iter_next(id, &ids)));
1255 free_patch_ids(&ids);
1258 /* How many extra uninteresting commits we want to see.. */
1261 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1262 struct commit **interesting_cache)
1265 * No source list at all? We're definitely done..
1271 * Does the destination list contain entries with a date
1272 * before the source list? Definitely _not_ done.
1274 if (date <= src->item->date)
1278 * Does the source list still have interesting commits in
1279 * it? Definitely not done..
1281 if (!everybody_uninteresting(src, interesting_cache))
1284 /* Ok, we're closing in.. */
1289 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1290 * of B but not ancestors of A but further limits the result to those
1291 * that are descendants of A. This takes the list of bottom commits and
1292 * the result of "A..B" without --ancestry-path, and limits the latter
1293 * further to the ones that can reach one of the commits in "bottom".
1295 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
1297 struct commit_list *p;
1298 struct commit_list *rlist = NULL;
1302 * Reverse the list so that it will be likely that we would
1303 * process parents before children.
1305 for (p = list; p; p = p->next)
1306 commit_list_insert(p->item, &rlist);
1308 for (p = bottom; p; p = p->next)
1309 p->item->object.flags |= TMP_MARK;
1312 * Mark the ones that can reach bottom commits in "list",
1313 * in a bottom-up fashion.
1317 for (p = rlist; p; p = p->next) {
1318 struct commit *c = p->item;
1319 struct commit_list *parents;
1320 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1322 for (parents = c->parents;
1324 parents = parents->next) {
1325 if (!(parents->item->object.flags & TMP_MARK))
1327 c->object.flags |= TMP_MARK;
1332 } while (made_progress);
1335 * NEEDSWORK: decide if we want to remove parents that are
1336 * not marked with TMP_MARK from commit->parents for commits
1337 * in the resulting list. We may not want to do that, though.
1341 * The ones that are not marked with TMP_MARK are uninteresting
1343 for (p = list; p; p = p->next) {
1344 struct commit *c = p->item;
1345 if (c->object.flags & TMP_MARK)
1347 c->object.flags |= UNINTERESTING;
1350 /* We are done with the TMP_MARK */
1351 for (p = list; p; p = p->next)
1352 p->item->object.flags &= ~TMP_MARK;
1353 for (p = bottom; p; p = p->next)
1354 p->item->object.flags &= ~TMP_MARK;
1355 free_commit_list(rlist);
1359 * Before walking the history, keep the set of "negative" refs the
1360 * caller has asked to exclude.
1362 * This is used to compute "rev-list --ancestry-path A..B", as we need
1363 * to filter the result of "A..B" further to the ones that can actually
1366 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1368 struct commit_list *elem, *bottom = NULL;
1369 for (elem = list; elem; elem = elem->next)
1370 if (elem->item->object.flags & BOTTOM)
1371 commit_list_insert(elem->item, &bottom);
1375 /* Assumes either left_only or right_only is set */
1376 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1378 struct commit_list *p;
1380 for (p = list; p; p = p->next) {
1381 struct commit *commit = p->item;
1383 if (revs->right_only) {
1384 if (commit->object.flags & SYMMETRIC_LEFT)
1385 commit->object.flags |= SHOWN;
1386 } else /* revs->left_only is set */
1387 if (!(commit->object.flags & SYMMETRIC_LEFT))
1388 commit->object.flags |= SHOWN;
1392 static int limit_list(struct rev_info *revs)
1395 timestamp_t date = TIME_MAX;
1396 struct commit_list *list = revs->commits;
1397 struct commit_list *newlist = NULL;
1398 struct commit_list **p = &newlist;
1399 struct commit_list *bottom = NULL;
1400 struct commit *interesting_cache = NULL;
1402 if (revs->ancestry_path) {
1403 bottom = collect_bottom_commits(list);
1405 die("--ancestry-path given but there are no bottom commits");
1409 struct commit *commit = pop_commit(&list);
1410 struct object *obj = &commit->object;
1411 show_early_output_fn_t show;
1413 if (commit == interesting_cache)
1414 interesting_cache = NULL;
1416 if (revs->max_age != -1 && (commit->date < revs->max_age))
1417 obj->flags |= UNINTERESTING;
1418 if (process_parents(revs, commit, &list, NULL) < 0)
1420 if (obj->flags & UNINTERESTING) {
1421 mark_parents_uninteresting(commit);
1422 slop = still_interesting(list, date, slop, &interesting_cache);
1427 if (revs->min_age != -1 && (commit->date > revs->min_age) &&
1428 !revs->line_level_traverse)
1430 date = commit->date;
1431 p = &commit_list_insert(commit, p)->next;
1433 show = show_early_output;
1437 show(revs, newlist);
1438 show_early_output = NULL;
1440 if (revs->cherry_pick || revs->cherry_mark)
1441 cherry_pick_list(newlist, revs);
1443 if (revs->left_only || revs->right_only)
1444 limit_left_right(newlist, revs);
1447 limit_to_ancestry(bottom, newlist);
1448 free_commit_list(bottom);
1452 * Check if any commits have become TREESAME by some of their parents
1453 * becoming UNINTERESTING.
1455 if (limiting_can_increase_treesame(revs))
1456 for (list = newlist; list; list = list->next) {
1457 struct commit *c = list->item;
1458 if (c->object.flags & (UNINTERESTING | TREESAME))
1460 update_treesame(revs, c);
1463 revs->commits = newlist;
1468 * Add an entry to refs->cmdline with the specified information.
1471 static void add_rev_cmdline(struct rev_info *revs,
1472 struct object *item,
1477 struct rev_cmdline_info *info = &revs->cmdline;
1478 unsigned int nr = info->nr;
1480 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1481 info->rev[nr].item = item;
1482 info->rev[nr].name = xstrdup(name);
1483 info->rev[nr].whence = whence;
1484 info->rev[nr].flags = flags;
1488 static void add_rev_cmdline_list(struct rev_info *revs,
1489 struct commit_list *commit_list,
1493 while (commit_list) {
1494 struct object *object = &commit_list->item->object;
1495 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1497 commit_list = commit_list->next;
1501 struct all_refs_cb {
1503 int warned_bad_reflog;
1504 struct rev_info *all_revs;
1505 const char *name_for_errormsg;
1506 struct worktree *wt;
1509 int ref_excluded(struct string_list *ref_excludes, const char *path)
1511 struct string_list_item *item;
1515 for_each_string_list_item(item, ref_excludes) {
1516 if (!wildmatch(item->string, path, 0))
1522 static int handle_one_ref(const char *path, const struct object_id *oid,
1523 int flag, void *cb_data)
1525 struct all_refs_cb *cb = cb_data;
1526 struct object *object;
1528 if (ref_excluded(cb->all_revs->ref_excludes, path))
1531 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1532 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1533 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1537 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1540 cb->all_revs = revs;
1541 cb->all_flags = flags;
1542 revs->rev_input_given = 1;
1546 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1548 if (*ref_excludes_p) {
1549 string_list_clear(*ref_excludes_p, 0);
1550 free(*ref_excludes_p);
1552 *ref_excludes_p = NULL;
1555 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1557 if (!*ref_excludes_p) {
1558 CALLOC_ARRAY(*ref_excludes_p, 1);
1559 (*ref_excludes_p)->strdup_strings = 1;
1561 string_list_append(*ref_excludes_p, exclude);
1564 static void handle_refs(struct ref_store *refs,
1565 struct rev_info *revs, unsigned flags,
1566 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1568 struct all_refs_cb cb;
1571 /* this could happen with uninitialized submodules */
1575 init_all_refs_cb(&cb, revs, flags);
1576 for_each(refs, handle_one_ref, &cb);
1579 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1581 struct all_refs_cb *cb = cb_data;
1582 if (!is_null_oid(oid)) {
1583 struct object *o = parse_object(cb->all_revs->repo, oid);
1585 o->flags |= cb->all_flags;
1586 /* ??? CMDLINEFLAGS ??? */
1587 add_pending_object(cb->all_revs, o, "");
1589 else if (!cb->warned_bad_reflog) {
1590 warning("reflog of '%s' references pruned commits",
1591 cb->name_for_errormsg);
1592 cb->warned_bad_reflog = 1;
1597 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1598 const char *email, timestamp_t timestamp, int tz,
1599 const char *message, void *cb_data)
1601 handle_one_reflog_commit(ooid, cb_data);
1602 handle_one_reflog_commit(noid, cb_data);
1606 static int handle_one_reflog(const char *refname_in_wt,
1607 const struct object_id *oid,
1608 int flag, void *cb_data)
1610 struct all_refs_cb *cb = cb_data;
1611 struct strbuf refname = STRBUF_INIT;
1613 cb->warned_bad_reflog = 0;
1614 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1615 cb->name_for_errormsg = refname.buf;
1616 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1618 handle_one_reflog_ent, cb_data);
1619 strbuf_release(&refname);
1623 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1625 struct worktree **worktrees, **p;
1627 worktrees = get_worktrees();
1628 for (p = worktrees; *p; p++) {
1629 struct worktree *wt = *p;
1635 refs_for_each_reflog(get_worktree_ref_store(wt),
1639 free_worktrees(worktrees);
1642 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1644 struct all_refs_cb cb;
1647 cb.all_flags = flags;
1649 for_each_reflog(handle_one_reflog, &cb);
1651 if (!revs->single_worktree)
1652 add_other_reflogs_to_pending(&cb);
1655 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1656 struct strbuf *path, unsigned int flags)
1658 size_t baselen = path->len;
1661 if (it->entry_count >= 0) {
1662 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1663 tree->object.flags |= flags;
1664 add_pending_object_with_path(revs, &tree->object, "",
1668 for (i = 0; i < it->subtree_nr; i++) {
1669 struct cache_tree_sub *sub = it->down[i];
1670 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1671 add_cache_tree(sub->cache_tree, revs, path, flags);
1672 strbuf_setlen(path, baselen);
1677 static void do_add_index_objects_to_pending(struct rev_info *revs,
1678 struct index_state *istate,
1683 for (i = 0; i < istate->cache_nr; i++) {
1684 struct cache_entry *ce = istate->cache[i];
1687 if (S_ISGITLINK(ce->ce_mode))
1690 blob = lookup_blob(revs->repo, &ce->oid);
1692 die("unable to add index blob to traversal");
1693 blob->object.flags |= flags;
1694 add_pending_object_with_path(revs, &blob->object, "",
1695 ce->ce_mode, ce->name);
1698 if (istate->cache_tree) {
1699 struct strbuf path = STRBUF_INIT;
1700 add_cache_tree(istate->cache_tree, revs, &path, flags);
1701 strbuf_release(&path);
1705 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1707 struct worktree **worktrees, **p;
1709 repo_read_index(revs->repo);
1710 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1712 if (revs->single_worktree)
1715 worktrees = get_worktrees();
1716 for (p = worktrees; *p; p++) {
1717 struct worktree *wt = *p;
1718 struct index_state istate = { NULL };
1721 continue; /* current index already taken care of */
1723 if (read_index_from(&istate,
1724 worktree_git_path(wt, "index"),
1725 get_worktree_git_dir(wt)) > 0)
1726 do_add_index_objects_to_pending(revs, &istate, flags);
1727 discard_index(&istate);
1729 free_worktrees(worktrees);
1732 struct add_alternate_refs_data {
1733 struct rev_info *revs;
1737 static void add_one_alternate_ref(const struct object_id *oid,
1740 const char *name = ".alternate";
1741 struct add_alternate_refs_data *data = vdata;
1744 obj = get_reference(data->revs, name, oid, data->flags);
1745 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1746 add_pending_object(data->revs, obj, name);
1749 static void add_alternate_refs_to_pending(struct rev_info *revs,
1752 struct add_alternate_refs_data data;
1755 for_each_alternate_ref(add_one_alternate_ref, &data);
1758 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1761 struct object_id oid;
1763 struct commit *commit;
1764 struct commit_list *parents;
1766 const char *arg = arg_;
1769 flags ^= UNINTERESTING | BOTTOM;
1772 if (get_oid_committish(arg, &oid))
1775 it = get_reference(revs, arg, &oid, 0);
1776 if (!it && revs->ignore_missing)
1778 if (it->type != OBJ_TAG)
1780 if (!((struct tag*)it)->tagged)
1782 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1784 if (it->type != OBJ_COMMIT)
1786 commit = (struct commit *)it;
1787 if (exclude_parent &&
1788 exclude_parent > commit_list_count(commit->parents))
1790 for (parents = commit->parents, parent_number = 1;
1792 parents = parents->next, parent_number++) {
1793 if (exclude_parent && parent_number != exclude_parent)
1796 it = &parents->item->object;
1798 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1799 add_pending_object(revs, it, arg);
1804 void repo_init_revisions(struct repository *r,
1805 struct rev_info *revs,
1808 memset(revs, 0, sizeof(*revs));
1811 revs->abbrev = DEFAULT_ABBREV;
1812 revs->simplify_history = 1;
1813 revs->pruning.repo = r;
1814 revs->pruning.flags.recursive = 1;
1815 revs->pruning.flags.quick = 1;
1816 revs->pruning.add_remove = file_add_remove;
1817 revs->pruning.change = file_change;
1818 revs->pruning.change_fn_data = revs;
1819 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1821 revs->prefix = prefix;
1824 revs->skip_count = -1;
1825 revs->max_count = -1;
1826 revs->max_parents = -1;
1827 revs->expand_tabs_in_log = -1;
1829 revs->commit_format = CMIT_FMT_DEFAULT;
1830 revs->expand_tabs_in_log_default = 8;
1832 grep_init(&revs->grep_filter, revs->repo, prefix);
1833 revs->grep_filter.status_only = 1;
1835 repo_diff_setup(revs->repo, &revs->diffopt);
1836 if (prefix && !revs->diffopt.prefix) {
1837 revs->diffopt.prefix = prefix;
1838 revs->diffopt.prefix_length = strlen(prefix);
1841 init_display_notes(&revs->notes_opt);
1844 static void add_pending_commit_list(struct rev_info *revs,
1845 struct commit_list *commit_list,
1848 while (commit_list) {
1849 struct object *object = &commit_list->item->object;
1850 object->flags |= flags;
1851 add_pending_object(revs, object, oid_to_hex(&object->oid));
1852 commit_list = commit_list->next;
1856 static void prepare_show_merge(struct rev_info *revs)
1858 struct commit_list *bases;
1859 struct commit *head, *other;
1860 struct object_id oid;
1861 const char **prune = NULL;
1862 int i, prune_num = 1; /* counting terminating NULL */
1863 struct index_state *istate = revs->repo->index;
1865 if (get_oid("HEAD", &oid))
1866 die("--merge without HEAD?");
1867 head = lookup_commit_or_die(&oid, "HEAD");
1868 if (get_oid("MERGE_HEAD", &oid))
1869 die("--merge without MERGE_HEAD?");
1870 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1871 add_pending_object(revs, &head->object, "HEAD");
1872 add_pending_object(revs, &other->object, "MERGE_HEAD");
1873 bases = get_merge_bases(head, other);
1874 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1875 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1876 free_commit_list(bases);
1877 head->object.flags |= SYMMETRIC_LEFT;
1879 if (!istate->cache_nr)
1880 repo_read_index(revs->repo);
1881 for (i = 0; i < istate->cache_nr; i++) {
1882 const struct cache_entry *ce = istate->cache[i];
1885 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1887 REALLOC_ARRAY(prune, prune_num);
1888 prune[prune_num-2] = ce->name;
1889 prune[prune_num-1] = NULL;
1891 while ((i+1 < istate->cache_nr) &&
1892 ce_same_name(ce, istate->cache[i+1]))
1895 clear_pathspec(&revs->prune_data);
1896 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1897 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1901 static int dotdot_missing(const char *arg, char *dotdot,
1902 struct rev_info *revs, int symmetric)
1904 if (revs->ignore_missing)
1906 /* de-munge so we report the full argument */
1909 ? "Invalid symmetric difference expression %s"
1910 : "Invalid revision range %s", arg);
1913 static int handle_dotdot_1(const char *arg, char *dotdot,
1914 struct rev_info *revs, int flags,
1915 int cant_be_filename,
1916 struct object_context *a_oc,
1917 struct object_context *b_oc)
1919 const char *a_name, *b_name;
1920 struct object_id a_oid, b_oid;
1921 struct object *a_obj, *b_obj;
1922 unsigned int a_flags, b_flags;
1924 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1925 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1931 b_name = dotdot + 2;
1932 if (*b_name == '.') {
1939 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
1940 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
1943 if (!cant_be_filename) {
1945 verify_non_filename(revs->prefix, arg);
1949 a_obj = parse_object(revs->repo, &a_oid);
1950 b_obj = parse_object(revs->repo, &b_oid);
1951 if (!a_obj || !b_obj)
1952 return dotdot_missing(arg, dotdot, revs, symmetric);
1957 a_flags = flags_exclude;
1959 /* A...B -- find merge bases between the two */
1960 struct commit *a, *b;
1961 struct commit_list *exclude;
1963 a = lookup_commit_reference(revs->repo, &a_obj->oid);
1964 b = lookup_commit_reference(revs->repo, &b_obj->oid);
1966 return dotdot_missing(arg, dotdot, revs, symmetric);
1968 exclude = get_merge_bases(a, b);
1969 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1971 add_pending_commit_list(revs, exclude, flags_exclude);
1972 free_commit_list(exclude);
1975 a_flags = flags | SYMMETRIC_LEFT;
1978 a_obj->flags |= a_flags;
1979 b_obj->flags |= b_flags;
1980 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1981 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1982 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1983 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1987 static int handle_dotdot(const char *arg,
1988 struct rev_info *revs, int flags,
1989 int cant_be_filename)
1991 struct object_context a_oc, b_oc;
1992 char *dotdot = strstr(arg, "..");
1998 memset(&a_oc, 0, sizeof(a_oc));
1999 memset(&b_oc, 0, sizeof(b_oc));
2002 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
2012 static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
2014 struct object_context oc;
2016 struct object *object;
2017 struct object_id oid;
2019 const char *arg = arg_;
2020 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
2021 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
2023 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
2025 if (!cant_be_filename && !strcmp(arg, "..")) {
2027 * Just ".."? That is not a range but the
2028 * pathspec for the parent directory.
2033 if (!handle_dotdot(arg, revs, flags, revarg_opt))
2036 mark = strstr(arg, "^@");
2037 if (mark && !mark[2]) {
2039 if (add_parents_only(revs, arg, flags, 0))
2043 mark = strstr(arg, "^!");
2044 if (mark && !mark[2]) {
2046 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
2049 mark = strstr(arg, "^-");
2051 int exclude_parent = 1;
2055 exclude_parent = strtoul(mark + 2, &end, 10);
2056 if (*end != '\0' || !exclude_parent)
2061 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
2067 local_flags = UNINTERESTING | BOTTOM;
2071 if (revarg_opt & REVARG_COMMITTISH)
2072 get_sha1_flags |= GET_OID_COMMITTISH;
2074 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
2075 return revs->ignore_missing ? 0 : -1;
2076 if (!cant_be_filename)
2077 verify_non_filename(revs->prefix, arg);
2078 object = get_reference(revs, arg, &oid, flags ^ local_flags);
2080 return revs->ignore_missing ? 0 : -1;
2081 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
2082 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
2087 int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
2089 int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
2091 revs->rev_input_given = 1;
2095 static void read_pathspec_from_stdin(struct strbuf *sb,
2096 struct strvec *prune)
2098 while (strbuf_getline(sb, stdin) != EOF)
2099 strvec_push(prune, sb->buf);
2102 static void read_revisions_from_stdin(struct rev_info *revs,
2103 struct strvec *prune)
2106 int seen_dashdash = 0;
2109 save_warning = warn_on_object_refname_ambiguity;
2110 warn_on_object_refname_ambiguity = 0;
2112 strbuf_init(&sb, 1000);
2113 while (strbuf_getline(&sb, stdin) != EOF) {
2117 if (sb.buf[0] == '-') {
2118 if (len == 2 && sb.buf[1] == '-') {
2122 die("options not supported in --stdin mode");
2124 if (handle_revision_arg(sb.buf, revs, 0,
2125 REVARG_CANNOT_BE_FILENAME))
2126 die("bad revision '%s'", sb.buf);
2129 read_pathspec_from_stdin(&sb, prune);
2131 strbuf_release(&sb);
2132 warn_on_object_refname_ambiguity = save_warning;
2135 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2137 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2140 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2142 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2145 static void add_message_grep(struct rev_info *revs, const char *pattern)
2147 add_grep(revs, pattern, GREP_PATTERN_BODY);
2150 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2151 int *unkc, const char **unkv,
2152 const struct setup_revision_opt* opt)
2154 const char *arg = argv[0];
2157 const unsigned hexsz = the_hash_algo->hexsz;
2159 /* pseudo revision arguments */
2160 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2161 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2162 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2163 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2164 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2165 !strcmp(arg, "--indexed-objects") ||
2166 !strcmp(arg, "--alternate-refs") ||
2167 starts_with(arg, "--exclude=") ||
2168 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2169 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2171 unkv[(*unkc)++] = arg;
2175 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2176 revs->max_count = atoi(optarg);
2179 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2180 revs->skip_count = atoi(optarg);
2182 } else if ((*arg == '-') && isdigit(arg[1])) {
2183 /* accept -<digit>, like traditional "head" */
2184 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2185 revs->max_count < 0)
2186 die("'%s': not a non-negative integer", arg + 1);
2188 } else if (!strcmp(arg, "-n")) {
2190 return error("-n requires an argument");
2191 revs->max_count = atoi(argv[1]);
2194 } else if (skip_prefix(arg, "-n", &optarg)) {
2195 revs->max_count = atoi(optarg);
2197 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2198 revs->max_age = atoi(optarg);
2200 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2201 revs->max_age = approxidate(optarg);
2203 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2204 revs->max_age = approxidate(optarg);
2206 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2207 revs->min_age = atoi(optarg);
2209 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2210 revs->min_age = approxidate(optarg);
2212 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2213 revs->min_age = approxidate(optarg);
2215 } else if (!strcmp(arg, "--first-parent")) {
2216 revs->first_parent_only = 1;
2217 } else if (!strcmp(arg, "--ancestry-path")) {
2218 revs->ancestry_path = 1;
2219 revs->simplify_history = 0;
2221 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2222 init_reflog_walk(&revs->reflog_info);
2223 } else if (!strcmp(arg, "--default")) {
2225 return error("bad --default argument");
2226 revs->def = argv[1];
2228 } else if (!strcmp(arg, "--merge")) {
2229 revs->show_merge = 1;
2230 } else if (!strcmp(arg, "--topo-order")) {
2231 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2232 revs->topo_order = 1;
2233 } else if (!strcmp(arg, "--simplify-merges")) {
2234 revs->simplify_merges = 1;
2235 revs->topo_order = 1;
2236 revs->rewrite_parents = 1;
2237 revs->simplify_history = 0;
2239 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2240 revs->simplify_merges = 1;
2241 revs->topo_order = 1;
2242 revs->rewrite_parents = 1;
2243 revs->simplify_history = 0;
2244 revs->simplify_by_decoration = 1;
2247 } else if (!strcmp(arg, "--date-order")) {
2248 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2249 revs->topo_order = 1;
2250 } else if (!strcmp(arg, "--author-date-order")) {
2251 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2252 revs->topo_order = 1;
2253 } else if (!strcmp(arg, "--early-output")) {
2254 revs->early_output = 100;
2255 revs->topo_order = 1;
2256 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2257 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2258 die("'%s': not a non-negative integer", optarg);
2259 revs->topo_order = 1;
2260 } else if (!strcmp(arg, "--parents")) {
2261 revs->rewrite_parents = 1;
2262 revs->print_parents = 1;
2263 } else if (!strcmp(arg, "--dense")) {
2265 } else if (!strcmp(arg, "--sparse")) {
2267 } else if (!strcmp(arg, "--in-commit-order")) {
2268 revs->tree_blobs_in_commit_order = 1;
2269 } else if (!strcmp(arg, "--remove-empty")) {
2270 revs->remove_empty_trees = 1;
2271 } else if (!strcmp(arg, "--merges")) {
2272 revs->min_parents = 2;
2273 } else if (!strcmp(arg, "--no-merges")) {
2274 revs->max_parents = 1;
2275 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2276 revs->min_parents = atoi(optarg);
2277 } else if (!strcmp(arg, "--no-min-parents")) {
2278 revs->min_parents = 0;
2279 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2280 revs->max_parents = atoi(optarg);
2281 } else if (!strcmp(arg, "--no-max-parents")) {
2282 revs->max_parents = -1;
2283 } else if (!strcmp(arg, "--boundary")) {
2285 } else if (!strcmp(arg, "--left-right")) {
2286 revs->left_right = 1;
2287 } else if (!strcmp(arg, "--left-only")) {
2288 if (revs->right_only)
2289 die("--left-only is incompatible with --right-only"
2291 revs->left_only = 1;
2292 } else if (!strcmp(arg, "--right-only")) {
2293 if (revs->left_only)
2294 die("--right-only is incompatible with --left-only");
2295 revs->right_only = 1;
2296 } else if (!strcmp(arg, "--cherry")) {
2297 if (revs->left_only)
2298 die("--cherry is incompatible with --left-only");
2299 revs->cherry_mark = 1;
2300 revs->right_only = 1;
2301 revs->max_parents = 1;
2303 } else if (!strcmp(arg, "--count")) {
2305 } else if (!strcmp(arg, "--cherry-mark")) {
2306 if (revs->cherry_pick)
2307 die("--cherry-mark is incompatible with --cherry-pick");
2308 revs->cherry_mark = 1;
2309 revs->limited = 1; /* needs limit_list() */
2310 } else if (!strcmp(arg, "--cherry-pick")) {
2311 if (revs->cherry_mark)
2312 die("--cherry-pick is incompatible with --cherry-mark");
2313 revs->cherry_pick = 1;
2315 } else if (!strcmp(arg, "--objects")) {
2316 revs->tag_objects = 1;
2317 revs->tree_objects = 1;
2318 revs->blob_objects = 1;
2319 } else if (!strcmp(arg, "--objects-edge")) {
2320 revs->tag_objects = 1;
2321 revs->tree_objects = 1;
2322 revs->blob_objects = 1;
2323 revs->edge_hint = 1;
2324 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2325 revs->tag_objects = 1;
2326 revs->tree_objects = 1;
2327 revs->blob_objects = 1;
2328 revs->edge_hint = 1;
2329 revs->edge_hint_aggressive = 1;
2330 } else if (!strcmp(arg, "--verify-objects")) {
2331 revs->tag_objects = 1;
2332 revs->tree_objects = 1;
2333 revs->blob_objects = 1;
2334 revs->verify_objects = 1;
2335 } else if (!strcmp(arg, "--unpacked")) {
2337 } else if (starts_with(arg, "--unpacked=")) {
2338 die(_("--unpacked=<packfile> no longer supported"));
2339 } else if (!strcmp(arg, "--no-kept-objects")) {
2340 revs->no_kept_objects = 1;
2341 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2342 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2343 } else if (skip_prefix(arg, "--no-kept-objects=", &optarg)) {
2344 revs->no_kept_objects = 1;
2345 if (!strcmp(optarg, "in-core"))
2346 revs->keep_pack_cache_flags |= IN_CORE_KEEP_PACKS;
2347 if (!strcmp(optarg, "on-disk"))
2348 revs->keep_pack_cache_flags |= ON_DISK_KEEP_PACKS;
2349 } else if (!strcmp(arg, "-r")) {
2351 revs->diffopt.flags.recursive = 1;
2352 } else if (!strcmp(arg, "-t")) {
2354 revs->diffopt.flags.recursive = 1;
2355 revs->diffopt.flags.tree_in_recursive = 1;
2356 } else if ((argcount = diff_merges_parse_opts(revs, argv))) {
2358 } else if (!strcmp(arg, "-v")) {
2359 revs->verbose_header = 1;
2360 } else if (!strcmp(arg, "--pretty")) {
2361 revs->verbose_header = 1;
2362 revs->pretty_given = 1;
2363 get_commit_format(NULL, revs);
2364 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2365 skip_prefix(arg, "--format=", &optarg)) {
2367 * Detached form ("--pretty X" as opposed to "--pretty=X")
2368 * not allowed, since the argument is optional.
2370 revs->verbose_header = 1;
2371 revs->pretty_given = 1;
2372 get_commit_format(optarg, revs);
2373 } else if (!strcmp(arg, "--expand-tabs")) {
2374 revs->expand_tabs_in_log = 8;
2375 } else if (!strcmp(arg, "--no-expand-tabs")) {
2376 revs->expand_tabs_in_log = 0;
2377 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2379 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2380 die("'%s': not a non-negative integer", arg);
2381 revs->expand_tabs_in_log = val;
2382 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2383 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2384 revs->show_notes_given = 1;
2385 } else if (!strcmp(arg, "--show-signature")) {
2386 revs->show_signature = 1;
2387 } else if (!strcmp(arg, "--no-show-signature")) {
2388 revs->show_signature = 0;
2389 } else if (!strcmp(arg, "--show-linear-break")) {
2390 revs->break_bar = " ..........";
2391 revs->track_linear = 1;
2392 revs->track_first_time = 1;
2393 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2394 revs->break_bar = xstrdup(optarg);
2395 revs->track_linear = 1;
2396 revs->track_first_time = 1;
2397 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2398 skip_prefix(arg, "--notes=", &optarg)) {
2399 if (starts_with(arg, "--show-notes=") &&
2400 revs->notes_opt.use_default_notes < 0)
2401 revs->notes_opt.use_default_notes = 1;
2402 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2403 revs->show_notes_given = 1;
2404 } else if (!strcmp(arg, "--no-notes")) {
2405 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2406 revs->show_notes_given = 1;
2407 } else if (!strcmp(arg, "--standard-notes")) {
2408 revs->show_notes_given = 1;
2409 revs->notes_opt.use_default_notes = 1;
2410 } else if (!strcmp(arg, "--no-standard-notes")) {
2411 revs->notes_opt.use_default_notes = 0;
2412 } else if (!strcmp(arg, "--oneline")) {
2413 revs->verbose_header = 1;
2414 get_commit_format("oneline", revs);
2415 revs->pretty_given = 1;
2416 revs->abbrev_commit = 1;
2417 } else if (!strcmp(arg, "--graph")) {
2418 revs->topo_order = 1;
2419 revs->rewrite_parents = 1;
2420 revs->graph = graph_init(revs);
2421 } else if (!strcmp(arg, "--encode-email-headers")) {
2422 revs->encode_email_headers = 1;
2423 } else if (!strcmp(arg, "--no-encode-email-headers")) {
2424 revs->encode_email_headers = 0;
2425 } else if (!strcmp(arg, "--root")) {
2426 revs->show_root_diff = 1;
2427 } else if (!strcmp(arg, "--no-commit-id")) {
2428 revs->no_commit_id = 1;
2429 } else if (!strcmp(arg, "--always")) {
2430 revs->always_show_header = 1;
2431 } else if (!strcmp(arg, "--no-abbrev")) {
2433 } else if (!strcmp(arg, "--abbrev")) {
2434 revs->abbrev = DEFAULT_ABBREV;
2435 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2436 revs->abbrev = strtoul(optarg, NULL, 10);
2437 if (revs->abbrev < MINIMUM_ABBREV)
2438 revs->abbrev = MINIMUM_ABBREV;
2439 else if (revs->abbrev > hexsz)
2440 revs->abbrev = hexsz;
2441 } else if (!strcmp(arg, "--abbrev-commit")) {
2442 revs->abbrev_commit = 1;
2443 revs->abbrev_commit_given = 1;
2444 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2445 revs->abbrev_commit = 0;
2446 } else if (!strcmp(arg, "--full-diff")) {
2448 revs->full_diff = 1;
2449 } else if (!strcmp(arg, "--show-pulls")) {
2450 revs->show_pulls = 1;
2451 } else if (!strcmp(arg, "--full-history")) {
2452 revs->simplify_history = 0;
2453 } else if (!strcmp(arg, "--relative-date")) {
2454 revs->date_mode.type = DATE_RELATIVE;
2455 revs->date_mode_explicit = 1;
2456 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2457 parse_date_format(optarg, &revs->date_mode);
2458 revs->date_mode_explicit = 1;
2460 } else if (!strcmp(arg, "--log-size")) {
2461 revs->show_log_size = 1;
2464 * Grepping the commit log
2466 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2467 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2469 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2470 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2472 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2473 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2475 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2476 add_message_grep(revs, optarg);
2478 } else if (!strcmp(arg, "--basic-regexp")) {
2479 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2480 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2481 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2482 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2483 revs->grep_filter.ignore_case = 1;
2484 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2485 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2486 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2487 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2488 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2489 } else if (!strcmp(arg, "--all-match")) {
2490 revs->grep_filter.all_match = 1;
2491 } else if (!strcmp(arg, "--invert-grep")) {
2492 revs->invert_grep = 1;
2493 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2494 if (strcmp(optarg, "none"))
2495 git_log_output_encoding = xstrdup(optarg);
2497 git_log_output_encoding = "";
2499 } else if (!strcmp(arg, "--reverse")) {
2501 } else if (!strcmp(arg, "--children")) {
2502 revs->children.name = "children";
2504 } else if (!strcmp(arg, "--ignore-missing")) {
2505 revs->ignore_missing = 1;
2506 } else if (opt && opt->allow_exclude_promisor_objects &&
2507 !strcmp(arg, "--exclude-promisor-objects")) {
2508 if (fetch_if_missing)
2509 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2510 revs->exclude_promisor_objects = 1;
2512 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2514 unkv[(*unkc)++] = arg;
2517 if (revs->graph && revs->track_linear)
2518 die("--show-linear-break and --graph are incompatible");
2523 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2524 const struct option *options,
2525 const char * const usagestr[])
2527 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2528 &ctx->cpidx, ctx->out, NULL);
2530 error("unknown option `%s'", ctx->argv[0]);
2531 usage_with_options(usagestr, options);
2537 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2538 void *cb_data, const char *term)
2540 struct strbuf bisect_refs = STRBUF_INIT;
2542 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2543 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2544 strbuf_release(&bisect_refs);
2548 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2550 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2553 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2555 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2558 static int handle_revision_pseudo_opt(const char *submodule,
2559 struct rev_info *revs,
2560 const char **argv, int *flags)
2562 const char *arg = argv[0];
2564 struct ref_store *refs;
2569 * We need some something like get_submodule_worktrees()
2570 * before we can go through all worktrees of a submodule,
2571 * .e.g with adding all HEADs from --all, which is not
2572 * supported right now, so stick to single worktree.
2574 if (!revs->single_worktree)
2575 BUG("--single-worktree cannot be used together with submodule");
2576 refs = get_submodule_ref_store(submodule);
2578 refs = get_main_ref_store(revs->repo);
2583 * Commands like "git shortlog" will not accept the options below
2584 * unless parse_revision_opt queues them (as opposed to erroring
2587 * When implementing your new pseudo-option, remember to
2588 * register it in the list at the top of handle_revision_opt.
2590 if (!strcmp(arg, "--all")) {
2591 handle_refs(refs, revs, *flags, refs_for_each_ref);
2592 handle_refs(refs, revs, *flags, refs_head_ref);
2593 if (!revs->single_worktree) {
2594 struct all_refs_cb cb;
2596 init_all_refs_cb(&cb, revs, *flags);
2597 other_head_refs(handle_one_ref, &cb);
2599 clear_ref_exclusion(&revs->ref_excludes);
2600 } else if (!strcmp(arg, "--branches")) {
2601 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2602 clear_ref_exclusion(&revs->ref_excludes);
2603 } else if (!strcmp(arg, "--bisect")) {
2604 read_bisect_terms(&term_bad, &term_good);
2605 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2606 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2607 for_each_good_bisect_ref);
2609 } else if (!strcmp(arg, "--tags")) {
2610 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2611 clear_ref_exclusion(&revs->ref_excludes);
2612 } else if (!strcmp(arg, "--remotes")) {
2613 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2614 clear_ref_exclusion(&revs->ref_excludes);
2615 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2616 struct all_refs_cb cb;
2617 init_all_refs_cb(&cb, revs, *flags);
2618 for_each_glob_ref(handle_one_ref, optarg, &cb);
2619 clear_ref_exclusion(&revs->ref_excludes);
2621 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2622 add_ref_exclusion(&revs->ref_excludes, optarg);
2624 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2625 struct all_refs_cb cb;
2626 init_all_refs_cb(&cb, revs, *flags);
2627 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2628 clear_ref_exclusion(&revs->ref_excludes);
2629 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2630 struct all_refs_cb cb;
2631 init_all_refs_cb(&cb, revs, *flags);
2632 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2633 clear_ref_exclusion(&revs->ref_excludes);
2634 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2635 struct all_refs_cb cb;
2636 init_all_refs_cb(&cb, revs, *flags);
2637 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2638 clear_ref_exclusion(&revs->ref_excludes);
2639 } else if (!strcmp(arg, "--reflog")) {
2640 add_reflogs_to_pending(revs, *flags);
2641 } else if (!strcmp(arg, "--indexed-objects")) {
2642 add_index_objects_to_pending(revs, *flags);
2643 } else if (!strcmp(arg, "--alternate-refs")) {
2644 add_alternate_refs_to_pending(revs, *flags);
2645 } else if (!strcmp(arg, "--not")) {
2646 *flags ^= UNINTERESTING | BOTTOM;
2647 } else if (!strcmp(arg, "--no-walk")) {
2648 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2649 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2651 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2652 * not allowed, since the argument is optional.
2654 if (!strcmp(optarg, "sorted"))
2655 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2656 else if (!strcmp(optarg, "unsorted"))
2657 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2659 return error("invalid argument to --no-walk");
2660 } else if (!strcmp(arg, "--do-walk")) {
2662 } else if (!strcmp(arg, "--single-worktree")) {
2663 revs->single_worktree = 1;
2671 static void NORETURN diagnose_missing_default(const char *def)
2674 const char *refname;
2676 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2677 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2678 die(_("your current branch appears to be broken"));
2680 skip_prefix(refname, "refs/heads/", &refname);
2681 die(_("your current branch '%s' does not have any commits yet"),
2686 * Parse revision information, filling in the "rev_info" structure,
2687 * and removing the used arguments from the argument list.
2689 * Returns the number of arguments left that weren't recognized
2690 * (which are also moved to the head of the argument list)
2692 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2694 int i, flags, left, seen_dashdash, revarg_opt;
2695 struct strvec prune_data = STRVEC_INIT;
2696 const char *submodule = NULL;
2697 int seen_end_of_options = 0;
2700 submodule = opt->submodule;
2702 /* First, search for "--" */
2703 if (opt && opt->assume_dashdash) {
2707 for (i = 1; i < argc; i++) {
2708 const char *arg = argv[i];
2709 if (strcmp(arg, "--"))
2714 strvec_pushv(&prune_data, argv + i + 1);
2720 /* Second, deal with arguments and options */
2722 revarg_opt = opt ? opt->revarg_opt : 0;
2724 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2725 for (left = i = 1; i < argc; i++) {
2726 const char *arg = argv[i];
2727 if (!seen_end_of_options && *arg == '-') {
2730 opts = handle_revision_pseudo_opt(submodule,
2738 if (!strcmp(arg, "--stdin")) {
2739 if (revs->disable_stdin) {
2743 if (revs->read_from_stdin++)
2744 die("--stdin given twice?");
2745 read_revisions_from_stdin(revs, &prune_data);
2749 if (!strcmp(arg, "--end-of-options")) {
2750 seen_end_of_options = 1;
2754 opts = handle_revision_opt(revs, argc - i, argv + i,
2766 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2768 if (seen_dashdash || *arg == '^')
2769 die("bad revision '%s'", arg);
2771 /* If we didn't have a "--":
2772 * (1) all filenames must exist;
2773 * (2) all rev-args must not be interpretable
2774 * as a valid filename.
2775 * but the latter we have checked in the main loop.
2777 for (j = i; j < argc; j++)
2778 verify_filename(revs->prefix, argv[j], j == i);
2780 strvec_pushv(&prune_data, argv + i);
2785 if (prune_data.nr) {
2787 * If we need to introduce the magic "a lone ':' means no
2788 * pathspec whatsoever", here is the place to do so.
2790 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2791 * prune_data.nr = 0;
2792 * prune_data.alloc = 0;
2793 * free(prune_data.path);
2794 * prune_data.path = NULL;
2796 * terminate prune_data.alloc with NULL and
2797 * call init_pathspec() to set revs->prune_data here.
2800 parse_pathspec(&revs->prune_data, 0, 0,
2801 revs->prefix, prune_data.v);
2803 strvec_clear(&prune_data);
2805 if (revs->def == NULL)
2806 revs->def = opt ? opt->def : NULL;
2807 if (opt && opt->tweak)
2808 opt->tweak(revs, opt);
2809 if (revs->show_merge)
2810 prepare_show_merge(revs);
2811 if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
2812 struct object_id oid;
2813 struct object *object;
2814 struct object_context oc;
2815 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2816 diagnose_missing_default(revs->def);
2817 object = get_reference(revs, revs->def, &oid, 0);
2818 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2821 /* Did the user ask for any diff output? Run the diff! */
2822 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2825 /* Pickaxe, diff-filter and rename following need diffs */
2826 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2827 revs->diffopt.filter ||
2828 revs->diffopt.flags.follow_renames)
2831 if (revs->diffopt.objfind)
2832 revs->simplify_history = 0;
2834 if (revs->line_level_traverse) {
2835 if (want_ancestry(revs))
2837 revs->topo_order = 1;
2840 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2843 if (revs->prune_data.nr) {
2844 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2845 /* Can't prune commits with rename following: the paths change.. */
2846 if (!revs->diffopt.flags.follow_renames)
2848 if (!revs->full_diff)
2849 copy_pathspec(&revs->diffopt.pathspec,
2853 diff_merges_setup_revs(revs);
2855 revs->diffopt.abbrev = revs->abbrev;
2857 diff_setup_done(&revs->diffopt);
2859 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2860 &revs->grep_filter);
2861 if (!is_encoding_utf8(get_log_output_encoding()))
2862 revs->grep_filter.ignore_locale = 1;
2863 compile_grep_patterns(&revs->grep_filter);
2865 if (revs->reverse && revs->reflog_info)
2866 die("cannot combine --reverse with --walk-reflogs");
2867 if (revs->reflog_info && revs->limited)
2868 die("cannot combine --walk-reflogs with history-limiting options");
2869 if (revs->rewrite_parents && revs->children.name)
2870 die("cannot combine --parents and --children");
2873 * Limitations on the graph functionality
2875 if (revs->reverse && revs->graph)
2876 die("cannot combine --reverse with --graph");
2878 if (revs->reflog_info && revs->graph)
2879 die("cannot combine --walk-reflogs with --graph");
2880 if (revs->no_walk && revs->graph)
2881 die("cannot combine --no-walk with --graph");
2882 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2883 die("cannot use --grep-reflog without --walk-reflogs");
2885 if (revs->line_level_traverse &&
2886 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2887 die(_("-L does not yet support diff formats besides -p and -s"));
2889 if (revs->expand_tabs_in_log < 0)
2890 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2895 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2897 struct commit_list *l = xcalloc(1, sizeof(*l));
2900 l->next = add_decoration(&revs->children, &parent->object, l);
2903 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2905 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2906 struct commit_list **pp, *p;
2907 int surviving_parents;
2909 /* Examine existing parents while marking ones we have seen... */
2910 pp = &commit->parents;
2911 surviving_parents = 0;
2912 while ((p = *pp) != NULL) {
2913 struct commit *parent = p->item;
2914 if (parent->object.flags & TMP_MARK) {
2917 compact_treesame(revs, commit, surviving_parents);
2920 parent->object.flags |= TMP_MARK;
2921 surviving_parents++;
2924 /* clear the temporary mark */
2925 for (p = commit->parents; p; p = p->next) {
2926 p->item->object.flags &= ~TMP_MARK;
2928 /* no update_treesame() - removing duplicates can't affect TREESAME */
2929 return surviving_parents;
2932 struct merge_simplify_state {
2933 struct commit *simplified;
2936 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2938 struct merge_simplify_state *st;
2940 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2942 CALLOC_ARRAY(st, 1);
2943 add_decoration(&revs->merge_simplification, &commit->object, st);
2948 static int mark_redundant_parents(struct commit *commit)
2950 struct commit_list *h = reduce_heads(commit->parents);
2951 int i = 0, marked = 0;
2952 struct commit_list *po, *pn;
2954 /* Want these for sanity-checking only */
2955 int orig_cnt = commit_list_count(commit->parents);
2956 int cnt = commit_list_count(h);
2959 * Not ready to remove items yet, just mark them for now, based
2960 * on the output of reduce_heads(). reduce_heads outputs the reduced
2961 * set in its original order, so this isn't too hard.
2963 po = commit->parents;
2966 if (pn && po->item == pn->item) {
2970 po->item->object.flags |= TMP_MARK;
2976 if (i != cnt || cnt+marked != orig_cnt)
2977 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2979 free_commit_list(h);
2984 static int mark_treesame_root_parents(struct commit *commit)
2986 struct commit_list *p;
2989 for (p = commit->parents; p; p = p->next) {
2990 struct commit *parent = p->item;
2991 if (!parent->parents && (parent->object.flags & TREESAME)) {
2992 parent->object.flags |= TMP_MARK;
3001 * Awkward naming - this means one parent we are TREESAME to.
3002 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
3003 * empty tree). Better name suggestions?
3005 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
3007 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
3008 struct commit *unmarked = NULL, *marked = NULL;
3009 struct commit_list *p;
3012 for (p = commit->parents, n = 0; p; p = p->next, n++) {
3013 if (ts->treesame[n]) {
3014 if (p->item->object.flags & TMP_MARK) {
3027 * If we are TREESAME to a marked-for-deletion parent, but not to any
3028 * unmarked parents, unmark the first TREESAME parent. This is the
3029 * parent that the default simplify_history==1 scan would have followed,
3030 * and it doesn't make sense to omit that path when asking for a
3031 * simplified full history. Retaining it improves the chances of
3032 * understanding odd missed merges that took an old version of a file.
3036 * I--------*X A modified the file, but mainline merge X used
3037 * \ / "-s ours", so took the version from I. X is
3038 * `-*A--' TREESAME to I and !TREESAME to A.
3040 * Default log from X would produce "I". Without this check,
3041 * --full-history --simplify-merges would produce "I-A-X", showing
3042 * the merge commit X and that it changed A, but not making clear that
3043 * it had just taken the I version. With this check, the topology above
3046 * Note that it is possible that the simplification chooses a different
3047 * TREESAME parent from the default, in which case this test doesn't
3048 * activate, and we _do_ drop the default parent. Example:
3050 * I------X A modified the file, but it was reverted in B,
3051 * \ / meaning mainline merge X is TREESAME to both
3054 * Default log would produce "I" by following the first parent;
3055 * --full-history --simplify-merges will produce "I-A-B". But this is a
3056 * reasonable result - it presents a logical full history leading from
3057 * I to X, and X is not an important merge.
3059 if (!unmarked && marked) {
3060 marked->object.flags &= ~TMP_MARK;
3067 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
3069 struct commit_list **pp, *p;
3070 int nth_parent, removed = 0;
3072 pp = &commit->parents;
3074 while ((p = *pp) != NULL) {
3075 struct commit *parent = p->item;
3076 if (parent->object.flags & TMP_MARK) {
3077 parent->object.flags &= ~TMP_MARK;
3081 compact_treesame(revs, commit, nth_parent);
3088 /* Removing parents can only increase TREESAMEness */
3089 if (removed && !(commit->object.flags & TREESAME))
3090 update_treesame(revs, commit);
3095 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3097 struct commit_list *p;
3098 struct commit *parent;
3099 struct merge_simplify_state *st, *pst;
3102 st = locate_simplify_state(revs, commit);
3105 * Have we handled this one?
3111 * An UNINTERESTING commit simplifies to itself, so does a
3112 * root commit. We do not rewrite parents of such commit
3115 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3116 st->simplified = commit;
3121 * Do we know what commit all of our parents that matter
3122 * should be rewritten to? Otherwise we are not ready to
3123 * rewrite this one yet.
3125 for (cnt = 0, p = commit->parents; p; p = p->next) {
3126 pst = locate_simplify_state(revs, p->item);
3127 if (!pst->simplified) {
3128 tail = &commit_list_insert(p->item, tail)->next;
3131 if (revs->first_parent_only)
3135 tail = &commit_list_insert(commit, tail)->next;
3140 * Rewrite our list of parents. Note that this cannot
3141 * affect our TREESAME flags in any way - a commit is
3142 * always TREESAME to its simplification.
3144 for (p = commit->parents; p; p = p->next) {
3145 pst = locate_simplify_state(revs, p->item);
3146 p->item = pst->simplified;
3147 if (revs->first_parent_only)
3151 if (revs->first_parent_only)
3154 cnt = remove_duplicate_parents(revs, commit);
3157 * It is possible that we are a merge and one side branch
3158 * does not have any commit that touches the given paths;
3159 * in such a case, the immediate parent from that branch
3160 * will be rewritten to be the merge base.
3162 * o----X X: the commit we are looking at;
3163 * / / o: a commit that touches the paths;
3166 * Further, a merge of an independent branch that doesn't
3167 * touch the path will reduce to a treesame root parent:
3169 * ----o----X X: the commit we are looking at;
3170 * / o: a commit that touches the paths;
3171 * r r: a root commit not touching the paths
3173 * Detect and simplify both cases.
3176 int marked = mark_redundant_parents(commit);
3177 marked += mark_treesame_root_parents(commit);
3179 marked -= leave_one_treesame_to_parent(revs, commit);
3181 cnt = remove_marked_parents(revs, commit);
3185 * A commit simplifies to itself if it is a root, if it is
3186 * UNINTERESTING, if it touches the given paths, or if it is a
3187 * merge and its parents don't simplify to one relevant commit
3188 * (the first two cases are already handled at the beginning of
3191 * Otherwise, it simplifies to what its sole relevant parent
3195 (commit->object.flags & UNINTERESTING) ||
3196 !(commit->object.flags & TREESAME) ||
3197 (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
3198 (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
3199 st->simplified = commit;
3201 pst = locate_simplify_state(revs, parent);
3202 st->simplified = pst->simplified;
3207 static void simplify_merges(struct rev_info *revs)
3209 struct commit_list *list, *next;
3210 struct commit_list *yet_to_do, **tail;
3211 struct commit *commit;
3216 /* feed the list reversed */
3218 for (list = revs->commits; list; list = next) {
3219 commit = list->item;
3222 * Do not free(list) here yet; the original list
3223 * is used later in this function.
3225 commit_list_insert(commit, &yet_to_do);
3232 commit = pop_commit(&list);
3233 tail = simplify_one(revs, commit, tail);
3237 /* clean up the result, removing the simplified ones */
3238 list = revs->commits;
3239 revs->commits = NULL;
3240 tail = &revs->commits;
3242 struct merge_simplify_state *st;
3244 commit = pop_commit(&list);
3245 st = locate_simplify_state(revs, commit);
3246 if (st->simplified == commit)
3247 tail = &commit_list_insert(commit, tail)->next;
3251 static void set_children(struct rev_info *revs)
3253 struct commit_list *l;
3254 for (l = revs->commits; l; l = l->next) {
3255 struct commit *commit = l->item;
3256 struct commit_list *p;
3258 for (p = commit->parents; p; p = p->next)
3259 add_child(revs, p->item, commit);
3263 void reset_revision_walk(void)
3265 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3268 static int mark_uninteresting(const struct object_id *oid,
3269 struct packed_git *pack,
3273 struct rev_info *revs = cb;
3274 struct object *o = parse_object(revs->repo, oid);
3275 o->flags |= UNINTERESTING | SEEN;
3279 define_commit_slab(indegree_slab, int);
3280 define_commit_slab(author_date_slab, timestamp_t);
3282 struct topo_walk_info {
3283 timestamp_t min_generation;
3284 struct prio_queue explore_queue;
3285 struct prio_queue indegree_queue;
3286 struct prio_queue topo_queue;
3287 struct indegree_slab indegree;
3288 struct author_date_slab author_date;
3291 static int topo_walk_atexit_registered;
3292 static unsigned int count_explore_walked;
3293 static unsigned int count_indegree_walked;
3294 static unsigned int count_topo_walked;
3296 static void trace2_topo_walk_statistics_atexit(void)
3298 struct json_writer jw = JSON_WRITER_INIT;
3300 jw_object_begin(&jw, 0);
3301 jw_object_intmax(&jw, "count_explore_walked", count_explore_walked);
3302 jw_object_intmax(&jw, "count_indegree_walked", count_indegree_walked);
3303 jw_object_intmax(&jw, "count_topo_walked", count_topo_walked);
3306 trace2_data_json("topo_walk", the_repository, "statistics", &jw);
3311 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3313 if (c->object.flags & flag)
3316 c->object.flags |= flag;
3317 prio_queue_put(q, c);
3320 static void explore_walk_step(struct rev_info *revs)
3322 struct topo_walk_info *info = revs->topo_walk_info;
3323 struct commit_list *p;
3324 struct commit *c = prio_queue_get(&info->explore_queue);
3329 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3332 count_explore_walked++;
3334 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3335 record_author_date(&info->author_date, c);
3337 if (revs->max_age != -1 && (c->date < revs->max_age))
3338 c->object.flags |= UNINTERESTING;
3340 if (process_parents(revs, c, NULL, NULL) < 0)
3343 if (c->object.flags & UNINTERESTING)
3344 mark_parents_uninteresting(c);
3346 for (p = c->parents; p; p = p->next)
3347 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3350 static void explore_to_depth(struct rev_info *revs,
3351 timestamp_t gen_cutoff)
3353 struct topo_walk_info *info = revs->topo_walk_info;
3355 while ((c = prio_queue_peek(&info->explore_queue)) &&
3356 commit_graph_generation(c) >= gen_cutoff)
3357 explore_walk_step(revs);
3360 static void indegree_walk_step(struct rev_info *revs)
3362 struct commit_list *p;
3363 struct topo_walk_info *info = revs->topo_walk_info;
3364 struct commit *c = prio_queue_get(&info->indegree_queue);
3369 if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
3372 count_indegree_walked++;
3374 explore_to_depth(revs, commit_graph_generation(c));
3376 for (p = c->parents; p; p = p->next) {
3377 struct commit *parent = p->item;
3378 int *pi = indegree_slab_at(&info->indegree, parent);
3380 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3388 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3390 if (revs->first_parent_only)
3395 static void compute_indegrees_to_depth(struct rev_info *revs,
3396 timestamp_t gen_cutoff)
3398 struct topo_walk_info *info = revs->topo_walk_info;
3400 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3401 commit_graph_generation(c) >= gen_cutoff)
3402 indegree_walk_step(revs);
3405 static void reset_topo_walk(struct rev_info *revs)
3407 struct topo_walk_info *info = revs->topo_walk_info;
3409 clear_prio_queue(&info->explore_queue);
3410 clear_prio_queue(&info->indegree_queue);
3411 clear_prio_queue(&info->topo_queue);
3412 clear_indegree_slab(&info->indegree);
3413 clear_author_date_slab(&info->author_date);
3415 FREE_AND_NULL(revs->topo_walk_info);
3418 static void init_topo_walk(struct rev_info *revs)
3420 struct topo_walk_info *info;
3421 struct commit_list *list;
3422 if (revs->topo_walk_info)
3423 reset_topo_walk(revs);
3425 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3426 info = revs->topo_walk_info;
3427 memset(info, 0, sizeof(struct topo_walk_info));
3429 init_indegree_slab(&info->indegree);
3430 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3431 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3432 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3434 switch (revs->sort_order) {
3435 default: /* REV_SORT_IN_GRAPH_ORDER */
3436 info->topo_queue.compare = NULL;
3438 case REV_SORT_BY_COMMIT_DATE:
3439 info->topo_queue.compare = compare_commits_by_commit_date;
3441 case REV_SORT_BY_AUTHOR_DATE:
3442 init_author_date_slab(&info->author_date);
3443 info->topo_queue.compare = compare_commits_by_author_date;
3444 info->topo_queue.cb_data = &info->author_date;
3448 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3449 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3451 info->min_generation = GENERATION_NUMBER_INFINITY;
3452 for (list = revs->commits; list; list = list->next) {
3453 struct commit *c = list->item;
3454 timestamp_t generation;
3456 if (repo_parse_commit_gently(revs->repo, c, 1))
3459 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3460 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3462 generation = commit_graph_generation(c);
3463 if (generation < info->min_generation)
3464 info->min_generation = generation;
3466 *(indegree_slab_at(&info->indegree, c)) = 1;
3468 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3469 record_author_date(&info->author_date, c);
3471 compute_indegrees_to_depth(revs, info->min_generation);
3473 for (list = revs->commits; list; list = list->next) {
3474 struct commit *c = list->item;
3476 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3477 prio_queue_put(&info->topo_queue, c);
3481 * This is unfortunate; the initial tips need to be shown
3482 * in the order given from the revision traversal machinery.
3484 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3485 prio_queue_reverse(&info->topo_queue);
3487 if (trace2_is_enabled() && !topo_walk_atexit_registered) {
3488 atexit(trace2_topo_walk_statistics_atexit);
3489 topo_walk_atexit_registered = 1;
3493 static struct commit *next_topo_commit(struct rev_info *revs)
3496 struct topo_walk_info *info = revs->topo_walk_info;
3498 /* pop next off of topo_queue */
3499 c = prio_queue_get(&info->topo_queue);
3502 *(indegree_slab_at(&info->indegree, c)) = 0;
3507 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3509 struct commit_list *p;
3510 struct topo_walk_info *info = revs->topo_walk_info;
3511 if (process_parents(revs, commit, NULL, NULL) < 0) {
3512 if (!revs->ignore_missing_links)
3513 die("Failed to traverse parents of commit %s",
3514 oid_to_hex(&commit->object.oid));
3517 count_topo_walked++;
3519 for (p = commit->parents; p; p = p->next) {
3520 struct commit *parent = p->item;
3522 timestamp_t generation;
3524 if (parent->object.flags & UNINTERESTING)
3527 if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
3530 generation = commit_graph_generation(parent);
3531 if (generation < info->min_generation) {
3532 info->min_generation = generation;
3533 compute_indegrees_to_depth(revs, info->min_generation);
3536 pi = indegree_slab_at(&info->indegree, parent);
3540 prio_queue_put(&info->topo_queue, parent);
3542 if (revs->first_parent_only)
3547 int prepare_revision_walk(struct rev_info *revs)
3550 struct object_array old_pending;
3551 struct commit_list **next = &revs->commits;
3553 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3554 revs->pending.nr = 0;
3555 revs->pending.alloc = 0;
3556 revs->pending.objects = NULL;
3557 for (i = 0; i < old_pending.nr; i++) {
3558 struct object_array_entry *e = old_pending.objects + i;
3559 struct commit *commit = handle_commit(revs, e);
3561 if (!(commit->object.flags & SEEN)) {
3562 commit->object.flags |= SEEN;
3563 next = commit_list_append(commit, next);
3567 object_array_clear(&old_pending);
3569 /* Signal whether we need per-parent treesame decoration */
3570 if (revs->simplify_merges ||
3571 (revs->limited && limiting_can_increase_treesame(revs)))
3572 revs->treesame.name = "treesame";
3574 if (revs->exclude_promisor_objects) {
3575 for_each_packed_object(mark_uninteresting, revs,
3576 FOR_EACH_OBJECT_PROMISOR_ONLY);
3579 if (!revs->reflog_info)
3580 prepare_to_use_bloom_filter(revs);
3581 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
3582 commit_list_sort_by_date(&revs->commits);
3585 if (revs->limited) {
3586 if (limit_list(revs) < 0)
3588 if (revs->topo_order)
3589 sort_in_topological_order(&revs->commits, revs->sort_order);
3590 } else if (revs->topo_order)
3591 init_topo_walk(revs);
3592 if (revs->line_level_traverse && want_ancestry(revs))
3594 * At the moment we can only do line-level log with parent
3595 * rewriting by performing this expensive pre-filtering step.
3596 * If parent rewriting is not requested, then we rather
3597 * perform the line-level log filtering during the regular
3598 * history traversal.
3600 line_log_filter(revs);
3601 if (revs->simplify_merges)
3602 simplify_merges(revs);
3603 if (revs->children.name)
3609 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3611 struct prio_queue *queue)
3614 struct commit *p = *pp;
3616 if (process_parents(revs, p, NULL, queue) < 0)
3617 return rewrite_one_error;
3618 if (p->object.flags & UNINTERESTING)
3619 return rewrite_one_ok;
3620 if (!(p->object.flags & TREESAME))
3621 return rewrite_one_ok;
3623 return rewrite_one_noparents;
3624 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
3625 return rewrite_one_ok;
3630 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3633 struct commit *item = prio_queue_peek(q);
3634 struct commit_list *p = *list;
3636 if (p && p->item->date >= item->date)
3639 p = commit_list_insert(item, list);
3640 list = &p->next; /* skip newly added item */
3641 prio_queue_get(q); /* pop item */
3646 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3648 struct prio_queue queue = { compare_commits_by_commit_date };
3649 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3650 merge_queue_into_list(&queue, &revs->commits);
3651 clear_prio_queue(&queue);
3655 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3656 rewrite_parent_fn_t rewrite_parent)
3658 struct commit_list **pp = &commit->parents;
3660 struct commit_list *parent = *pp;
3661 switch (rewrite_parent(revs, &parent->item)) {
3662 case rewrite_one_ok:
3664 case rewrite_one_noparents:
3667 case rewrite_one_error:
3672 remove_duplicate_parents(revs, commit);
3676 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
3678 char *person, *endp;
3679 size_t len, namelen, maillen;
3682 struct ident_split ident;
3684 person = strstr(buf->buf, what);
3688 person += strlen(what);
3689 endp = strchr(person, '\n');
3693 len = endp - person;
3695 if (split_ident_line(&ident, person, len))
3698 mail = ident.mail_begin;
3699 maillen = ident.mail_end - ident.mail_begin;
3700 name = ident.name_begin;
3701 namelen = ident.name_end - ident.name_begin;
3703 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3704 struct strbuf namemail = STRBUF_INIT;
3706 strbuf_addf(&namemail, "%.*s <%.*s>",
3707 (int)namelen, name, (int)maillen, mail);
3709 strbuf_splice(buf, ident.name_begin - buf->buf,
3710 ident.mail_end - ident.name_begin + 1,
3711 namemail.buf, namemail.len);
3713 strbuf_release(&namemail);
3721 static int commit_match(struct commit *commit, struct rev_info *opt)
3724 const char *encoding;
3725 const char *message;
3726 struct strbuf buf = STRBUF_INIT;
3728 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3731 /* Prepend "fake" headers as needed */
3732 if (opt->grep_filter.use_reflog_filter) {
3733 strbuf_addstr(&buf, "reflog ");
3734 get_reflog_message(&buf, opt->reflog_info);
3735 strbuf_addch(&buf, '\n');
3739 * We grep in the user's output encoding, under the assumption that it
3740 * is the encoding they are most likely to write their grep pattern
3741 * for. In addition, it means we will match the "notes" encoding below,
3742 * so we will not end up with a buffer that has two different encodings
3745 encoding = get_log_output_encoding();
3746 message = logmsg_reencode(commit, NULL, encoding);
3748 /* Copy the commit to temporary if we are using "fake" headers */
3750 strbuf_addstr(&buf, message);
3752 if (opt->grep_filter.header_list && opt->mailmap) {
3754 strbuf_addstr(&buf, message);
3756 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3757 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3760 /* Append "fake" message parts as needed */
3761 if (opt->show_notes) {
3763 strbuf_addstr(&buf, message);
3764 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3768 * Find either in the original commit message, or in the temporary.
3769 * Note that we cast away the constness of "message" here. It is
3770 * const because it may come from the cached commit buffer. That's OK,
3771 * because we know that it is modifiable heap memory, and that while
3772 * grep_buffer may modify it for speed, it will restore any
3773 * changes before returning.
3776 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3778 retval = grep_buffer(&opt->grep_filter,
3779 (char *)message, strlen(message));
3780 strbuf_release(&buf);
3781 unuse_commit_buffer(commit, message);
3782 return opt->invert_grep ? !retval : retval;
3785 static inline int want_ancestry(const struct rev_info *revs)
3787 return (revs->rewrite_parents || revs->children.name);
3791 * Return a timestamp to be used for --since/--until comparisons for this
3792 * commit, based on the revision options.
3794 static timestamp_t comparison_date(const struct rev_info *revs,
3795 struct commit *commit)
3797 return revs->reflog_info ?
3798 get_reflog_timestamp(revs->reflog_info) :
3802 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3804 if (commit->object.flags & SHOWN)
3805 return commit_ignore;
3806 if (revs->unpacked && has_object_pack(&commit->object.oid))
3807 return commit_ignore;
3808 if (revs->no_kept_objects) {
3809 if (has_object_kept_pack(&commit->object.oid,
3810 revs->keep_pack_cache_flags))
3811 return commit_ignore;
3813 if (commit->object.flags & UNINTERESTING)
3814 return commit_ignore;
3815 if (revs->line_level_traverse && !want_ancestry(revs)) {
3817 * In case of line-level log with parent rewriting
3818 * prepare_revision_walk() already took care of all line-level
3819 * log filtering, and there is nothing left to do here.
3821 * If parent rewriting was not requested, then this is the
3822 * place to perform the line-level log filtering. Notably,
3823 * this check, though expensive, must come before the other,
3824 * cheaper filtering conditions, because the tracked line
3825 * ranges must be adjusted even when the commit will end up
3826 * being ignored based on other conditions.
3828 if (!line_log_process_ranges_arbitrary_commit(revs, commit))
3829 return commit_ignore;
3831 if (revs->min_age != -1 &&
3832 comparison_date(revs, commit) > revs->min_age)
3833 return commit_ignore;
3834 if (revs->min_parents || (revs->max_parents >= 0)) {
3835 int n = commit_list_count(commit->parents);
3836 if ((n < revs->min_parents) ||
3837 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3838 return commit_ignore;
3840 if (!commit_match(commit, revs))
3841 return commit_ignore;
3842 if (revs->prune && revs->dense) {
3843 /* Commit without changes? */
3844 if (commit->object.flags & TREESAME) {
3846 struct commit_list *p;
3847 /* drop merges unless we want parenthood */
3848 if (!want_ancestry(revs))
3849 return commit_ignore;
3851 if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
3855 * If we want ancestry, then need to keep any merges
3856 * between relevant commits to tie together topology.
3857 * For consistency with TREESAME and simplification
3858 * use "relevant" here rather than just INTERESTING,
3859 * to treat bottom commit(s) as part of the topology.
3861 for (n = 0, p = commit->parents; p; p = p->next)
3862 if (relevant_commit(p->item))
3865 return commit_ignore;
3871 define_commit_slab(saved_parents, struct commit_list *);
3873 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3876 * You may only call save_parents() once per commit (this is checked
3877 * for non-root commits).
3879 static void save_parents(struct rev_info *revs, struct commit *commit)
3881 struct commit_list **pp;
3883 if (!revs->saved_parents_slab) {
3884 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3885 init_saved_parents(revs->saved_parents_slab);
3888 pp = saved_parents_at(revs->saved_parents_slab, commit);
3891 * When walking with reflogs, we may visit the same commit
3892 * several times: once for each appearance in the reflog.
3894 * In this case, save_parents() will be called multiple times.
3895 * We want to keep only the first set of parents. We need to
3896 * store a sentinel value for an empty (i.e., NULL) parent
3897 * list to distinguish it from a not-yet-saved list, however.
3901 if (commit->parents)
3902 *pp = copy_commit_list(commit->parents);
3904 *pp = EMPTY_PARENT_LIST;
3907 static void free_saved_parents(struct rev_info *revs)
3909 if (revs->saved_parents_slab)
3910 clear_saved_parents(revs->saved_parents_slab);
3913 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3915 struct commit_list *parents;
3917 if (!revs->saved_parents_slab)
3918 return commit->parents;
3920 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3921 if (parents == EMPTY_PARENT_LIST)
3926 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3928 enum commit_action action = get_commit_action(revs, commit);
3930 if (action == commit_show &&
3931 revs->prune && revs->dense && want_ancestry(revs)) {
3933 * --full-diff on simplified parents is no good: it
3934 * will show spurious changes from the commits that
3935 * were elided. So we save the parents on the side
3936 * when --full-diff is in effect.
3938 if (revs->full_diff)
3939 save_parents(revs, commit);
3940 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3941 return commit_error;
3946 static void track_linear(struct rev_info *revs, struct commit *commit)
3948 if (revs->track_first_time) {
3950 revs->track_first_time = 0;
3952 struct commit_list *p;
3953 for (p = revs->previous_parents; p; p = p->next)
3954 if (p->item == NULL || /* first commit */
3955 oideq(&p->item->object.oid, &commit->object.oid))
3957 revs->linear = p != NULL;
3959 if (revs->reverse) {
3961 commit->object.flags |= TRACK_LINEAR;
3963 free_commit_list(revs->previous_parents);
3964 revs->previous_parents = copy_commit_list(commit->parents);
3967 static struct commit *get_revision_1(struct rev_info *revs)
3970 struct commit *commit;
3972 if (revs->reflog_info)
3973 commit = next_reflog_entry(revs->reflog_info);
3974 else if (revs->topo_walk_info)
3975 commit = next_topo_commit(revs);
3977 commit = pop_commit(&revs->commits);
3982 if (revs->reflog_info)
3983 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3986 * If we haven't done the list limiting, we need to look at
3987 * the parents here. We also need to do the date-based limiting
3988 * that we'd otherwise have done in limit_list().
3990 if (!revs->limited) {
3991 if (revs->max_age != -1 &&
3992 comparison_date(revs, commit) < revs->max_age)
3995 if (revs->reflog_info)
3996 try_to_simplify_commit(revs, commit);
3997 else if (revs->topo_walk_info)
3998 expand_topo_walk(revs, commit);
3999 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
4000 if (!revs->ignore_missing_links)
4001 die("Failed to traverse parents of commit %s",
4002 oid_to_hex(&commit->object.oid));
4006 switch (simplify_commit(revs, commit)) {
4010 die("Failed to simplify parents of commit %s",
4011 oid_to_hex(&commit->object.oid));
4013 if (revs->track_linear)
4014 track_linear(revs, commit);
4021 * Return true for entries that have not yet been shown. (This is an
4022 * object_array_each_func_t.)
4024 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
4026 return !(entry->item->flags & SHOWN);
4030 * If array is on the verge of a realloc, garbage-collect any entries
4031 * that have already been shown to try to free up some space.
4033 static void gc_boundary(struct object_array *array)
4035 if (array->nr == array->alloc)
4036 object_array_filter(array, entry_unshown, NULL);
4039 static void create_boundary_commit_list(struct rev_info *revs)
4043 struct object_array *array = &revs->boundary_commits;
4044 struct object_array_entry *objects = array->objects;
4047 * If revs->commits is non-NULL at this point, an error occurred in
4048 * get_revision_1(). Ignore the error and continue printing the
4049 * boundary commits anyway. (This is what the code has always
4052 if (revs->commits) {
4053 free_commit_list(revs->commits);
4054 revs->commits = NULL;
4058 * Put all of the actual boundary commits from revs->boundary_commits
4059 * into revs->commits
4061 for (i = 0; i < array->nr; i++) {
4062 c = (struct commit *)(objects[i].item);
4065 if (!(c->object.flags & CHILD_SHOWN))
4067 if (c->object.flags & (SHOWN | BOUNDARY))
4069 c->object.flags |= BOUNDARY;
4070 commit_list_insert(c, &revs->commits);
4074 * If revs->topo_order is set, sort the boundary commits
4075 * in topological order
4077 sort_in_topological_order(&revs->commits, revs->sort_order);
4080 static struct commit *get_revision_internal(struct rev_info *revs)
4082 struct commit *c = NULL;
4083 struct commit_list *l;
4085 if (revs->boundary == 2) {
4087 * All of the normal commits have already been returned,
4088 * and we are now returning boundary commits.
4089 * create_boundary_commit_list() has populated
4090 * revs->commits with the remaining commits to return.
4092 c = pop_commit(&revs->commits);
4094 c->object.flags |= SHOWN;
4099 * If our max_count counter has reached zero, then we are done. We
4100 * don't simply return NULL because we still might need to show
4101 * boundary commits. But we want to avoid calling get_revision_1, which
4102 * might do a considerable amount of work finding the next commit only
4103 * for us to throw it away.
4105 * If it is non-zero, then either we don't have a max_count at all
4106 * (-1), or it is still counting, in which case we decrement.
4108 if (revs->max_count) {
4109 c = get_revision_1(revs);
4111 while (revs->skip_count > 0) {
4113 c = get_revision_1(revs);
4119 if (revs->max_count > 0)
4124 c->object.flags |= SHOWN;
4126 if (!revs->boundary)
4131 * get_revision_1() runs out the commits, and
4132 * we are done computing the boundaries.
4133 * switch to boundary commits output mode.
4138 * Update revs->commits to contain the list of
4141 create_boundary_commit_list(revs);
4143 return get_revision_internal(revs);
4147 * boundary commits are the commits that are parents of the
4148 * ones we got from get_revision_1() but they themselves are
4149 * not returned from get_revision_1(). Before returning
4150 * 'c', we need to mark its parents that they could be boundaries.
4153 for (l = c->parents; l; l = l->next) {
4155 p = &(l->item->object);
4156 if (p->flags & (CHILD_SHOWN | SHOWN))
4158 p->flags |= CHILD_SHOWN;
4159 gc_boundary(&revs->boundary_commits);
4160 add_object_array(p, NULL, &revs->boundary_commits);
4166 struct commit *get_revision(struct rev_info *revs)
4169 struct commit_list *reversed;
4171 if (revs->reverse) {
4173 while ((c = get_revision_internal(revs)))
4174 commit_list_insert(c, &reversed);
4175 revs->commits = reversed;
4177 revs->reverse_output_stage = 1;
4180 if (revs->reverse_output_stage) {
4181 c = pop_commit(&revs->commits);
4182 if (revs->track_linear)
4183 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4187 c = get_revision_internal(revs);
4188 if (c && revs->graph)
4189 graph_update(revs->graph, c);
4191 free_saved_parents(revs);
4192 if (revs->previous_parents) {
4193 free_commit_list(revs->previous_parents);
4194 revs->previous_parents = NULL;
4200 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4202 if (commit->object.flags & BOUNDARY)
4204 else if (commit->object.flags & UNINTERESTING)
4206 else if (commit->object.flags & PATCHSAME)
4208 else if (!revs || revs->left_right) {
4209 if (commit->object.flags & SYMMETRIC_LEFT)
4213 } else if (revs->graph)
4215 else if (revs->cherry_mark)
4220 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4222 const char *mark = get_revision_mark(revs, commit);
4225 fputs(mark, stdout);