2 #include "object-store.h"
10 #include "repository.h"
13 #include "reflog-walk.h"
14 #include "patch-ids.h"
17 #include "string-list.h"
20 #include "commit-slab.h"
22 #include "cache-tree.h"
26 #include "argv-array.h"
27 #include "commit-reach.h"
28 #include "commit-graph.h"
29 #include "prio-queue.h"
34 volatile show_early_output_fn_t show_early_output;
36 static const char *term_bad;
37 static const char *term_good;
39 implement_shared_commit_slab(revision_sources, char *);
41 void show_object_with_name(FILE *out, struct object *obj, const char *name)
45 fprintf(out, "%s ", oid_to_hex(&obj->oid));
46 for (p = name; *p && *p != '\n'; p++)
51 static void mark_blob_uninteresting(struct blob *blob)
55 if (blob->object.flags & UNINTERESTING)
57 blob->object.flags |= UNINTERESTING;
60 static void mark_tree_contents_uninteresting(struct repository *r,
63 struct tree_desc desc;
64 struct name_entry entry;
66 if (parse_tree_gently(tree, 1) < 0)
69 init_tree_desc(&desc, tree->buffer, tree->size);
70 while (tree_entry(&desc, &entry)) {
71 switch (object_type(entry.mode)) {
73 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
76 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
79 /* Subproject commit - not in this repository */
85 * We don't care about the tree any more
86 * after it has been marked uninteresting.
88 free_tree_buffer(tree);
91 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
99 if (obj->flags & UNINTERESTING)
101 obj->flags |= UNINTERESTING;
102 mark_tree_contents_uninteresting(r, tree);
105 struct path_and_oids_entry {
106 struct hashmap_entry ent;
111 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
112 const struct hashmap_entry *eptr,
113 const struct hashmap_entry *entry_or_key,
116 const struct path_and_oids_entry *e1, *e2;
118 e1 = container_of(eptr, const struct path_and_oids_entry, ent);
119 e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
121 return strcmp(e1->path, e2->path);
124 static void paths_and_oids_init(struct hashmap *map)
126 hashmap_init(map, path_and_oids_cmp, NULL, 0);
129 static void paths_and_oids_clear(struct hashmap *map)
131 struct hashmap_iter iter;
132 struct path_and_oids_entry *entry;
134 hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
135 oidset_clear(&entry->trees);
139 hashmap_free_entries(map, struct path_and_oids_entry, ent);
142 static void paths_and_oids_insert(struct hashmap *map,
144 const struct object_id *oid)
146 int hash = strhash(path);
147 struct path_and_oids_entry key;
148 struct path_and_oids_entry *entry;
150 hashmap_entry_init(&key.ent, hash);
152 /* use a shallow copy for the lookup */
153 key.path = (char *)path;
154 oidset_init(&key.trees, 0);
156 entry = hashmap_get_entry(map, &key, ent, NULL);
158 entry = xcalloc(1, sizeof(struct path_and_oids_entry));
159 hashmap_entry_init(&entry->ent, hash);
160 entry->path = xstrdup(key.path);
161 oidset_init(&entry->trees, 16);
162 hashmap_put(map, &entry->ent);
165 oidset_insert(&entry->trees, oid);
168 static void add_children_by_path(struct repository *r,
172 struct tree_desc desc;
173 struct name_entry entry;
178 if (parse_tree_gently(tree, 1) < 0)
181 init_tree_desc(&desc, tree->buffer, tree->size);
182 while (tree_entry(&desc, &entry)) {
183 switch (object_type(entry.mode)) {
185 paths_and_oids_insert(map, entry.path, &entry.oid);
187 if (tree->object.flags & UNINTERESTING) {
188 struct tree *child = lookup_tree(r, &entry.oid);
190 child->object.flags |= UNINTERESTING;
194 if (tree->object.flags & UNINTERESTING) {
195 struct blob *child = lookup_blob(r, &entry.oid);
197 child->object.flags |= UNINTERESTING;
201 /* Subproject commit - not in this repository */
206 free_tree_buffer(tree);
209 void mark_trees_uninteresting_sparse(struct repository *r,
210 struct oidset *trees)
212 unsigned has_interesting = 0, has_uninteresting = 0;
214 struct hashmap_iter map_iter;
215 struct path_and_oids_entry *entry;
216 struct object_id *oid;
217 struct oidset_iter iter;
219 oidset_iter_init(trees, &iter);
220 while ((!has_interesting || !has_uninteresting) &&
221 (oid = oidset_iter_next(&iter))) {
222 struct tree *tree = lookup_tree(r, oid);
227 if (tree->object.flags & UNINTERESTING)
228 has_uninteresting = 1;
233 /* Do not walk unless we have both types of trees. */
234 if (!has_uninteresting || !has_interesting)
237 paths_and_oids_init(&map);
239 oidset_iter_init(trees, &iter);
240 while ((oid = oidset_iter_next(&iter))) {
241 struct tree *tree = lookup_tree(r, oid);
242 add_children_by_path(r, tree, &map);
245 hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
246 mark_trees_uninteresting_sparse(r, &entry->trees);
248 paths_and_oids_clear(&map);
251 struct commit_stack {
252 struct commit **items;
255 #define COMMIT_STACK_INIT { NULL, 0, 0 }
257 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
259 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
260 stack->items[stack->nr++] = commit;
263 static struct commit *commit_stack_pop(struct commit_stack *stack)
265 return stack->nr ? stack->items[--stack->nr] : NULL;
268 static void commit_stack_clear(struct commit_stack *stack)
270 FREE_AND_NULL(stack->items);
271 stack->nr = stack->alloc = 0;
274 static void mark_one_parent_uninteresting(struct commit *commit,
275 struct commit_stack *pending)
277 struct commit_list *l;
279 if (commit->object.flags & UNINTERESTING)
281 commit->object.flags |= UNINTERESTING;
284 * Normally we haven't parsed the parent
285 * yet, so we won't have a parent of a parent
286 * here. However, it may turn out that we've
287 * reached this commit some other way (where it
288 * wasn't uninteresting), in which case we need
289 * to mark its parents recursively too..
291 for (l = commit->parents; l; l = l->next)
292 commit_stack_push(pending, l->item);
295 void mark_parents_uninteresting(struct commit *commit)
297 struct commit_stack pending = COMMIT_STACK_INIT;
298 struct commit_list *l;
300 for (l = commit->parents; l; l = l->next)
301 mark_one_parent_uninteresting(l->item, &pending);
303 while (pending.nr > 0)
304 mark_one_parent_uninteresting(commit_stack_pop(&pending),
307 commit_stack_clear(&pending);
310 static void add_pending_object_with_path(struct rev_info *revs,
312 const char *name, unsigned mode,
317 if (revs->no_walk && (obj->flags & UNINTERESTING))
319 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
320 struct strbuf buf = STRBUF_INIT;
321 int len = interpret_branch_name(name, 0, &buf, 0);
323 if (0 < len && name[len] && buf.len)
324 strbuf_addstr(&buf, name + len);
325 add_reflog_for_walk(revs->reflog_info,
326 (struct commit *)obj,
327 buf.buf[0] ? buf.buf: name);
328 strbuf_release(&buf);
329 return; /* do not add the commit itself */
331 add_object_array_with_path(obj, name, &revs->pending, mode, path);
334 static void add_pending_object_with_mode(struct rev_info *revs,
336 const char *name, unsigned mode)
338 add_pending_object_with_path(revs, obj, name, mode, NULL);
341 void add_pending_object(struct rev_info *revs,
342 struct object *obj, const char *name)
344 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
347 void add_head_to_pending(struct rev_info *revs)
349 struct object_id oid;
351 if (get_oid("HEAD", &oid))
353 obj = parse_object(revs->repo, &oid);
356 add_pending_object(revs, obj, "HEAD");
359 static struct object *get_reference(struct rev_info *revs, const char *name,
360 const struct object_id *oid,
363 struct object *object;
366 * If the repository has commit graphs, repo_parse_commit() avoids
367 * reading the object buffer, so use it whenever possible.
369 if (oid_object_info(revs->repo, oid, NULL) == OBJ_COMMIT) {
370 struct commit *c = lookup_commit(revs->repo, oid);
371 if (!repo_parse_commit(revs->repo, c))
372 object = (struct object *) c;
376 object = parse_object(revs->repo, oid);
380 if (revs->ignore_missing)
382 if (revs->exclude_promisor_objects && is_promisor_object(oid))
384 die("bad object %s", name);
386 object->flags |= flags;
390 void add_pending_oid(struct rev_info *revs, const char *name,
391 const struct object_id *oid, unsigned int flags)
393 struct object *object = get_reference(revs, name, oid, flags);
394 add_pending_object(revs, object, name);
397 static struct commit *handle_commit(struct rev_info *revs,
398 struct object_array_entry *entry)
400 struct object *object = entry->item;
401 const char *name = entry->name;
402 const char *path = entry->path;
403 unsigned int mode = entry->mode;
404 unsigned long flags = object->flags;
407 * Tag object? Look what it points to..
409 while (object->type == OBJ_TAG) {
410 struct tag *tag = (struct tag *) object;
411 if (revs->tag_objects && !(flags & UNINTERESTING))
412 add_pending_object(revs, object, tag->tag);
413 object = parse_object(revs->repo, get_tagged_oid(tag));
415 if (revs->ignore_missing_links || (flags & UNINTERESTING))
417 if (revs->exclude_promisor_objects &&
418 is_promisor_object(&tag->tagged->oid))
420 die("bad object %s", oid_to_hex(&tag->tagged->oid));
422 object->flags |= flags;
424 * We'll handle the tagged object by looping or dropping
425 * through to the non-tag handlers below. Do not
426 * propagate path data from the tag's pending entry.
433 * Commit object? Just return it, we'll do all the complex
436 if (object->type == OBJ_COMMIT) {
437 struct commit *commit = (struct commit *)object;
439 if (parse_commit(commit) < 0)
440 die("unable to parse commit %s", name);
441 if (flags & UNINTERESTING) {
442 mark_parents_uninteresting(commit);
444 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
448 char **slot = revision_sources_at(revs->sources, commit);
451 *slot = xstrdup(name);
457 * Tree object? Either mark it uninteresting, or add it
458 * to the list of objects to look at later..
460 if (object->type == OBJ_TREE) {
461 struct tree *tree = (struct tree *)object;
462 if (!revs->tree_objects)
464 if (flags & UNINTERESTING) {
465 mark_tree_contents_uninteresting(revs->repo, tree);
468 add_pending_object_with_path(revs, object, name, mode, path);
473 * Blob object? You know the drill by now..
475 if (object->type == OBJ_BLOB) {
476 if (!revs->blob_objects)
478 if (flags & UNINTERESTING)
480 add_pending_object_with_path(revs, object, name, mode, path);
483 die("%s is unknown object", name);
486 static int everybody_uninteresting(struct commit_list *orig,
487 struct commit **interesting_cache)
489 struct commit_list *list = orig;
491 if (*interesting_cache) {
492 struct commit *commit = *interesting_cache;
493 if (!(commit->object.flags & UNINTERESTING))
498 struct commit *commit = list->item;
500 if (commit->object.flags & UNINTERESTING)
503 *interesting_cache = commit;
510 * A definition of "relevant" commit that we can use to simplify limited graphs
511 * by eliminating side branches.
513 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
514 * in our list), or that is a specified BOTTOM commit. Then after computing
515 * a limited list, during processing we can generally ignore boundary merges
516 * coming from outside the graph, (ie from irrelevant parents), and treat
517 * those merges as if they were single-parent. TREESAME is defined to consider
518 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
519 * we don't care if we were !TREESAME to non-graph parents.
521 * Treating bottom commits as relevant ensures that a limited graph's
522 * connection to the actual bottom commit is not viewed as a side branch, but
523 * treated as part of the graph. For example:
525 * ....Z...A---X---o---o---B
529 * When computing "A..B", the A-X connection is at least as important as
530 * Y-X, despite A being flagged UNINTERESTING.
532 * And when computing --ancestry-path "A..B", the A-X connection is more
533 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
535 static inline int relevant_commit(struct commit *commit)
537 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
541 * Return a single relevant commit from a parent list. If we are a TREESAME
542 * commit, and this selects one of our parents, then we can safely simplify to
545 static struct commit *one_relevant_parent(const struct rev_info *revs,
546 struct commit_list *orig)
548 struct commit_list *list = orig;
549 struct commit *relevant = NULL;
555 * For 1-parent commits, or if first-parent-only, then return that
556 * first parent (even if not "relevant" by the above definition).
557 * TREESAME will have been set purely on that parent.
559 if (revs->first_parent_only || !orig->next)
563 * For multi-parent commits, identify a sole relevant parent, if any.
564 * If we have only one relevant parent, then TREESAME will be set purely
565 * with regard to that parent, and we can simplify accordingly.
567 * If we have more than one relevant parent, or no relevant parents
568 * (and multiple irrelevant ones), then we can't select a parent here
572 struct commit *commit = list->item;
574 if (relevant_commit(commit)) {
584 * The goal is to get REV_TREE_NEW as the result only if the
585 * diff consists of all '+' (and no other changes), REV_TREE_OLD
586 * if the whole diff is removal of old data, and otherwise
587 * REV_TREE_DIFFERENT (of course if the trees are the same we
588 * want REV_TREE_SAME).
590 * The only time we care about the distinction is when
591 * remove_empty_trees is in effect, in which case we care only about
592 * whether the whole change is REV_TREE_NEW, or if there's another type
593 * of change. Which means we can stop the diff early in either of these
596 * 1. We're not using remove_empty_trees at all.
598 * 2. We saw anything except REV_TREE_NEW.
600 static int tree_difference = REV_TREE_SAME;
602 static void file_add_remove(struct diff_options *options,
603 int addremove, unsigned mode,
604 const struct object_id *oid,
606 const char *fullpath, unsigned dirty_submodule)
608 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
609 struct rev_info *revs = options->change_fn_data;
611 tree_difference |= diff;
612 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
613 options->flags.has_changes = 1;
616 static void file_change(struct diff_options *options,
617 unsigned old_mode, unsigned new_mode,
618 const struct object_id *old_oid,
619 const struct object_id *new_oid,
620 int old_oid_valid, int new_oid_valid,
621 const char *fullpath,
622 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
624 tree_difference = REV_TREE_DIFFERENT;
625 options->flags.has_changes = 1;
628 static void prepare_to_use_bloom_filter(struct rev_info *revs)
630 struct pathspec_item *pi;
631 char *path_alloc = NULL;
639 repo_parse_commit(revs->repo, revs->commits->item);
641 if (!revs->repo->objects->commit_graph)
644 revs->bloom_filter_settings = revs->repo->objects->commit_graph->bloom_filter_settings;
645 if (!revs->bloom_filter_settings)
648 pi = &revs->pruning.pathspec.items[0];
649 last_index = pi->len - 1;
651 /* remove single trailing slash from path, if needed */
652 if (pi->match[last_index] == '/') {
653 path_alloc = xstrdup(pi->match);
654 path_alloc[last_index] = '\0';
661 revs->bloom_key = xmalloc(sizeof(struct bloom_key));
662 fill_bloom_key(path, len, revs->bloom_key, revs->bloom_filter_settings);
667 static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
668 struct commit *commit)
670 struct bloom_filter *filter;
673 if (!revs->repo->objects->commit_graph)
676 if (commit->generation == GENERATION_NUMBER_INFINITY)
679 filter = get_bloom_filter(revs->repo, commit, 0);
689 result = bloom_filter_contains(filter,
691 revs->bloom_filter_settings);
696 static int rev_compare_tree(struct rev_info *revs,
697 struct commit *parent, struct commit *commit, int nth_parent)
699 struct tree *t1 = get_commit_tree(parent);
700 struct tree *t2 = get_commit_tree(commit);
708 if (revs->simplify_by_decoration) {
710 * If we are simplifying by decoration, then the commit
711 * is worth showing if it has a tag pointing at it.
713 if (get_name_decoration(&commit->object))
714 return REV_TREE_DIFFERENT;
716 * A commit that is not pointed by a tag is uninteresting
717 * if we are not limited by path. This means that you will
718 * see the usual "commits that touch the paths" plus any
719 * tagged commit by specifying both --simplify-by-decoration
722 if (!revs->prune_data.nr)
723 return REV_TREE_SAME;
726 if (revs->bloom_key && !nth_parent) {
727 bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
730 return REV_TREE_SAME;
733 tree_difference = REV_TREE_SAME;
734 revs->pruning.flags.has_changes = 0;
735 if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
737 return REV_TREE_DIFFERENT;
739 return tree_difference;
742 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
745 struct tree *t1 = get_commit_tree(commit);
750 tree_difference = REV_TREE_SAME;
751 revs->pruning.flags.has_changes = 0;
752 retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
754 return retval >= 0 && (tree_difference == REV_TREE_SAME);
757 struct treesame_state {
758 unsigned int nparents;
759 unsigned char treesame[FLEX_ARRAY];
762 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
764 unsigned n = commit_list_count(commit->parents);
765 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
767 add_decoration(&revs->treesame, &commit->object, st);
772 * Must be called immediately after removing the nth_parent from a commit's
773 * parent list, if we are maintaining the per-parent treesame[] decoration.
774 * This does not recalculate the master TREESAME flag - update_treesame()
775 * should be called to update it after a sequence of treesame[] modifications
776 * that may have affected it.
778 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
780 struct treesame_state *st;
783 if (!commit->parents) {
785 * Have just removed the only parent from a non-merge.
786 * Different handling, as we lack decoration.
789 die("compact_treesame %u", nth_parent);
790 old_same = !!(commit->object.flags & TREESAME);
791 if (rev_same_tree_as_empty(revs, commit))
792 commit->object.flags |= TREESAME;
794 commit->object.flags &= ~TREESAME;
798 st = lookup_decoration(&revs->treesame, &commit->object);
799 if (!st || nth_parent >= st->nparents)
800 die("compact_treesame %u", nth_parent);
802 old_same = st->treesame[nth_parent];
803 memmove(st->treesame + nth_parent,
804 st->treesame + nth_parent + 1,
805 st->nparents - nth_parent - 1);
808 * If we've just become a non-merge commit, update TREESAME
809 * immediately, and remove the no-longer-needed decoration.
810 * If still a merge, defer update until update_treesame().
812 if (--st->nparents == 1) {
813 if (commit->parents->next)
814 die("compact_treesame parents mismatch");
815 if (st->treesame[0] && revs->dense)
816 commit->object.flags |= TREESAME;
818 commit->object.flags &= ~TREESAME;
819 free(add_decoration(&revs->treesame, &commit->object, NULL));
825 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
827 if (commit->parents && commit->parents->next) {
829 struct treesame_state *st;
830 struct commit_list *p;
831 unsigned relevant_parents;
832 unsigned relevant_change, irrelevant_change;
834 st = lookup_decoration(&revs->treesame, &commit->object);
836 die("update_treesame %s", oid_to_hex(&commit->object.oid));
837 relevant_parents = 0;
838 relevant_change = irrelevant_change = 0;
839 for (p = commit->parents, n = 0; p; n++, p = p->next) {
840 if (relevant_commit(p->item)) {
841 relevant_change |= !st->treesame[n];
844 irrelevant_change |= !st->treesame[n];
846 if (relevant_parents ? relevant_change : irrelevant_change)
847 commit->object.flags &= ~TREESAME;
849 commit->object.flags |= TREESAME;
852 return commit->object.flags & TREESAME;
855 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
858 * TREESAME is irrelevant unless prune && dense;
859 * if simplify_history is set, we can't have a mixture of TREESAME and
860 * !TREESAME INTERESTING parents (and we don't have treesame[]
861 * decoration anyway);
862 * if first_parent_only is set, then the TREESAME flag is locked
863 * against the first parent (and again we lack treesame[] decoration).
865 return revs->prune && revs->dense &&
866 !revs->simplify_history &&
867 !revs->first_parent_only;
870 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
872 struct commit_list **pp, *parent;
873 struct treesame_state *ts = NULL;
874 int relevant_change = 0, irrelevant_change = 0;
875 int relevant_parents, nth_parent;
878 * If we don't do pruning, everything is interesting
883 if (!get_commit_tree(commit))
886 if (!commit->parents) {
887 if (rev_same_tree_as_empty(revs, commit))
888 commit->object.flags |= TREESAME;
893 * Normal non-merge commit? If we don't want to make the
894 * history dense, we consider it always to be a change..
896 if (!revs->dense && !commit->parents->next)
899 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
900 (parent = *pp) != NULL;
901 pp = &parent->next, nth_parent++) {
902 struct commit *p = parent->item;
903 if (relevant_commit(p))
906 if (nth_parent == 1) {
908 * This our second loop iteration - so we now know
909 * we're dealing with a merge.
911 * Do not compare with later parents when we care only about
912 * the first parent chain, in order to avoid derailing the
913 * traversal to follow a side branch that brought everything
914 * in the path we are limited to by the pathspec.
916 if (revs->first_parent_only)
919 * If this will remain a potentially-simplifiable
920 * merge, remember per-parent treesame if needed.
921 * Initialise the array with the comparison from our
924 if (revs->treesame.name &&
925 !revs->simplify_history &&
926 !(commit->object.flags & UNINTERESTING)) {
927 ts = initialise_treesame(revs, commit);
928 if (!(irrelevant_change || relevant_change))
932 if (parse_commit(p) < 0)
933 die("cannot simplify commit %s (because of %s)",
934 oid_to_hex(&commit->object.oid),
935 oid_to_hex(&p->object.oid));
936 switch (rev_compare_tree(revs, p, commit, nth_parent)) {
938 if (!revs->simplify_history || !relevant_commit(p)) {
939 /* Even if a merge with an uninteresting
940 * side branch brought the entire change
941 * we are interested in, we do not want
942 * to lose the other branches of this
943 * merge, so we just keep going.
946 ts->treesame[nth_parent] = 1;
950 commit->parents = parent;
951 commit->object.flags |= TREESAME;
955 if (revs->remove_empty_trees &&
956 rev_same_tree_as_empty(revs, p)) {
957 /* We are adding all the specified
958 * paths from this parent, so the
959 * history beyond this parent is not
960 * interesting. Remove its parents
961 * (they are grandparents for us).
962 * IOW, we pretend this parent is a
965 if (parse_commit(p) < 0)
966 die("cannot simplify commit %s (invalid %s)",
967 oid_to_hex(&commit->object.oid),
968 oid_to_hex(&p->object.oid));
973 case REV_TREE_DIFFERENT:
974 if (relevant_commit(p))
977 irrelevant_change = 1;
980 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
984 * TREESAME is straightforward for single-parent commits. For merge
985 * commits, it is most useful to define it so that "irrelevant"
986 * parents cannot make us !TREESAME - if we have any relevant
987 * parents, then we only consider TREESAMEness with respect to them,
988 * allowing irrelevant merges from uninteresting branches to be
989 * simplified away. Only if we have only irrelevant parents do we
990 * base TREESAME on them. Note that this logic is replicated in
991 * update_treesame, which should be kept in sync.
993 if (relevant_parents ? !relevant_change : !irrelevant_change)
994 commit->object.flags |= TREESAME;
997 static int process_parents(struct rev_info *revs, struct commit *commit,
998 struct commit_list **list, struct prio_queue *queue)
1000 struct commit_list *parent = commit->parents;
1003 if (commit->object.flags & ADDED)
1005 commit->object.flags |= ADDED;
1007 if (revs->include_check &&
1008 !revs->include_check(commit, revs->include_check_data))
1012 * If the commit is uninteresting, don't try to
1013 * prune parents - we want the maximal uninteresting
1016 * Normally we haven't parsed the parent
1017 * yet, so we won't have a parent of a parent
1018 * here. However, it may turn out that we've
1019 * reached this commit some other way (where it
1020 * wasn't uninteresting), in which case we need
1021 * to mark its parents recursively too..
1023 if (commit->object.flags & UNINTERESTING) {
1025 struct commit *p = parent->item;
1026 parent = parent->next;
1028 p->object.flags |= UNINTERESTING;
1029 if (parse_commit_gently(p, 1) < 0)
1032 mark_parents_uninteresting(p);
1033 if (p->object.flags & SEEN)
1035 p->object.flags |= SEEN;
1037 commit_list_insert_by_date(p, list);
1039 prio_queue_put(queue, p);
1045 * Ok, the commit wasn't uninteresting. Try to
1046 * simplify the commit history and find the parent
1047 * that has no differences in the path set if one exists.
1049 try_to_simplify_commit(revs, commit);
1054 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
1056 for (parent = commit->parents; parent; parent = parent->next) {
1057 struct commit *p = parent->item;
1058 int gently = revs->ignore_missing_links ||
1059 revs->exclude_promisor_objects;
1060 if (parse_commit_gently(p, gently) < 0) {
1061 if (revs->exclude_promisor_objects &&
1062 is_promisor_object(&p->object.oid)) {
1063 if (revs->first_parent_only)
1069 if (revs->sources) {
1070 char **slot = revision_sources_at(revs->sources, p);
1073 *slot = *revision_sources_at(revs->sources, commit);
1075 p->object.flags |= left_flag;
1076 if (!(p->object.flags & SEEN)) {
1077 p->object.flags |= SEEN;
1079 commit_list_insert_by_date(p, list);
1081 prio_queue_put(queue, p);
1083 if (revs->first_parent_only)
1089 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1091 struct commit_list *p;
1092 int left_count = 0, right_count = 0;
1094 struct patch_ids ids;
1095 unsigned cherry_flag;
1097 /* First count the commits on the left and on the right */
1098 for (p = list; p; p = p->next) {
1099 struct commit *commit = p->item;
1100 unsigned flags = commit->object.flags;
1101 if (flags & BOUNDARY)
1103 else if (flags & SYMMETRIC_LEFT)
1109 if (!left_count || !right_count)
1112 left_first = left_count < right_count;
1113 init_patch_ids(revs->repo, &ids);
1114 ids.diffopts.pathspec = revs->diffopt.pathspec;
1116 /* Compute patch-ids for one side */
1117 for (p = list; p; p = p->next) {
1118 struct commit *commit = p->item;
1119 unsigned flags = commit->object.flags;
1121 if (flags & BOUNDARY)
1124 * If we have fewer left, left_first is set and we omit
1125 * commits on the right branch in this loop. If we have
1126 * fewer right, we skip the left ones.
1128 if (left_first != !!(flags & SYMMETRIC_LEFT))
1130 add_commit_patch_id(commit, &ids);
1133 /* either cherry_mark or cherry_pick are true */
1134 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1136 /* Check the other side */
1137 for (p = list; p; p = p->next) {
1138 struct commit *commit = p->item;
1139 struct patch_id *id;
1140 unsigned flags = commit->object.flags;
1142 if (flags & BOUNDARY)
1145 * If we have fewer left, left_first is set and we omit
1146 * commits on the left branch in this loop.
1148 if (left_first == !!(flags & SYMMETRIC_LEFT))
1152 * Have we seen the same patch id?
1154 id = has_commit_patch_id(commit, &ids);
1158 commit->object.flags |= cherry_flag;
1159 id->commit->object.flags |= cherry_flag;
1162 free_patch_ids(&ids);
1165 /* How many extra uninteresting commits we want to see.. */
1168 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1169 struct commit **interesting_cache)
1172 * No source list at all? We're definitely done..
1178 * Does the destination list contain entries with a date
1179 * before the source list? Definitely _not_ done.
1181 if (date <= src->item->date)
1185 * Does the source list still have interesting commits in
1186 * it? Definitely not done..
1188 if (!everybody_uninteresting(src, interesting_cache))
1191 /* Ok, we're closing in.. */
1196 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1197 * of B but not ancestors of A but further limits the result to those
1198 * that are descendants of A. This takes the list of bottom commits and
1199 * the result of "A..B" without --ancestry-path, and limits the latter
1200 * further to the ones that can reach one of the commits in "bottom".
1202 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
1204 struct commit_list *p;
1205 struct commit_list *rlist = NULL;
1209 * Reverse the list so that it will be likely that we would
1210 * process parents before children.
1212 for (p = list; p; p = p->next)
1213 commit_list_insert(p->item, &rlist);
1215 for (p = bottom; p; p = p->next)
1216 p->item->object.flags |= TMP_MARK;
1219 * Mark the ones that can reach bottom commits in "list",
1220 * in a bottom-up fashion.
1224 for (p = rlist; p; p = p->next) {
1225 struct commit *c = p->item;
1226 struct commit_list *parents;
1227 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1229 for (parents = c->parents;
1231 parents = parents->next) {
1232 if (!(parents->item->object.flags & TMP_MARK))
1234 c->object.flags |= TMP_MARK;
1239 } while (made_progress);
1242 * NEEDSWORK: decide if we want to remove parents that are
1243 * not marked with TMP_MARK from commit->parents for commits
1244 * in the resulting list. We may not want to do that, though.
1248 * The ones that are not marked with TMP_MARK are uninteresting
1250 for (p = list; p; p = p->next) {
1251 struct commit *c = p->item;
1252 if (c->object.flags & TMP_MARK)
1254 c->object.flags |= UNINTERESTING;
1257 /* We are done with the TMP_MARK */
1258 for (p = list; p; p = p->next)
1259 p->item->object.flags &= ~TMP_MARK;
1260 for (p = bottom; p; p = p->next)
1261 p->item->object.flags &= ~TMP_MARK;
1262 free_commit_list(rlist);
1266 * Before walking the history, keep the set of "negative" refs the
1267 * caller has asked to exclude.
1269 * This is used to compute "rev-list --ancestry-path A..B", as we need
1270 * to filter the result of "A..B" further to the ones that can actually
1273 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1275 struct commit_list *elem, *bottom = NULL;
1276 for (elem = list; elem; elem = elem->next)
1277 if (elem->item->object.flags & BOTTOM)
1278 commit_list_insert(elem->item, &bottom);
1282 /* Assumes either left_only or right_only is set */
1283 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1285 struct commit_list *p;
1287 for (p = list; p; p = p->next) {
1288 struct commit *commit = p->item;
1290 if (revs->right_only) {
1291 if (commit->object.flags & SYMMETRIC_LEFT)
1292 commit->object.flags |= SHOWN;
1293 } else /* revs->left_only is set */
1294 if (!(commit->object.flags & SYMMETRIC_LEFT))
1295 commit->object.flags |= SHOWN;
1299 static int limit_list(struct rev_info *revs)
1302 timestamp_t date = TIME_MAX;
1303 struct commit_list *list = revs->commits;
1304 struct commit_list *newlist = NULL;
1305 struct commit_list **p = &newlist;
1306 struct commit_list *bottom = NULL;
1307 struct commit *interesting_cache = NULL;
1309 if (revs->ancestry_path) {
1310 bottom = collect_bottom_commits(list);
1312 die("--ancestry-path given but there are no bottom commits");
1316 struct commit *commit = pop_commit(&list);
1317 struct object *obj = &commit->object;
1318 show_early_output_fn_t show;
1320 if (commit == interesting_cache)
1321 interesting_cache = NULL;
1323 if (revs->max_age != -1 && (commit->date < revs->max_age))
1324 obj->flags |= UNINTERESTING;
1325 if (process_parents(revs, commit, &list, NULL) < 0)
1327 if (obj->flags & UNINTERESTING) {
1328 mark_parents_uninteresting(commit);
1329 slop = still_interesting(list, date, slop, &interesting_cache);
1334 if (revs->min_age != -1 && (commit->date > revs->min_age))
1336 date = commit->date;
1337 p = &commit_list_insert(commit, p)->next;
1339 show = show_early_output;
1343 show(revs, newlist);
1344 show_early_output = NULL;
1346 if (revs->cherry_pick || revs->cherry_mark)
1347 cherry_pick_list(newlist, revs);
1349 if (revs->left_only || revs->right_only)
1350 limit_left_right(newlist, revs);
1353 limit_to_ancestry(bottom, newlist);
1354 free_commit_list(bottom);
1358 * Check if any commits have become TREESAME by some of their parents
1359 * becoming UNINTERESTING.
1361 if (limiting_can_increase_treesame(revs))
1362 for (list = newlist; list; list = list->next) {
1363 struct commit *c = list->item;
1364 if (c->object.flags & (UNINTERESTING | TREESAME))
1366 update_treesame(revs, c);
1369 revs->commits = newlist;
1374 * Add an entry to refs->cmdline with the specified information.
1377 static void add_rev_cmdline(struct rev_info *revs,
1378 struct object *item,
1383 struct rev_cmdline_info *info = &revs->cmdline;
1384 unsigned int nr = info->nr;
1386 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1387 info->rev[nr].item = item;
1388 info->rev[nr].name = xstrdup(name);
1389 info->rev[nr].whence = whence;
1390 info->rev[nr].flags = flags;
1394 static void add_rev_cmdline_list(struct rev_info *revs,
1395 struct commit_list *commit_list,
1399 while (commit_list) {
1400 struct object *object = &commit_list->item->object;
1401 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1403 commit_list = commit_list->next;
1407 struct all_refs_cb {
1409 int warned_bad_reflog;
1410 struct rev_info *all_revs;
1411 const char *name_for_errormsg;
1412 struct worktree *wt;
1415 int ref_excluded(struct string_list *ref_excludes, const char *path)
1417 struct string_list_item *item;
1421 for_each_string_list_item(item, ref_excludes) {
1422 if (!wildmatch(item->string, path, 0))
1428 static int handle_one_ref(const char *path, const struct object_id *oid,
1429 int flag, void *cb_data)
1431 struct all_refs_cb *cb = cb_data;
1432 struct object *object;
1434 if (ref_excluded(cb->all_revs->ref_excludes, path))
1437 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1438 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1439 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1443 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1446 cb->all_revs = revs;
1447 cb->all_flags = flags;
1448 revs->rev_input_given = 1;
1452 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1454 if (*ref_excludes_p) {
1455 string_list_clear(*ref_excludes_p, 0);
1456 free(*ref_excludes_p);
1458 *ref_excludes_p = NULL;
1461 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1463 if (!*ref_excludes_p) {
1464 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1465 (*ref_excludes_p)->strdup_strings = 1;
1467 string_list_append(*ref_excludes_p, exclude);
1470 static void handle_refs(struct ref_store *refs,
1471 struct rev_info *revs, unsigned flags,
1472 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1474 struct all_refs_cb cb;
1477 /* this could happen with uninitialized submodules */
1481 init_all_refs_cb(&cb, revs, flags);
1482 for_each(refs, handle_one_ref, &cb);
1485 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1487 struct all_refs_cb *cb = cb_data;
1488 if (!is_null_oid(oid)) {
1489 struct object *o = parse_object(cb->all_revs->repo, oid);
1491 o->flags |= cb->all_flags;
1492 /* ??? CMDLINEFLAGS ??? */
1493 add_pending_object(cb->all_revs, o, "");
1495 else if (!cb->warned_bad_reflog) {
1496 warning("reflog of '%s' references pruned commits",
1497 cb->name_for_errormsg);
1498 cb->warned_bad_reflog = 1;
1503 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1504 const char *email, timestamp_t timestamp, int tz,
1505 const char *message, void *cb_data)
1507 handle_one_reflog_commit(ooid, cb_data);
1508 handle_one_reflog_commit(noid, cb_data);
1512 static int handle_one_reflog(const char *refname_in_wt,
1513 const struct object_id *oid,
1514 int flag, void *cb_data)
1516 struct all_refs_cb *cb = cb_data;
1517 struct strbuf refname = STRBUF_INIT;
1519 cb->warned_bad_reflog = 0;
1520 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1521 cb->name_for_errormsg = refname.buf;
1522 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1524 handle_one_reflog_ent, cb_data);
1525 strbuf_release(&refname);
1529 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1531 struct worktree **worktrees, **p;
1533 worktrees = get_worktrees(0);
1534 for (p = worktrees; *p; p++) {
1535 struct worktree *wt = *p;
1541 refs_for_each_reflog(get_worktree_ref_store(wt),
1545 free_worktrees(worktrees);
1548 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1550 struct all_refs_cb cb;
1553 cb.all_flags = flags;
1555 for_each_reflog(handle_one_reflog, &cb);
1557 if (!revs->single_worktree)
1558 add_other_reflogs_to_pending(&cb);
1561 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1562 struct strbuf *path, unsigned int flags)
1564 size_t baselen = path->len;
1567 if (it->entry_count >= 0) {
1568 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1569 tree->object.flags |= flags;
1570 add_pending_object_with_path(revs, &tree->object, "",
1574 for (i = 0; i < it->subtree_nr; i++) {
1575 struct cache_tree_sub *sub = it->down[i];
1576 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1577 add_cache_tree(sub->cache_tree, revs, path, flags);
1578 strbuf_setlen(path, baselen);
1583 static void do_add_index_objects_to_pending(struct rev_info *revs,
1584 struct index_state *istate,
1589 for (i = 0; i < istate->cache_nr; i++) {
1590 struct cache_entry *ce = istate->cache[i];
1593 if (S_ISGITLINK(ce->ce_mode))
1596 blob = lookup_blob(revs->repo, &ce->oid);
1598 die("unable to add index blob to traversal");
1599 blob->object.flags |= flags;
1600 add_pending_object_with_path(revs, &blob->object, "",
1601 ce->ce_mode, ce->name);
1604 if (istate->cache_tree) {
1605 struct strbuf path = STRBUF_INIT;
1606 add_cache_tree(istate->cache_tree, revs, &path, flags);
1607 strbuf_release(&path);
1611 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1613 struct worktree **worktrees, **p;
1615 repo_read_index(revs->repo);
1616 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1618 if (revs->single_worktree)
1621 worktrees = get_worktrees(0);
1622 for (p = worktrees; *p; p++) {
1623 struct worktree *wt = *p;
1624 struct index_state istate = { NULL };
1627 continue; /* current index already taken care of */
1629 if (read_index_from(&istate,
1630 worktree_git_path(wt, "index"),
1631 get_worktree_git_dir(wt)) > 0)
1632 do_add_index_objects_to_pending(revs, &istate, flags);
1633 discard_index(&istate);
1635 free_worktrees(worktrees);
1638 struct add_alternate_refs_data {
1639 struct rev_info *revs;
1643 static void add_one_alternate_ref(const struct object_id *oid,
1646 const char *name = ".alternate";
1647 struct add_alternate_refs_data *data = vdata;
1650 obj = get_reference(data->revs, name, oid, data->flags);
1651 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1652 add_pending_object(data->revs, obj, name);
1655 static void add_alternate_refs_to_pending(struct rev_info *revs,
1658 struct add_alternate_refs_data data;
1661 for_each_alternate_ref(add_one_alternate_ref, &data);
1664 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1667 struct object_id oid;
1669 struct commit *commit;
1670 struct commit_list *parents;
1672 const char *arg = arg_;
1675 flags ^= UNINTERESTING | BOTTOM;
1678 if (get_oid_committish(arg, &oid))
1681 it = get_reference(revs, arg, &oid, 0);
1682 if (!it && revs->ignore_missing)
1684 if (it->type != OBJ_TAG)
1686 if (!((struct tag*)it)->tagged)
1688 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1690 if (it->type != OBJ_COMMIT)
1692 commit = (struct commit *)it;
1693 if (exclude_parent &&
1694 exclude_parent > commit_list_count(commit->parents))
1696 for (parents = commit->parents, parent_number = 1;
1698 parents = parents->next, parent_number++) {
1699 if (exclude_parent && parent_number != exclude_parent)
1702 it = &parents->item->object;
1704 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1705 add_pending_object(revs, it, arg);
1710 void repo_init_revisions(struct repository *r,
1711 struct rev_info *revs,
1714 memset(revs, 0, sizeof(*revs));
1717 revs->abbrev = DEFAULT_ABBREV;
1718 revs->ignore_merges = 1;
1719 revs->simplify_history = 1;
1720 revs->pruning.repo = r;
1721 revs->pruning.flags.recursive = 1;
1722 revs->pruning.flags.quick = 1;
1723 revs->pruning.add_remove = file_add_remove;
1724 revs->pruning.change = file_change;
1725 revs->pruning.change_fn_data = revs;
1726 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1728 revs->prefix = prefix;
1731 revs->skip_count = -1;
1732 revs->max_count = -1;
1733 revs->max_parents = -1;
1734 revs->expand_tabs_in_log = -1;
1736 revs->commit_format = CMIT_FMT_DEFAULT;
1737 revs->expand_tabs_in_log_default = 8;
1739 init_grep_defaults(revs->repo);
1740 grep_init(&revs->grep_filter, revs->repo, prefix);
1741 revs->grep_filter.status_only = 1;
1743 repo_diff_setup(revs->repo, &revs->diffopt);
1744 if (prefix && !revs->diffopt.prefix) {
1745 revs->diffopt.prefix = prefix;
1746 revs->diffopt.prefix_length = strlen(prefix);
1749 init_display_notes(&revs->notes_opt);
1752 static void add_pending_commit_list(struct rev_info *revs,
1753 struct commit_list *commit_list,
1756 while (commit_list) {
1757 struct object *object = &commit_list->item->object;
1758 object->flags |= flags;
1759 add_pending_object(revs, object, oid_to_hex(&object->oid));
1760 commit_list = commit_list->next;
1764 static void prepare_show_merge(struct rev_info *revs)
1766 struct commit_list *bases;
1767 struct commit *head, *other;
1768 struct object_id oid;
1769 const char **prune = NULL;
1770 int i, prune_num = 1; /* counting terminating NULL */
1771 struct index_state *istate = revs->repo->index;
1773 if (get_oid("HEAD", &oid))
1774 die("--merge without HEAD?");
1775 head = lookup_commit_or_die(&oid, "HEAD");
1776 if (get_oid("MERGE_HEAD", &oid))
1777 die("--merge without MERGE_HEAD?");
1778 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1779 add_pending_object(revs, &head->object, "HEAD");
1780 add_pending_object(revs, &other->object, "MERGE_HEAD");
1781 bases = get_merge_bases(head, other);
1782 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1783 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1784 free_commit_list(bases);
1785 head->object.flags |= SYMMETRIC_LEFT;
1787 if (!istate->cache_nr)
1788 repo_read_index(revs->repo);
1789 for (i = 0; i < istate->cache_nr; i++) {
1790 const struct cache_entry *ce = istate->cache[i];
1793 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1795 REALLOC_ARRAY(prune, prune_num);
1796 prune[prune_num-2] = ce->name;
1797 prune[prune_num-1] = NULL;
1799 while ((i+1 < istate->cache_nr) &&
1800 ce_same_name(ce, istate->cache[i+1]))
1803 clear_pathspec(&revs->prune_data);
1804 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1805 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1809 static int dotdot_missing(const char *arg, char *dotdot,
1810 struct rev_info *revs, int symmetric)
1812 if (revs->ignore_missing)
1814 /* de-munge so we report the full argument */
1817 ? "Invalid symmetric difference expression %s"
1818 : "Invalid revision range %s", arg);
1821 static int handle_dotdot_1(const char *arg, char *dotdot,
1822 struct rev_info *revs, int flags,
1823 int cant_be_filename,
1824 struct object_context *a_oc,
1825 struct object_context *b_oc)
1827 const char *a_name, *b_name;
1828 struct object_id a_oid, b_oid;
1829 struct object *a_obj, *b_obj;
1830 unsigned int a_flags, b_flags;
1832 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1833 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1839 b_name = dotdot + 2;
1840 if (*b_name == '.') {
1847 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
1848 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
1851 if (!cant_be_filename) {
1853 verify_non_filename(revs->prefix, arg);
1857 a_obj = parse_object(revs->repo, &a_oid);
1858 b_obj = parse_object(revs->repo, &b_oid);
1859 if (!a_obj || !b_obj)
1860 return dotdot_missing(arg, dotdot, revs, symmetric);
1865 a_flags = flags_exclude;
1867 /* A...B -- find merge bases between the two */
1868 struct commit *a, *b;
1869 struct commit_list *exclude;
1871 a = lookup_commit_reference(revs->repo, &a_obj->oid);
1872 b = lookup_commit_reference(revs->repo, &b_obj->oid);
1874 return dotdot_missing(arg, dotdot, revs, symmetric);
1876 exclude = get_merge_bases(a, b);
1877 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1879 add_pending_commit_list(revs, exclude, flags_exclude);
1880 free_commit_list(exclude);
1883 a_flags = flags | SYMMETRIC_LEFT;
1886 a_obj->flags |= a_flags;
1887 b_obj->flags |= b_flags;
1888 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1889 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1890 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1891 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1895 static int handle_dotdot(const char *arg,
1896 struct rev_info *revs, int flags,
1897 int cant_be_filename)
1899 struct object_context a_oc, b_oc;
1900 char *dotdot = strstr(arg, "..");
1906 memset(&a_oc, 0, sizeof(a_oc));
1907 memset(&b_oc, 0, sizeof(b_oc));
1910 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
1920 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1922 struct object_context oc;
1924 struct object *object;
1925 struct object_id oid;
1927 const char *arg = arg_;
1928 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1929 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
1931 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1933 if (!cant_be_filename && !strcmp(arg, "..")) {
1935 * Just ".."? That is not a range but the
1936 * pathspec for the parent directory.
1941 if (!handle_dotdot(arg, revs, flags, revarg_opt))
1944 mark = strstr(arg, "^@");
1945 if (mark && !mark[2]) {
1947 if (add_parents_only(revs, arg, flags, 0))
1951 mark = strstr(arg, "^!");
1952 if (mark && !mark[2]) {
1954 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
1957 mark = strstr(arg, "^-");
1959 int exclude_parent = 1;
1963 exclude_parent = strtoul(mark + 2, &end, 10);
1964 if (*end != '\0' || !exclude_parent)
1969 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
1975 local_flags = UNINTERESTING | BOTTOM;
1979 if (revarg_opt & REVARG_COMMITTISH)
1980 get_sha1_flags |= GET_OID_COMMITTISH;
1982 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
1983 return revs->ignore_missing ? 0 : -1;
1984 if (!cant_be_filename)
1985 verify_non_filename(revs->prefix, arg);
1986 object = get_reference(revs, arg, &oid, flags ^ local_flags);
1988 return revs->ignore_missing ? 0 : -1;
1989 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1990 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
1995 static void read_pathspec_from_stdin(struct strbuf *sb,
1996 struct argv_array *prune)
1998 while (strbuf_getline(sb, stdin) != EOF)
1999 argv_array_push(prune, sb->buf);
2002 static void read_revisions_from_stdin(struct rev_info *revs,
2003 struct argv_array *prune)
2006 int seen_dashdash = 0;
2009 save_warning = warn_on_object_refname_ambiguity;
2010 warn_on_object_refname_ambiguity = 0;
2012 strbuf_init(&sb, 1000);
2013 while (strbuf_getline(&sb, stdin) != EOF) {
2017 if (sb.buf[0] == '-') {
2018 if (len == 2 && sb.buf[1] == '-') {
2022 die("options not supported in --stdin mode");
2024 if (handle_revision_arg(sb.buf, revs, 0,
2025 REVARG_CANNOT_BE_FILENAME))
2026 die("bad revision '%s'", sb.buf);
2029 read_pathspec_from_stdin(&sb, prune);
2031 strbuf_release(&sb);
2032 warn_on_object_refname_ambiguity = save_warning;
2035 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
2037 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
2040 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
2042 append_header_grep_pattern(&revs->grep_filter, field, pattern);
2045 static void add_message_grep(struct rev_info *revs, const char *pattern)
2047 add_grep(revs, pattern, GREP_PATTERN_BODY);
2050 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
2051 int *unkc, const char **unkv,
2052 const struct setup_revision_opt* opt)
2054 const char *arg = argv[0];
2057 const unsigned hexsz = the_hash_algo->hexsz;
2059 /* pseudo revision arguments */
2060 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
2061 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
2062 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
2063 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
2064 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
2065 !strcmp(arg, "--indexed-objects") ||
2066 !strcmp(arg, "--alternate-refs") ||
2067 starts_with(arg, "--exclude=") ||
2068 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
2069 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
2071 unkv[(*unkc)++] = arg;
2075 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
2076 revs->max_count = atoi(optarg);
2079 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
2080 revs->skip_count = atoi(optarg);
2082 } else if ((*arg == '-') && isdigit(arg[1])) {
2083 /* accept -<digit>, like traditional "head" */
2084 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2085 revs->max_count < 0)
2086 die("'%s': not a non-negative integer", arg + 1);
2088 } else if (!strcmp(arg, "-n")) {
2090 return error("-n requires an argument");
2091 revs->max_count = atoi(argv[1]);
2094 } else if (skip_prefix(arg, "-n", &optarg)) {
2095 revs->max_count = atoi(optarg);
2097 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2098 revs->max_age = atoi(optarg);
2100 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2101 revs->max_age = approxidate(optarg);
2103 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2104 revs->max_age = approxidate(optarg);
2106 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2107 revs->min_age = atoi(optarg);
2109 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2110 revs->min_age = approxidate(optarg);
2112 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2113 revs->min_age = approxidate(optarg);
2115 } else if (!strcmp(arg, "--first-parent")) {
2116 revs->first_parent_only = 1;
2117 } else if (!strcmp(arg, "--ancestry-path")) {
2118 revs->ancestry_path = 1;
2119 revs->simplify_history = 0;
2121 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2122 init_reflog_walk(&revs->reflog_info);
2123 } else if (!strcmp(arg, "--default")) {
2125 return error("bad --default argument");
2126 revs->def = argv[1];
2128 } else if (!strcmp(arg, "--merge")) {
2129 revs->show_merge = 1;
2130 } else if (!strcmp(arg, "--topo-order")) {
2131 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2132 revs->topo_order = 1;
2133 } else if (!strcmp(arg, "--simplify-merges")) {
2134 revs->simplify_merges = 1;
2135 revs->topo_order = 1;
2136 revs->rewrite_parents = 1;
2137 revs->simplify_history = 0;
2139 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2140 revs->simplify_merges = 1;
2141 revs->topo_order = 1;
2142 revs->rewrite_parents = 1;
2143 revs->simplify_history = 0;
2144 revs->simplify_by_decoration = 1;
2147 } else if (!strcmp(arg, "--date-order")) {
2148 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2149 revs->topo_order = 1;
2150 } else if (!strcmp(arg, "--author-date-order")) {
2151 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2152 revs->topo_order = 1;
2153 } else if (!strcmp(arg, "--early-output")) {
2154 revs->early_output = 100;
2155 revs->topo_order = 1;
2156 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2157 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2158 die("'%s': not a non-negative integer", optarg);
2159 revs->topo_order = 1;
2160 } else if (!strcmp(arg, "--parents")) {
2161 revs->rewrite_parents = 1;
2162 revs->print_parents = 1;
2163 } else if (!strcmp(arg, "--dense")) {
2165 } else if (!strcmp(arg, "--sparse")) {
2167 } else if (!strcmp(arg, "--in-commit-order")) {
2168 revs->tree_blobs_in_commit_order = 1;
2169 } else if (!strcmp(arg, "--remove-empty")) {
2170 revs->remove_empty_trees = 1;
2171 } else if (!strcmp(arg, "--merges")) {
2172 revs->min_parents = 2;
2173 } else if (!strcmp(arg, "--no-merges")) {
2174 revs->max_parents = 1;
2175 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2176 revs->min_parents = atoi(optarg);
2177 } else if (!strcmp(arg, "--no-min-parents")) {
2178 revs->min_parents = 0;
2179 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2180 revs->max_parents = atoi(optarg);
2181 } else if (!strcmp(arg, "--no-max-parents")) {
2182 revs->max_parents = -1;
2183 } else if (!strcmp(arg, "--boundary")) {
2185 } else if (!strcmp(arg, "--left-right")) {
2186 revs->left_right = 1;
2187 } else if (!strcmp(arg, "--left-only")) {
2188 if (revs->right_only)
2189 die("--left-only is incompatible with --right-only"
2191 revs->left_only = 1;
2192 } else if (!strcmp(arg, "--right-only")) {
2193 if (revs->left_only)
2194 die("--right-only is incompatible with --left-only");
2195 revs->right_only = 1;
2196 } else if (!strcmp(arg, "--cherry")) {
2197 if (revs->left_only)
2198 die("--cherry is incompatible with --left-only");
2199 revs->cherry_mark = 1;
2200 revs->right_only = 1;
2201 revs->max_parents = 1;
2203 } else if (!strcmp(arg, "--count")) {
2205 } else if (!strcmp(arg, "--cherry-mark")) {
2206 if (revs->cherry_pick)
2207 die("--cherry-mark is incompatible with --cherry-pick");
2208 revs->cherry_mark = 1;
2209 revs->limited = 1; /* needs limit_list() */
2210 } else if (!strcmp(arg, "--cherry-pick")) {
2211 if (revs->cherry_mark)
2212 die("--cherry-pick is incompatible with --cherry-mark");
2213 revs->cherry_pick = 1;
2215 } else if (!strcmp(arg, "--objects")) {
2216 revs->tag_objects = 1;
2217 revs->tree_objects = 1;
2218 revs->blob_objects = 1;
2219 } else if (!strcmp(arg, "--objects-edge")) {
2220 revs->tag_objects = 1;
2221 revs->tree_objects = 1;
2222 revs->blob_objects = 1;
2223 revs->edge_hint = 1;
2224 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2225 revs->tag_objects = 1;
2226 revs->tree_objects = 1;
2227 revs->blob_objects = 1;
2228 revs->edge_hint = 1;
2229 revs->edge_hint_aggressive = 1;
2230 } else if (!strcmp(arg, "--verify-objects")) {
2231 revs->tag_objects = 1;
2232 revs->tree_objects = 1;
2233 revs->blob_objects = 1;
2234 revs->verify_objects = 1;
2235 } else if (!strcmp(arg, "--unpacked")) {
2237 } else if (starts_with(arg, "--unpacked=")) {
2238 die("--unpacked=<packfile> no longer supported.");
2239 } else if (!strcmp(arg, "-r")) {
2241 revs->diffopt.flags.recursive = 1;
2242 } else if (!strcmp(arg, "-t")) {
2244 revs->diffopt.flags.recursive = 1;
2245 revs->diffopt.flags.tree_in_recursive = 1;
2246 } else if (!strcmp(arg, "-m")) {
2247 revs->ignore_merges = 0;
2248 } else if (!strcmp(arg, "-c")) {
2250 revs->dense_combined_merges = 0;
2251 revs->combine_merges = 1;
2252 } else if (!strcmp(arg, "--combined-all-paths")) {
2254 revs->combined_all_paths = 1;
2255 } else if (!strcmp(arg, "--cc")) {
2257 revs->dense_combined_merges = 1;
2258 revs->combine_merges = 1;
2259 } else if (!strcmp(arg, "-v")) {
2260 revs->verbose_header = 1;
2261 } else if (!strcmp(arg, "--pretty")) {
2262 revs->verbose_header = 1;
2263 revs->pretty_given = 1;
2264 get_commit_format(NULL, revs);
2265 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2266 skip_prefix(arg, "--format=", &optarg)) {
2268 * Detached form ("--pretty X" as opposed to "--pretty=X")
2269 * not allowed, since the argument is optional.
2271 revs->verbose_header = 1;
2272 revs->pretty_given = 1;
2273 get_commit_format(optarg, revs);
2274 } else if (!strcmp(arg, "--expand-tabs")) {
2275 revs->expand_tabs_in_log = 8;
2276 } else if (!strcmp(arg, "--no-expand-tabs")) {
2277 revs->expand_tabs_in_log = 0;
2278 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2280 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2281 die("'%s': not a non-negative integer", arg);
2282 revs->expand_tabs_in_log = val;
2283 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2284 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
2285 revs->show_notes_given = 1;
2286 } else if (!strcmp(arg, "--show-signature")) {
2287 revs->show_signature = 1;
2288 } else if (!strcmp(arg, "--no-show-signature")) {
2289 revs->show_signature = 0;
2290 } else if (!strcmp(arg, "--show-linear-break")) {
2291 revs->break_bar = " ..........";
2292 revs->track_linear = 1;
2293 revs->track_first_time = 1;
2294 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2295 revs->break_bar = xstrdup(optarg);
2296 revs->track_linear = 1;
2297 revs->track_first_time = 1;
2298 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2299 skip_prefix(arg, "--notes=", &optarg)) {
2300 if (starts_with(arg, "--show-notes=") &&
2301 revs->notes_opt.use_default_notes < 0)
2302 revs->notes_opt.use_default_notes = 1;
2303 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2304 revs->show_notes_given = 1;
2305 } else if (!strcmp(arg, "--no-notes")) {
2306 disable_display_notes(&revs->notes_opt, &revs->show_notes);
2307 revs->show_notes_given = 1;
2308 } else if (!strcmp(arg, "--standard-notes")) {
2309 revs->show_notes_given = 1;
2310 revs->notes_opt.use_default_notes = 1;
2311 } else if (!strcmp(arg, "--no-standard-notes")) {
2312 revs->notes_opt.use_default_notes = 0;
2313 } else if (!strcmp(arg, "--oneline")) {
2314 revs->verbose_header = 1;
2315 get_commit_format("oneline", revs);
2316 revs->pretty_given = 1;
2317 revs->abbrev_commit = 1;
2318 } else if (!strcmp(arg, "--graph")) {
2319 revs->topo_order = 1;
2320 revs->rewrite_parents = 1;
2321 revs->graph = graph_init(revs);
2322 } else if (!strcmp(arg, "--root")) {
2323 revs->show_root_diff = 1;
2324 } else if (!strcmp(arg, "--no-commit-id")) {
2325 revs->no_commit_id = 1;
2326 } else if (!strcmp(arg, "--always")) {
2327 revs->always_show_header = 1;
2328 } else if (!strcmp(arg, "--no-abbrev")) {
2330 } else if (!strcmp(arg, "--abbrev")) {
2331 revs->abbrev = DEFAULT_ABBREV;
2332 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2333 revs->abbrev = strtoul(optarg, NULL, 10);
2334 if (revs->abbrev < MINIMUM_ABBREV)
2335 revs->abbrev = MINIMUM_ABBREV;
2336 else if (revs->abbrev > hexsz)
2337 revs->abbrev = hexsz;
2338 } else if (!strcmp(arg, "--abbrev-commit")) {
2339 revs->abbrev_commit = 1;
2340 revs->abbrev_commit_given = 1;
2341 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2342 revs->abbrev_commit = 0;
2343 } else if (!strcmp(arg, "--full-diff")) {
2345 revs->full_diff = 1;
2346 } else if (!strcmp(arg, "--full-history")) {
2347 revs->simplify_history = 0;
2348 } else if (!strcmp(arg, "--relative-date")) {
2349 revs->date_mode.type = DATE_RELATIVE;
2350 revs->date_mode_explicit = 1;
2351 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2352 parse_date_format(optarg, &revs->date_mode);
2353 revs->date_mode_explicit = 1;
2355 } else if (!strcmp(arg, "--log-size")) {
2356 revs->show_log_size = 1;
2359 * Grepping the commit log
2361 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2362 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2364 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2365 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2367 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2368 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2370 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2371 add_message_grep(revs, optarg);
2373 } else if (!strcmp(arg, "--grep-debug")) {
2374 revs->grep_filter.debug = 1;
2375 } else if (!strcmp(arg, "--basic-regexp")) {
2376 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2377 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2378 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2379 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2380 revs->grep_filter.ignore_case = 1;
2381 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2382 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2383 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2384 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2385 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2386 } else if (!strcmp(arg, "--all-match")) {
2387 revs->grep_filter.all_match = 1;
2388 } else if (!strcmp(arg, "--invert-grep")) {
2389 revs->invert_grep = 1;
2390 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2391 if (strcmp(optarg, "none"))
2392 git_log_output_encoding = xstrdup(optarg);
2394 git_log_output_encoding = "";
2396 } else if (!strcmp(arg, "--reverse")) {
2398 } else if (!strcmp(arg, "--children")) {
2399 revs->children.name = "children";
2401 } else if (!strcmp(arg, "--ignore-missing")) {
2402 revs->ignore_missing = 1;
2403 } else if (opt && opt->allow_exclude_promisor_objects &&
2404 !strcmp(arg, "--exclude-promisor-objects")) {
2405 if (fetch_if_missing)
2406 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2407 revs->exclude_promisor_objects = 1;
2409 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2411 unkv[(*unkc)++] = arg;
2414 if (revs->graph && revs->track_linear)
2415 die("--show-linear-break and --graph are incompatible");
2420 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2421 const struct option *options,
2422 const char * const usagestr[])
2424 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2425 &ctx->cpidx, ctx->out, NULL);
2427 error("unknown option `%s'", ctx->argv[0]);
2428 usage_with_options(usagestr, options);
2434 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2435 void *cb_data, const char *term)
2437 struct strbuf bisect_refs = STRBUF_INIT;
2439 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2440 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2441 strbuf_release(&bisect_refs);
2445 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2447 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2450 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2452 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2455 static int handle_revision_pseudo_opt(const char *submodule,
2456 struct rev_info *revs,
2457 int argc, const char **argv, int *flags)
2459 const char *arg = argv[0];
2461 struct ref_store *refs;
2466 * We need some something like get_submodule_worktrees()
2467 * before we can go through all worktrees of a submodule,
2468 * .e.g with adding all HEADs from --all, which is not
2469 * supported right now, so stick to single worktree.
2471 if (!revs->single_worktree)
2472 BUG("--single-worktree cannot be used together with submodule");
2473 refs = get_submodule_ref_store(submodule);
2475 refs = get_main_ref_store(revs->repo);
2480 * Commands like "git shortlog" will not accept the options below
2481 * unless parse_revision_opt queues them (as opposed to erroring
2484 * When implementing your new pseudo-option, remember to
2485 * register it in the list at the top of handle_revision_opt.
2487 if (!strcmp(arg, "--all")) {
2488 handle_refs(refs, revs, *flags, refs_for_each_ref);
2489 handle_refs(refs, revs, *flags, refs_head_ref);
2490 if (!revs->single_worktree) {
2491 struct all_refs_cb cb;
2493 init_all_refs_cb(&cb, revs, *flags);
2494 other_head_refs(handle_one_ref, &cb);
2496 clear_ref_exclusion(&revs->ref_excludes);
2497 } else if (!strcmp(arg, "--branches")) {
2498 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2499 clear_ref_exclusion(&revs->ref_excludes);
2500 } else if (!strcmp(arg, "--bisect")) {
2501 read_bisect_terms(&term_bad, &term_good);
2502 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2503 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2504 for_each_good_bisect_ref);
2506 } else if (!strcmp(arg, "--tags")) {
2507 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2508 clear_ref_exclusion(&revs->ref_excludes);
2509 } else if (!strcmp(arg, "--remotes")) {
2510 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2511 clear_ref_exclusion(&revs->ref_excludes);
2512 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2513 struct all_refs_cb cb;
2514 init_all_refs_cb(&cb, revs, *flags);
2515 for_each_glob_ref(handle_one_ref, optarg, &cb);
2516 clear_ref_exclusion(&revs->ref_excludes);
2518 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2519 add_ref_exclusion(&revs->ref_excludes, optarg);
2521 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2522 struct all_refs_cb cb;
2523 init_all_refs_cb(&cb, revs, *flags);
2524 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2525 clear_ref_exclusion(&revs->ref_excludes);
2526 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2527 struct all_refs_cb cb;
2528 init_all_refs_cb(&cb, revs, *flags);
2529 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2530 clear_ref_exclusion(&revs->ref_excludes);
2531 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2532 struct all_refs_cb cb;
2533 init_all_refs_cb(&cb, revs, *flags);
2534 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2535 clear_ref_exclusion(&revs->ref_excludes);
2536 } else if (!strcmp(arg, "--reflog")) {
2537 add_reflogs_to_pending(revs, *flags);
2538 } else if (!strcmp(arg, "--indexed-objects")) {
2539 add_index_objects_to_pending(revs, *flags);
2540 } else if (!strcmp(arg, "--alternate-refs")) {
2541 add_alternate_refs_to_pending(revs, *flags);
2542 } else if (!strcmp(arg, "--not")) {
2543 *flags ^= UNINTERESTING | BOTTOM;
2544 } else if (!strcmp(arg, "--no-walk")) {
2545 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2546 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2548 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2549 * not allowed, since the argument is optional.
2551 if (!strcmp(optarg, "sorted"))
2552 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2553 else if (!strcmp(optarg, "unsorted"))
2554 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2556 return error("invalid argument to --no-walk");
2557 } else if (!strcmp(arg, "--do-walk")) {
2559 } else if (!strcmp(arg, "--single-worktree")) {
2560 revs->single_worktree = 1;
2568 static void NORETURN diagnose_missing_default(const char *def)
2571 const char *refname;
2573 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2574 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2575 die(_("your current branch appears to be broken"));
2577 skip_prefix(refname, "refs/heads/", &refname);
2578 die(_("your current branch '%s' does not have any commits yet"),
2583 * Parse revision information, filling in the "rev_info" structure,
2584 * and removing the used arguments from the argument list.
2586 * Returns the number of arguments left that weren't recognized
2587 * (which are also moved to the head of the argument list)
2589 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2591 int i, flags, left, seen_dashdash, got_rev_arg = 0, revarg_opt;
2592 struct argv_array prune_data = ARGV_ARRAY_INIT;
2593 const char *submodule = NULL;
2594 int seen_end_of_options = 0;
2597 submodule = opt->submodule;
2599 /* First, search for "--" */
2600 if (opt && opt->assume_dashdash) {
2604 for (i = 1; i < argc; i++) {
2605 const char *arg = argv[i];
2606 if (strcmp(arg, "--"))
2611 argv_array_pushv(&prune_data, argv + i + 1);
2617 /* Second, deal with arguments and options */
2619 revarg_opt = opt ? opt->revarg_opt : 0;
2621 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2622 for (left = i = 1; i < argc; i++) {
2623 const char *arg = argv[i];
2624 if (!seen_end_of_options && *arg == '-') {
2627 opts = handle_revision_pseudo_opt(submodule,
2628 revs, argc - i, argv + i,
2635 if (!strcmp(arg, "--stdin")) {
2636 if (revs->disable_stdin) {
2640 if (revs->read_from_stdin++)
2641 die("--stdin given twice?");
2642 read_revisions_from_stdin(revs, &prune_data);
2646 if (!strcmp(arg, "--end-of-options")) {
2647 seen_end_of_options = 1;
2651 opts = handle_revision_opt(revs, argc - i, argv + i,
2663 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2665 if (seen_dashdash || *arg == '^')
2666 die("bad revision '%s'", arg);
2668 /* If we didn't have a "--":
2669 * (1) all filenames must exist;
2670 * (2) all rev-args must not be interpretable
2671 * as a valid filename.
2672 * but the latter we have checked in the main loop.
2674 for (j = i; j < argc; j++)
2675 verify_filename(revs->prefix, argv[j], j == i);
2677 argv_array_pushv(&prune_data, argv + i);
2684 if (prune_data.argc) {
2686 * If we need to introduce the magic "a lone ':' means no
2687 * pathspec whatsoever", here is the place to do so.
2689 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2690 * prune_data.nr = 0;
2691 * prune_data.alloc = 0;
2692 * free(prune_data.path);
2693 * prune_data.path = NULL;
2695 * terminate prune_data.alloc with NULL and
2696 * call init_pathspec() to set revs->prune_data here.
2699 parse_pathspec(&revs->prune_data, 0, 0,
2700 revs->prefix, prune_data.argv);
2702 argv_array_clear(&prune_data);
2704 if (revs->def == NULL)
2705 revs->def = opt ? opt->def : NULL;
2706 if (opt && opt->tweak)
2707 opt->tweak(revs, opt);
2708 if (revs->show_merge)
2709 prepare_show_merge(revs);
2710 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
2711 struct object_id oid;
2712 struct object *object;
2713 struct object_context oc;
2714 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2715 diagnose_missing_default(revs->def);
2716 object = get_reference(revs, revs->def, &oid, 0);
2717 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2720 /* Did the user ask for any diff output? Run the diff! */
2721 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2724 /* Pickaxe, diff-filter and rename following need diffs */
2725 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2726 revs->diffopt.filter ||
2727 revs->diffopt.flags.follow_renames)
2730 if (revs->diffopt.objfind)
2731 revs->simplify_history = 0;
2733 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2736 if (revs->prune_data.nr) {
2737 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2738 /* Can't prune commits with rename following: the paths change.. */
2739 if (!revs->diffopt.flags.follow_renames)
2741 if (!revs->full_diff)
2742 copy_pathspec(&revs->diffopt.pathspec,
2745 if (revs->combine_merges)
2746 revs->ignore_merges = 0;
2747 if (revs->combined_all_paths && !revs->combine_merges)
2748 die("--combined-all-paths makes no sense without -c or --cc");
2750 revs->diffopt.abbrev = revs->abbrev;
2752 if (revs->line_level_traverse) {
2754 revs->topo_order = 1;
2757 diff_setup_done(&revs->diffopt);
2759 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2760 &revs->grep_filter);
2761 if (!is_encoding_utf8(get_log_output_encoding()))
2762 revs->grep_filter.ignore_locale = 1;
2763 compile_grep_patterns(&revs->grep_filter);
2765 if (revs->reverse && revs->reflog_info)
2766 die("cannot combine --reverse with --walk-reflogs");
2767 if (revs->reflog_info && revs->limited)
2768 die("cannot combine --walk-reflogs with history-limiting options");
2769 if (revs->rewrite_parents && revs->children.name)
2770 die("cannot combine --parents and --children");
2773 * Limitations on the graph functionality
2775 if (revs->reverse && revs->graph)
2776 die("cannot combine --reverse with --graph");
2778 if (revs->reflog_info && revs->graph)
2779 die("cannot combine --walk-reflogs with --graph");
2780 if (revs->no_walk && revs->graph)
2781 die("cannot combine --no-walk with --graph");
2782 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2783 die("cannot use --grep-reflog without --walk-reflogs");
2785 if (revs->first_parent_only && revs->bisect)
2786 die(_("--first-parent is incompatible with --bisect"));
2788 if (revs->line_level_traverse &&
2789 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2790 die(_("-L does not yet support diff formats besides -p and -s"));
2792 if (revs->expand_tabs_in_log < 0)
2793 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2798 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2800 struct commit_list *l = xcalloc(1, sizeof(*l));
2803 l->next = add_decoration(&revs->children, &parent->object, l);
2806 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2808 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2809 struct commit_list **pp, *p;
2810 int surviving_parents;
2812 /* Examine existing parents while marking ones we have seen... */
2813 pp = &commit->parents;
2814 surviving_parents = 0;
2815 while ((p = *pp) != NULL) {
2816 struct commit *parent = p->item;
2817 if (parent->object.flags & TMP_MARK) {
2820 compact_treesame(revs, commit, surviving_parents);
2823 parent->object.flags |= TMP_MARK;
2824 surviving_parents++;
2827 /* clear the temporary mark */
2828 for (p = commit->parents; p; p = p->next) {
2829 p->item->object.flags &= ~TMP_MARK;
2831 /* no update_treesame() - removing duplicates can't affect TREESAME */
2832 return surviving_parents;
2835 struct merge_simplify_state {
2836 struct commit *simplified;
2839 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2841 struct merge_simplify_state *st;
2843 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2845 st = xcalloc(1, sizeof(*st));
2846 add_decoration(&revs->merge_simplification, &commit->object, st);
2851 static int mark_redundant_parents(struct commit *commit)
2853 struct commit_list *h = reduce_heads(commit->parents);
2854 int i = 0, marked = 0;
2855 struct commit_list *po, *pn;
2857 /* Want these for sanity-checking only */
2858 int orig_cnt = commit_list_count(commit->parents);
2859 int cnt = commit_list_count(h);
2862 * Not ready to remove items yet, just mark them for now, based
2863 * on the output of reduce_heads(). reduce_heads outputs the reduced
2864 * set in its original order, so this isn't too hard.
2866 po = commit->parents;
2869 if (pn && po->item == pn->item) {
2873 po->item->object.flags |= TMP_MARK;
2879 if (i != cnt || cnt+marked != orig_cnt)
2880 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2882 free_commit_list(h);
2887 static int mark_treesame_root_parents(struct commit *commit)
2889 struct commit_list *p;
2892 for (p = commit->parents; p; p = p->next) {
2893 struct commit *parent = p->item;
2894 if (!parent->parents && (parent->object.flags & TREESAME)) {
2895 parent->object.flags |= TMP_MARK;
2904 * Awkward naming - this means one parent we are TREESAME to.
2905 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2906 * empty tree). Better name suggestions?
2908 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2910 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2911 struct commit *unmarked = NULL, *marked = NULL;
2912 struct commit_list *p;
2915 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2916 if (ts->treesame[n]) {
2917 if (p->item->object.flags & TMP_MARK) {
2930 * If we are TREESAME to a marked-for-deletion parent, but not to any
2931 * unmarked parents, unmark the first TREESAME parent. This is the
2932 * parent that the default simplify_history==1 scan would have followed,
2933 * and it doesn't make sense to omit that path when asking for a
2934 * simplified full history. Retaining it improves the chances of
2935 * understanding odd missed merges that took an old version of a file.
2939 * I--------*X A modified the file, but mainline merge X used
2940 * \ / "-s ours", so took the version from I. X is
2941 * `-*A--' TREESAME to I and !TREESAME to A.
2943 * Default log from X would produce "I". Without this check,
2944 * --full-history --simplify-merges would produce "I-A-X", showing
2945 * the merge commit X and that it changed A, but not making clear that
2946 * it had just taken the I version. With this check, the topology above
2949 * Note that it is possible that the simplification chooses a different
2950 * TREESAME parent from the default, in which case this test doesn't
2951 * activate, and we _do_ drop the default parent. Example:
2953 * I------X A modified the file, but it was reverted in B,
2954 * \ / meaning mainline merge X is TREESAME to both
2957 * Default log would produce "I" by following the first parent;
2958 * --full-history --simplify-merges will produce "I-A-B". But this is a
2959 * reasonable result - it presents a logical full history leading from
2960 * I to X, and X is not an important merge.
2962 if (!unmarked && marked) {
2963 marked->object.flags &= ~TMP_MARK;
2970 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2972 struct commit_list **pp, *p;
2973 int nth_parent, removed = 0;
2975 pp = &commit->parents;
2977 while ((p = *pp) != NULL) {
2978 struct commit *parent = p->item;
2979 if (parent->object.flags & TMP_MARK) {
2980 parent->object.flags &= ~TMP_MARK;
2984 compact_treesame(revs, commit, nth_parent);
2991 /* Removing parents can only increase TREESAMEness */
2992 if (removed && !(commit->object.flags & TREESAME))
2993 update_treesame(revs, commit);
2998 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
3000 struct commit_list *p;
3001 struct commit *parent;
3002 struct merge_simplify_state *st, *pst;
3005 st = locate_simplify_state(revs, commit);
3008 * Have we handled this one?
3014 * An UNINTERESTING commit simplifies to itself, so does a
3015 * root commit. We do not rewrite parents of such commit
3018 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
3019 st->simplified = commit;
3024 * Do we know what commit all of our parents that matter
3025 * should be rewritten to? Otherwise we are not ready to
3026 * rewrite this one yet.
3028 for (cnt = 0, p = commit->parents; p; p = p->next) {
3029 pst = locate_simplify_state(revs, p->item);
3030 if (!pst->simplified) {
3031 tail = &commit_list_insert(p->item, tail)->next;
3034 if (revs->first_parent_only)
3038 tail = &commit_list_insert(commit, tail)->next;
3043 * Rewrite our list of parents. Note that this cannot
3044 * affect our TREESAME flags in any way - a commit is
3045 * always TREESAME to its simplification.
3047 for (p = commit->parents; p; p = p->next) {
3048 pst = locate_simplify_state(revs, p->item);
3049 p->item = pst->simplified;
3050 if (revs->first_parent_only)
3054 if (revs->first_parent_only)
3057 cnt = remove_duplicate_parents(revs, commit);
3060 * It is possible that we are a merge and one side branch
3061 * does not have any commit that touches the given paths;
3062 * in such a case, the immediate parent from that branch
3063 * will be rewritten to be the merge base.
3065 * o----X X: the commit we are looking at;
3066 * / / o: a commit that touches the paths;
3069 * Further, a merge of an independent branch that doesn't
3070 * touch the path will reduce to a treesame root parent:
3072 * ----o----X X: the commit we are looking at;
3073 * / o: a commit that touches the paths;
3074 * r r: a root commit not touching the paths
3076 * Detect and simplify both cases.
3079 int marked = mark_redundant_parents(commit);
3080 marked += mark_treesame_root_parents(commit);
3082 marked -= leave_one_treesame_to_parent(revs, commit);
3084 cnt = remove_marked_parents(revs, commit);
3088 * A commit simplifies to itself if it is a root, if it is
3089 * UNINTERESTING, if it touches the given paths, or if it is a
3090 * merge and its parents don't simplify to one relevant commit
3091 * (the first two cases are already handled at the beginning of
3094 * Otherwise, it simplifies to what its sole relevant parent
3098 (commit->object.flags & UNINTERESTING) ||
3099 !(commit->object.flags & TREESAME) ||
3100 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
3101 st->simplified = commit;
3103 pst = locate_simplify_state(revs, parent);
3104 st->simplified = pst->simplified;
3109 static void simplify_merges(struct rev_info *revs)
3111 struct commit_list *list, *next;
3112 struct commit_list *yet_to_do, **tail;
3113 struct commit *commit;
3118 /* feed the list reversed */
3120 for (list = revs->commits; list; list = next) {
3121 commit = list->item;
3124 * Do not free(list) here yet; the original list
3125 * is used later in this function.
3127 commit_list_insert(commit, &yet_to_do);
3134 commit = pop_commit(&list);
3135 tail = simplify_one(revs, commit, tail);
3139 /* clean up the result, removing the simplified ones */
3140 list = revs->commits;
3141 revs->commits = NULL;
3142 tail = &revs->commits;
3144 struct merge_simplify_state *st;
3146 commit = pop_commit(&list);
3147 st = locate_simplify_state(revs, commit);
3148 if (st->simplified == commit)
3149 tail = &commit_list_insert(commit, tail)->next;
3153 static void set_children(struct rev_info *revs)
3155 struct commit_list *l;
3156 for (l = revs->commits; l; l = l->next) {
3157 struct commit *commit = l->item;
3158 struct commit_list *p;
3160 for (p = commit->parents; p; p = p->next)
3161 add_child(revs, p->item, commit);
3165 void reset_revision_walk(void)
3167 clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
3170 static int mark_uninteresting(const struct object_id *oid,
3171 struct packed_git *pack,
3175 struct rev_info *revs = cb;
3176 struct object *o = parse_object(revs->repo, oid);
3177 o->flags |= UNINTERESTING | SEEN;
3181 define_commit_slab(indegree_slab, int);
3182 define_commit_slab(author_date_slab, timestamp_t);
3184 struct topo_walk_info {
3185 uint32_t min_generation;
3186 struct prio_queue explore_queue;
3187 struct prio_queue indegree_queue;
3188 struct prio_queue topo_queue;
3189 struct indegree_slab indegree;
3190 struct author_date_slab author_date;
3193 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3195 if (c->object.flags & flag)
3198 c->object.flags |= flag;
3199 prio_queue_put(q, c);
3202 static void explore_walk_step(struct rev_info *revs)
3204 struct topo_walk_info *info = revs->topo_walk_info;
3205 struct commit_list *p;
3206 struct commit *c = prio_queue_get(&info->explore_queue);
3211 if (parse_commit_gently(c, 1) < 0)
3214 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3215 record_author_date(&info->author_date, c);
3217 if (revs->max_age != -1 && (c->date < revs->max_age))
3218 c->object.flags |= UNINTERESTING;
3220 if (process_parents(revs, c, NULL, NULL) < 0)
3223 if (c->object.flags & UNINTERESTING)
3224 mark_parents_uninteresting(c);
3226 for (p = c->parents; p; p = p->next)
3227 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3230 static void explore_to_depth(struct rev_info *revs,
3231 uint32_t gen_cutoff)
3233 struct topo_walk_info *info = revs->topo_walk_info;
3235 while ((c = prio_queue_peek(&info->explore_queue)) &&
3236 c->generation >= gen_cutoff)
3237 explore_walk_step(revs);
3240 static void indegree_walk_step(struct rev_info *revs)
3242 struct commit_list *p;
3243 struct topo_walk_info *info = revs->topo_walk_info;
3244 struct commit *c = prio_queue_get(&info->indegree_queue);
3249 if (parse_commit_gently(c, 1) < 0)
3252 explore_to_depth(revs, c->generation);
3254 for (p = c->parents; p; p = p->next) {
3255 struct commit *parent = p->item;
3256 int *pi = indegree_slab_at(&info->indegree, parent);
3263 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3265 if (revs->first_parent_only)
3270 static void compute_indegrees_to_depth(struct rev_info *revs,
3271 uint32_t gen_cutoff)
3273 struct topo_walk_info *info = revs->topo_walk_info;
3275 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3276 c->generation >= gen_cutoff)
3277 indegree_walk_step(revs);
3280 static void reset_topo_walk(struct rev_info *revs)
3282 struct topo_walk_info *info = revs->topo_walk_info;
3284 clear_prio_queue(&info->explore_queue);
3285 clear_prio_queue(&info->indegree_queue);
3286 clear_prio_queue(&info->topo_queue);
3287 clear_indegree_slab(&info->indegree);
3288 clear_author_date_slab(&info->author_date);
3290 FREE_AND_NULL(revs->topo_walk_info);
3293 static void init_topo_walk(struct rev_info *revs)
3295 struct topo_walk_info *info;
3296 struct commit_list *list;
3297 if (revs->topo_walk_info)
3298 reset_topo_walk(revs);
3300 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3301 info = revs->topo_walk_info;
3302 memset(info, 0, sizeof(struct topo_walk_info));
3304 init_indegree_slab(&info->indegree);
3305 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3306 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3307 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3309 switch (revs->sort_order) {
3310 default: /* REV_SORT_IN_GRAPH_ORDER */
3311 info->topo_queue.compare = NULL;
3313 case REV_SORT_BY_COMMIT_DATE:
3314 info->topo_queue.compare = compare_commits_by_commit_date;
3316 case REV_SORT_BY_AUTHOR_DATE:
3317 init_author_date_slab(&info->author_date);
3318 info->topo_queue.compare = compare_commits_by_author_date;
3319 info->topo_queue.cb_data = &info->author_date;
3323 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3324 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3326 info->min_generation = GENERATION_NUMBER_INFINITY;
3327 for (list = revs->commits; list; list = list->next) {
3328 struct commit *c = list->item;
3330 if (parse_commit_gently(c, 1))
3333 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3334 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3336 if (c->generation < info->min_generation)
3337 info->min_generation = c->generation;
3339 *(indegree_slab_at(&info->indegree, c)) = 1;
3341 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3342 record_author_date(&info->author_date, c);
3344 compute_indegrees_to_depth(revs, info->min_generation);
3346 for (list = revs->commits; list; list = list->next) {
3347 struct commit *c = list->item;
3349 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3350 prio_queue_put(&info->topo_queue, c);
3354 * This is unfortunate; the initial tips need to be shown
3355 * in the order given from the revision traversal machinery.
3357 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3358 prio_queue_reverse(&info->topo_queue);
3361 static struct commit *next_topo_commit(struct rev_info *revs)
3364 struct topo_walk_info *info = revs->topo_walk_info;
3366 /* pop next off of topo_queue */
3367 c = prio_queue_get(&info->topo_queue);
3370 *(indegree_slab_at(&info->indegree, c)) = 0;
3375 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3377 struct commit_list *p;
3378 struct topo_walk_info *info = revs->topo_walk_info;
3379 if (process_parents(revs, commit, NULL, NULL) < 0) {
3380 if (!revs->ignore_missing_links)
3381 die("Failed to traverse parents of commit %s",
3382 oid_to_hex(&commit->object.oid));
3385 for (p = commit->parents; p; p = p->next) {
3386 struct commit *parent = p->item;
3389 if (parent->object.flags & UNINTERESTING)
3392 if (parse_commit_gently(parent, 1) < 0)
3395 if (parent->generation < info->min_generation) {
3396 info->min_generation = parent->generation;
3397 compute_indegrees_to_depth(revs, info->min_generation);
3400 pi = indegree_slab_at(&info->indegree, parent);
3404 prio_queue_put(&info->topo_queue, parent);
3406 if (revs->first_parent_only)
3411 int prepare_revision_walk(struct rev_info *revs)
3414 struct object_array old_pending;
3415 struct commit_list **next = &revs->commits;
3417 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3418 revs->pending.nr = 0;
3419 revs->pending.alloc = 0;
3420 revs->pending.objects = NULL;
3421 for (i = 0; i < old_pending.nr; i++) {
3422 struct object_array_entry *e = old_pending.objects + i;
3423 struct commit *commit = handle_commit(revs, e);
3425 if (!(commit->object.flags & SEEN)) {
3426 commit->object.flags |= SEEN;
3427 next = commit_list_append(commit, next);
3431 object_array_clear(&old_pending);
3433 /* Signal whether we need per-parent treesame decoration */
3434 if (revs->simplify_merges ||
3435 (revs->limited && limiting_can_increase_treesame(revs)))
3436 revs->treesame.name = "treesame";
3438 if (revs->exclude_promisor_objects) {
3439 for_each_packed_object(mark_uninteresting, revs,
3440 FOR_EACH_OBJECT_PROMISOR_ONLY);
3443 if (revs->pruning.pathspec.nr == 1 && !revs->reflog_info)
3444 prepare_to_use_bloom_filter(revs);
3445 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
3446 commit_list_sort_by_date(&revs->commits);
3449 if (revs->limited) {
3450 if (limit_list(revs) < 0)
3452 if (revs->topo_order)
3453 sort_in_topological_order(&revs->commits, revs->sort_order);
3454 } else if (revs->topo_order)
3455 init_topo_walk(revs);
3456 if (revs->line_level_traverse)
3457 line_log_filter(revs);
3458 if (revs->simplify_merges)
3459 simplify_merges(revs);
3460 if (revs->children.name)
3466 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3468 struct prio_queue *queue)
3471 struct commit *p = *pp;
3473 if (process_parents(revs, p, NULL, queue) < 0)
3474 return rewrite_one_error;
3475 if (p->object.flags & UNINTERESTING)
3476 return rewrite_one_ok;
3477 if (!(p->object.flags & TREESAME))
3478 return rewrite_one_ok;
3480 return rewrite_one_noparents;
3481 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
3482 return rewrite_one_ok;
3487 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3490 struct commit *item = prio_queue_peek(q);
3491 struct commit_list *p = *list;
3493 if (p && p->item->date >= item->date)
3496 p = commit_list_insert(item, list);
3497 list = &p->next; /* skip newly added item */
3498 prio_queue_get(q); /* pop item */
3503 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3505 struct prio_queue queue = { compare_commits_by_commit_date };
3506 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3507 merge_queue_into_list(&queue, &revs->commits);
3508 clear_prio_queue(&queue);
3512 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3513 rewrite_parent_fn_t rewrite_parent)
3515 struct commit_list **pp = &commit->parents;
3517 struct commit_list *parent = *pp;
3518 switch (rewrite_parent(revs, &parent->item)) {
3519 case rewrite_one_ok:
3521 case rewrite_one_noparents:
3524 case rewrite_one_error:
3529 remove_duplicate_parents(revs, commit);
3533 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
3535 char *person, *endp;
3536 size_t len, namelen, maillen;
3539 struct ident_split ident;
3541 person = strstr(buf->buf, what);
3545 person += strlen(what);
3546 endp = strchr(person, '\n');
3550 len = endp - person;
3552 if (split_ident_line(&ident, person, len))
3555 mail = ident.mail_begin;
3556 maillen = ident.mail_end - ident.mail_begin;
3557 name = ident.name_begin;
3558 namelen = ident.name_end - ident.name_begin;
3560 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3561 struct strbuf namemail = STRBUF_INIT;
3563 strbuf_addf(&namemail, "%.*s <%.*s>",
3564 (int)namelen, name, (int)maillen, mail);
3566 strbuf_splice(buf, ident.name_begin - buf->buf,
3567 ident.mail_end - ident.name_begin + 1,
3568 namemail.buf, namemail.len);
3570 strbuf_release(&namemail);
3578 static int commit_match(struct commit *commit, struct rev_info *opt)
3581 const char *encoding;
3582 const char *message;
3583 struct strbuf buf = STRBUF_INIT;
3585 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3588 /* Prepend "fake" headers as needed */
3589 if (opt->grep_filter.use_reflog_filter) {
3590 strbuf_addstr(&buf, "reflog ");
3591 get_reflog_message(&buf, opt->reflog_info);
3592 strbuf_addch(&buf, '\n');
3596 * We grep in the user's output encoding, under the assumption that it
3597 * is the encoding they are most likely to write their grep pattern
3598 * for. In addition, it means we will match the "notes" encoding below,
3599 * so we will not end up with a buffer that has two different encodings
3602 encoding = get_log_output_encoding();
3603 message = logmsg_reencode(commit, NULL, encoding);
3605 /* Copy the commit to temporary if we are using "fake" headers */
3607 strbuf_addstr(&buf, message);
3609 if (opt->grep_filter.header_list && opt->mailmap) {
3611 strbuf_addstr(&buf, message);
3613 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3614 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3617 /* Append "fake" message parts as needed */
3618 if (opt->show_notes) {
3620 strbuf_addstr(&buf, message);
3621 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3625 * Find either in the original commit message, or in the temporary.
3626 * Note that we cast away the constness of "message" here. It is
3627 * const because it may come from the cached commit buffer. That's OK,
3628 * because we know that it is modifiable heap memory, and that while
3629 * grep_buffer may modify it for speed, it will restore any
3630 * changes before returning.
3633 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3635 retval = grep_buffer(&opt->grep_filter,
3636 (char *)message, strlen(message));
3637 strbuf_release(&buf);
3638 unuse_commit_buffer(commit, message);
3639 return opt->invert_grep ? !retval : retval;
3642 static inline int want_ancestry(const struct rev_info *revs)
3644 return (revs->rewrite_parents || revs->children.name);
3648 * Return a timestamp to be used for --since/--until comparisons for this
3649 * commit, based on the revision options.
3651 static timestamp_t comparison_date(const struct rev_info *revs,
3652 struct commit *commit)
3654 return revs->reflog_info ?
3655 get_reflog_timestamp(revs->reflog_info) :
3659 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3661 if (commit->object.flags & SHOWN)
3662 return commit_ignore;
3663 if (revs->unpacked && has_object_pack(&commit->object.oid))
3664 return commit_ignore;
3665 if (commit->object.flags & UNINTERESTING)
3666 return commit_ignore;
3667 if (revs->min_age != -1 &&
3668 comparison_date(revs, commit) > revs->min_age)
3669 return commit_ignore;
3670 if (revs->min_parents || (revs->max_parents >= 0)) {
3671 int n = commit_list_count(commit->parents);
3672 if ((n < revs->min_parents) ||
3673 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3674 return commit_ignore;
3676 if (!commit_match(commit, revs))
3677 return commit_ignore;
3678 if (revs->prune && revs->dense) {
3679 /* Commit without changes? */
3680 if (commit->object.flags & TREESAME) {
3682 struct commit_list *p;
3683 /* drop merges unless we want parenthood */
3684 if (!want_ancestry(revs))
3685 return commit_ignore;
3687 * If we want ancestry, then need to keep any merges
3688 * between relevant commits to tie together topology.
3689 * For consistency with TREESAME and simplification
3690 * use "relevant" here rather than just INTERESTING,
3691 * to treat bottom commit(s) as part of the topology.
3693 for (n = 0, p = commit->parents; p; p = p->next)
3694 if (relevant_commit(p->item))
3697 return commit_ignore;
3703 define_commit_slab(saved_parents, struct commit_list *);
3705 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3708 * You may only call save_parents() once per commit (this is checked
3709 * for non-root commits).
3711 static void save_parents(struct rev_info *revs, struct commit *commit)
3713 struct commit_list **pp;
3715 if (!revs->saved_parents_slab) {
3716 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3717 init_saved_parents(revs->saved_parents_slab);
3720 pp = saved_parents_at(revs->saved_parents_slab, commit);
3723 * When walking with reflogs, we may visit the same commit
3724 * several times: once for each appearance in the reflog.
3726 * In this case, save_parents() will be called multiple times.
3727 * We want to keep only the first set of parents. We need to
3728 * store a sentinel value for an empty (i.e., NULL) parent
3729 * list to distinguish it from a not-yet-saved list, however.
3733 if (commit->parents)
3734 *pp = copy_commit_list(commit->parents);
3736 *pp = EMPTY_PARENT_LIST;
3739 static void free_saved_parents(struct rev_info *revs)
3741 if (revs->saved_parents_slab)
3742 clear_saved_parents(revs->saved_parents_slab);
3745 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3747 struct commit_list *parents;
3749 if (!revs->saved_parents_slab)
3750 return commit->parents;
3752 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3753 if (parents == EMPTY_PARENT_LIST)
3758 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3760 enum commit_action action = get_commit_action(revs, commit);
3762 if (action == commit_show &&
3763 revs->prune && revs->dense && want_ancestry(revs)) {
3765 * --full-diff on simplified parents is no good: it
3766 * will show spurious changes from the commits that
3767 * were elided. So we save the parents on the side
3768 * when --full-diff is in effect.
3770 if (revs->full_diff)
3771 save_parents(revs, commit);
3772 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3773 return commit_error;
3778 static void track_linear(struct rev_info *revs, struct commit *commit)
3780 if (revs->track_first_time) {
3782 revs->track_first_time = 0;
3784 struct commit_list *p;
3785 for (p = revs->previous_parents; p; p = p->next)
3786 if (p->item == NULL || /* first commit */
3787 oideq(&p->item->object.oid, &commit->object.oid))
3789 revs->linear = p != NULL;
3791 if (revs->reverse) {
3793 commit->object.flags |= TRACK_LINEAR;
3795 free_commit_list(revs->previous_parents);
3796 revs->previous_parents = copy_commit_list(commit->parents);
3799 static struct commit *get_revision_1(struct rev_info *revs)
3802 struct commit *commit;
3804 if (revs->reflog_info)
3805 commit = next_reflog_entry(revs->reflog_info);
3806 else if (revs->topo_walk_info)
3807 commit = next_topo_commit(revs);
3809 commit = pop_commit(&revs->commits);
3814 if (revs->reflog_info)
3815 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3818 * If we haven't done the list limiting, we need to look at
3819 * the parents here. We also need to do the date-based limiting
3820 * that we'd otherwise have done in limit_list().
3822 if (!revs->limited) {
3823 if (revs->max_age != -1 &&
3824 comparison_date(revs, commit) < revs->max_age)
3827 if (revs->reflog_info)
3828 try_to_simplify_commit(revs, commit);
3829 else if (revs->topo_walk_info)
3830 expand_topo_walk(revs, commit);
3831 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
3832 if (!revs->ignore_missing_links)
3833 die("Failed to traverse parents of commit %s",
3834 oid_to_hex(&commit->object.oid));
3838 switch (simplify_commit(revs, commit)) {
3842 die("Failed to simplify parents of commit %s",
3843 oid_to_hex(&commit->object.oid));
3845 if (revs->track_linear)
3846 track_linear(revs, commit);
3853 * Return true for entries that have not yet been shown. (This is an
3854 * object_array_each_func_t.)
3856 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3858 return !(entry->item->flags & SHOWN);
3862 * If array is on the verge of a realloc, garbage-collect any entries
3863 * that have already been shown to try to free up some space.
3865 static void gc_boundary(struct object_array *array)
3867 if (array->nr == array->alloc)
3868 object_array_filter(array, entry_unshown, NULL);
3871 static void create_boundary_commit_list(struct rev_info *revs)
3875 struct object_array *array = &revs->boundary_commits;
3876 struct object_array_entry *objects = array->objects;
3879 * If revs->commits is non-NULL at this point, an error occurred in
3880 * get_revision_1(). Ignore the error and continue printing the
3881 * boundary commits anyway. (This is what the code has always
3884 if (revs->commits) {
3885 free_commit_list(revs->commits);
3886 revs->commits = NULL;
3890 * Put all of the actual boundary commits from revs->boundary_commits
3891 * into revs->commits
3893 for (i = 0; i < array->nr; i++) {
3894 c = (struct commit *)(objects[i].item);
3897 if (!(c->object.flags & CHILD_SHOWN))
3899 if (c->object.flags & (SHOWN | BOUNDARY))
3901 c->object.flags |= BOUNDARY;
3902 commit_list_insert(c, &revs->commits);
3906 * If revs->topo_order is set, sort the boundary commits
3907 * in topological order
3909 sort_in_topological_order(&revs->commits, revs->sort_order);
3912 static struct commit *get_revision_internal(struct rev_info *revs)
3914 struct commit *c = NULL;
3915 struct commit_list *l;
3917 if (revs->boundary == 2) {
3919 * All of the normal commits have already been returned,
3920 * and we are now returning boundary commits.
3921 * create_boundary_commit_list() has populated
3922 * revs->commits with the remaining commits to return.
3924 c = pop_commit(&revs->commits);
3926 c->object.flags |= SHOWN;
3931 * If our max_count counter has reached zero, then we are done. We
3932 * don't simply return NULL because we still might need to show
3933 * boundary commits. But we want to avoid calling get_revision_1, which
3934 * might do a considerable amount of work finding the next commit only
3935 * for us to throw it away.
3937 * If it is non-zero, then either we don't have a max_count at all
3938 * (-1), or it is still counting, in which case we decrement.
3940 if (revs->max_count) {
3941 c = get_revision_1(revs);
3943 while (revs->skip_count > 0) {
3945 c = get_revision_1(revs);
3951 if (revs->max_count > 0)
3956 c->object.flags |= SHOWN;
3958 if (!revs->boundary)
3963 * get_revision_1() runs out the commits, and
3964 * we are done computing the boundaries.
3965 * switch to boundary commits output mode.
3970 * Update revs->commits to contain the list of
3973 create_boundary_commit_list(revs);
3975 return get_revision_internal(revs);
3979 * boundary commits are the commits that are parents of the
3980 * ones we got from get_revision_1() but they themselves are
3981 * not returned from get_revision_1(). Before returning
3982 * 'c', we need to mark its parents that they could be boundaries.
3985 for (l = c->parents; l; l = l->next) {
3987 p = &(l->item->object);
3988 if (p->flags & (CHILD_SHOWN | SHOWN))
3990 p->flags |= CHILD_SHOWN;
3991 gc_boundary(&revs->boundary_commits);
3992 add_object_array(p, NULL, &revs->boundary_commits);
3998 struct commit *get_revision(struct rev_info *revs)
4001 struct commit_list *reversed;
4003 if (revs->reverse) {
4005 while ((c = get_revision_internal(revs)))
4006 commit_list_insert(c, &reversed);
4007 revs->commits = reversed;
4009 revs->reverse_output_stage = 1;
4012 if (revs->reverse_output_stage) {
4013 c = pop_commit(&revs->commits);
4014 if (revs->track_linear)
4015 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
4019 c = get_revision_internal(revs);
4020 if (c && revs->graph)
4021 graph_update(revs->graph, c);
4023 free_saved_parents(revs);
4024 if (revs->previous_parents) {
4025 free_commit_list(revs->previous_parents);
4026 revs->previous_parents = NULL;
4032 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
4034 if (commit->object.flags & BOUNDARY)
4036 else if (commit->object.flags & UNINTERESTING)
4038 else if (commit->object.flags & PATCHSAME)
4040 else if (!revs || revs->left_right) {
4041 if (commit->object.flags & SYMMETRIC_LEFT)
4045 } else if (revs->graph)
4047 else if (revs->cherry_mark)
4052 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
4054 const char *mark = get_revision_mark(revs, commit);
4057 fputs(mark, stdout);