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"
32 volatile show_early_output_fn_t show_early_output;
34 static const char *term_bad;
35 static const char *term_good;
37 implement_shared_commit_slab(revision_sources, char *);
39 void show_object_with_name(FILE *out, struct object *obj, const char *name)
43 fprintf(out, "%s ", oid_to_hex(&obj->oid));
44 for (p = name; *p && *p != '\n'; p++)
49 static void mark_blob_uninteresting(struct blob *blob)
53 if (blob->object.flags & UNINTERESTING)
55 blob->object.flags |= UNINTERESTING;
58 static void mark_tree_contents_uninteresting(struct repository *r,
61 struct tree_desc desc;
62 struct name_entry entry;
64 if (parse_tree_gently(tree, 1) < 0)
67 init_tree_desc(&desc, tree->buffer, tree->size);
68 while (tree_entry(&desc, &entry)) {
69 switch (object_type(entry.mode)) {
71 mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
74 mark_blob_uninteresting(lookup_blob(r, &entry.oid));
77 /* Subproject commit - not in this repository */
83 * We don't care about the tree any more
84 * after it has been marked uninteresting.
86 free_tree_buffer(tree);
89 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
97 if (obj->flags & UNINTERESTING)
99 obj->flags |= UNINTERESTING;
100 mark_tree_contents_uninteresting(r, tree);
103 struct path_and_oids_entry {
104 struct hashmap_entry ent;
109 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
110 const struct path_and_oids_entry *e1,
111 const struct path_and_oids_entry *e2,
114 return strcmp(e1->path, e2->path);
117 static void paths_and_oids_init(struct hashmap *map)
119 hashmap_init(map, (hashmap_cmp_fn) path_and_oids_cmp, NULL, 0);
122 static void paths_and_oids_clear(struct hashmap *map)
124 struct hashmap_iter iter;
125 struct path_and_oids_entry *entry;
126 hashmap_iter_init(map, &iter);
128 while ((entry = (struct path_and_oids_entry *)hashmap_iter_next(&iter))) {
129 oidset_clear(&entry->trees);
133 hashmap_free(map, 1);
136 static void paths_and_oids_insert(struct hashmap *map,
138 const struct object_id *oid)
140 int hash = strhash(path);
141 struct path_and_oids_entry key;
142 struct path_and_oids_entry *entry;
144 hashmap_entry_init(&key, hash);
146 /* use a shallow copy for the lookup */
147 key.path = (char *)path;
148 oidset_init(&key.trees, 0);
150 if (!(entry = (struct path_and_oids_entry *)hashmap_get(map, &key, NULL))) {
151 entry = xcalloc(1, sizeof(struct path_and_oids_entry));
152 hashmap_entry_init(entry, hash);
153 entry->path = xstrdup(key.path);
154 oidset_init(&entry->trees, 16);
155 hashmap_put(map, entry);
158 oidset_insert(&entry->trees, oid);
161 static void add_children_by_path(struct repository *r,
165 struct tree_desc desc;
166 struct name_entry entry;
171 if (parse_tree_gently(tree, 1) < 0)
174 init_tree_desc(&desc, tree->buffer, tree->size);
175 while (tree_entry(&desc, &entry)) {
176 switch (object_type(entry.mode)) {
178 paths_and_oids_insert(map, entry.path, &entry.oid);
180 if (tree->object.flags & UNINTERESTING) {
181 struct tree *child = lookup_tree(r, &entry.oid);
183 child->object.flags |= UNINTERESTING;
187 if (tree->object.flags & UNINTERESTING) {
188 struct blob *child = lookup_blob(r, &entry.oid);
190 child->object.flags |= UNINTERESTING;
194 /* Subproject commit - not in this repository */
199 free_tree_buffer(tree);
202 void mark_trees_uninteresting_sparse(struct repository *r,
203 struct oidset *trees)
205 unsigned has_interesting = 0, has_uninteresting = 0;
207 struct hashmap_iter map_iter;
208 struct path_and_oids_entry *entry;
209 struct object_id *oid;
210 struct oidset_iter iter;
212 oidset_iter_init(trees, &iter);
213 while ((!has_interesting || !has_uninteresting) &&
214 (oid = oidset_iter_next(&iter))) {
215 struct tree *tree = lookup_tree(r, oid);
220 if (tree->object.flags & UNINTERESTING)
221 has_uninteresting = 1;
226 /* Do not walk unless we have both types of trees. */
227 if (!has_uninteresting || !has_interesting)
230 paths_and_oids_init(&map);
232 oidset_iter_init(trees, &iter);
233 while ((oid = oidset_iter_next(&iter))) {
234 struct tree *tree = lookup_tree(r, oid);
235 add_children_by_path(r, tree, &map);
238 hashmap_iter_init(&map, &map_iter);
239 while ((entry = hashmap_iter_next(&map_iter)))
240 mark_trees_uninteresting_sparse(r, &entry->trees);
242 paths_and_oids_clear(&map);
245 struct commit_stack {
246 struct commit **items;
249 #define COMMIT_STACK_INIT { NULL, 0, 0 }
251 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
253 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
254 stack->items[stack->nr++] = commit;
257 static struct commit *commit_stack_pop(struct commit_stack *stack)
259 return stack->nr ? stack->items[--stack->nr] : NULL;
262 static void commit_stack_clear(struct commit_stack *stack)
264 FREE_AND_NULL(stack->items);
265 stack->nr = stack->alloc = 0;
268 static void mark_one_parent_uninteresting(struct commit *commit,
269 struct commit_stack *pending)
271 struct commit_list *l;
273 if (commit->object.flags & UNINTERESTING)
275 commit->object.flags |= UNINTERESTING;
278 * Normally we haven't parsed the parent
279 * yet, so we won't have a parent of a parent
280 * here. However, it may turn out that we've
281 * reached this commit some other way (where it
282 * wasn't uninteresting), in which case we need
283 * to mark its parents recursively too..
285 for (l = commit->parents; l; l = l->next)
286 commit_stack_push(pending, l->item);
289 void mark_parents_uninteresting(struct commit *commit)
291 struct commit_stack pending = COMMIT_STACK_INIT;
292 struct commit_list *l;
294 for (l = commit->parents; l; l = l->next)
295 mark_one_parent_uninteresting(l->item, &pending);
297 while (pending.nr > 0)
298 mark_one_parent_uninteresting(commit_stack_pop(&pending),
301 commit_stack_clear(&pending);
304 static void add_pending_object_with_path(struct rev_info *revs,
306 const char *name, unsigned mode,
311 if (revs->no_walk && (obj->flags & UNINTERESTING))
313 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
314 struct strbuf buf = STRBUF_INIT;
315 int len = interpret_branch_name(name, 0, &buf, 0);
317 if (0 < len && name[len] && buf.len)
318 strbuf_addstr(&buf, name + len);
319 add_reflog_for_walk(revs->reflog_info,
320 (struct commit *)obj,
321 buf.buf[0] ? buf.buf: name);
322 strbuf_release(&buf);
323 return; /* do not add the commit itself */
325 add_object_array_with_path(obj, name, &revs->pending, mode, path);
328 static void add_pending_object_with_mode(struct rev_info *revs,
330 const char *name, unsigned mode)
332 add_pending_object_with_path(revs, obj, name, mode, NULL);
335 void add_pending_object(struct rev_info *revs,
336 struct object *obj, const char *name)
338 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
341 void add_head_to_pending(struct rev_info *revs)
343 struct object_id oid;
345 if (get_oid("HEAD", &oid))
347 obj = parse_object(revs->repo, &oid);
350 add_pending_object(revs, obj, "HEAD");
353 static struct object *get_reference(struct rev_info *revs, const char *name,
354 const struct object_id *oid,
357 struct object *object;
360 * If the repository has commit graphs, repo_parse_commit() avoids
361 * reading the object buffer, so use it whenever possible.
363 if (oid_object_info(revs->repo, oid, NULL) == OBJ_COMMIT) {
364 struct commit *c = lookup_commit(revs->repo, oid);
365 if (!repo_parse_commit(revs->repo, c))
366 object = (struct object *) c;
370 object = parse_object(revs->repo, oid);
374 if (revs->ignore_missing)
376 if (revs->exclude_promisor_objects && is_promisor_object(oid))
378 die("bad object %s", name);
380 object->flags |= flags;
384 void add_pending_oid(struct rev_info *revs, const char *name,
385 const struct object_id *oid, unsigned int flags)
387 struct object *object = get_reference(revs, name, oid, flags);
388 add_pending_object(revs, object, name);
391 static struct commit *handle_commit(struct rev_info *revs,
392 struct object_array_entry *entry)
394 struct object *object = entry->item;
395 const char *name = entry->name;
396 const char *path = entry->path;
397 unsigned int mode = entry->mode;
398 unsigned long flags = object->flags;
401 * Tag object? Look what it points to..
403 while (object->type == OBJ_TAG) {
404 struct tag *tag = (struct tag *) object;
405 if (revs->tag_objects && !(flags & UNINTERESTING))
406 add_pending_object(revs, object, tag->tag);
409 object = parse_object(revs->repo, &tag->tagged->oid);
411 if (revs->ignore_missing_links || (flags & UNINTERESTING))
413 if (revs->exclude_promisor_objects &&
414 is_promisor_object(&tag->tagged->oid))
416 die("bad object %s", oid_to_hex(&tag->tagged->oid));
418 object->flags |= flags;
420 * We'll handle the tagged object by looping or dropping
421 * through to the non-tag handlers below. Do not
422 * propagate path data from the tag's pending entry.
429 * Commit object? Just return it, we'll do all the complex
432 if (object->type == OBJ_COMMIT) {
433 struct commit *commit = (struct commit *)object;
435 if (parse_commit(commit) < 0)
436 die("unable to parse commit %s", name);
437 if (flags & UNINTERESTING) {
438 mark_parents_uninteresting(commit);
440 if (!revs->topo_order || !generation_numbers_enabled(the_repository))
444 char **slot = revision_sources_at(revs->sources, commit);
447 *slot = xstrdup(name);
453 * Tree object? Either mark it uninteresting, or add it
454 * to the list of objects to look at later..
456 if (object->type == OBJ_TREE) {
457 struct tree *tree = (struct tree *)object;
458 if (!revs->tree_objects)
460 if (flags & UNINTERESTING) {
461 mark_tree_contents_uninteresting(revs->repo, tree);
464 add_pending_object_with_path(revs, object, name, mode, path);
469 * Blob object? You know the drill by now..
471 if (object->type == OBJ_BLOB) {
472 if (!revs->blob_objects)
474 if (flags & UNINTERESTING)
476 add_pending_object_with_path(revs, object, name, mode, path);
479 die("%s is unknown object", name);
482 static int everybody_uninteresting(struct commit_list *orig,
483 struct commit **interesting_cache)
485 struct commit_list *list = orig;
487 if (*interesting_cache) {
488 struct commit *commit = *interesting_cache;
489 if (!(commit->object.flags & UNINTERESTING))
494 struct commit *commit = list->item;
496 if (commit->object.flags & UNINTERESTING)
499 *interesting_cache = commit;
506 * A definition of "relevant" commit that we can use to simplify limited graphs
507 * by eliminating side branches.
509 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
510 * in our list), or that is a specified BOTTOM commit. Then after computing
511 * a limited list, during processing we can generally ignore boundary merges
512 * coming from outside the graph, (ie from irrelevant parents), and treat
513 * those merges as if they were single-parent. TREESAME is defined to consider
514 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
515 * we don't care if we were !TREESAME to non-graph parents.
517 * Treating bottom commits as relevant ensures that a limited graph's
518 * connection to the actual bottom commit is not viewed as a side branch, but
519 * treated as part of the graph. For example:
521 * ....Z...A---X---o---o---B
525 * When computing "A..B", the A-X connection is at least as important as
526 * Y-X, despite A being flagged UNINTERESTING.
528 * And when computing --ancestry-path "A..B", the A-X connection is more
529 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
531 static inline int relevant_commit(struct commit *commit)
533 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
537 * Return a single relevant commit from a parent list. If we are a TREESAME
538 * commit, and this selects one of our parents, then we can safely simplify to
541 static struct commit *one_relevant_parent(const struct rev_info *revs,
542 struct commit_list *orig)
544 struct commit_list *list = orig;
545 struct commit *relevant = NULL;
551 * For 1-parent commits, or if first-parent-only, then return that
552 * first parent (even if not "relevant" by the above definition).
553 * TREESAME will have been set purely on that parent.
555 if (revs->first_parent_only || !orig->next)
559 * For multi-parent commits, identify a sole relevant parent, if any.
560 * If we have only one relevant parent, then TREESAME will be set purely
561 * with regard to that parent, and we can simplify accordingly.
563 * If we have more than one relevant parent, or no relevant parents
564 * (and multiple irrelevant ones), then we can't select a parent here
568 struct commit *commit = list->item;
570 if (relevant_commit(commit)) {
580 * The goal is to get REV_TREE_NEW as the result only if the
581 * diff consists of all '+' (and no other changes), REV_TREE_OLD
582 * if the whole diff is removal of old data, and otherwise
583 * REV_TREE_DIFFERENT (of course if the trees are the same we
584 * want REV_TREE_SAME).
586 * The only time we care about the distinction is when
587 * remove_empty_trees is in effect, in which case we care only about
588 * whether the whole change is REV_TREE_NEW, or if there's another type
589 * of change. Which means we can stop the diff early in either of these
592 * 1. We're not using remove_empty_trees at all.
594 * 2. We saw anything except REV_TREE_NEW.
596 static int tree_difference = REV_TREE_SAME;
598 static void file_add_remove(struct diff_options *options,
599 int addremove, unsigned mode,
600 const struct object_id *oid,
602 const char *fullpath, unsigned dirty_submodule)
604 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
605 struct rev_info *revs = options->change_fn_data;
607 tree_difference |= diff;
608 if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
609 options->flags.has_changes = 1;
612 static void file_change(struct diff_options *options,
613 unsigned old_mode, unsigned new_mode,
614 const struct object_id *old_oid,
615 const struct object_id *new_oid,
616 int old_oid_valid, int new_oid_valid,
617 const char *fullpath,
618 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
620 tree_difference = REV_TREE_DIFFERENT;
621 options->flags.has_changes = 1;
624 static int rev_compare_tree(struct rev_info *revs,
625 struct commit *parent, struct commit *commit)
627 struct tree *t1 = get_commit_tree(parent);
628 struct tree *t2 = get_commit_tree(commit);
635 if (revs->simplify_by_decoration) {
637 * If we are simplifying by decoration, then the commit
638 * is worth showing if it has a tag pointing at it.
640 if (get_name_decoration(&commit->object))
641 return REV_TREE_DIFFERENT;
643 * A commit that is not pointed by a tag is uninteresting
644 * if we are not limited by path. This means that you will
645 * see the usual "commits that touch the paths" plus any
646 * tagged commit by specifying both --simplify-by-decoration
649 if (!revs->prune_data.nr)
650 return REV_TREE_SAME;
653 tree_difference = REV_TREE_SAME;
654 revs->pruning.flags.has_changes = 0;
655 if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
657 return REV_TREE_DIFFERENT;
658 return tree_difference;
661 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
664 struct tree *t1 = get_commit_tree(commit);
669 tree_difference = REV_TREE_SAME;
670 revs->pruning.flags.has_changes = 0;
671 retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
673 return retval >= 0 && (tree_difference == REV_TREE_SAME);
676 struct treesame_state {
677 unsigned int nparents;
678 unsigned char treesame[FLEX_ARRAY];
681 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
683 unsigned n = commit_list_count(commit->parents);
684 struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
686 add_decoration(&revs->treesame, &commit->object, st);
691 * Must be called immediately after removing the nth_parent from a commit's
692 * parent list, if we are maintaining the per-parent treesame[] decoration.
693 * This does not recalculate the master TREESAME flag - update_treesame()
694 * should be called to update it after a sequence of treesame[] modifications
695 * that may have affected it.
697 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
699 struct treesame_state *st;
702 if (!commit->parents) {
704 * Have just removed the only parent from a non-merge.
705 * Different handling, as we lack decoration.
708 die("compact_treesame %u", nth_parent);
709 old_same = !!(commit->object.flags & TREESAME);
710 if (rev_same_tree_as_empty(revs, commit))
711 commit->object.flags |= TREESAME;
713 commit->object.flags &= ~TREESAME;
717 st = lookup_decoration(&revs->treesame, &commit->object);
718 if (!st || nth_parent >= st->nparents)
719 die("compact_treesame %u", nth_parent);
721 old_same = st->treesame[nth_parent];
722 memmove(st->treesame + nth_parent,
723 st->treesame + nth_parent + 1,
724 st->nparents - nth_parent - 1);
727 * If we've just become a non-merge commit, update TREESAME
728 * immediately, and remove the no-longer-needed decoration.
729 * If still a merge, defer update until update_treesame().
731 if (--st->nparents == 1) {
732 if (commit->parents->next)
733 die("compact_treesame parents mismatch");
734 if (st->treesame[0] && revs->dense)
735 commit->object.flags |= TREESAME;
737 commit->object.flags &= ~TREESAME;
738 free(add_decoration(&revs->treesame, &commit->object, NULL));
744 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
746 if (commit->parents && commit->parents->next) {
748 struct treesame_state *st;
749 struct commit_list *p;
750 unsigned relevant_parents;
751 unsigned relevant_change, irrelevant_change;
753 st = lookup_decoration(&revs->treesame, &commit->object);
755 die("update_treesame %s", oid_to_hex(&commit->object.oid));
756 relevant_parents = 0;
757 relevant_change = irrelevant_change = 0;
758 for (p = commit->parents, n = 0; p; n++, p = p->next) {
759 if (relevant_commit(p->item)) {
760 relevant_change |= !st->treesame[n];
763 irrelevant_change |= !st->treesame[n];
765 if (relevant_parents ? relevant_change : irrelevant_change)
766 commit->object.flags &= ~TREESAME;
768 commit->object.flags |= TREESAME;
771 return commit->object.flags & TREESAME;
774 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
777 * TREESAME is irrelevant unless prune && dense;
778 * if simplify_history is set, we can't have a mixture of TREESAME and
779 * !TREESAME INTERESTING parents (and we don't have treesame[]
780 * decoration anyway);
781 * if first_parent_only is set, then the TREESAME flag is locked
782 * against the first parent (and again we lack treesame[] decoration).
784 return revs->prune && revs->dense &&
785 !revs->simplify_history &&
786 !revs->first_parent_only;
789 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
791 struct commit_list **pp, *parent;
792 struct treesame_state *ts = NULL;
793 int relevant_change = 0, irrelevant_change = 0;
794 int relevant_parents, nth_parent;
797 * If we don't do pruning, everything is interesting
802 if (!get_commit_tree(commit))
805 if (!commit->parents) {
806 if (rev_same_tree_as_empty(revs, commit))
807 commit->object.flags |= TREESAME;
812 * Normal non-merge commit? If we don't want to make the
813 * history dense, we consider it always to be a change..
815 if (!revs->dense && !commit->parents->next)
818 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
819 (parent = *pp) != NULL;
820 pp = &parent->next, nth_parent++) {
821 struct commit *p = parent->item;
822 if (relevant_commit(p))
825 if (nth_parent == 1) {
827 * This our second loop iteration - so we now know
828 * we're dealing with a merge.
830 * Do not compare with later parents when we care only about
831 * the first parent chain, in order to avoid derailing the
832 * traversal to follow a side branch that brought everything
833 * in the path we are limited to by the pathspec.
835 if (revs->first_parent_only)
838 * If this will remain a potentially-simplifiable
839 * merge, remember per-parent treesame if needed.
840 * Initialise the array with the comparison from our
843 if (revs->treesame.name &&
844 !revs->simplify_history &&
845 !(commit->object.flags & UNINTERESTING)) {
846 ts = initialise_treesame(revs, commit);
847 if (!(irrelevant_change || relevant_change))
851 if (parse_commit(p) < 0)
852 die("cannot simplify commit %s (because of %s)",
853 oid_to_hex(&commit->object.oid),
854 oid_to_hex(&p->object.oid));
855 switch (rev_compare_tree(revs, p, commit)) {
857 if (!revs->simplify_history || !relevant_commit(p)) {
858 /* Even if a merge with an uninteresting
859 * side branch brought the entire change
860 * we are interested in, we do not want
861 * to lose the other branches of this
862 * merge, so we just keep going.
865 ts->treesame[nth_parent] = 1;
869 commit->parents = parent;
870 commit->object.flags |= TREESAME;
874 if (revs->remove_empty_trees &&
875 rev_same_tree_as_empty(revs, p)) {
876 /* We are adding all the specified
877 * paths from this parent, so the
878 * history beyond this parent is not
879 * interesting. Remove its parents
880 * (they are grandparents for us).
881 * IOW, we pretend this parent is a
884 if (parse_commit(p) < 0)
885 die("cannot simplify commit %s (invalid %s)",
886 oid_to_hex(&commit->object.oid),
887 oid_to_hex(&p->object.oid));
892 case REV_TREE_DIFFERENT:
893 if (relevant_commit(p))
896 irrelevant_change = 1;
899 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
903 * TREESAME is straightforward for single-parent commits. For merge
904 * commits, it is most useful to define it so that "irrelevant"
905 * parents cannot make us !TREESAME - if we have any relevant
906 * parents, then we only consider TREESAMEness with respect to them,
907 * allowing irrelevant merges from uninteresting branches to be
908 * simplified away. Only if we have only irrelevant parents do we
909 * base TREESAME on them. Note that this logic is replicated in
910 * update_treesame, which should be kept in sync.
912 if (relevant_parents ? !relevant_change : !irrelevant_change)
913 commit->object.flags |= TREESAME;
916 static int process_parents(struct rev_info *revs, struct commit *commit,
917 struct commit_list **list, struct prio_queue *queue)
919 struct commit_list *parent = commit->parents;
922 if (commit->object.flags & ADDED)
924 commit->object.flags |= ADDED;
926 if (revs->include_check &&
927 !revs->include_check(commit, revs->include_check_data))
931 * If the commit is uninteresting, don't try to
932 * prune parents - we want the maximal uninteresting
935 * Normally we haven't parsed the parent
936 * yet, so we won't have a parent of a parent
937 * here. However, it may turn out that we've
938 * reached this commit some other way (where it
939 * wasn't uninteresting), in which case we need
940 * to mark its parents recursively too..
942 if (commit->object.flags & UNINTERESTING) {
944 struct commit *p = parent->item;
945 parent = parent->next;
947 p->object.flags |= UNINTERESTING;
948 if (parse_commit_gently(p, 1) < 0)
951 mark_parents_uninteresting(p);
952 if (p->object.flags & SEEN)
954 p->object.flags |= SEEN;
956 commit_list_insert_by_date(p, list);
958 prio_queue_put(queue, p);
964 * Ok, the commit wasn't uninteresting. Try to
965 * simplify the commit history and find the parent
966 * that has no differences in the path set if one exists.
968 try_to_simplify_commit(revs, commit);
973 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
975 for (parent = commit->parents; parent; parent = parent->next) {
976 struct commit *p = parent->item;
977 int gently = revs->ignore_missing_links ||
978 revs->exclude_promisor_objects;
979 if (parse_commit_gently(p, gently) < 0) {
980 if (revs->exclude_promisor_objects &&
981 is_promisor_object(&p->object.oid)) {
982 if (revs->first_parent_only)
989 char **slot = revision_sources_at(revs->sources, p);
992 *slot = *revision_sources_at(revs->sources, commit);
994 p->object.flags |= left_flag;
995 if (!(p->object.flags & SEEN)) {
996 p->object.flags |= SEEN;
998 commit_list_insert_by_date(p, list);
1000 prio_queue_put(queue, p);
1002 if (revs->first_parent_only)
1008 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
1010 struct commit_list *p;
1011 int left_count = 0, right_count = 0;
1013 struct patch_ids ids;
1014 unsigned cherry_flag;
1016 /* First count the commits on the left and on the right */
1017 for (p = list; p; p = p->next) {
1018 struct commit *commit = p->item;
1019 unsigned flags = commit->object.flags;
1020 if (flags & BOUNDARY)
1022 else if (flags & SYMMETRIC_LEFT)
1028 if (!left_count || !right_count)
1031 left_first = left_count < right_count;
1032 init_patch_ids(revs->repo, &ids);
1033 ids.diffopts.pathspec = revs->diffopt.pathspec;
1035 /* Compute patch-ids for one side */
1036 for (p = list; p; p = p->next) {
1037 struct commit *commit = p->item;
1038 unsigned flags = commit->object.flags;
1040 if (flags & BOUNDARY)
1043 * If we have fewer left, left_first is set and we omit
1044 * commits on the right branch in this loop. If we have
1045 * fewer right, we skip the left ones.
1047 if (left_first != !!(flags & SYMMETRIC_LEFT))
1049 add_commit_patch_id(commit, &ids);
1052 /* either cherry_mark or cherry_pick are true */
1053 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
1055 /* Check the other side */
1056 for (p = list; p; p = p->next) {
1057 struct commit *commit = p->item;
1058 struct patch_id *id;
1059 unsigned flags = commit->object.flags;
1061 if (flags & BOUNDARY)
1064 * If we have fewer left, left_first is set and we omit
1065 * commits on the left branch in this loop.
1067 if (left_first == !!(flags & SYMMETRIC_LEFT))
1071 * Have we seen the same patch id?
1073 id = has_commit_patch_id(commit, &ids);
1077 commit->object.flags |= cherry_flag;
1078 id->commit->object.flags |= cherry_flag;
1081 free_patch_ids(&ids);
1084 /* How many extra uninteresting commits we want to see.. */
1087 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
1088 struct commit **interesting_cache)
1091 * No source list at all? We're definitely done..
1097 * Does the destination list contain entries with a date
1098 * before the source list? Definitely _not_ done.
1100 if (date <= src->item->date)
1104 * Does the source list still have interesting commits in
1105 * it? Definitely not done..
1107 if (!everybody_uninteresting(src, interesting_cache))
1110 /* Ok, we're closing in.. */
1115 * "rev-list --ancestry-path A..B" computes commits that are ancestors
1116 * of B but not ancestors of A but further limits the result to those
1117 * that are descendants of A. This takes the list of bottom commits and
1118 * the result of "A..B" without --ancestry-path, and limits the latter
1119 * further to the ones that can reach one of the commits in "bottom".
1121 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
1123 struct commit_list *p;
1124 struct commit_list *rlist = NULL;
1128 * Reverse the list so that it will be likely that we would
1129 * process parents before children.
1131 for (p = list; p; p = p->next)
1132 commit_list_insert(p->item, &rlist);
1134 for (p = bottom; p; p = p->next)
1135 p->item->object.flags |= TMP_MARK;
1138 * Mark the ones that can reach bottom commits in "list",
1139 * in a bottom-up fashion.
1143 for (p = rlist; p; p = p->next) {
1144 struct commit *c = p->item;
1145 struct commit_list *parents;
1146 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1148 for (parents = c->parents;
1150 parents = parents->next) {
1151 if (!(parents->item->object.flags & TMP_MARK))
1153 c->object.flags |= TMP_MARK;
1158 } while (made_progress);
1161 * NEEDSWORK: decide if we want to remove parents that are
1162 * not marked with TMP_MARK from commit->parents for commits
1163 * in the resulting list. We may not want to do that, though.
1167 * The ones that are not marked with TMP_MARK are uninteresting
1169 for (p = list; p; p = p->next) {
1170 struct commit *c = p->item;
1171 if (c->object.flags & TMP_MARK)
1173 c->object.flags |= UNINTERESTING;
1176 /* We are done with the TMP_MARK */
1177 for (p = list; p; p = p->next)
1178 p->item->object.flags &= ~TMP_MARK;
1179 for (p = bottom; p; p = p->next)
1180 p->item->object.flags &= ~TMP_MARK;
1181 free_commit_list(rlist);
1185 * Before walking the history, keep the set of "negative" refs the
1186 * caller has asked to exclude.
1188 * This is used to compute "rev-list --ancestry-path A..B", as we need
1189 * to filter the result of "A..B" further to the ones that can actually
1192 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1194 struct commit_list *elem, *bottom = NULL;
1195 for (elem = list; elem; elem = elem->next)
1196 if (elem->item->object.flags & BOTTOM)
1197 commit_list_insert(elem->item, &bottom);
1201 /* Assumes either left_only or right_only is set */
1202 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1204 struct commit_list *p;
1206 for (p = list; p; p = p->next) {
1207 struct commit *commit = p->item;
1209 if (revs->right_only) {
1210 if (commit->object.flags & SYMMETRIC_LEFT)
1211 commit->object.flags |= SHOWN;
1212 } else /* revs->left_only is set */
1213 if (!(commit->object.flags & SYMMETRIC_LEFT))
1214 commit->object.flags |= SHOWN;
1218 static int limit_list(struct rev_info *revs)
1221 timestamp_t date = TIME_MAX;
1222 struct commit_list *list = revs->commits;
1223 struct commit_list *newlist = NULL;
1224 struct commit_list **p = &newlist;
1225 struct commit_list *bottom = NULL;
1226 struct commit *interesting_cache = NULL;
1228 if (revs->ancestry_path) {
1229 bottom = collect_bottom_commits(list);
1231 die("--ancestry-path given but there are no bottom commits");
1235 struct commit *commit = pop_commit(&list);
1236 struct object *obj = &commit->object;
1237 show_early_output_fn_t show;
1239 if (commit == interesting_cache)
1240 interesting_cache = NULL;
1242 if (revs->max_age != -1 && (commit->date < revs->max_age))
1243 obj->flags |= UNINTERESTING;
1244 if (process_parents(revs, commit, &list, NULL) < 0)
1246 if (obj->flags & UNINTERESTING) {
1247 mark_parents_uninteresting(commit);
1248 slop = still_interesting(list, date, slop, &interesting_cache);
1253 if (revs->min_age != -1 && (commit->date > revs->min_age))
1255 date = commit->date;
1256 p = &commit_list_insert(commit, p)->next;
1258 show = show_early_output;
1262 show(revs, newlist);
1263 show_early_output = NULL;
1265 if (revs->cherry_pick || revs->cherry_mark)
1266 cherry_pick_list(newlist, revs);
1268 if (revs->left_only || revs->right_only)
1269 limit_left_right(newlist, revs);
1272 limit_to_ancestry(bottom, newlist);
1273 free_commit_list(bottom);
1277 * Check if any commits have become TREESAME by some of their parents
1278 * becoming UNINTERESTING.
1280 if (limiting_can_increase_treesame(revs))
1281 for (list = newlist; list; list = list->next) {
1282 struct commit *c = list->item;
1283 if (c->object.flags & (UNINTERESTING | TREESAME))
1285 update_treesame(revs, c);
1288 revs->commits = newlist;
1293 * Add an entry to refs->cmdline with the specified information.
1296 static void add_rev_cmdline(struct rev_info *revs,
1297 struct object *item,
1302 struct rev_cmdline_info *info = &revs->cmdline;
1303 unsigned int nr = info->nr;
1305 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1306 info->rev[nr].item = item;
1307 info->rev[nr].name = xstrdup(name);
1308 info->rev[nr].whence = whence;
1309 info->rev[nr].flags = flags;
1313 static void add_rev_cmdline_list(struct rev_info *revs,
1314 struct commit_list *commit_list,
1318 while (commit_list) {
1319 struct object *object = &commit_list->item->object;
1320 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1322 commit_list = commit_list->next;
1326 struct all_refs_cb {
1328 int warned_bad_reflog;
1329 struct rev_info *all_revs;
1330 const char *name_for_errormsg;
1331 struct worktree *wt;
1334 int ref_excluded(struct string_list *ref_excludes, const char *path)
1336 struct string_list_item *item;
1340 for_each_string_list_item(item, ref_excludes) {
1341 if (!wildmatch(item->string, path, 0))
1347 static int handle_one_ref(const char *path, const struct object_id *oid,
1348 int flag, void *cb_data)
1350 struct all_refs_cb *cb = cb_data;
1351 struct object *object;
1353 if (ref_excluded(cb->all_revs->ref_excludes, path))
1356 object = get_reference(cb->all_revs, path, oid, cb->all_flags);
1357 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1358 add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
1362 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1365 cb->all_revs = revs;
1366 cb->all_flags = flags;
1367 revs->rev_input_given = 1;
1371 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1373 if (*ref_excludes_p) {
1374 string_list_clear(*ref_excludes_p, 0);
1375 free(*ref_excludes_p);
1377 *ref_excludes_p = NULL;
1380 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1382 if (!*ref_excludes_p) {
1383 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1384 (*ref_excludes_p)->strdup_strings = 1;
1386 string_list_append(*ref_excludes_p, exclude);
1389 static void handle_refs(struct ref_store *refs,
1390 struct rev_info *revs, unsigned flags,
1391 int (*for_each)(struct ref_store *, each_ref_fn, void *))
1393 struct all_refs_cb cb;
1396 /* this could happen with uninitialized submodules */
1400 init_all_refs_cb(&cb, revs, flags);
1401 for_each(refs, handle_one_ref, &cb);
1404 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
1406 struct all_refs_cb *cb = cb_data;
1407 if (!is_null_oid(oid)) {
1408 struct object *o = parse_object(cb->all_revs->repo, oid);
1410 o->flags |= cb->all_flags;
1411 /* ??? CMDLINEFLAGS ??? */
1412 add_pending_object(cb->all_revs, o, "");
1414 else if (!cb->warned_bad_reflog) {
1415 warning("reflog of '%s' references pruned commits",
1416 cb->name_for_errormsg);
1417 cb->warned_bad_reflog = 1;
1422 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1423 const char *email, timestamp_t timestamp, int tz,
1424 const char *message, void *cb_data)
1426 handle_one_reflog_commit(ooid, cb_data);
1427 handle_one_reflog_commit(noid, cb_data);
1431 static int handle_one_reflog(const char *refname_in_wt,
1432 const struct object_id *oid,
1433 int flag, void *cb_data)
1435 struct all_refs_cb *cb = cb_data;
1436 struct strbuf refname = STRBUF_INIT;
1438 cb->warned_bad_reflog = 0;
1439 strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
1440 cb->name_for_errormsg = refname.buf;
1441 refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1443 handle_one_reflog_ent, cb_data);
1444 strbuf_release(&refname);
1448 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
1450 struct worktree **worktrees, **p;
1452 worktrees = get_worktrees(0);
1453 for (p = worktrees; *p; p++) {
1454 struct worktree *wt = *p;
1460 refs_for_each_reflog(get_worktree_ref_store(wt),
1464 free_worktrees(worktrees);
1467 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1469 struct all_refs_cb cb;
1472 cb.all_flags = flags;
1474 for_each_reflog(handle_one_reflog, &cb);
1476 if (!revs->single_worktree)
1477 add_other_reflogs_to_pending(&cb);
1480 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1481 struct strbuf *path, unsigned int flags)
1483 size_t baselen = path->len;
1486 if (it->entry_count >= 0) {
1487 struct tree *tree = lookup_tree(revs->repo, &it->oid);
1488 tree->object.flags |= flags;
1489 add_pending_object_with_path(revs, &tree->object, "",
1493 for (i = 0; i < it->subtree_nr; i++) {
1494 struct cache_tree_sub *sub = it->down[i];
1495 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1496 add_cache_tree(sub->cache_tree, revs, path, flags);
1497 strbuf_setlen(path, baselen);
1502 static void do_add_index_objects_to_pending(struct rev_info *revs,
1503 struct index_state *istate,
1508 for (i = 0; i < istate->cache_nr; i++) {
1509 struct cache_entry *ce = istate->cache[i];
1512 if (S_ISGITLINK(ce->ce_mode))
1515 blob = lookup_blob(revs->repo, &ce->oid);
1517 die("unable to add index blob to traversal");
1518 blob->object.flags |= flags;
1519 add_pending_object_with_path(revs, &blob->object, "",
1520 ce->ce_mode, ce->name);
1523 if (istate->cache_tree) {
1524 struct strbuf path = STRBUF_INIT;
1525 add_cache_tree(istate->cache_tree, revs, &path, flags);
1526 strbuf_release(&path);
1530 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
1532 struct worktree **worktrees, **p;
1534 repo_read_index(revs->repo);
1535 do_add_index_objects_to_pending(revs, revs->repo->index, flags);
1537 if (revs->single_worktree)
1540 worktrees = get_worktrees(0);
1541 for (p = worktrees; *p; p++) {
1542 struct worktree *wt = *p;
1543 struct index_state istate = { NULL };
1546 continue; /* current index already taken care of */
1548 if (read_index_from(&istate,
1549 worktree_git_path(wt, "index"),
1550 get_worktree_git_dir(wt)) > 0)
1551 do_add_index_objects_to_pending(revs, &istate, flags);
1552 discard_index(&istate);
1554 free_worktrees(worktrees);
1557 struct add_alternate_refs_data {
1558 struct rev_info *revs;
1562 static void add_one_alternate_ref(const struct object_id *oid,
1565 const char *name = ".alternate";
1566 struct add_alternate_refs_data *data = vdata;
1569 obj = get_reference(data->revs, name, oid, data->flags);
1570 add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
1571 add_pending_object(data->revs, obj, name);
1574 static void add_alternate_refs_to_pending(struct rev_info *revs,
1577 struct add_alternate_refs_data data;
1580 for_each_alternate_ref(add_one_alternate_ref, &data);
1583 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1586 struct object_id oid;
1588 struct commit *commit;
1589 struct commit_list *parents;
1591 const char *arg = arg_;
1594 flags ^= UNINTERESTING | BOTTOM;
1597 if (get_oid_committish(arg, &oid))
1600 it = get_reference(revs, arg, &oid, 0);
1601 if (!it && revs->ignore_missing)
1603 if (it->type != OBJ_TAG)
1605 if (!((struct tag*)it)->tagged)
1607 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
1609 if (it->type != OBJ_COMMIT)
1611 commit = (struct commit *)it;
1612 if (exclude_parent &&
1613 exclude_parent > commit_list_count(commit->parents))
1615 for (parents = commit->parents, parent_number = 1;
1617 parents = parents->next, parent_number++) {
1618 if (exclude_parent && parent_number != exclude_parent)
1621 it = &parents->item->object;
1623 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1624 add_pending_object(revs, it, arg);
1629 void repo_init_revisions(struct repository *r,
1630 struct rev_info *revs,
1633 memset(revs, 0, sizeof(*revs));
1636 revs->abbrev = DEFAULT_ABBREV;
1637 revs->ignore_merges = 1;
1638 revs->simplify_history = 1;
1639 revs->pruning.repo = r;
1640 revs->pruning.flags.recursive = 1;
1641 revs->pruning.flags.quick = 1;
1642 revs->pruning.add_remove = file_add_remove;
1643 revs->pruning.change = file_change;
1644 revs->pruning.change_fn_data = revs;
1645 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1647 revs->prefix = prefix;
1650 revs->skip_count = -1;
1651 revs->max_count = -1;
1652 revs->max_parents = -1;
1653 revs->expand_tabs_in_log = -1;
1655 revs->commit_format = CMIT_FMT_DEFAULT;
1656 revs->expand_tabs_in_log_default = 8;
1658 init_grep_defaults(revs->repo);
1659 grep_init(&revs->grep_filter, revs->repo, prefix);
1660 revs->grep_filter.status_only = 1;
1662 repo_diff_setup(revs->repo, &revs->diffopt);
1663 if (prefix && !revs->diffopt.prefix) {
1664 revs->diffopt.prefix = prefix;
1665 revs->diffopt.prefix_length = strlen(prefix);
1668 revs->notes_opt.use_default_notes = -1;
1671 static void add_pending_commit_list(struct rev_info *revs,
1672 struct commit_list *commit_list,
1675 while (commit_list) {
1676 struct object *object = &commit_list->item->object;
1677 object->flags |= flags;
1678 add_pending_object(revs, object, oid_to_hex(&object->oid));
1679 commit_list = commit_list->next;
1683 static void prepare_show_merge(struct rev_info *revs)
1685 struct commit_list *bases;
1686 struct commit *head, *other;
1687 struct object_id oid;
1688 const char **prune = NULL;
1689 int i, prune_num = 1; /* counting terminating NULL */
1690 struct index_state *istate = revs->repo->index;
1692 if (get_oid("HEAD", &oid))
1693 die("--merge without HEAD?");
1694 head = lookup_commit_or_die(&oid, "HEAD");
1695 if (get_oid("MERGE_HEAD", &oid))
1696 die("--merge without MERGE_HEAD?");
1697 other = lookup_commit_or_die(&oid, "MERGE_HEAD");
1698 add_pending_object(revs, &head->object, "HEAD");
1699 add_pending_object(revs, &other->object, "MERGE_HEAD");
1700 bases = get_merge_bases(head, other);
1701 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1702 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1703 free_commit_list(bases);
1704 head->object.flags |= SYMMETRIC_LEFT;
1706 if (!istate->cache_nr)
1707 repo_read_index(revs->repo);
1708 for (i = 0; i < istate->cache_nr; i++) {
1709 const struct cache_entry *ce = istate->cache[i];
1712 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
1714 REALLOC_ARRAY(prune, prune_num);
1715 prune[prune_num-2] = ce->name;
1716 prune[prune_num-1] = NULL;
1718 while ((i+1 < istate->cache_nr) &&
1719 ce_same_name(ce, istate->cache[i+1]))
1722 clear_pathspec(&revs->prune_data);
1723 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1724 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1728 static int dotdot_missing(const char *arg, char *dotdot,
1729 struct rev_info *revs, int symmetric)
1731 if (revs->ignore_missing)
1733 /* de-munge so we report the full argument */
1736 ? "Invalid symmetric difference expression %s"
1737 : "Invalid revision range %s", arg);
1740 static int handle_dotdot_1(const char *arg, char *dotdot,
1741 struct rev_info *revs, int flags,
1742 int cant_be_filename,
1743 struct object_context *a_oc,
1744 struct object_context *b_oc)
1746 const char *a_name, *b_name;
1747 struct object_id a_oid, b_oid;
1748 struct object *a_obj, *b_obj;
1749 unsigned int a_flags, b_flags;
1751 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1752 unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
1758 b_name = dotdot + 2;
1759 if (*b_name == '.') {
1766 if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
1767 get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
1770 if (!cant_be_filename) {
1772 verify_non_filename(revs->prefix, arg);
1776 a_obj = parse_object(revs->repo, &a_oid);
1777 b_obj = parse_object(revs->repo, &b_oid);
1778 if (!a_obj || !b_obj)
1779 return dotdot_missing(arg, dotdot, revs, symmetric);
1784 a_flags = flags_exclude;
1786 /* A...B -- find merge bases between the two */
1787 struct commit *a, *b;
1788 struct commit_list *exclude;
1790 a = lookup_commit_reference(revs->repo, &a_obj->oid);
1791 b = lookup_commit_reference(revs->repo, &b_obj->oid);
1793 return dotdot_missing(arg, dotdot, revs, symmetric);
1795 exclude = get_merge_bases(a, b);
1796 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
1798 add_pending_commit_list(revs, exclude, flags_exclude);
1799 free_commit_list(exclude);
1802 a_flags = flags | SYMMETRIC_LEFT;
1805 a_obj->flags |= a_flags;
1806 b_obj->flags |= b_flags;
1807 add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
1808 add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
1809 add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
1810 add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
1814 static int handle_dotdot(const char *arg,
1815 struct rev_info *revs, int flags,
1816 int cant_be_filename)
1818 struct object_context a_oc, b_oc;
1819 char *dotdot = strstr(arg, "..");
1825 memset(&a_oc, 0, sizeof(a_oc));
1826 memset(&b_oc, 0, sizeof(b_oc));
1829 ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
1839 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1841 struct object_context oc;
1843 struct object *object;
1844 struct object_id oid;
1846 const char *arg = arg_;
1847 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1848 unsigned get_sha1_flags = GET_OID_RECORD_PATH;
1850 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1852 if (!cant_be_filename && !strcmp(arg, "..")) {
1854 * Just ".."? That is not a range but the
1855 * pathspec for the parent directory.
1860 if (!handle_dotdot(arg, revs, flags, revarg_opt))
1863 mark = strstr(arg, "^@");
1864 if (mark && !mark[2]) {
1866 if (add_parents_only(revs, arg, flags, 0))
1870 mark = strstr(arg, "^!");
1871 if (mark && !mark[2]) {
1873 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
1876 mark = strstr(arg, "^-");
1878 int exclude_parent = 1;
1882 exclude_parent = strtoul(mark + 2, &end, 10);
1883 if (*end != '\0' || !exclude_parent)
1888 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
1894 local_flags = UNINTERESTING | BOTTOM;
1898 if (revarg_opt & REVARG_COMMITTISH)
1899 get_sha1_flags |= GET_OID_COMMITTISH;
1901 if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
1902 return revs->ignore_missing ? 0 : -1;
1903 if (!cant_be_filename)
1904 verify_non_filename(revs->prefix, arg);
1905 object = get_reference(revs, arg, &oid, flags ^ local_flags);
1907 return revs->ignore_missing ? 0 : -1;
1908 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1909 add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
1914 static void read_pathspec_from_stdin(struct strbuf *sb,
1915 struct argv_array *prune)
1917 while (strbuf_getline(sb, stdin) != EOF)
1918 argv_array_push(prune, sb->buf);
1921 static void read_revisions_from_stdin(struct rev_info *revs,
1922 struct argv_array *prune)
1925 int seen_dashdash = 0;
1928 save_warning = warn_on_object_refname_ambiguity;
1929 warn_on_object_refname_ambiguity = 0;
1931 strbuf_init(&sb, 1000);
1932 while (strbuf_getline(&sb, stdin) != EOF) {
1936 if (sb.buf[0] == '-') {
1937 if (len == 2 && sb.buf[1] == '-') {
1941 die("options not supported in --stdin mode");
1943 if (handle_revision_arg(sb.buf, revs, 0,
1944 REVARG_CANNOT_BE_FILENAME))
1945 die("bad revision '%s'", sb.buf);
1948 read_pathspec_from_stdin(&sb, prune);
1950 strbuf_release(&sb);
1951 warn_on_object_refname_ambiguity = save_warning;
1954 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1956 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1959 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1961 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1964 static void add_message_grep(struct rev_info *revs, const char *pattern)
1966 add_grep(revs, pattern, GREP_PATTERN_BODY);
1969 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1970 int *unkc, const char **unkv,
1971 const struct setup_revision_opt* opt)
1973 const char *arg = argv[0];
1976 const unsigned hexsz = the_hash_algo->hexsz;
1978 /* pseudo revision arguments */
1979 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1980 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1981 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1982 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1983 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1984 !strcmp(arg, "--indexed-objects") ||
1985 !strcmp(arg, "--alternate-refs") ||
1986 starts_with(arg, "--exclude=") ||
1987 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1988 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1990 unkv[(*unkc)++] = arg;
1994 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1995 revs->max_count = atoi(optarg);
1998 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1999 revs->skip_count = atoi(optarg);
2001 } else if ((*arg == '-') && isdigit(arg[1])) {
2002 /* accept -<digit>, like traditional "head" */
2003 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
2004 revs->max_count < 0)
2005 die("'%s': not a non-negative integer", arg + 1);
2007 } else if (!strcmp(arg, "-n")) {
2009 return error("-n requires an argument");
2010 revs->max_count = atoi(argv[1]);
2013 } else if (skip_prefix(arg, "-n", &optarg)) {
2014 revs->max_count = atoi(optarg);
2016 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
2017 revs->max_age = atoi(optarg);
2019 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
2020 revs->max_age = approxidate(optarg);
2022 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
2023 revs->max_age = approxidate(optarg);
2025 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
2026 revs->min_age = atoi(optarg);
2028 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
2029 revs->min_age = approxidate(optarg);
2031 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
2032 revs->min_age = approxidate(optarg);
2034 } else if (!strcmp(arg, "--first-parent")) {
2035 revs->first_parent_only = 1;
2036 } else if (!strcmp(arg, "--ancestry-path")) {
2037 revs->ancestry_path = 1;
2038 revs->simplify_history = 0;
2040 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
2041 init_reflog_walk(&revs->reflog_info);
2042 } else if (!strcmp(arg, "--default")) {
2044 return error("bad --default argument");
2045 revs->def = argv[1];
2047 } else if (!strcmp(arg, "--merge")) {
2048 revs->show_merge = 1;
2049 } else if (!strcmp(arg, "--topo-order")) {
2050 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
2051 revs->topo_order = 1;
2052 } else if (!strcmp(arg, "--simplify-merges")) {
2053 revs->simplify_merges = 1;
2054 revs->topo_order = 1;
2055 revs->rewrite_parents = 1;
2056 revs->simplify_history = 0;
2058 } else if (!strcmp(arg, "--simplify-by-decoration")) {
2059 revs->simplify_merges = 1;
2060 revs->topo_order = 1;
2061 revs->rewrite_parents = 1;
2062 revs->simplify_history = 0;
2063 revs->simplify_by_decoration = 1;
2066 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
2067 } else if (!strcmp(arg, "--date-order")) {
2068 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
2069 revs->topo_order = 1;
2070 } else if (!strcmp(arg, "--author-date-order")) {
2071 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
2072 revs->topo_order = 1;
2073 } else if (!strcmp(arg, "--early-output")) {
2074 revs->early_output = 100;
2075 revs->topo_order = 1;
2076 } else if (skip_prefix(arg, "--early-output=", &optarg)) {
2077 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
2078 die("'%s': not a non-negative integer", optarg);
2079 revs->topo_order = 1;
2080 } else if (!strcmp(arg, "--parents")) {
2081 revs->rewrite_parents = 1;
2082 revs->print_parents = 1;
2083 } else if (!strcmp(arg, "--dense")) {
2085 } else if (!strcmp(arg, "--sparse")) {
2087 } else if (!strcmp(arg, "--in-commit-order")) {
2088 revs->tree_blobs_in_commit_order = 1;
2089 } else if (!strcmp(arg, "--remove-empty")) {
2090 revs->remove_empty_trees = 1;
2091 } else if (!strcmp(arg, "--merges")) {
2092 revs->min_parents = 2;
2093 } else if (!strcmp(arg, "--no-merges")) {
2094 revs->max_parents = 1;
2095 } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
2096 revs->min_parents = atoi(optarg);
2097 } else if (!strcmp(arg, "--no-min-parents")) {
2098 revs->min_parents = 0;
2099 } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
2100 revs->max_parents = atoi(optarg);
2101 } else if (!strcmp(arg, "--no-max-parents")) {
2102 revs->max_parents = -1;
2103 } else if (!strcmp(arg, "--boundary")) {
2105 } else if (!strcmp(arg, "--left-right")) {
2106 revs->left_right = 1;
2107 } else if (!strcmp(arg, "--left-only")) {
2108 if (revs->right_only)
2109 die("--left-only is incompatible with --right-only"
2111 revs->left_only = 1;
2112 } else if (!strcmp(arg, "--right-only")) {
2113 if (revs->left_only)
2114 die("--right-only is incompatible with --left-only");
2115 revs->right_only = 1;
2116 } else if (!strcmp(arg, "--cherry")) {
2117 if (revs->left_only)
2118 die("--cherry is incompatible with --left-only");
2119 revs->cherry_mark = 1;
2120 revs->right_only = 1;
2121 revs->max_parents = 1;
2123 } else if (!strcmp(arg, "--count")) {
2125 } else if (!strcmp(arg, "--cherry-mark")) {
2126 if (revs->cherry_pick)
2127 die("--cherry-mark is incompatible with --cherry-pick");
2128 revs->cherry_mark = 1;
2129 revs->limited = 1; /* needs limit_list() */
2130 } else if (!strcmp(arg, "--cherry-pick")) {
2131 if (revs->cherry_mark)
2132 die("--cherry-pick is incompatible with --cherry-mark");
2133 revs->cherry_pick = 1;
2135 } else if (!strcmp(arg, "--objects")) {
2136 revs->tag_objects = 1;
2137 revs->tree_objects = 1;
2138 revs->blob_objects = 1;
2139 } else if (!strcmp(arg, "--objects-edge")) {
2140 revs->tag_objects = 1;
2141 revs->tree_objects = 1;
2142 revs->blob_objects = 1;
2143 revs->edge_hint = 1;
2144 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
2145 revs->tag_objects = 1;
2146 revs->tree_objects = 1;
2147 revs->blob_objects = 1;
2148 revs->edge_hint = 1;
2149 revs->edge_hint_aggressive = 1;
2150 } else if (!strcmp(arg, "--verify-objects")) {
2151 revs->tag_objects = 1;
2152 revs->tree_objects = 1;
2153 revs->blob_objects = 1;
2154 revs->verify_objects = 1;
2155 } else if (!strcmp(arg, "--unpacked")) {
2157 } else if (starts_with(arg, "--unpacked=")) {
2158 die("--unpacked=<packfile> no longer supported.");
2159 } else if (!strcmp(arg, "-r")) {
2161 revs->diffopt.flags.recursive = 1;
2162 } else if (!strcmp(arg, "-t")) {
2164 revs->diffopt.flags.recursive = 1;
2165 revs->diffopt.flags.tree_in_recursive = 1;
2166 } else if (!strcmp(arg, "-m")) {
2167 revs->ignore_merges = 0;
2168 } else if (!strcmp(arg, "-c")) {
2170 revs->dense_combined_merges = 0;
2171 revs->combine_merges = 1;
2172 } else if (!strcmp(arg, "--combined-all-paths")) {
2174 revs->combined_all_paths = 1;
2175 } else if (!strcmp(arg, "--cc")) {
2177 revs->dense_combined_merges = 1;
2178 revs->combine_merges = 1;
2179 } else if (!strcmp(arg, "-v")) {
2180 revs->verbose_header = 1;
2181 } else if (!strcmp(arg, "--pretty")) {
2182 revs->verbose_header = 1;
2183 revs->pretty_given = 1;
2184 get_commit_format(NULL, revs);
2185 } else if (skip_prefix(arg, "--pretty=", &optarg) ||
2186 skip_prefix(arg, "--format=", &optarg)) {
2188 * Detached form ("--pretty X" as opposed to "--pretty=X")
2189 * not allowed, since the argument is optional.
2191 revs->verbose_header = 1;
2192 revs->pretty_given = 1;
2193 get_commit_format(optarg, revs);
2194 } else if (!strcmp(arg, "--expand-tabs")) {
2195 revs->expand_tabs_in_log = 8;
2196 } else if (!strcmp(arg, "--no-expand-tabs")) {
2197 revs->expand_tabs_in_log = 0;
2198 } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
2200 if (strtol_i(arg, 10, &val) < 0 || val < 0)
2201 die("'%s': not a non-negative integer", arg);
2202 revs->expand_tabs_in_log = val;
2203 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2204 revs->show_notes = 1;
2205 revs->show_notes_given = 1;
2206 revs->notes_opt.use_default_notes = 1;
2207 } else if (!strcmp(arg, "--show-signature")) {
2208 revs->show_signature = 1;
2209 } else if (!strcmp(arg, "--no-show-signature")) {
2210 revs->show_signature = 0;
2211 } else if (!strcmp(arg, "--show-linear-break")) {
2212 revs->break_bar = " ..........";
2213 revs->track_linear = 1;
2214 revs->track_first_time = 1;
2215 } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
2216 revs->break_bar = xstrdup(optarg);
2217 revs->track_linear = 1;
2218 revs->track_first_time = 1;
2219 } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
2220 skip_prefix(arg, "--notes=", &optarg)) {
2221 struct strbuf buf = STRBUF_INIT;
2222 revs->show_notes = 1;
2223 revs->show_notes_given = 1;
2224 if (starts_with(arg, "--show-notes=") &&
2225 revs->notes_opt.use_default_notes < 0)
2226 revs->notes_opt.use_default_notes = 1;
2227 strbuf_addstr(&buf, optarg);
2228 expand_notes_ref(&buf);
2229 string_list_append(&revs->notes_opt.extra_notes_refs,
2230 strbuf_detach(&buf, NULL));
2231 } else if (!strcmp(arg, "--no-notes")) {
2232 revs->show_notes = 0;
2233 revs->show_notes_given = 1;
2234 revs->notes_opt.use_default_notes = -1;
2235 /* we have been strdup'ing ourselves, so trick
2236 * string_list into free()ing strings */
2237 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
2238 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
2239 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
2240 } else if (!strcmp(arg, "--standard-notes")) {
2241 revs->show_notes_given = 1;
2242 revs->notes_opt.use_default_notes = 1;
2243 } else if (!strcmp(arg, "--no-standard-notes")) {
2244 revs->notes_opt.use_default_notes = 0;
2245 } else if (!strcmp(arg, "--oneline")) {
2246 revs->verbose_header = 1;
2247 get_commit_format("oneline", revs);
2248 revs->pretty_given = 1;
2249 revs->abbrev_commit = 1;
2250 } else if (!strcmp(arg, "--graph")) {
2251 revs->topo_order = 1;
2252 revs->rewrite_parents = 1;
2253 revs->graph = graph_init(revs);
2254 } else if (!strcmp(arg, "--root")) {
2255 revs->show_root_diff = 1;
2256 } else if (!strcmp(arg, "--no-commit-id")) {
2257 revs->no_commit_id = 1;
2258 } else if (!strcmp(arg, "--always")) {
2259 revs->always_show_header = 1;
2260 } else if (!strcmp(arg, "--no-abbrev")) {
2262 } else if (!strcmp(arg, "--abbrev")) {
2263 revs->abbrev = DEFAULT_ABBREV;
2264 } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
2265 revs->abbrev = strtoul(optarg, NULL, 10);
2266 if (revs->abbrev < MINIMUM_ABBREV)
2267 revs->abbrev = MINIMUM_ABBREV;
2268 else if (revs->abbrev > hexsz)
2269 revs->abbrev = hexsz;
2270 } else if (!strcmp(arg, "--abbrev-commit")) {
2271 revs->abbrev_commit = 1;
2272 revs->abbrev_commit_given = 1;
2273 } else if (!strcmp(arg, "--no-abbrev-commit")) {
2274 revs->abbrev_commit = 0;
2275 } else if (!strcmp(arg, "--full-diff")) {
2277 revs->full_diff = 1;
2278 } else if (!strcmp(arg, "--full-history")) {
2279 revs->simplify_history = 0;
2280 } else if (!strcmp(arg, "--relative-date")) {
2281 revs->date_mode.type = DATE_RELATIVE;
2282 revs->date_mode_explicit = 1;
2283 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2284 parse_date_format(optarg, &revs->date_mode);
2285 revs->date_mode_explicit = 1;
2287 } else if (!strcmp(arg, "--log-size")) {
2288 revs->show_log_size = 1;
2291 * Grepping the commit log
2293 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2294 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2296 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2297 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2299 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2300 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2302 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2303 add_message_grep(revs, optarg);
2305 } else if (!strcmp(arg, "--grep-debug")) {
2306 revs->grep_filter.debug = 1;
2307 } else if (!strcmp(arg, "--basic-regexp")) {
2308 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2309 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2310 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2311 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2312 revs->grep_filter.ignore_case = 1;
2313 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
2314 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2315 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2316 } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
2317 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2318 } else if (!strcmp(arg, "--all-match")) {
2319 revs->grep_filter.all_match = 1;
2320 } else if (!strcmp(arg, "--invert-grep")) {
2321 revs->invert_grep = 1;
2322 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2323 if (strcmp(optarg, "none"))
2324 git_log_output_encoding = xstrdup(optarg);
2326 git_log_output_encoding = "";
2328 } else if (!strcmp(arg, "--reverse")) {
2330 } else if (!strcmp(arg, "--children")) {
2331 revs->children.name = "children";
2333 } else if (!strcmp(arg, "--ignore-missing")) {
2334 revs->ignore_missing = 1;
2335 } else if (opt && opt->allow_exclude_promisor_objects &&
2336 !strcmp(arg, "--exclude-promisor-objects")) {
2337 if (fetch_if_missing)
2338 BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
2339 revs->exclude_promisor_objects = 1;
2341 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2343 unkv[(*unkc)++] = arg;
2346 if (revs->graph && revs->track_linear)
2347 die("--show-linear-break and --graph are incompatible");
2352 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2353 const struct option *options,
2354 const char * const usagestr[])
2356 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2357 &ctx->cpidx, ctx->out, NULL);
2359 error("unknown option `%s'", ctx->argv[0]);
2360 usage_with_options(usagestr, options);
2366 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
2367 void *cb_data, const char *term)
2369 struct strbuf bisect_refs = STRBUF_INIT;
2371 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2372 status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
2373 strbuf_release(&bisect_refs);
2377 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2379 return for_each_bisect_ref(refs, fn, cb_data, term_bad);
2382 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
2384 return for_each_bisect_ref(refs, fn, cb_data, term_good);
2387 static int handle_revision_pseudo_opt(const char *submodule,
2388 struct rev_info *revs,
2389 int argc, const char **argv, int *flags)
2391 const char *arg = argv[0];
2393 struct ref_store *refs;
2398 * We need some something like get_submodule_worktrees()
2399 * before we can go through all worktrees of a submodule,
2400 * .e.g with adding all HEADs from --all, which is not
2401 * supported right now, so stick to single worktree.
2403 if (!revs->single_worktree)
2404 BUG("--single-worktree cannot be used together with submodule");
2405 refs = get_submodule_ref_store(submodule);
2407 refs = get_main_ref_store(revs->repo);
2412 * Commands like "git shortlog" will not accept the options below
2413 * unless parse_revision_opt queues them (as opposed to erroring
2416 * When implementing your new pseudo-option, remember to
2417 * register it in the list at the top of handle_revision_opt.
2419 if (!strcmp(arg, "--all")) {
2420 handle_refs(refs, revs, *flags, refs_for_each_ref);
2421 handle_refs(refs, revs, *flags, refs_head_ref);
2422 if (!revs->single_worktree) {
2423 struct all_refs_cb cb;
2425 init_all_refs_cb(&cb, revs, *flags);
2426 other_head_refs(handle_one_ref, &cb);
2428 clear_ref_exclusion(&revs->ref_excludes);
2429 } else if (!strcmp(arg, "--branches")) {
2430 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2431 clear_ref_exclusion(&revs->ref_excludes);
2432 } else if (!strcmp(arg, "--bisect")) {
2433 read_bisect_terms(&term_bad, &term_good);
2434 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
2435 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
2436 for_each_good_bisect_ref);
2438 } else if (!strcmp(arg, "--tags")) {
2439 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2440 clear_ref_exclusion(&revs->ref_excludes);
2441 } else if (!strcmp(arg, "--remotes")) {
2442 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2443 clear_ref_exclusion(&revs->ref_excludes);
2444 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2445 struct all_refs_cb cb;
2446 init_all_refs_cb(&cb, revs, *flags);
2447 for_each_glob_ref(handle_one_ref, optarg, &cb);
2448 clear_ref_exclusion(&revs->ref_excludes);
2450 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2451 add_ref_exclusion(&revs->ref_excludes, optarg);
2453 } else if (skip_prefix(arg, "--branches=", &optarg)) {
2454 struct all_refs_cb cb;
2455 init_all_refs_cb(&cb, revs, *flags);
2456 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2457 clear_ref_exclusion(&revs->ref_excludes);
2458 } else if (skip_prefix(arg, "--tags=", &optarg)) {
2459 struct all_refs_cb cb;
2460 init_all_refs_cb(&cb, revs, *flags);
2461 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2462 clear_ref_exclusion(&revs->ref_excludes);
2463 } else if (skip_prefix(arg, "--remotes=", &optarg)) {
2464 struct all_refs_cb cb;
2465 init_all_refs_cb(&cb, revs, *flags);
2466 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2467 clear_ref_exclusion(&revs->ref_excludes);
2468 } else if (!strcmp(arg, "--reflog")) {
2469 add_reflogs_to_pending(revs, *flags);
2470 } else if (!strcmp(arg, "--indexed-objects")) {
2471 add_index_objects_to_pending(revs, *flags);
2472 } else if (!strcmp(arg, "--alternate-refs")) {
2473 add_alternate_refs_to_pending(revs, *flags);
2474 } else if (!strcmp(arg, "--not")) {
2475 *flags ^= UNINTERESTING | BOTTOM;
2476 } else if (!strcmp(arg, "--no-walk")) {
2477 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2478 } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
2480 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2481 * not allowed, since the argument is optional.
2483 if (!strcmp(optarg, "sorted"))
2484 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2485 else if (!strcmp(optarg, "unsorted"))
2486 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2488 return error("invalid argument to --no-walk");
2489 } else if (!strcmp(arg, "--do-walk")) {
2491 } else if (!strcmp(arg, "--single-worktree")) {
2492 revs->single_worktree = 1;
2500 static void NORETURN diagnose_missing_default(const char *def)
2503 const char *refname;
2505 refname = resolve_ref_unsafe(def, 0, NULL, &flags);
2506 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2507 die(_("your current branch appears to be broken"));
2509 skip_prefix(refname, "refs/heads/", &refname);
2510 die(_("your current branch '%s' does not have any commits yet"),
2515 * Parse revision information, filling in the "rev_info" structure,
2516 * and removing the used arguments from the argument list.
2518 * Returns the number of arguments left that weren't recognized
2519 * (which are also moved to the head of the argument list)
2521 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2523 int i, flags, left, seen_dashdash, got_rev_arg = 0, revarg_opt;
2524 struct argv_array prune_data = ARGV_ARRAY_INIT;
2525 const char *submodule = NULL;
2526 int seen_end_of_options = 0;
2529 submodule = opt->submodule;
2531 /* First, search for "--" */
2532 if (opt && opt->assume_dashdash) {
2536 for (i = 1; i < argc; i++) {
2537 const char *arg = argv[i];
2538 if (strcmp(arg, "--"))
2543 argv_array_pushv(&prune_data, argv + i + 1);
2549 /* Second, deal with arguments and options */
2551 revarg_opt = opt ? opt->revarg_opt : 0;
2553 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2554 for (left = i = 1; i < argc; i++) {
2555 const char *arg = argv[i];
2556 if (!seen_end_of_options && *arg == '-') {
2559 opts = handle_revision_pseudo_opt(submodule,
2560 revs, argc - i, argv + i,
2567 if (!strcmp(arg, "--stdin")) {
2568 if (revs->disable_stdin) {
2572 if (revs->read_from_stdin++)
2573 die("--stdin given twice?");
2574 read_revisions_from_stdin(revs, &prune_data);
2578 if (!strcmp(arg, "--end-of-options")) {
2579 seen_end_of_options = 1;
2583 opts = handle_revision_opt(revs, argc - i, argv + i,
2595 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2597 if (seen_dashdash || *arg == '^')
2598 die("bad revision '%s'", arg);
2600 /* If we didn't have a "--":
2601 * (1) all filenames must exist;
2602 * (2) all rev-args must not be interpretable
2603 * as a valid filename.
2604 * but the latter we have checked in the main loop.
2606 for (j = i; j < argc; j++)
2607 verify_filename(revs->prefix, argv[j], j == i);
2609 argv_array_pushv(&prune_data, argv + i);
2616 if (prune_data.argc) {
2618 * If we need to introduce the magic "a lone ':' means no
2619 * pathspec whatsoever", here is the place to do so.
2621 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2622 * prune_data.nr = 0;
2623 * prune_data.alloc = 0;
2624 * free(prune_data.path);
2625 * prune_data.path = NULL;
2627 * terminate prune_data.alloc with NULL and
2628 * call init_pathspec() to set revs->prune_data here.
2631 parse_pathspec(&revs->prune_data, 0, 0,
2632 revs->prefix, prune_data.argv);
2634 argv_array_clear(&prune_data);
2636 if (revs->def == NULL)
2637 revs->def = opt ? opt->def : NULL;
2638 if (opt && opt->tweak)
2639 opt->tweak(revs, opt);
2640 if (revs->show_merge)
2641 prepare_show_merge(revs);
2642 if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
2643 struct object_id oid;
2644 struct object *object;
2645 struct object_context oc;
2646 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
2647 diagnose_missing_default(revs->def);
2648 object = get_reference(revs, revs->def, &oid, 0);
2649 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2652 /* Did the user ask for any diff output? Run the diff! */
2653 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2656 /* Pickaxe, diff-filter and rename following need diffs */
2657 if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
2658 revs->diffopt.filter ||
2659 revs->diffopt.flags.follow_renames)
2662 if (revs->diffopt.objfind)
2663 revs->simplify_history = 0;
2665 if (revs->topo_order && !generation_numbers_enabled(the_repository))
2668 if (revs->prune_data.nr) {
2669 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2670 /* Can't prune commits with rename following: the paths change.. */
2671 if (!revs->diffopt.flags.follow_renames)
2673 if (!revs->full_diff)
2674 copy_pathspec(&revs->diffopt.pathspec,
2677 if (revs->combine_merges)
2678 revs->ignore_merges = 0;
2679 if (revs->combined_all_paths && !revs->combine_merges)
2680 die("--combined-all-paths makes no sense without -c or --cc");
2682 revs->diffopt.abbrev = revs->abbrev;
2684 if (revs->line_level_traverse) {
2686 revs->topo_order = 1;
2689 diff_setup_done(&revs->diffopt);
2691 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2692 &revs->grep_filter);
2693 compile_grep_patterns(&revs->grep_filter);
2695 if (revs->reverse && revs->reflog_info)
2696 die("cannot combine --reverse with --walk-reflogs");
2697 if (revs->reflog_info && revs->limited)
2698 die("cannot combine --walk-reflogs with history-limiting options");
2699 if (revs->rewrite_parents && revs->children.name)
2700 die("cannot combine --parents and --children");
2703 * Limitations on the graph functionality
2705 if (revs->reverse && revs->graph)
2706 die("cannot combine --reverse with --graph");
2708 if (revs->reflog_info && revs->graph)
2709 die("cannot combine --walk-reflogs with --graph");
2710 if (revs->no_walk && revs->graph)
2711 die("cannot combine --no-walk with --graph");
2712 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2713 die("cannot use --grep-reflog without --walk-reflogs");
2715 if (revs->first_parent_only && revs->bisect)
2716 die(_("--first-parent is incompatible with --bisect"));
2718 if (revs->line_level_traverse &&
2719 (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
2720 die(_("-L does not yet support diff formats besides -p and -s"));
2722 if (revs->expand_tabs_in_log < 0)
2723 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2728 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2730 struct commit_list *l = xcalloc(1, sizeof(*l));
2733 l->next = add_decoration(&revs->children, &parent->object, l);
2736 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2738 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2739 struct commit_list **pp, *p;
2740 int surviving_parents;
2742 /* Examine existing parents while marking ones we have seen... */
2743 pp = &commit->parents;
2744 surviving_parents = 0;
2745 while ((p = *pp) != NULL) {
2746 struct commit *parent = p->item;
2747 if (parent->object.flags & TMP_MARK) {
2750 compact_treesame(revs, commit, surviving_parents);
2753 parent->object.flags |= TMP_MARK;
2754 surviving_parents++;
2757 /* clear the temporary mark */
2758 for (p = commit->parents; p; p = p->next) {
2759 p->item->object.flags &= ~TMP_MARK;
2761 /* no update_treesame() - removing duplicates can't affect TREESAME */
2762 return surviving_parents;
2765 struct merge_simplify_state {
2766 struct commit *simplified;
2769 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2771 struct merge_simplify_state *st;
2773 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2775 st = xcalloc(1, sizeof(*st));
2776 add_decoration(&revs->merge_simplification, &commit->object, st);
2781 static int mark_redundant_parents(struct commit *commit)
2783 struct commit_list *h = reduce_heads(commit->parents);
2784 int i = 0, marked = 0;
2785 struct commit_list *po, *pn;
2787 /* Want these for sanity-checking only */
2788 int orig_cnt = commit_list_count(commit->parents);
2789 int cnt = commit_list_count(h);
2792 * Not ready to remove items yet, just mark them for now, based
2793 * on the output of reduce_heads(). reduce_heads outputs the reduced
2794 * set in its original order, so this isn't too hard.
2796 po = commit->parents;
2799 if (pn && po->item == pn->item) {
2803 po->item->object.flags |= TMP_MARK;
2809 if (i != cnt || cnt+marked != orig_cnt)
2810 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2812 free_commit_list(h);
2817 static int mark_treesame_root_parents(struct commit *commit)
2819 struct commit_list *p;
2822 for (p = commit->parents; p; p = p->next) {
2823 struct commit *parent = p->item;
2824 if (!parent->parents && (parent->object.flags & TREESAME)) {
2825 parent->object.flags |= TMP_MARK;
2834 * Awkward naming - this means one parent we are TREESAME to.
2835 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2836 * empty tree). Better name suggestions?
2838 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2840 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2841 struct commit *unmarked = NULL, *marked = NULL;
2842 struct commit_list *p;
2845 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2846 if (ts->treesame[n]) {
2847 if (p->item->object.flags & TMP_MARK) {
2860 * If we are TREESAME to a marked-for-deletion parent, but not to any
2861 * unmarked parents, unmark the first TREESAME parent. This is the
2862 * parent that the default simplify_history==1 scan would have followed,
2863 * and it doesn't make sense to omit that path when asking for a
2864 * simplified full history. Retaining it improves the chances of
2865 * understanding odd missed merges that took an old version of a file.
2869 * I--------*X A modified the file, but mainline merge X used
2870 * \ / "-s ours", so took the version from I. X is
2871 * `-*A--' TREESAME to I and !TREESAME to A.
2873 * Default log from X would produce "I". Without this check,
2874 * --full-history --simplify-merges would produce "I-A-X", showing
2875 * the merge commit X and that it changed A, but not making clear that
2876 * it had just taken the I version. With this check, the topology above
2879 * Note that it is possible that the simplification chooses a different
2880 * TREESAME parent from the default, in which case this test doesn't
2881 * activate, and we _do_ drop the default parent. Example:
2883 * I------X A modified the file, but it was reverted in B,
2884 * \ / meaning mainline merge X is TREESAME to both
2887 * Default log would produce "I" by following the first parent;
2888 * --full-history --simplify-merges will produce "I-A-B". But this is a
2889 * reasonable result - it presents a logical full history leading from
2890 * I to X, and X is not an important merge.
2892 if (!unmarked && marked) {
2893 marked->object.flags &= ~TMP_MARK;
2900 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2902 struct commit_list **pp, *p;
2903 int nth_parent, removed = 0;
2905 pp = &commit->parents;
2907 while ((p = *pp) != NULL) {
2908 struct commit *parent = p->item;
2909 if (parent->object.flags & TMP_MARK) {
2910 parent->object.flags &= ~TMP_MARK;
2914 compact_treesame(revs, commit, nth_parent);
2921 /* Removing parents can only increase TREESAMEness */
2922 if (removed && !(commit->object.flags & TREESAME))
2923 update_treesame(revs, commit);
2928 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2930 struct commit_list *p;
2931 struct commit *parent;
2932 struct merge_simplify_state *st, *pst;
2935 st = locate_simplify_state(revs, commit);
2938 * Have we handled this one?
2944 * An UNINTERESTING commit simplifies to itself, so does a
2945 * root commit. We do not rewrite parents of such commit
2948 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2949 st->simplified = commit;
2954 * Do we know what commit all of our parents that matter
2955 * should be rewritten to? Otherwise we are not ready to
2956 * rewrite this one yet.
2958 for (cnt = 0, p = commit->parents; p; p = p->next) {
2959 pst = locate_simplify_state(revs, p->item);
2960 if (!pst->simplified) {
2961 tail = &commit_list_insert(p->item, tail)->next;
2964 if (revs->first_parent_only)
2968 tail = &commit_list_insert(commit, tail)->next;
2973 * Rewrite our list of parents. Note that this cannot
2974 * affect our TREESAME flags in any way - a commit is
2975 * always TREESAME to its simplification.
2977 for (p = commit->parents; p; p = p->next) {
2978 pst = locate_simplify_state(revs, p->item);
2979 p->item = pst->simplified;
2980 if (revs->first_parent_only)
2984 if (revs->first_parent_only)
2987 cnt = remove_duplicate_parents(revs, commit);
2990 * It is possible that we are a merge and one side branch
2991 * does not have any commit that touches the given paths;
2992 * in such a case, the immediate parent from that branch
2993 * will be rewritten to be the merge base.
2995 * o----X X: the commit we are looking at;
2996 * / / o: a commit that touches the paths;
2999 * Further, a merge of an independent branch that doesn't
3000 * touch the path will reduce to a treesame root parent:
3002 * ----o----X X: the commit we are looking at;
3003 * / o: a commit that touches the paths;
3004 * r r: a root commit not touching the paths
3006 * Detect and simplify both cases.
3009 int marked = mark_redundant_parents(commit);
3010 marked += mark_treesame_root_parents(commit);
3012 marked -= leave_one_treesame_to_parent(revs, commit);
3014 cnt = remove_marked_parents(revs, commit);
3018 * A commit simplifies to itself if it is a root, if it is
3019 * UNINTERESTING, if it touches the given paths, or if it is a
3020 * merge and its parents don't simplify to one relevant commit
3021 * (the first two cases are already handled at the beginning of
3024 * Otherwise, it simplifies to what its sole relevant parent
3028 (commit->object.flags & UNINTERESTING) ||
3029 !(commit->object.flags & TREESAME) ||
3030 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
3031 st->simplified = commit;
3033 pst = locate_simplify_state(revs, parent);
3034 st->simplified = pst->simplified;
3039 static void simplify_merges(struct rev_info *revs)
3041 struct commit_list *list, *next;
3042 struct commit_list *yet_to_do, **tail;
3043 struct commit *commit;
3048 /* feed the list reversed */
3050 for (list = revs->commits; list; list = next) {
3051 commit = list->item;
3054 * Do not free(list) here yet; the original list
3055 * is used later in this function.
3057 commit_list_insert(commit, &yet_to_do);
3064 commit = pop_commit(&list);
3065 tail = simplify_one(revs, commit, tail);
3069 /* clean up the result, removing the simplified ones */
3070 list = revs->commits;
3071 revs->commits = NULL;
3072 tail = &revs->commits;
3074 struct merge_simplify_state *st;
3076 commit = pop_commit(&list);
3077 st = locate_simplify_state(revs, commit);
3078 if (st->simplified == commit)
3079 tail = &commit_list_insert(commit, tail)->next;
3083 static void set_children(struct rev_info *revs)
3085 struct commit_list *l;
3086 for (l = revs->commits; l; l = l->next) {
3087 struct commit *commit = l->item;
3088 struct commit_list *p;
3090 for (p = commit->parents; p; p = p->next)
3091 add_child(revs, p->item, commit);
3095 void reset_revision_walk(void)
3097 clear_object_flags(SEEN | ADDED | SHOWN);
3100 static int mark_uninteresting(const struct object_id *oid,
3101 struct packed_git *pack,
3105 struct rev_info *revs = cb;
3106 struct object *o = parse_object(revs->repo, oid);
3107 o->flags |= UNINTERESTING | SEEN;
3111 define_commit_slab(indegree_slab, int);
3112 define_commit_slab(author_date_slab, timestamp_t);
3114 struct topo_walk_info {
3115 uint32_t min_generation;
3116 struct prio_queue explore_queue;
3117 struct prio_queue indegree_queue;
3118 struct prio_queue topo_queue;
3119 struct indegree_slab indegree;
3120 struct author_date_slab author_date;
3123 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
3125 if (c->object.flags & flag)
3128 c->object.flags |= flag;
3129 prio_queue_put(q, c);
3132 static void explore_walk_step(struct rev_info *revs)
3134 struct topo_walk_info *info = revs->topo_walk_info;
3135 struct commit_list *p;
3136 struct commit *c = prio_queue_get(&info->explore_queue);
3141 if (parse_commit_gently(c, 1) < 0)
3144 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3145 record_author_date(&info->author_date, c);
3147 if (revs->max_age != -1 && (c->date < revs->max_age))
3148 c->object.flags |= UNINTERESTING;
3150 if (process_parents(revs, c, NULL, NULL) < 0)
3153 if (c->object.flags & UNINTERESTING)
3154 mark_parents_uninteresting(c);
3156 for (p = c->parents; p; p = p->next)
3157 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
3160 static void explore_to_depth(struct rev_info *revs,
3161 uint32_t gen_cutoff)
3163 struct topo_walk_info *info = revs->topo_walk_info;
3165 while ((c = prio_queue_peek(&info->explore_queue)) &&
3166 c->generation >= gen_cutoff)
3167 explore_walk_step(revs);
3170 static void indegree_walk_step(struct rev_info *revs)
3172 struct commit_list *p;
3173 struct topo_walk_info *info = revs->topo_walk_info;
3174 struct commit *c = prio_queue_get(&info->indegree_queue);
3179 if (parse_commit_gently(c, 1) < 0)
3182 explore_to_depth(revs, c->generation);
3184 for (p = c->parents; p; p = p->next) {
3185 struct commit *parent = p->item;
3186 int *pi = indegree_slab_at(&info->indegree, parent);
3193 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
3195 if (revs->first_parent_only)
3200 static void compute_indegrees_to_depth(struct rev_info *revs,
3201 uint32_t gen_cutoff)
3203 struct topo_walk_info *info = revs->topo_walk_info;
3205 while ((c = prio_queue_peek(&info->indegree_queue)) &&
3206 c->generation >= gen_cutoff)
3207 indegree_walk_step(revs);
3210 static void init_topo_walk(struct rev_info *revs)
3212 struct topo_walk_info *info;
3213 struct commit_list *list;
3214 revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
3215 info = revs->topo_walk_info;
3216 memset(info, 0, sizeof(struct topo_walk_info));
3218 init_indegree_slab(&info->indegree);
3219 memset(&info->explore_queue, 0, sizeof(info->explore_queue));
3220 memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
3221 memset(&info->topo_queue, 0, sizeof(info->topo_queue));
3223 switch (revs->sort_order) {
3224 default: /* REV_SORT_IN_GRAPH_ORDER */
3225 info->topo_queue.compare = NULL;
3227 case REV_SORT_BY_COMMIT_DATE:
3228 info->topo_queue.compare = compare_commits_by_commit_date;
3230 case REV_SORT_BY_AUTHOR_DATE:
3231 init_author_date_slab(&info->author_date);
3232 info->topo_queue.compare = compare_commits_by_author_date;
3233 info->topo_queue.cb_data = &info->author_date;
3237 info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
3238 info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
3240 info->min_generation = GENERATION_NUMBER_INFINITY;
3241 for (list = revs->commits; list; list = list->next) {
3242 struct commit *c = list->item;
3244 if (parse_commit_gently(c, 1))
3247 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
3248 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
3250 if (c->generation < info->min_generation)
3251 info->min_generation = c->generation;
3253 *(indegree_slab_at(&info->indegree, c)) = 1;
3255 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
3256 record_author_date(&info->author_date, c);
3258 compute_indegrees_to_depth(revs, info->min_generation);
3260 for (list = revs->commits; list; list = list->next) {
3261 struct commit *c = list->item;
3263 if (*(indegree_slab_at(&info->indegree, c)) == 1)
3264 prio_queue_put(&info->topo_queue, c);
3268 * This is unfortunate; the initial tips need to be shown
3269 * in the order given from the revision traversal machinery.
3271 if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
3272 prio_queue_reverse(&info->topo_queue);
3275 static struct commit *next_topo_commit(struct rev_info *revs)
3278 struct topo_walk_info *info = revs->topo_walk_info;
3280 /* pop next off of topo_queue */
3281 c = prio_queue_get(&info->topo_queue);
3284 *(indegree_slab_at(&info->indegree, c)) = 0;
3289 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
3291 struct commit_list *p;
3292 struct topo_walk_info *info = revs->topo_walk_info;
3293 if (process_parents(revs, commit, NULL, NULL) < 0) {
3294 if (!revs->ignore_missing_links)
3295 die("Failed to traverse parents of commit %s",
3296 oid_to_hex(&commit->object.oid));
3299 for (p = commit->parents; p; p = p->next) {
3300 struct commit *parent = p->item;
3303 if (parent->object.flags & UNINTERESTING)
3306 if (parse_commit_gently(parent, 1) < 0)
3309 if (parent->generation < info->min_generation) {
3310 info->min_generation = parent->generation;
3311 compute_indegrees_to_depth(revs, info->min_generation);
3314 pi = indegree_slab_at(&info->indegree, parent);
3318 prio_queue_put(&info->topo_queue, parent);
3320 if (revs->first_parent_only)
3325 int prepare_revision_walk(struct rev_info *revs)
3328 struct object_array old_pending;
3329 struct commit_list **next = &revs->commits;
3331 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
3332 revs->pending.nr = 0;
3333 revs->pending.alloc = 0;
3334 revs->pending.objects = NULL;
3335 for (i = 0; i < old_pending.nr; i++) {
3336 struct object_array_entry *e = old_pending.objects + i;
3337 struct commit *commit = handle_commit(revs, e);
3339 if (!(commit->object.flags & SEEN)) {
3340 commit->object.flags |= SEEN;
3341 next = commit_list_append(commit, next);
3345 object_array_clear(&old_pending);
3347 /* Signal whether we need per-parent treesame decoration */
3348 if (revs->simplify_merges ||
3349 (revs->limited && limiting_can_increase_treesame(revs)))
3350 revs->treesame.name = "treesame";
3352 if (revs->exclude_promisor_objects) {
3353 for_each_packed_object(mark_uninteresting, revs,
3354 FOR_EACH_OBJECT_PROMISOR_ONLY);
3357 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
3358 commit_list_sort_by_date(&revs->commits);
3361 if (revs->limited) {
3362 if (limit_list(revs) < 0)
3364 if (revs->topo_order)
3365 sort_in_topological_order(&revs->commits, revs->sort_order);
3366 } else if (revs->topo_order)
3367 init_topo_walk(revs);
3368 if (revs->line_level_traverse)
3369 line_log_filter(revs);
3370 if (revs->simplify_merges)
3371 simplify_merges(revs);
3372 if (revs->children.name)
3377 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
3379 struct prio_queue *queue)
3382 struct commit *p = *pp;
3384 if (process_parents(revs, p, NULL, queue) < 0)
3385 return rewrite_one_error;
3386 if (p->object.flags & UNINTERESTING)
3387 return rewrite_one_ok;
3388 if (!(p->object.flags & TREESAME))
3389 return rewrite_one_ok;
3391 return rewrite_one_noparents;
3392 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
3393 return rewrite_one_ok;
3398 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
3401 struct commit *item = prio_queue_peek(q);
3402 struct commit_list *p = *list;
3404 if (p && p->item->date >= item->date)
3407 p = commit_list_insert(item, list);
3408 list = &p->next; /* skip newly added item */
3409 prio_queue_get(q); /* pop item */
3414 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
3416 struct prio_queue queue = { compare_commits_by_commit_date };
3417 enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
3418 merge_queue_into_list(&queue, &revs->commits);
3419 clear_prio_queue(&queue);
3423 int rewrite_parents(struct rev_info *revs, struct commit *commit,
3424 rewrite_parent_fn_t rewrite_parent)
3426 struct commit_list **pp = &commit->parents;
3428 struct commit_list *parent = *pp;
3429 switch (rewrite_parent(revs, &parent->item)) {
3430 case rewrite_one_ok:
3432 case rewrite_one_noparents:
3435 case rewrite_one_error:
3440 remove_duplicate_parents(revs, commit);
3444 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
3446 char *person, *endp;
3447 size_t len, namelen, maillen;
3450 struct ident_split ident;
3452 person = strstr(buf->buf, what);
3456 person += strlen(what);
3457 endp = strchr(person, '\n');
3461 len = endp - person;
3463 if (split_ident_line(&ident, person, len))
3466 mail = ident.mail_begin;
3467 maillen = ident.mail_end - ident.mail_begin;
3468 name = ident.name_begin;
3469 namelen = ident.name_end - ident.name_begin;
3471 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
3472 struct strbuf namemail = STRBUF_INIT;
3474 strbuf_addf(&namemail, "%.*s <%.*s>",
3475 (int)namelen, name, (int)maillen, mail);
3477 strbuf_splice(buf, ident.name_begin - buf->buf,
3478 ident.mail_end - ident.name_begin + 1,
3479 namemail.buf, namemail.len);
3481 strbuf_release(&namemail);
3489 static int commit_match(struct commit *commit, struct rev_info *opt)
3492 const char *encoding;
3493 const char *message;
3494 struct strbuf buf = STRBUF_INIT;
3496 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
3499 /* Prepend "fake" headers as needed */
3500 if (opt->grep_filter.use_reflog_filter) {
3501 strbuf_addstr(&buf, "reflog ");
3502 get_reflog_message(&buf, opt->reflog_info);
3503 strbuf_addch(&buf, '\n');
3507 * We grep in the user's output encoding, under the assumption that it
3508 * is the encoding they are most likely to write their grep pattern
3509 * for. In addition, it means we will match the "notes" encoding below,
3510 * so we will not end up with a buffer that has two different encodings
3513 encoding = get_log_output_encoding();
3514 message = logmsg_reencode(commit, NULL, encoding);
3516 /* Copy the commit to temporary if we are using "fake" headers */
3518 strbuf_addstr(&buf, message);
3520 if (opt->grep_filter.header_list && opt->mailmap) {
3522 strbuf_addstr(&buf, message);
3524 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
3525 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
3528 /* Append "fake" message parts as needed */
3529 if (opt->show_notes) {
3531 strbuf_addstr(&buf, message);
3532 format_display_notes(&commit->object.oid, &buf, encoding, 1);
3536 * Find either in the original commit message, or in the temporary.
3537 * Note that we cast away the constness of "message" here. It is
3538 * const because it may come from the cached commit buffer. That's OK,
3539 * because we know that it is modifiable heap memory, and that while
3540 * grep_buffer may modify it for speed, it will restore any
3541 * changes before returning.
3544 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
3546 retval = grep_buffer(&opt->grep_filter,
3547 (char *)message, strlen(message));
3548 strbuf_release(&buf);
3549 unuse_commit_buffer(commit, message);
3550 return opt->invert_grep ? !retval : retval;
3553 static inline int want_ancestry(const struct rev_info *revs)
3555 return (revs->rewrite_parents || revs->children.name);
3559 * Return a timestamp to be used for --since/--until comparisons for this
3560 * commit, based on the revision options.
3562 static timestamp_t comparison_date(const struct rev_info *revs,
3563 struct commit *commit)
3565 return revs->reflog_info ?
3566 get_reflog_timestamp(revs->reflog_info) :
3570 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
3572 if (commit->object.flags & SHOWN)
3573 return commit_ignore;
3574 if (revs->unpacked && has_object_pack(&commit->object.oid))
3575 return commit_ignore;
3576 if (commit->object.flags & UNINTERESTING)
3577 return commit_ignore;
3578 if (revs->min_age != -1 &&
3579 comparison_date(revs, commit) > revs->min_age)
3580 return commit_ignore;
3581 if (revs->min_parents || (revs->max_parents >= 0)) {
3582 int n = commit_list_count(commit->parents);
3583 if ((n < revs->min_parents) ||
3584 ((revs->max_parents >= 0) && (n > revs->max_parents)))
3585 return commit_ignore;
3587 if (!commit_match(commit, revs))
3588 return commit_ignore;
3589 if (revs->prune && revs->dense) {
3590 /* Commit without changes? */
3591 if (commit->object.flags & TREESAME) {
3593 struct commit_list *p;
3594 /* drop merges unless we want parenthood */
3595 if (!want_ancestry(revs))
3596 return commit_ignore;
3598 * If we want ancestry, then need to keep any merges
3599 * between relevant commits to tie together topology.
3600 * For consistency with TREESAME and simplification
3601 * use "relevant" here rather than just INTERESTING,
3602 * to treat bottom commit(s) as part of the topology.
3604 for (n = 0, p = commit->parents; p; p = p->next)
3605 if (relevant_commit(p->item))
3608 return commit_ignore;
3614 define_commit_slab(saved_parents, struct commit_list *);
3616 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3619 * You may only call save_parents() once per commit (this is checked
3620 * for non-root commits).
3622 static void save_parents(struct rev_info *revs, struct commit *commit)
3624 struct commit_list **pp;
3626 if (!revs->saved_parents_slab) {
3627 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3628 init_saved_parents(revs->saved_parents_slab);
3631 pp = saved_parents_at(revs->saved_parents_slab, commit);
3634 * When walking with reflogs, we may visit the same commit
3635 * several times: once for each appearance in the reflog.
3637 * In this case, save_parents() will be called multiple times.
3638 * We want to keep only the first set of parents. We need to
3639 * store a sentinel value for an empty (i.e., NULL) parent
3640 * list to distinguish it from a not-yet-saved list, however.
3644 if (commit->parents)
3645 *pp = copy_commit_list(commit->parents);
3647 *pp = EMPTY_PARENT_LIST;
3650 static void free_saved_parents(struct rev_info *revs)
3652 if (revs->saved_parents_slab)
3653 clear_saved_parents(revs->saved_parents_slab);
3656 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3658 struct commit_list *parents;
3660 if (!revs->saved_parents_slab)
3661 return commit->parents;
3663 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3664 if (parents == EMPTY_PARENT_LIST)
3669 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3671 enum commit_action action = get_commit_action(revs, commit);
3673 if (action == commit_show &&
3674 revs->prune && revs->dense && want_ancestry(revs)) {
3676 * --full-diff on simplified parents is no good: it
3677 * will show spurious changes from the commits that
3678 * were elided. So we save the parents on the side
3679 * when --full-diff is in effect.
3681 if (revs->full_diff)
3682 save_parents(revs, commit);
3683 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3684 return commit_error;
3689 static void track_linear(struct rev_info *revs, struct commit *commit)
3691 if (revs->track_first_time) {
3693 revs->track_first_time = 0;
3695 struct commit_list *p;
3696 for (p = revs->previous_parents; p; p = p->next)
3697 if (p->item == NULL || /* first commit */
3698 oideq(&p->item->object.oid, &commit->object.oid))
3700 revs->linear = p != NULL;
3702 if (revs->reverse) {
3704 commit->object.flags |= TRACK_LINEAR;
3706 free_commit_list(revs->previous_parents);
3707 revs->previous_parents = copy_commit_list(commit->parents);
3710 static struct commit *get_revision_1(struct rev_info *revs)
3713 struct commit *commit;
3715 if (revs->reflog_info)
3716 commit = next_reflog_entry(revs->reflog_info);
3717 else if (revs->topo_walk_info)
3718 commit = next_topo_commit(revs);
3720 commit = pop_commit(&revs->commits);
3725 if (revs->reflog_info)
3726 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3729 * If we haven't done the list limiting, we need to look at
3730 * the parents here. We also need to do the date-based limiting
3731 * that we'd otherwise have done in limit_list().
3733 if (!revs->limited) {
3734 if (revs->max_age != -1 &&
3735 comparison_date(revs, commit) < revs->max_age)
3738 if (revs->reflog_info)
3739 try_to_simplify_commit(revs, commit);
3740 else if (revs->topo_walk_info)
3741 expand_topo_walk(revs, commit);
3742 else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
3743 if (!revs->ignore_missing_links)
3744 die("Failed to traverse parents of commit %s",
3745 oid_to_hex(&commit->object.oid));
3749 switch (simplify_commit(revs, commit)) {
3753 die("Failed to simplify parents of commit %s",
3754 oid_to_hex(&commit->object.oid));
3756 if (revs->track_linear)
3757 track_linear(revs, commit);
3764 * Return true for entries that have not yet been shown. (This is an
3765 * object_array_each_func_t.)
3767 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3769 return !(entry->item->flags & SHOWN);
3773 * If array is on the verge of a realloc, garbage-collect any entries
3774 * that have already been shown to try to free up some space.
3776 static void gc_boundary(struct object_array *array)
3778 if (array->nr == array->alloc)
3779 object_array_filter(array, entry_unshown, NULL);
3782 static void create_boundary_commit_list(struct rev_info *revs)
3786 struct object_array *array = &revs->boundary_commits;
3787 struct object_array_entry *objects = array->objects;
3790 * If revs->commits is non-NULL at this point, an error occurred in
3791 * get_revision_1(). Ignore the error and continue printing the
3792 * boundary commits anyway. (This is what the code has always
3795 if (revs->commits) {
3796 free_commit_list(revs->commits);
3797 revs->commits = NULL;
3801 * Put all of the actual boundary commits from revs->boundary_commits
3802 * into revs->commits
3804 for (i = 0; i < array->nr; i++) {
3805 c = (struct commit *)(objects[i].item);
3808 if (!(c->object.flags & CHILD_SHOWN))
3810 if (c->object.flags & (SHOWN | BOUNDARY))
3812 c->object.flags |= BOUNDARY;
3813 commit_list_insert(c, &revs->commits);
3817 * If revs->topo_order is set, sort the boundary commits
3818 * in topological order
3820 sort_in_topological_order(&revs->commits, revs->sort_order);
3823 static struct commit *get_revision_internal(struct rev_info *revs)
3825 struct commit *c = NULL;
3826 struct commit_list *l;
3828 if (revs->boundary == 2) {
3830 * All of the normal commits have already been returned,
3831 * and we are now returning boundary commits.
3832 * create_boundary_commit_list() has populated
3833 * revs->commits with the remaining commits to return.
3835 c = pop_commit(&revs->commits);
3837 c->object.flags |= SHOWN;
3842 * If our max_count counter has reached zero, then we are done. We
3843 * don't simply return NULL because we still might need to show
3844 * boundary commits. But we want to avoid calling get_revision_1, which
3845 * might do a considerable amount of work finding the next commit only
3846 * for us to throw it away.
3848 * If it is non-zero, then either we don't have a max_count at all
3849 * (-1), or it is still counting, in which case we decrement.
3851 if (revs->max_count) {
3852 c = get_revision_1(revs);
3854 while (revs->skip_count > 0) {
3856 c = get_revision_1(revs);
3862 if (revs->max_count > 0)
3867 c->object.flags |= SHOWN;
3869 if (!revs->boundary)
3874 * get_revision_1() runs out the commits, and
3875 * we are done computing the boundaries.
3876 * switch to boundary commits output mode.
3881 * Update revs->commits to contain the list of
3884 create_boundary_commit_list(revs);
3886 return get_revision_internal(revs);
3890 * boundary commits are the commits that are parents of the
3891 * ones we got from get_revision_1() but they themselves are
3892 * not returned from get_revision_1(). Before returning
3893 * 'c', we need to mark its parents that they could be boundaries.
3896 for (l = c->parents; l; l = l->next) {
3898 p = &(l->item->object);
3899 if (p->flags & (CHILD_SHOWN | SHOWN))
3901 p->flags |= CHILD_SHOWN;
3902 gc_boundary(&revs->boundary_commits);
3903 add_object_array(p, NULL, &revs->boundary_commits);
3909 struct commit *get_revision(struct rev_info *revs)
3912 struct commit_list *reversed;
3914 if (revs->reverse) {
3916 while ((c = get_revision_internal(revs)))
3917 commit_list_insert(c, &reversed);
3918 revs->commits = reversed;
3920 revs->reverse_output_stage = 1;
3923 if (revs->reverse_output_stage) {
3924 c = pop_commit(&revs->commits);
3925 if (revs->track_linear)
3926 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3930 c = get_revision_internal(revs);
3931 if (c && revs->graph)
3932 graph_update(revs->graph, c);
3934 free_saved_parents(revs);
3935 if (revs->previous_parents) {
3936 free_commit_list(revs->previous_parents);
3937 revs->previous_parents = NULL;
3943 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3945 if (commit->object.flags & BOUNDARY)
3947 else if (commit->object.flags & UNINTERESTING)
3949 else if (commit->object.flags & PATCHSAME)
3951 else if (!revs || revs->left_right) {
3952 if (commit->object.flags & SYMMETRIC_LEFT)
3956 } else if (revs->graph)
3958 else if (revs->cherry_mark)
3963 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3965 char *mark = get_revision_mark(revs, commit);
3968 fputs(mark, stdout);