11 #include "reflog-walk.h"
12 #include "patch-ids.h"
15 #include "string-list.h"
18 #include "commit-slab.h"
20 #include "cache-tree.h"
22 volatile show_early_output_fn_t show_early_output;
24 void show_object_with_name(FILE *out, struct object *obj, const char *name)
28 fprintf(out, "%s ", sha1_to_hex(obj->sha1));
29 for (p = name; *p && *p != '\n'; p++)
34 static void mark_blob_uninteresting(struct blob *blob)
38 if (blob->object.flags & UNINTERESTING)
40 blob->object.flags |= UNINTERESTING;
43 static void mark_tree_contents_uninteresting(struct tree *tree)
45 struct tree_desc desc;
46 struct name_entry entry;
47 struct object *obj = &tree->object;
49 if (!has_sha1_file(obj->sha1))
51 if (parse_tree(tree) < 0)
52 die("bad tree %s", sha1_to_hex(obj->sha1));
54 init_tree_desc(&desc, tree->buffer, tree->size);
55 while (tree_entry(&desc, &entry)) {
56 switch (object_type(entry.mode)) {
58 mark_tree_uninteresting(lookup_tree(entry.sha1));
61 mark_blob_uninteresting(lookup_blob(entry.sha1));
64 /* Subproject commit - not in this repository */
70 * We don't care about the tree any more
71 * after it has been marked uninteresting.
73 free_tree_buffer(tree);
76 void mark_tree_uninteresting(struct tree *tree)
78 struct object *obj = &tree->object;
82 if (obj->flags & UNINTERESTING)
84 obj->flags |= UNINTERESTING;
85 mark_tree_contents_uninteresting(tree);
88 void mark_parents_uninteresting(struct commit *commit)
90 struct commit_list *parents = NULL, *l;
92 for (l = commit->parents; l; l = l->next)
93 commit_list_insert(l->item, &parents);
96 struct commit *commit = parents->item;
98 parents = parents->next;
103 * A missing commit is ok iff its parent is marked
106 * We just mark such a thing parsed, so that when
107 * it is popped next time around, we won't be trying
108 * to parse it and get an error.
110 if (!has_sha1_file(commit->object.sha1))
111 commit->object.parsed = 1;
113 if (commit->object.flags & UNINTERESTING)
116 commit->object.flags |= UNINTERESTING;
119 * Normally we haven't parsed the parent
120 * yet, so we won't have a parent of a parent
121 * here. However, it may turn out that we've
122 * reached this commit some other way (where it
123 * wasn't uninteresting), in which case we need
124 * to mark its parents recursively too..
126 if (!commit->parents)
129 for (l = commit->parents->next; l; l = l->next)
130 commit_list_insert(l->item, &parents);
131 commit = commit->parents->item;
136 static void add_pending_object_with_path(struct rev_info *revs,
138 const char *name, unsigned mode,
143 if (revs->no_walk && (obj->flags & UNINTERESTING))
145 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
146 struct strbuf buf = STRBUF_INIT;
147 int len = interpret_branch_name(name, 0, &buf);
150 if (0 < len && name[len] && buf.len)
151 strbuf_addstr(&buf, name + len);
152 st = add_reflog_for_walk(revs->reflog_info,
153 (struct commit *)obj,
154 buf.buf[0] ? buf.buf: name);
155 strbuf_release(&buf);
159 add_object_array_with_path(obj, name, &revs->pending, mode, path);
162 static void add_pending_object_with_mode(struct rev_info *revs,
164 const char *name, unsigned mode)
166 add_pending_object_with_path(revs, obj, name, mode, NULL);
169 void add_pending_object(struct rev_info *revs,
170 struct object *obj, const char *name)
172 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
175 void add_head_to_pending(struct rev_info *revs)
177 unsigned char sha1[20];
179 if (get_sha1("HEAD", sha1))
181 obj = parse_object(sha1);
184 add_pending_object(revs, obj, "HEAD");
187 static struct object *get_reference(struct rev_info *revs, const char *name,
188 const unsigned char *sha1,
191 struct object *object;
193 object = parse_object(sha1);
195 if (revs->ignore_missing)
197 die("bad object %s", name);
199 object->flags |= flags;
203 void add_pending_sha1(struct rev_info *revs, const char *name,
204 const unsigned char *sha1, unsigned int flags)
206 struct object *object = get_reference(revs, name, sha1, flags);
207 add_pending_object(revs, object, name);
210 static struct commit *handle_commit(struct rev_info *revs,
211 struct object_array_entry *entry)
213 struct object *object = entry->item;
214 const char *name = entry->name;
215 const char *path = entry->path;
216 unsigned int mode = entry->mode;
217 unsigned long flags = object->flags;
220 * Tag object? Look what it points to..
222 while (object->type == OBJ_TAG) {
223 struct tag *tag = (struct tag *) object;
224 if (revs->tag_objects && !(flags & UNINTERESTING))
225 add_pending_object(revs, object, tag->tag);
228 object = parse_object(tag->tagged->sha1);
230 if (flags & UNINTERESTING)
232 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
234 object->flags |= flags;
236 * We'll handle the tagged object by looping or dropping
237 * through to the non-tag handlers below. Do not
238 * propagate data from the tag's pending entry.
246 * Commit object? Just return it, we'll do all the complex
249 if (object->type == OBJ_COMMIT) {
250 struct commit *commit = (struct commit *)object;
251 if (parse_commit(commit) < 0)
252 die("unable to parse commit %s", name);
253 if (flags & UNINTERESTING) {
254 mark_parents_uninteresting(commit);
257 if (revs->show_source && !commit->util)
258 commit->util = xstrdup(name);
263 * Tree object? Either mark it uninteresting, or add it
264 * to the list of objects to look at later..
266 if (object->type == OBJ_TREE) {
267 struct tree *tree = (struct tree *)object;
268 if (!revs->tree_objects)
270 if (flags & UNINTERESTING) {
271 mark_tree_contents_uninteresting(tree);
274 add_pending_object_with_path(revs, object, name, mode, path);
279 * Blob object? You know the drill by now..
281 if (object->type == OBJ_BLOB) {
282 if (!revs->blob_objects)
284 if (flags & UNINTERESTING)
286 add_pending_object_with_path(revs, object, name, mode, path);
289 die("%s is unknown object", name);
292 static int everybody_uninteresting(struct commit_list *orig,
293 struct commit **interesting_cache)
295 struct commit_list *list = orig;
297 if (*interesting_cache) {
298 struct commit *commit = *interesting_cache;
299 if (!(commit->object.flags & UNINTERESTING))
304 struct commit *commit = list->item;
306 if (commit->object.flags & UNINTERESTING)
308 if (interesting_cache)
309 *interesting_cache = commit;
316 * A definition of "relevant" commit that we can use to simplify limited graphs
317 * by eliminating side branches.
319 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
320 * in our list), or that is a specified BOTTOM commit. Then after computing
321 * a limited list, during processing we can generally ignore boundary merges
322 * coming from outside the graph, (ie from irrelevant parents), and treat
323 * those merges as if they were single-parent. TREESAME is defined to consider
324 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
325 * we don't care if we were !TREESAME to non-graph parents.
327 * Treating bottom commits as relevant ensures that a limited graph's
328 * connection to the actual bottom commit is not viewed as a side branch, but
329 * treated as part of the graph. For example:
331 * ....Z...A---X---o---o---B
335 * When computing "A..B", the A-X connection is at least as important as
336 * Y-X, despite A being flagged UNINTERESTING.
338 * And when computing --ancestry-path "A..B", the A-X connection is more
339 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
341 static inline int relevant_commit(struct commit *commit)
343 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
347 * Return a single relevant commit from a parent list. If we are a TREESAME
348 * commit, and this selects one of our parents, then we can safely simplify to
351 static struct commit *one_relevant_parent(const struct rev_info *revs,
352 struct commit_list *orig)
354 struct commit_list *list = orig;
355 struct commit *relevant = NULL;
361 * For 1-parent commits, or if first-parent-only, then return that
362 * first parent (even if not "relevant" by the above definition).
363 * TREESAME will have been set purely on that parent.
365 if (revs->first_parent_only || !orig->next)
369 * For multi-parent commits, identify a sole relevant parent, if any.
370 * If we have only one relevant parent, then TREESAME will be set purely
371 * with regard to that parent, and we can simplify accordingly.
373 * If we have more than one relevant parent, or no relevant parents
374 * (and multiple irrelevant ones), then we can't select a parent here
378 struct commit *commit = list->item;
380 if (relevant_commit(commit)) {
390 * The goal is to get REV_TREE_NEW as the result only if the
391 * diff consists of all '+' (and no other changes), REV_TREE_OLD
392 * if the whole diff is removal of old data, and otherwise
393 * REV_TREE_DIFFERENT (of course if the trees are the same we
394 * want REV_TREE_SAME).
395 * That means that once we get to REV_TREE_DIFFERENT, we do not
396 * have to look any further.
398 static int tree_difference = REV_TREE_SAME;
400 static void file_add_remove(struct diff_options *options,
401 int addremove, unsigned mode,
402 const unsigned char *sha1,
404 const char *fullpath, unsigned dirty_submodule)
406 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
408 tree_difference |= diff;
409 if (tree_difference == REV_TREE_DIFFERENT)
410 DIFF_OPT_SET(options, HAS_CHANGES);
413 static void file_change(struct diff_options *options,
414 unsigned old_mode, unsigned new_mode,
415 const unsigned char *old_sha1,
416 const unsigned char *new_sha1,
417 int old_sha1_valid, int new_sha1_valid,
418 const char *fullpath,
419 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
421 tree_difference = REV_TREE_DIFFERENT;
422 DIFF_OPT_SET(options, HAS_CHANGES);
425 static int rev_compare_tree(struct rev_info *revs,
426 struct commit *parent, struct commit *commit)
428 struct tree *t1 = parent->tree;
429 struct tree *t2 = commit->tree;
436 if (revs->simplify_by_decoration) {
438 * If we are simplifying by decoration, then the commit
439 * is worth showing if it has a tag pointing at it.
441 if (get_name_decoration(&commit->object))
442 return REV_TREE_DIFFERENT;
444 * A commit that is not pointed by a tag is uninteresting
445 * if we are not limited by path. This means that you will
446 * see the usual "commits that touch the paths" plus any
447 * tagged commit by specifying both --simplify-by-decoration
450 if (!revs->prune_data.nr)
451 return REV_TREE_SAME;
454 tree_difference = REV_TREE_SAME;
455 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
456 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
458 return REV_TREE_DIFFERENT;
459 return tree_difference;
462 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
465 struct tree *t1 = commit->tree;
470 tree_difference = REV_TREE_SAME;
471 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
472 retval = diff_tree_sha1(NULL, t1->object.sha1, "", &revs->pruning);
474 return retval >= 0 && (tree_difference == REV_TREE_SAME);
477 struct treesame_state {
478 unsigned int nparents;
479 unsigned char treesame[FLEX_ARRAY];
482 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
484 unsigned n = commit_list_count(commit->parents);
485 struct treesame_state *st = xcalloc(1, sizeof(*st) + n);
487 add_decoration(&revs->treesame, &commit->object, st);
492 * Must be called immediately after removing the nth_parent from a commit's
493 * parent list, if we are maintaining the per-parent treesame[] decoration.
494 * This does not recalculate the master TREESAME flag - update_treesame()
495 * should be called to update it after a sequence of treesame[] modifications
496 * that may have affected it.
498 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
500 struct treesame_state *st;
503 if (!commit->parents) {
505 * Have just removed the only parent from a non-merge.
506 * Different handling, as we lack decoration.
509 die("compact_treesame %u", nth_parent);
510 old_same = !!(commit->object.flags & TREESAME);
511 if (rev_same_tree_as_empty(revs, commit))
512 commit->object.flags |= TREESAME;
514 commit->object.flags &= ~TREESAME;
518 st = lookup_decoration(&revs->treesame, &commit->object);
519 if (!st || nth_parent >= st->nparents)
520 die("compact_treesame %u", nth_parent);
522 old_same = st->treesame[nth_parent];
523 memmove(st->treesame + nth_parent,
524 st->treesame + nth_parent + 1,
525 st->nparents - nth_parent - 1);
528 * If we've just become a non-merge commit, update TREESAME
529 * immediately, and remove the no-longer-needed decoration.
530 * If still a merge, defer update until update_treesame().
532 if (--st->nparents == 1) {
533 if (commit->parents->next)
534 die("compact_treesame parents mismatch");
535 if (st->treesame[0] && revs->dense)
536 commit->object.flags |= TREESAME;
538 commit->object.flags &= ~TREESAME;
539 free(add_decoration(&revs->treesame, &commit->object, NULL));
545 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
547 if (commit->parents && commit->parents->next) {
549 struct treesame_state *st;
550 struct commit_list *p;
551 unsigned relevant_parents;
552 unsigned relevant_change, irrelevant_change;
554 st = lookup_decoration(&revs->treesame, &commit->object);
556 die("update_treesame %s", sha1_to_hex(commit->object.sha1));
557 relevant_parents = 0;
558 relevant_change = irrelevant_change = 0;
559 for (p = commit->parents, n = 0; p; n++, p = p->next) {
560 if (relevant_commit(p->item)) {
561 relevant_change |= !st->treesame[n];
564 irrelevant_change |= !st->treesame[n];
566 if (relevant_parents ? relevant_change : irrelevant_change)
567 commit->object.flags &= ~TREESAME;
569 commit->object.flags |= TREESAME;
572 return commit->object.flags & TREESAME;
575 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
578 * TREESAME is irrelevant unless prune && dense;
579 * if simplify_history is set, we can't have a mixture of TREESAME and
580 * !TREESAME INTERESTING parents (and we don't have treesame[]
581 * decoration anyway);
582 * if first_parent_only is set, then the TREESAME flag is locked
583 * against the first parent (and again we lack treesame[] decoration).
585 return revs->prune && revs->dense &&
586 !revs->simplify_history &&
587 !revs->first_parent_only;
590 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
592 struct commit_list **pp, *parent;
593 struct treesame_state *ts = NULL;
594 int relevant_change = 0, irrelevant_change = 0;
595 int relevant_parents, nth_parent;
598 * If we don't do pruning, everything is interesting
606 if (!commit->parents) {
607 if (rev_same_tree_as_empty(revs, commit))
608 commit->object.flags |= TREESAME;
613 * Normal non-merge commit? If we don't want to make the
614 * history dense, we consider it always to be a change..
616 if (!revs->dense && !commit->parents->next)
619 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
620 (parent = *pp) != NULL;
621 pp = &parent->next, nth_parent++) {
622 struct commit *p = parent->item;
623 if (relevant_commit(p))
626 if (nth_parent == 1) {
628 * This our second loop iteration - so we now know
629 * we're dealing with a merge.
631 * Do not compare with later parents when we care only about
632 * the first parent chain, in order to avoid derailing the
633 * traversal to follow a side branch that brought everything
634 * in the path we are limited to by the pathspec.
636 if (revs->first_parent_only)
639 * If this will remain a potentially-simplifiable
640 * merge, remember per-parent treesame if needed.
641 * Initialise the array with the comparison from our
644 if (revs->treesame.name &&
645 !revs->simplify_history &&
646 !(commit->object.flags & UNINTERESTING)) {
647 ts = initialise_treesame(revs, commit);
648 if (!(irrelevant_change || relevant_change))
652 if (parse_commit(p) < 0)
653 die("cannot simplify commit %s (because of %s)",
654 sha1_to_hex(commit->object.sha1),
655 sha1_to_hex(p->object.sha1));
656 switch (rev_compare_tree(revs, p, commit)) {
658 if (!revs->simplify_history || !relevant_commit(p)) {
659 /* Even if a merge with an uninteresting
660 * side branch brought the entire change
661 * we are interested in, we do not want
662 * to lose the other branches of this
663 * merge, so we just keep going.
666 ts->treesame[nth_parent] = 1;
670 commit->parents = parent;
671 commit->object.flags |= TREESAME;
675 if (revs->remove_empty_trees &&
676 rev_same_tree_as_empty(revs, p)) {
677 /* We are adding all the specified
678 * paths from this parent, so the
679 * history beyond this parent is not
680 * interesting. Remove its parents
681 * (they are grandparents for us).
682 * IOW, we pretend this parent is a
685 if (parse_commit(p) < 0)
686 die("cannot simplify commit %s (invalid %s)",
687 sha1_to_hex(commit->object.sha1),
688 sha1_to_hex(p->object.sha1));
693 case REV_TREE_DIFFERENT:
694 if (relevant_commit(p))
697 irrelevant_change = 1;
700 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
704 * TREESAME is straightforward for single-parent commits. For merge
705 * commits, it is most useful to define it so that "irrelevant"
706 * parents cannot make us !TREESAME - if we have any relevant
707 * parents, then we only consider TREESAMEness with respect to them,
708 * allowing irrelevant merges from uninteresting branches to be
709 * simplified away. Only if we have only irrelevant parents do we
710 * base TREESAME on them. Note that this logic is replicated in
711 * update_treesame, which should be kept in sync.
713 if (relevant_parents ? !relevant_change : !irrelevant_change)
714 commit->object.flags |= TREESAME;
717 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
718 struct commit_list *cached_base, struct commit_list **cache)
720 struct commit_list *new_entry;
722 if (cached_base && p->date < cached_base->item->date)
723 new_entry = commit_list_insert_by_date(p, &cached_base->next);
725 new_entry = commit_list_insert_by_date(p, head);
727 if (cache && (!*cache || p->date < (*cache)->item->date))
731 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
732 struct commit_list **list, struct commit_list **cache_ptr)
734 struct commit_list *parent = commit->parents;
736 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
738 if (commit->object.flags & ADDED)
740 commit->object.flags |= ADDED;
742 if (revs->include_check &&
743 !revs->include_check(commit, revs->include_check_data))
747 * If the commit is uninteresting, don't try to
748 * prune parents - we want the maximal uninteresting
751 * Normally we haven't parsed the parent
752 * yet, so we won't have a parent of a parent
753 * here. However, it may turn out that we've
754 * reached this commit some other way (where it
755 * wasn't uninteresting), in which case we need
756 * to mark its parents recursively too..
758 if (commit->object.flags & UNINTERESTING) {
760 struct commit *p = parent->item;
761 parent = parent->next;
763 p->object.flags |= UNINTERESTING;
764 if (parse_commit_gently(p, 1) < 0)
767 mark_parents_uninteresting(p);
768 if (p->object.flags & SEEN)
770 p->object.flags |= SEEN;
771 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
777 * Ok, the commit wasn't uninteresting. Try to
778 * simplify the commit history and find the parent
779 * that has no differences in the path set if one exists.
781 try_to_simplify_commit(revs, commit);
786 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
788 for (parent = commit->parents; parent; parent = parent->next) {
789 struct commit *p = parent->item;
791 if (parse_commit_gently(p, revs->ignore_missing_links) < 0)
793 if (revs->show_source && !p->util)
794 p->util = commit->util;
795 p->object.flags |= left_flag;
796 if (!(p->object.flags & SEEN)) {
797 p->object.flags |= SEEN;
798 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
800 if (revs->first_parent_only)
806 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
808 struct commit_list *p;
809 int left_count = 0, right_count = 0;
811 struct patch_ids ids;
812 unsigned cherry_flag;
814 /* First count the commits on the left and on the right */
815 for (p = list; p; p = p->next) {
816 struct commit *commit = p->item;
817 unsigned flags = commit->object.flags;
818 if (flags & BOUNDARY)
820 else if (flags & SYMMETRIC_LEFT)
826 if (!left_count || !right_count)
829 left_first = left_count < right_count;
830 init_patch_ids(&ids);
831 ids.diffopts.pathspec = revs->diffopt.pathspec;
833 /* Compute patch-ids for one side */
834 for (p = list; p; p = p->next) {
835 struct commit *commit = p->item;
836 unsigned flags = commit->object.flags;
838 if (flags & BOUNDARY)
841 * If we have fewer left, left_first is set and we omit
842 * commits on the right branch in this loop. If we have
843 * fewer right, we skip the left ones.
845 if (left_first != !!(flags & SYMMETRIC_LEFT))
847 commit->util = add_commit_patch_id(commit, &ids);
850 /* either cherry_mark or cherry_pick are true */
851 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
853 /* Check the other side */
854 for (p = list; p; p = p->next) {
855 struct commit *commit = p->item;
857 unsigned flags = commit->object.flags;
859 if (flags & BOUNDARY)
862 * If we have fewer left, left_first is set and we omit
863 * commits on the left branch in this loop.
865 if (left_first == !!(flags & SYMMETRIC_LEFT))
869 * Have we seen the same patch id?
871 id = has_commit_patch_id(commit, &ids);
875 commit->object.flags |= cherry_flag;
878 /* Now check the original side for seen ones */
879 for (p = list; p; p = p->next) {
880 struct commit *commit = p->item;
881 struct patch_id *ent;
887 commit->object.flags |= cherry_flag;
891 free_patch_ids(&ids);
894 /* How many extra uninteresting commits we want to see.. */
897 static int still_interesting(struct commit_list *src, unsigned long date, int slop,
898 struct commit **interesting_cache)
901 * No source list at all? We're definitely done..
907 * Does the destination list contain entries with a date
908 * before the source list? Definitely _not_ done.
910 if (date <= src->item->date)
914 * Does the source list still have interesting commits in
915 * it? Definitely not done..
917 if (!everybody_uninteresting(src, interesting_cache))
920 /* Ok, we're closing in.. */
925 * "rev-list --ancestry-path A..B" computes commits that are ancestors
926 * of B but not ancestors of A but further limits the result to those
927 * that are descendants of A. This takes the list of bottom commits and
928 * the result of "A..B" without --ancestry-path, and limits the latter
929 * further to the ones that can reach one of the commits in "bottom".
931 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
933 struct commit_list *p;
934 struct commit_list *rlist = NULL;
938 * Reverse the list so that it will be likely that we would
939 * process parents before children.
941 for (p = list; p; p = p->next)
942 commit_list_insert(p->item, &rlist);
944 for (p = bottom; p; p = p->next)
945 p->item->object.flags |= TMP_MARK;
948 * Mark the ones that can reach bottom commits in "list",
949 * in a bottom-up fashion.
953 for (p = rlist; p; p = p->next) {
954 struct commit *c = p->item;
955 struct commit_list *parents;
956 if (c->object.flags & (TMP_MARK | UNINTERESTING))
958 for (parents = c->parents;
960 parents = parents->next) {
961 if (!(parents->item->object.flags & TMP_MARK))
963 c->object.flags |= TMP_MARK;
968 } while (made_progress);
971 * NEEDSWORK: decide if we want to remove parents that are
972 * not marked with TMP_MARK from commit->parents for commits
973 * in the resulting list. We may not want to do that, though.
977 * The ones that are not marked with TMP_MARK are uninteresting
979 for (p = list; p; p = p->next) {
980 struct commit *c = p->item;
981 if (c->object.flags & TMP_MARK)
983 c->object.flags |= UNINTERESTING;
986 /* We are done with the TMP_MARK */
987 for (p = list; p; p = p->next)
988 p->item->object.flags &= ~TMP_MARK;
989 for (p = bottom; p; p = p->next)
990 p->item->object.flags &= ~TMP_MARK;
991 free_commit_list(rlist);
995 * Before walking the history, keep the set of "negative" refs the
996 * caller has asked to exclude.
998 * This is used to compute "rev-list --ancestry-path A..B", as we need
999 * to filter the result of "A..B" further to the ones that can actually
1002 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1004 struct commit_list *elem, *bottom = NULL;
1005 for (elem = list; elem; elem = elem->next)
1006 if (elem->item->object.flags & BOTTOM)
1007 commit_list_insert(elem->item, &bottom);
1011 /* Assumes either left_only or right_only is set */
1012 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1014 struct commit_list *p;
1016 for (p = list; p; p = p->next) {
1017 struct commit *commit = p->item;
1019 if (revs->right_only) {
1020 if (commit->object.flags & SYMMETRIC_LEFT)
1021 commit->object.flags |= SHOWN;
1022 } else /* revs->left_only is set */
1023 if (!(commit->object.flags & SYMMETRIC_LEFT))
1024 commit->object.flags |= SHOWN;
1028 static int limit_list(struct rev_info *revs)
1031 unsigned long date = ~0ul;
1032 struct commit_list *list = revs->commits;
1033 struct commit_list *newlist = NULL;
1034 struct commit_list **p = &newlist;
1035 struct commit_list *bottom = NULL;
1036 struct commit *interesting_cache = NULL;
1038 if (revs->ancestry_path) {
1039 bottom = collect_bottom_commits(list);
1041 die("--ancestry-path given but there are no bottom commits");
1045 struct commit_list *entry = list;
1046 struct commit *commit = list->item;
1047 struct object *obj = &commit->object;
1048 show_early_output_fn_t show;
1053 if (commit == interesting_cache)
1054 interesting_cache = NULL;
1056 if (revs->max_age != -1 && (commit->date < revs->max_age))
1057 obj->flags |= UNINTERESTING;
1058 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1060 if (obj->flags & UNINTERESTING) {
1061 mark_parents_uninteresting(commit);
1063 p = &commit_list_insert(commit, p)->next;
1064 slop = still_interesting(list, date, slop, &interesting_cache);
1067 /* If showing all, add the whole pending list to the end */
1072 if (revs->min_age != -1 && (commit->date > revs->min_age))
1074 date = commit->date;
1075 p = &commit_list_insert(commit, p)->next;
1077 show = show_early_output;
1081 show(revs, newlist);
1082 show_early_output = NULL;
1084 if (revs->cherry_pick || revs->cherry_mark)
1085 cherry_pick_list(newlist, revs);
1087 if (revs->left_only || revs->right_only)
1088 limit_left_right(newlist, revs);
1091 limit_to_ancestry(bottom, newlist);
1092 free_commit_list(bottom);
1096 * Check if any commits have become TREESAME by some of their parents
1097 * becoming UNINTERESTING.
1099 if (limiting_can_increase_treesame(revs))
1100 for (list = newlist; list; list = list->next) {
1101 struct commit *c = list->item;
1102 if (c->object.flags & (UNINTERESTING | TREESAME))
1104 update_treesame(revs, c);
1107 revs->commits = newlist;
1112 * Add an entry to refs->cmdline with the specified information.
1115 static void add_rev_cmdline(struct rev_info *revs,
1116 struct object *item,
1121 struct rev_cmdline_info *info = &revs->cmdline;
1124 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1125 info->rev[nr].item = item;
1126 info->rev[nr].name = xstrdup(name);
1127 info->rev[nr].whence = whence;
1128 info->rev[nr].flags = flags;
1132 static void add_rev_cmdline_list(struct rev_info *revs,
1133 struct commit_list *commit_list,
1137 while (commit_list) {
1138 struct object *object = &commit_list->item->object;
1139 add_rev_cmdline(revs, object, sha1_to_hex(object->sha1),
1141 commit_list = commit_list->next;
1145 struct all_refs_cb {
1147 int warned_bad_reflog;
1148 struct rev_info *all_revs;
1149 const char *name_for_errormsg;
1152 int ref_excluded(struct string_list *ref_excludes, const char *path)
1154 struct string_list_item *item;
1158 for_each_string_list_item(item, ref_excludes) {
1159 if (!wildmatch(item->string, path, 0, NULL))
1165 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
1167 struct all_refs_cb *cb = cb_data;
1168 struct object *object;
1170 if (ref_excluded(cb->all_revs->ref_excludes, path))
1173 object = get_reference(cb->all_revs, path, sha1, cb->all_flags);
1174 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1175 add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags);
1179 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1182 cb->all_revs = revs;
1183 cb->all_flags = flags;
1186 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1188 if (*ref_excludes_p) {
1189 string_list_clear(*ref_excludes_p, 0);
1190 free(*ref_excludes_p);
1192 *ref_excludes_p = NULL;
1195 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1197 if (!*ref_excludes_p) {
1198 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1199 (*ref_excludes_p)->strdup_strings = 1;
1201 string_list_append(*ref_excludes_p, exclude);
1204 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
1205 int (*for_each)(const char *, each_ref_fn, void *))
1207 struct all_refs_cb cb;
1208 init_all_refs_cb(&cb, revs, flags);
1209 for_each(submodule, handle_one_ref, &cb);
1212 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
1214 struct all_refs_cb *cb = cb_data;
1215 if (!is_null_sha1(sha1)) {
1216 struct object *o = parse_object(sha1);
1218 o->flags |= cb->all_flags;
1219 /* ??? CMDLINEFLAGS ??? */
1220 add_pending_object(cb->all_revs, o, "");
1222 else if (!cb->warned_bad_reflog) {
1223 warning("reflog of '%s' references pruned commits",
1224 cb->name_for_errormsg);
1225 cb->warned_bad_reflog = 1;
1230 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
1231 const char *email, unsigned long timestamp, int tz,
1232 const char *message, void *cb_data)
1234 handle_one_reflog_commit(osha1, cb_data);
1235 handle_one_reflog_commit(nsha1, cb_data);
1239 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
1241 struct all_refs_cb *cb = cb_data;
1242 cb->warned_bad_reflog = 0;
1243 cb->name_for_errormsg = path;
1244 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
1248 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1250 struct all_refs_cb cb;
1252 cb.all_flags = flags;
1253 for_each_reflog(handle_one_reflog, &cb);
1256 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1257 struct strbuf *path)
1259 size_t baselen = path->len;
1262 if (it->entry_count >= 0) {
1263 struct tree *tree = lookup_tree(it->sha1);
1264 add_pending_object_with_path(revs, &tree->object, "",
1268 for (i = 0; i < it->subtree_nr; i++) {
1269 struct cache_tree_sub *sub = it->down[i];
1270 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1271 add_cache_tree(sub->cache_tree, revs, path);
1272 strbuf_setlen(path, baselen);
1277 void add_index_objects_to_pending(struct rev_info *revs, unsigned flags)
1282 for (i = 0; i < active_nr; i++) {
1283 struct cache_entry *ce = active_cache[i];
1286 if (S_ISGITLINK(ce->ce_mode))
1289 blob = lookup_blob(ce->sha1);
1291 die("unable to add index blob to traversal");
1292 add_pending_object_with_path(revs, &blob->object, "",
1293 ce->ce_mode, ce->name);
1296 if (active_cache_tree) {
1297 struct strbuf path = STRBUF_INIT;
1298 add_cache_tree(active_cache_tree, revs, &path);
1299 strbuf_release(&path);
1303 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
1305 unsigned char sha1[20];
1307 struct commit *commit;
1308 struct commit_list *parents;
1309 const char *arg = arg_;
1312 flags ^= UNINTERESTING | BOTTOM;
1315 if (get_sha1_committish(arg, sha1))
1318 it = get_reference(revs, arg, sha1, 0);
1319 if (!it && revs->ignore_missing)
1321 if (it->type != OBJ_TAG)
1323 if (!((struct tag*)it)->tagged)
1325 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
1327 if (it->type != OBJ_COMMIT)
1329 commit = (struct commit *)it;
1330 for (parents = commit->parents; parents; parents = parents->next) {
1331 it = &parents->item->object;
1333 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1334 add_pending_object(revs, it, arg);
1339 void init_revisions(struct rev_info *revs, const char *prefix)
1341 memset(revs, 0, sizeof(*revs));
1343 revs->abbrev = DEFAULT_ABBREV;
1344 revs->ignore_merges = 1;
1345 revs->simplify_history = 1;
1346 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1347 DIFF_OPT_SET(&revs->pruning, QUICK);
1348 revs->pruning.add_remove = file_add_remove;
1349 revs->pruning.change = file_change;
1350 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1352 revs->prefix = prefix;
1355 revs->skip_count = -1;
1356 revs->max_count = -1;
1357 revs->max_parents = -1;
1359 revs->commit_format = CMIT_FMT_DEFAULT;
1361 init_grep_defaults();
1362 grep_init(&revs->grep_filter, prefix);
1363 revs->grep_filter.status_only = 1;
1364 revs->grep_filter.regflags = REG_NEWLINE;
1366 diff_setup(&revs->diffopt);
1367 if (prefix && !revs->diffopt.prefix) {
1368 revs->diffopt.prefix = prefix;
1369 revs->diffopt.prefix_length = strlen(prefix);
1372 revs->notes_opt.use_default_notes = -1;
1375 static void add_pending_commit_list(struct rev_info *revs,
1376 struct commit_list *commit_list,
1379 while (commit_list) {
1380 struct object *object = &commit_list->item->object;
1381 object->flags |= flags;
1382 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1383 commit_list = commit_list->next;
1387 static void prepare_show_merge(struct rev_info *revs)
1389 struct commit_list *bases;
1390 struct commit *head, *other;
1391 unsigned char sha1[20];
1392 const char **prune = NULL;
1393 int i, prune_num = 1; /* counting terminating NULL */
1395 if (get_sha1("HEAD", sha1))
1396 die("--merge without HEAD?");
1397 head = lookup_commit_or_die(sha1, "HEAD");
1398 if (get_sha1("MERGE_HEAD", sha1))
1399 die("--merge without MERGE_HEAD?");
1400 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
1401 add_pending_object(revs, &head->object, "HEAD");
1402 add_pending_object(revs, &other->object, "MERGE_HEAD");
1403 bases = get_merge_bases(head, other);
1404 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1405 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1406 free_commit_list(bases);
1407 head->object.flags |= SYMMETRIC_LEFT;
1411 for (i = 0; i < active_nr; i++) {
1412 const struct cache_entry *ce = active_cache[i];
1415 if (ce_path_match(ce, &revs->prune_data, NULL)) {
1417 REALLOC_ARRAY(prune, prune_num);
1418 prune[prune_num-2] = ce->name;
1419 prune[prune_num-1] = NULL;
1421 while ((i+1 < active_nr) &&
1422 ce_same_name(ce, active_cache[i+1]))
1425 free_pathspec(&revs->prune_data);
1426 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1427 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1431 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1433 struct object_context oc;
1435 struct object *object;
1436 unsigned char sha1[20];
1438 const char *arg = arg_;
1439 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1440 unsigned get_sha1_flags = 0;
1442 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1444 dotdot = strstr(arg, "..");
1446 unsigned char from_sha1[20];
1447 const char *next = dotdot + 2;
1448 const char *this = arg;
1449 int symmetric = *next == '.';
1450 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1451 static const char head_by_default[] = "HEAD";
1452 unsigned int a_flags;
1458 next = head_by_default;
1460 this = head_by_default;
1461 if (this == head_by_default && next == head_by_default &&
1464 * Just ".."? That is not a range but the
1465 * pathspec for the parent directory.
1467 if (!cant_be_filename) {
1472 if (!get_sha1_committish(this, from_sha1) &&
1473 !get_sha1_committish(next, sha1)) {
1474 struct object *a_obj, *b_obj;
1476 if (!cant_be_filename) {
1478 verify_non_filename(revs->prefix, arg);
1481 a_obj = parse_object(from_sha1);
1482 b_obj = parse_object(sha1);
1483 if (!a_obj || !b_obj) {
1485 if (revs->ignore_missing)
1488 ? "Invalid symmetric difference expression %s"
1489 : "Invalid revision range %s", arg);
1494 a_flags = flags_exclude;
1496 /* A...B -- find merge bases between the two */
1497 struct commit *a, *b;
1498 struct commit_list *exclude;
1500 a = (a_obj->type == OBJ_COMMIT
1501 ? (struct commit *)a_obj
1502 : lookup_commit_reference(a_obj->sha1));
1503 b = (b_obj->type == OBJ_COMMIT
1504 ? (struct commit *)b_obj
1505 : lookup_commit_reference(b_obj->sha1));
1508 exclude = get_merge_bases(a, b);
1509 add_rev_cmdline_list(revs, exclude,
1512 add_pending_commit_list(revs, exclude,
1514 free_commit_list(exclude);
1516 a_flags = flags | SYMMETRIC_LEFT;
1519 a_obj->flags |= a_flags;
1520 b_obj->flags |= flags;
1521 add_rev_cmdline(revs, a_obj, this,
1522 REV_CMD_LEFT, a_flags);
1523 add_rev_cmdline(revs, b_obj, next,
1524 REV_CMD_RIGHT, flags);
1525 add_pending_object(revs, a_obj, this);
1526 add_pending_object(revs, b_obj, next);
1531 dotdot = strstr(arg, "^@");
1532 if (dotdot && !dotdot[2]) {
1534 if (add_parents_only(revs, arg, flags))
1538 dotdot = strstr(arg, "^!");
1539 if (dotdot && !dotdot[2]) {
1541 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM)))
1547 local_flags = UNINTERESTING | BOTTOM;
1551 if (revarg_opt & REVARG_COMMITTISH)
1552 get_sha1_flags = GET_SHA1_COMMITTISH;
1554 if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
1555 return revs->ignore_missing ? 0 : -1;
1556 if (!cant_be_filename)
1557 verify_non_filename(revs->prefix, arg);
1558 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1559 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1560 add_pending_object_with_mode(revs, object, arg, oc.mode);
1564 struct cmdline_pathspec {
1570 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1573 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1574 prune->path[prune->nr++] = *(av++);
1578 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1579 struct cmdline_pathspec *prune)
1581 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1583 if (len && sb->buf[len - 1] == '\n')
1584 sb->buf[--len] = '\0';
1585 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1586 prune->path[prune->nr++] = xstrdup(sb->buf);
1590 static void read_revisions_from_stdin(struct rev_info *revs,
1591 struct cmdline_pathspec *prune)
1594 int seen_dashdash = 0;
1597 save_warning = warn_on_object_refname_ambiguity;
1598 warn_on_object_refname_ambiguity = 0;
1600 strbuf_init(&sb, 1000);
1601 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1603 if (len && sb.buf[len - 1] == '\n')
1604 sb.buf[--len] = '\0';
1607 if (sb.buf[0] == '-') {
1608 if (len == 2 && sb.buf[1] == '-') {
1612 die("options not supported in --stdin mode");
1614 if (handle_revision_arg(sb.buf, revs, 0,
1615 REVARG_CANNOT_BE_FILENAME))
1616 die("bad revision '%s'", sb.buf);
1619 read_pathspec_from_stdin(revs, &sb, prune);
1621 strbuf_release(&sb);
1622 warn_on_object_refname_ambiguity = save_warning;
1625 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1627 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1630 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1632 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1635 static void add_message_grep(struct rev_info *revs, const char *pattern)
1637 add_grep(revs, pattern, GREP_PATTERN_BODY);
1640 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1641 int *unkc, const char **unkv)
1643 const char *arg = argv[0];
1647 /* pseudo revision arguments */
1648 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1649 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1650 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1651 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1652 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1653 !strcmp(arg, "--indexed-objects") ||
1654 starts_with(arg, "--exclude=") ||
1655 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1656 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1658 unkv[(*unkc)++] = arg;
1662 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1663 revs->max_count = atoi(optarg);
1666 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1667 revs->skip_count = atoi(optarg);
1669 } else if ((*arg == '-') && isdigit(arg[1])) {
1670 /* accept -<digit>, like traditional "head" */
1671 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1672 revs->max_count < 0)
1673 die("'%s': not a non-negative integer", arg + 1);
1675 } else if (!strcmp(arg, "-n")) {
1677 return error("-n requires an argument");
1678 revs->max_count = atoi(argv[1]);
1681 } else if (starts_with(arg, "-n")) {
1682 revs->max_count = atoi(arg + 2);
1684 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1685 revs->max_age = atoi(optarg);
1687 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1688 revs->max_age = approxidate(optarg);
1690 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1691 revs->max_age = approxidate(optarg);
1693 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1694 revs->min_age = atoi(optarg);
1696 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1697 revs->min_age = approxidate(optarg);
1699 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1700 revs->min_age = approxidate(optarg);
1702 } else if (!strcmp(arg, "--first-parent")) {
1703 revs->first_parent_only = 1;
1704 } else if (!strcmp(arg, "--ancestry-path")) {
1705 revs->ancestry_path = 1;
1706 revs->simplify_history = 0;
1708 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1709 init_reflog_walk(&revs->reflog_info);
1710 } else if (!strcmp(arg, "--default")) {
1712 return error("bad --default argument");
1713 revs->def = argv[1];
1715 } else if (!strcmp(arg, "--merge")) {
1716 revs->show_merge = 1;
1717 } else if (!strcmp(arg, "--topo-order")) {
1718 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1719 revs->topo_order = 1;
1720 } else if (!strcmp(arg, "--simplify-merges")) {
1721 revs->simplify_merges = 1;
1722 revs->topo_order = 1;
1723 revs->rewrite_parents = 1;
1724 revs->simplify_history = 0;
1726 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1727 revs->simplify_merges = 1;
1728 revs->topo_order = 1;
1729 revs->rewrite_parents = 1;
1730 revs->simplify_history = 0;
1731 revs->simplify_by_decoration = 1;
1734 load_ref_decorations(DECORATE_SHORT_REFS);
1735 } else if (!strcmp(arg, "--date-order")) {
1736 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1737 revs->topo_order = 1;
1738 } else if (!strcmp(arg, "--author-date-order")) {
1739 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1740 revs->topo_order = 1;
1741 } else if (starts_with(arg, "--early-output")) {
1745 count = atoi(arg+15);
1748 revs->topo_order = 1;
1749 revs->early_output = count;
1751 } else if (!strcmp(arg, "--parents")) {
1752 revs->rewrite_parents = 1;
1753 revs->print_parents = 1;
1754 } else if (!strcmp(arg, "--dense")) {
1756 } else if (!strcmp(arg, "--sparse")) {
1758 } else if (!strcmp(arg, "--show-all")) {
1760 } else if (!strcmp(arg, "--remove-empty")) {
1761 revs->remove_empty_trees = 1;
1762 } else if (!strcmp(arg, "--merges")) {
1763 revs->min_parents = 2;
1764 } else if (!strcmp(arg, "--no-merges")) {
1765 revs->max_parents = 1;
1766 } else if (starts_with(arg, "--min-parents=")) {
1767 revs->min_parents = atoi(arg+14);
1768 } else if (starts_with(arg, "--no-min-parents")) {
1769 revs->min_parents = 0;
1770 } else if (starts_with(arg, "--max-parents=")) {
1771 revs->max_parents = atoi(arg+14);
1772 } else if (starts_with(arg, "--no-max-parents")) {
1773 revs->max_parents = -1;
1774 } else if (!strcmp(arg, "--boundary")) {
1776 } else if (!strcmp(arg, "--left-right")) {
1777 revs->left_right = 1;
1778 } else if (!strcmp(arg, "--left-only")) {
1779 if (revs->right_only)
1780 die("--left-only is incompatible with --right-only"
1782 revs->left_only = 1;
1783 } else if (!strcmp(arg, "--right-only")) {
1784 if (revs->left_only)
1785 die("--right-only is incompatible with --left-only");
1786 revs->right_only = 1;
1787 } else if (!strcmp(arg, "--cherry")) {
1788 if (revs->left_only)
1789 die("--cherry is incompatible with --left-only");
1790 revs->cherry_mark = 1;
1791 revs->right_only = 1;
1792 revs->max_parents = 1;
1794 } else if (!strcmp(arg, "--count")) {
1796 } else if (!strcmp(arg, "--cherry-mark")) {
1797 if (revs->cherry_pick)
1798 die("--cherry-mark is incompatible with --cherry-pick");
1799 revs->cherry_mark = 1;
1800 revs->limited = 1; /* needs limit_list() */
1801 } else if (!strcmp(arg, "--cherry-pick")) {
1802 if (revs->cherry_mark)
1803 die("--cherry-pick is incompatible with --cherry-mark");
1804 revs->cherry_pick = 1;
1806 } else if (!strcmp(arg, "--objects")) {
1807 revs->tag_objects = 1;
1808 revs->tree_objects = 1;
1809 revs->blob_objects = 1;
1810 } else if (!strcmp(arg, "--objects-edge")) {
1811 revs->tag_objects = 1;
1812 revs->tree_objects = 1;
1813 revs->blob_objects = 1;
1814 revs->edge_hint = 1;
1815 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1816 revs->tag_objects = 1;
1817 revs->tree_objects = 1;
1818 revs->blob_objects = 1;
1819 revs->edge_hint = 1;
1820 revs->edge_hint_aggressive = 1;
1821 } else if (!strcmp(arg, "--verify-objects")) {
1822 revs->tag_objects = 1;
1823 revs->tree_objects = 1;
1824 revs->blob_objects = 1;
1825 revs->verify_objects = 1;
1826 } else if (!strcmp(arg, "--unpacked")) {
1828 } else if (starts_with(arg, "--unpacked=")) {
1829 die("--unpacked=<packfile> no longer supported.");
1830 } else if (!strcmp(arg, "-r")) {
1832 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1833 } else if (!strcmp(arg, "-t")) {
1835 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1836 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1837 } else if (!strcmp(arg, "-m")) {
1838 revs->ignore_merges = 0;
1839 } else if (!strcmp(arg, "-c")) {
1841 revs->dense_combined_merges = 0;
1842 revs->combine_merges = 1;
1843 } else if (!strcmp(arg, "--cc")) {
1845 revs->dense_combined_merges = 1;
1846 revs->combine_merges = 1;
1847 } else if (!strcmp(arg, "-v")) {
1848 revs->verbose_header = 1;
1849 } else if (!strcmp(arg, "--pretty")) {
1850 revs->verbose_header = 1;
1851 revs->pretty_given = 1;
1852 get_commit_format(NULL, revs);
1853 } else if (starts_with(arg, "--pretty=") || starts_with(arg, "--format=")) {
1855 * Detached form ("--pretty X" as opposed to "--pretty=X")
1856 * not allowed, since the argument is optional.
1858 revs->verbose_header = 1;
1859 revs->pretty_given = 1;
1860 get_commit_format(arg+9, revs);
1861 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1862 revs->show_notes = 1;
1863 revs->show_notes_given = 1;
1864 revs->notes_opt.use_default_notes = 1;
1865 } else if (!strcmp(arg, "--show-signature")) {
1866 revs->show_signature = 1;
1867 } else if (!strcmp(arg, "--show-linear-break") ||
1868 starts_with(arg, "--show-linear-break=")) {
1869 if (starts_with(arg, "--show-linear-break="))
1870 revs->break_bar = xstrdup(arg + 20);
1872 revs->break_bar = " ..........";
1873 revs->track_linear = 1;
1874 revs->track_first_time = 1;
1875 } else if (starts_with(arg, "--show-notes=") ||
1876 starts_with(arg, "--notes=")) {
1877 struct strbuf buf = STRBUF_INIT;
1878 revs->show_notes = 1;
1879 revs->show_notes_given = 1;
1880 if (starts_with(arg, "--show-notes")) {
1881 if (revs->notes_opt.use_default_notes < 0)
1882 revs->notes_opt.use_default_notes = 1;
1883 strbuf_addstr(&buf, arg+13);
1886 strbuf_addstr(&buf, arg+8);
1887 expand_notes_ref(&buf);
1888 string_list_append(&revs->notes_opt.extra_notes_refs,
1889 strbuf_detach(&buf, NULL));
1890 } else if (!strcmp(arg, "--no-notes")) {
1891 revs->show_notes = 0;
1892 revs->show_notes_given = 1;
1893 revs->notes_opt.use_default_notes = -1;
1894 /* we have been strdup'ing ourselves, so trick
1895 * string_list into free()ing strings */
1896 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1897 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1898 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1899 } else if (!strcmp(arg, "--standard-notes")) {
1900 revs->show_notes_given = 1;
1901 revs->notes_opt.use_default_notes = 1;
1902 } else if (!strcmp(arg, "--no-standard-notes")) {
1903 revs->notes_opt.use_default_notes = 0;
1904 } else if (!strcmp(arg, "--oneline")) {
1905 revs->verbose_header = 1;
1906 get_commit_format("oneline", revs);
1907 revs->pretty_given = 1;
1908 revs->abbrev_commit = 1;
1909 } else if (!strcmp(arg, "--graph")) {
1910 revs->topo_order = 1;
1911 revs->rewrite_parents = 1;
1912 revs->graph = graph_init(revs);
1913 } else if (!strcmp(arg, "--root")) {
1914 revs->show_root_diff = 1;
1915 } else if (!strcmp(arg, "--no-commit-id")) {
1916 revs->no_commit_id = 1;
1917 } else if (!strcmp(arg, "--always")) {
1918 revs->always_show_header = 1;
1919 } else if (!strcmp(arg, "--no-abbrev")) {
1921 } else if (!strcmp(arg, "--abbrev")) {
1922 revs->abbrev = DEFAULT_ABBREV;
1923 } else if (starts_with(arg, "--abbrev=")) {
1924 revs->abbrev = strtoul(arg + 9, NULL, 10);
1925 if (revs->abbrev < MINIMUM_ABBREV)
1926 revs->abbrev = MINIMUM_ABBREV;
1927 else if (revs->abbrev > 40)
1929 } else if (!strcmp(arg, "--abbrev-commit")) {
1930 revs->abbrev_commit = 1;
1931 revs->abbrev_commit_given = 1;
1932 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1933 revs->abbrev_commit = 0;
1934 } else if (!strcmp(arg, "--full-diff")) {
1936 revs->full_diff = 1;
1937 } else if (!strcmp(arg, "--full-history")) {
1938 revs->simplify_history = 0;
1939 } else if (!strcmp(arg, "--relative-date")) {
1940 revs->date_mode = DATE_RELATIVE;
1941 revs->date_mode_explicit = 1;
1942 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1943 revs->date_mode = parse_date_format(optarg);
1944 revs->date_mode_explicit = 1;
1946 } else if (!strcmp(arg, "--log-size")) {
1947 revs->show_log_size = 1;
1950 * Grepping the commit log
1952 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1953 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1955 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1956 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1958 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
1959 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
1961 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1962 add_message_grep(revs, optarg);
1964 } else if (!strcmp(arg, "--grep-debug")) {
1965 revs->grep_filter.debug = 1;
1966 } else if (!strcmp(arg, "--basic-regexp")) {
1967 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter);
1968 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1969 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter);
1970 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1971 revs->grep_filter.regflags |= REG_ICASE;
1972 DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
1973 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1974 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter);
1975 } else if (!strcmp(arg, "--perl-regexp")) {
1976 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
1977 } else if (!strcmp(arg, "--all-match")) {
1978 revs->grep_filter.all_match = 1;
1979 } else if (!strcmp(arg, "--invert-grep")) {
1980 revs->invert_grep = 1;
1981 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1982 if (strcmp(optarg, "none"))
1983 git_log_output_encoding = xstrdup(optarg);
1985 git_log_output_encoding = "";
1987 } else if (!strcmp(arg, "--reverse")) {
1989 } else if (!strcmp(arg, "--children")) {
1990 revs->children.name = "children";
1992 } else if (!strcmp(arg, "--ignore-missing")) {
1993 revs->ignore_missing = 1;
1995 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1997 unkv[(*unkc)++] = arg;
2000 if (revs->graph && revs->track_linear)
2001 die("--show-linear-break and --graph are incompatible");
2006 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2007 const struct option *options,
2008 const char * const usagestr[])
2010 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2011 &ctx->cpidx, ctx->out);
2013 error("unknown option `%s'", ctx->argv[0]);
2014 usage_with_options(usagestr, options);
2020 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2022 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
2025 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2027 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
2030 static int handle_revision_pseudo_opt(const char *submodule,
2031 struct rev_info *revs,
2032 int argc, const char **argv, int *flags)
2034 const char *arg = argv[0];
2041 * Commands like "git shortlog" will not accept the options below
2042 * unless parse_revision_opt queues them (as opposed to erroring
2045 * When implementing your new pseudo-option, remember to
2046 * register it in the list at the top of handle_revision_opt.
2048 if (!strcmp(arg, "--all")) {
2049 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
2050 handle_refs(submodule, revs, *flags, head_ref_submodule);
2051 clear_ref_exclusion(&revs->ref_excludes);
2052 } else if (!strcmp(arg, "--branches")) {
2053 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
2054 clear_ref_exclusion(&revs->ref_excludes);
2055 } else if (!strcmp(arg, "--bisect")) {
2056 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
2057 handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
2059 } else if (!strcmp(arg, "--tags")) {
2060 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
2061 clear_ref_exclusion(&revs->ref_excludes);
2062 } else if (!strcmp(arg, "--remotes")) {
2063 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
2064 clear_ref_exclusion(&revs->ref_excludes);
2065 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2066 struct all_refs_cb cb;
2067 init_all_refs_cb(&cb, revs, *flags);
2068 for_each_glob_ref(handle_one_ref, optarg, &cb);
2069 clear_ref_exclusion(&revs->ref_excludes);
2071 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2072 add_ref_exclusion(&revs->ref_excludes, optarg);
2074 } else if (starts_with(arg, "--branches=")) {
2075 struct all_refs_cb cb;
2076 init_all_refs_cb(&cb, revs, *flags);
2077 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
2078 clear_ref_exclusion(&revs->ref_excludes);
2079 } else if (starts_with(arg, "--tags=")) {
2080 struct all_refs_cb cb;
2081 init_all_refs_cb(&cb, revs, *flags);
2082 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
2083 clear_ref_exclusion(&revs->ref_excludes);
2084 } else if (starts_with(arg, "--remotes=")) {
2085 struct all_refs_cb cb;
2086 init_all_refs_cb(&cb, revs, *flags);
2087 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
2088 clear_ref_exclusion(&revs->ref_excludes);
2089 } else if (!strcmp(arg, "--reflog")) {
2090 add_reflogs_to_pending(revs, *flags);
2091 } else if (!strcmp(arg, "--indexed-objects")) {
2092 add_index_objects_to_pending(revs, *flags);
2093 } else if (!strcmp(arg, "--not")) {
2094 *flags ^= UNINTERESTING | BOTTOM;
2095 } else if (!strcmp(arg, "--no-walk")) {
2096 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2097 } else if (starts_with(arg, "--no-walk=")) {
2099 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2100 * not allowed, since the argument is optional.
2102 if (!strcmp(arg + 10, "sorted"))
2103 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2104 else if (!strcmp(arg + 10, "unsorted"))
2105 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2107 return error("invalid argument to --no-walk");
2108 } else if (!strcmp(arg, "--do-walk")) {
2118 * Parse revision information, filling in the "rev_info" structure,
2119 * and removing the used arguments from the argument list.
2121 * Returns the number of arguments left that weren't recognized
2122 * (which are also moved to the head of the argument list)
2124 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2126 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2127 struct cmdline_pathspec prune_data;
2128 const char *submodule = NULL;
2130 memset(&prune_data, 0, sizeof(prune_data));
2132 submodule = opt->submodule;
2134 /* First, search for "--" */
2135 if (opt && opt->assume_dashdash) {
2139 for (i = 1; i < argc; i++) {
2140 const char *arg = argv[i];
2141 if (strcmp(arg, "--"))
2146 append_prune_data(&prune_data, argv + i + 1);
2152 /* Second, deal with arguments and options */
2154 revarg_opt = opt ? opt->revarg_opt : 0;
2156 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2157 read_from_stdin = 0;
2158 for (left = i = 1; i < argc; i++) {
2159 const char *arg = argv[i];
2163 opts = handle_revision_pseudo_opt(submodule,
2164 revs, argc - i, argv + i,
2171 if (!strcmp(arg, "--stdin")) {
2172 if (revs->disable_stdin) {
2176 if (read_from_stdin++)
2177 die("--stdin given twice?");
2178 read_revisions_from_stdin(revs, &prune_data);
2182 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2193 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2195 if (seen_dashdash || *arg == '^')
2196 die("bad revision '%s'", arg);
2198 /* If we didn't have a "--":
2199 * (1) all filenames must exist;
2200 * (2) all rev-args must not be interpretable
2201 * as a valid filename.
2202 * but the latter we have checked in the main loop.
2204 for (j = i; j < argc; j++)
2205 verify_filename(revs->prefix, argv[j], j == i);
2207 append_prune_data(&prune_data, argv + i);
2214 if (prune_data.nr) {
2216 * If we need to introduce the magic "a lone ':' means no
2217 * pathspec whatsoever", here is the place to do so.
2219 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2220 * prune_data.nr = 0;
2221 * prune_data.alloc = 0;
2222 * free(prune_data.path);
2223 * prune_data.path = NULL;
2225 * terminate prune_data.alloc with NULL and
2226 * call init_pathspec() to set revs->prune_data here.
2229 ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc);
2230 prune_data.path[prune_data.nr++] = NULL;
2231 parse_pathspec(&revs->prune_data, 0, 0,
2232 revs->prefix, prune_data.path);
2235 if (revs->def == NULL)
2236 revs->def = opt ? opt->def : NULL;
2237 if (opt && opt->tweak)
2238 opt->tweak(revs, opt);
2239 if (revs->show_merge)
2240 prepare_show_merge(revs);
2241 if (revs->def && !revs->pending.nr && !got_rev_arg) {
2242 unsigned char sha1[20];
2243 struct object *object;
2244 struct object_context oc;
2245 if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2246 die("bad default revision '%s'", revs->def);
2247 object = get_reference(revs, revs->def, sha1, 0);
2248 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2251 /* Did the user ask for any diff output? Run the diff! */
2252 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2255 /* Pickaxe, diff-filter and rename following need diffs */
2256 if (revs->diffopt.pickaxe ||
2257 revs->diffopt.filter ||
2258 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2261 if (revs->topo_order)
2264 if (revs->prune_data.nr) {
2265 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2266 /* Can't prune commits with rename following: the paths change.. */
2267 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2269 if (!revs->full_diff)
2270 copy_pathspec(&revs->diffopt.pathspec,
2273 if (revs->combine_merges)
2274 revs->ignore_merges = 0;
2275 revs->diffopt.abbrev = revs->abbrev;
2277 if (revs->line_level_traverse) {
2279 revs->topo_order = 1;
2282 diff_setup_done(&revs->diffopt);
2284 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2285 &revs->grep_filter);
2286 compile_grep_patterns(&revs->grep_filter);
2288 if (revs->reverse && revs->reflog_info)
2289 die("cannot combine --reverse with --walk-reflogs");
2290 if (revs->rewrite_parents && revs->children.name)
2291 die("cannot combine --parents and --children");
2294 * Limitations on the graph functionality
2296 if (revs->reverse && revs->graph)
2297 die("cannot combine --reverse with --graph");
2299 if (revs->reflog_info && revs->graph)
2300 die("cannot combine --walk-reflogs with --graph");
2301 if (revs->no_walk && revs->graph)
2302 die("cannot combine --no-walk with --graph");
2303 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2304 die("cannot use --grep-reflog without --walk-reflogs");
2306 if (revs->first_parent_only && revs->bisect)
2307 die(_("--first-parent is incompatible with --bisect"));
2312 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2314 struct commit_list *l = xcalloc(1, sizeof(*l));
2317 l->next = add_decoration(&revs->children, &parent->object, l);
2320 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2322 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2323 struct commit_list **pp, *p;
2324 int surviving_parents;
2326 /* Examine existing parents while marking ones we have seen... */
2327 pp = &commit->parents;
2328 surviving_parents = 0;
2329 while ((p = *pp) != NULL) {
2330 struct commit *parent = p->item;
2331 if (parent->object.flags & TMP_MARK) {
2334 compact_treesame(revs, commit, surviving_parents);
2337 parent->object.flags |= TMP_MARK;
2338 surviving_parents++;
2341 /* clear the temporary mark */
2342 for (p = commit->parents; p; p = p->next) {
2343 p->item->object.flags &= ~TMP_MARK;
2345 /* no update_treesame() - removing duplicates can't affect TREESAME */
2346 return surviving_parents;
2349 struct merge_simplify_state {
2350 struct commit *simplified;
2353 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2355 struct merge_simplify_state *st;
2357 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2359 st = xcalloc(1, sizeof(*st));
2360 add_decoration(&revs->merge_simplification, &commit->object, st);
2365 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2367 struct commit_list *h = reduce_heads(commit->parents);
2368 int i = 0, marked = 0;
2369 struct commit_list *po, *pn;
2371 /* Want these for sanity-checking only */
2372 int orig_cnt = commit_list_count(commit->parents);
2373 int cnt = commit_list_count(h);
2376 * Not ready to remove items yet, just mark them for now, based
2377 * on the output of reduce_heads(). reduce_heads outputs the reduced
2378 * set in its original order, so this isn't too hard.
2380 po = commit->parents;
2383 if (pn && po->item == pn->item) {
2387 po->item->object.flags |= TMP_MARK;
2393 if (i != cnt || cnt+marked != orig_cnt)
2394 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2396 free_commit_list(h);
2401 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2403 struct commit_list *p;
2406 for (p = commit->parents; p; p = p->next) {
2407 struct commit *parent = p->item;
2408 if (!parent->parents && (parent->object.flags & TREESAME)) {
2409 parent->object.flags |= TMP_MARK;
2418 * Awkward naming - this means one parent we are TREESAME to.
2419 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2420 * empty tree). Better name suggestions?
2422 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2424 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2425 struct commit *unmarked = NULL, *marked = NULL;
2426 struct commit_list *p;
2429 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2430 if (ts->treesame[n]) {
2431 if (p->item->object.flags & TMP_MARK) {
2444 * If we are TREESAME to a marked-for-deletion parent, but not to any
2445 * unmarked parents, unmark the first TREESAME parent. This is the
2446 * parent that the default simplify_history==1 scan would have followed,
2447 * and it doesn't make sense to omit that path when asking for a
2448 * simplified full history. Retaining it improves the chances of
2449 * understanding odd missed merges that took an old version of a file.
2453 * I--------*X A modified the file, but mainline merge X used
2454 * \ / "-s ours", so took the version from I. X is
2455 * `-*A--' TREESAME to I and !TREESAME to A.
2457 * Default log from X would produce "I". Without this check,
2458 * --full-history --simplify-merges would produce "I-A-X", showing
2459 * the merge commit X and that it changed A, but not making clear that
2460 * it had just taken the I version. With this check, the topology above
2463 * Note that it is possible that the simplification chooses a different
2464 * TREESAME parent from the default, in which case this test doesn't
2465 * activate, and we _do_ drop the default parent. Example:
2467 * I------X A modified the file, but it was reverted in B,
2468 * \ / meaning mainline merge X is TREESAME to both
2471 * Default log would produce "I" by following the first parent;
2472 * --full-history --simplify-merges will produce "I-A-B". But this is a
2473 * reasonable result - it presents a logical full history leading from
2474 * I to X, and X is not an important merge.
2476 if (!unmarked && marked) {
2477 marked->object.flags &= ~TMP_MARK;
2484 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2486 struct commit_list **pp, *p;
2487 int nth_parent, removed = 0;
2489 pp = &commit->parents;
2491 while ((p = *pp) != NULL) {
2492 struct commit *parent = p->item;
2493 if (parent->object.flags & TMP_MARK) {
2494 parent->object.flags &= ~TMP_MARK;
2498 compact_treesame(revs, commit, nth_parent);
2505 /* Removing parents can only increase TREESAMEness */
2506 if (removed && !(commit->object.flags & TREESAME))
2507 update_treesame(revs, commit);
2512 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2514 struct commit_list *p;
2515 struct commit *parent;
2516 struct merge_simplify_state *st, *pst;
2519 st = locate_simplify_state(revs, commit);
2522 * Have we handled this one?
2528 * An UNINTERESTING commit simplifies to itself, so does a
2529 * root commit. We do not rewrite parents of such commit
2532 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2533 st->simplified = commit;
2538 * Do we know what commit all of our parents that matter
2539 * should be rewritten to? Otherwise we are not ready to
2540 * rewrite this one yet.
2542 for (cnt = 0, p = commit->parents; p; p = p->next) {
2543 pst = locate_simplify_state(revs, p->item);
2544 if (!pst->simplified) {
2545 tail = &commit_list_insert(p->item, tail)->next;
2548 if (revs->first_parent_only)
2552 tail = &commit_list_insert(commit, tail)->next;
2557 * Rewrite our list of parents. Note that this cannot
2558 * affect our TREESAME flags in any way - a commit is
2559 * always TREESAME to its simplification.
2561 for (p = commit->parents; p; p = p->next) {
2562 pst = locate_simplify_state(revs, p->item);
2563 p->item = pst->simplified;
2564 if (revs->first_parent_only)
2568 if (revs->first_parent_only)
2571 cnt = remove_duplicate_parents(revs, commit);
2574 * It is possible that we are a merge and one side branch
2575 * does not have any commit that touches the given paths;
2576 * in such a case, the immediate parent from that branch
2577 * will be rewritten to be the merge base.
2579 * o----X X: the commit we are looking at;
2580 * / / o: a commit that touches the paths;
2583 * Further, a merge of an independent branch that doesn't
2584 * touch the path will reduce to a treesame root parent:
2586 * ----o----X X: the commit we are looking at;
2587 * / o: a commit that touches the paths;
2588 * r r: a root commit not touching the paths
2590 * Detect and simplify both cases.
2593 int marked = mark_redundant_parents(revs, commit);
2594 marked += mark_treesame_root_parents(revs, commit);
2596 marked -= leave_one_treesame_to_parent(revs, commit);
2598 cnt = remove_marked_parents(revs, commit);
2602 * A commit simplifies to itself if it is a root, if it is
2603 * UNINTERESTING, if it touches the given paths, or if it is a
2604 * merge and its parents don't simplify to one relevant commit
2605 * (the first two cases are already handled at the beginning of
2608 * Otherwise, it simplifies to what its sole relevant parent
2612 (commit->object.flags & UNINTERESTING) ||
2613 !(commit->object.flags & TREESAME) ||
2614 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2615 st->simplified = commit;
2617 pst = locate_simplify_state(revs, parent);
2618 st->simplified = pst->simplified;
2623 static void simplify_merges(struct rev_info *revs)
2625 struct commit_list *list, *next;
2626 struct commit_list *yet_to_do, **tail;
2627 struct commit *commit;
2632 /* feed the list reversed */
2634 for (list = revs->commits; list; list = next) {
2635 commit = list->item;
2638 * Do not free(list) here yet; the original list
2639 * is used later in this function.
2641 commit_list_insert(commit, &yet_to_do);
2648 commit = list->item;
2652 tail = simplify_one(revs, commit, tail);
2656 /* clean up the result, removing the simplified ones */
2657 list = revs->commits;
2658 revs->commits = NULL;
2659 tail = &revs->commits;
2661 struct merge_simplify_state *st;
2663 commit = list->item;
2667 st = locate_simplify_state(revs, commit);
2668 if (st->simplified == commit)
2669 tail = &commit_list_insert(commit, tail)->next;
2673 static void set_children(struct rev_info *revs)
2675 struct commit_list *l;
2676 for (l = revs->commits; l; l = l->next) {
2677 struct commit *commit = l->item;
2678 struct commit_list *p;
2680 for (p = commit->parents; p; p = p->next)
2681 add_child(revs, p->item, commit);
2685 void reset_revision_walk(void)
2687 clear_object_flags(SEEN | ADDED | SHOWN);
2690 int prepare_revision_walk(struct rev_info *revs)
2693 struct object_array old_pending;
2694 struct commit_list **next = &revs->commits;
2696 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2697 revs->pending.nr = 0;
2698 revs->pending.alloc = 0;
2699 revs->pending.objects = NULL;
2700 for (i = 0; i < old_pending.nr; i++) {
2701 struct object_array_entry *e = old_pending.objects + i;
2702 struct commit *commit = handle_commit(revs, e);
2704 if (!(commit->object.flags & SEEN)) {
2705 commit->object.flags |= SEEN;
2706 next = commit_list_append(commit, next);
2710 if (!revs->leak_pending)
2711 object_array_clear(&old_pending);
2713 /* Signal whether we need per-parent treesame decoration */
2714 if (revs->simplify_merges ||
2715 (revs->limited && limiting_can_increase_treesame(revs)))
2716 revs->treesame.name = "treesame";
2718 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2719 commit_list_sort_by_date(&revs->commits);
2723 if (limit_list(revs) < 0)
2725 if (revs->topo_order)
2726 sort_in_topological_order(&revs->commits, revs->sort_order);
2727 if (revs->line_level_traverse)
2728 line_log_filter(revs);
2729 if (revs->simplify_merges)
2730 simplify_merges(revs);
2731 if (revs->children.name)
2736 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2738 struct commit_list *cache = NULL;
2741 struct commit *p = *pp;
2743 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2744 return rewrite_one_error;
2745 if (p->object.flags & UNINTERESTING)
2746 return rewrite_one_ok;
2747 if (!(p->object.flags & TREESAME))
2748 return rewrite_one_ok;
2750 return rewrite_one_noparents;
2751 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2752 return rewrite_one_ok;
2757 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2758 rewrite_parent_fn_t rewrite_parent)
2760 struct commit_list **pp = &commit->parents;
2762 struct commit_list *parent = *pp;
2763 switch (rewrite_parent(revs, &parent->item)) {
2764 case rewrite_one_ok:
2766 case rewrite_one_noparents:
2769 case rewrite_one_error:
2774 remove_duplicate_parents(revs, commit);
2778 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2780 char *person, *endp;
2781 size_t len, namelen, maillen;
2784 struct ident_split ident;
2786 person = strstr(buf->buf, what);
2790 person += strlen(what);
2791 endp = strchr(person, '\n');
2795 len = endp - person;
2797 if (split_ident_line(&ident, person, len))
2800 mail = ident.mail_begin;
2801 maillen = ident.mail_end - ident.mail_begin;
2802 name = ident.name_begin;
2803 namelen = ident.name_end - ident.name_begin;
2805 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2806 struct strbuf namemail = STRBUF_INIT;
2808 strbuf_addf(&namemail, "%.*s <%.*s>",
2809 (int)namelen, name, (int)maillen, mail);
2811 strbuf_splice(buf, ident.name_begin - buf->buf,
2812 ident.mail_end - ident.name_begin + 1,
2813 namemail.buf, namemail.len);
2815 strbuf_release(&namemail);
2823 static int commit_match(struct commit *commit, struct rev_info *opt)
2826 const char *encoding;
2827 const char *message;
2828 struct strbuf buf = STRBUF_INIT;
2830 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2833 /* Prepend "fake" headers as needed */
2834 if (opt->grep_filter.use_reflog_filter) {
2835 strbuf_addstr(&buf, "reflog ");
2836 get_reflog_message(&buf, opt->reflog_info);
2837 strbuf_addch(&buf, '\n');
2841 * We grep in the user's output encoding, under the assumption that it
2842 * is the encoding they are most likely to write their grep pattern
2843 * for. In addition, it means we will match the "notes" encoding below,
2844 * so we will not end up with a buffer that has two different encodings
2847 encoding = get_log_output_encoding();
2848 message = logmsg_reencode(commit, NULL, encoding);
2850 /* Copy the commit to temporary if we are using "fake" headers */
2852 strbuf_addstr(&buf, message);
2854 if (opt->grep_filter.header_list && opt->mailmap) {
2856 strbuf_addstr(&buf, message);
2858 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2859 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2862 /* Append "fake" message parts as needed */
2863 if (opt->show_notes) {
2865 strbuf_addstr(&buf, message);
2866 format_display_notes(commit->object.sha1, &buf, encoding, 1);
2870 * Find either in the original commit message, or in the temporary.
2871 * Note that we cast away the constness of "message" here. It is
2872 * const because it may come from the cached commit buffer. That's OK,
2873 * because we know that it is modifiable heap memory, and that while
2874 * grep_buffer may modify it for speed, it will restore any
2875 * changes before returning.
2878 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2880 retval = grep_buffer(&opt->grep_filter,
2881 (char *)message, strlen(message));
2882 strbuf_release(&buf);
2883 unuse_commit_buffer(commit, message);
2884 return opt->invert_grep ? !retval : retval;
2887 static inline int want_ancestry(const struct rev_info *revs)
2889 return (revs->rewrite_parents || revs->children.name);
2892 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2894 if (commit->object.flags & SHOWN)
2895 return commit_ignore;
2896 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2897 return commit_ignore;
2900 if (commit->object.flags & UNINTERESTING)
2901 return commit_ignore;
2902 if (revs->min_age != -1 && (commit->date > revs->min_age))
2903 return commit_ignore;
2904 if (revs->min_parents || (revs->max_parents >= 0)) {
2905 int n = commit_list_count(commit->parents);
2906 if ((n < revs->min_parents) ||
2907 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2908 return commit_ignore;
2910 if (!commit_match(commit, revs))
2911 return commit_ignore;
2912 if (revs->prune && revs->dense) {
2913 /* Commit without changes? */
2914 if (commit->object.flags & TREESAME) {
2916 struct commit_list *p;
2917 /* drop merges unless we want parenthood */
2918 if (!want_ancestry(revs))
2919 return commit_ignore;
2921 * If we want ancestry, then need to keep any merges
2922 * between relevant commits to tie together topology.
2923 * For consistency with TREESAME and simplification
2924 * use "relevant" here rather than just INTERESTING,
2925 * to treat bottom commit(s) as part of the topology.
2927 for (n = 0, p = commit->parents; p; p = p->next)
2928 if (relevant_commit(p->item))
2931 return commit_ignore;
2937 define_commit_slab(saved_parents, struct commit_list *);
2939 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
2942 * You may only call save_parents() once per commit (this is checked
2943 * for non-root commits).
2945 static void save_parents(struct rev_info *revs, struct commit *commit)
2947 struct commit_list **pp;
2949 if (!revs->saved_parents_slab) {
2950 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
2951 init_saved_parents(revs->saved_parents_slab);
2954 pp = saved_parents_at(revs->saved_parents_slab, commit);
2957 * When walking with reflogs, we may visit the same commit
2958 * several times: once for each appearance in the reflog.
2960 * In this case, save_parents() will be called multiple times.
2961 * We want to keep only the first set of parents. We need to
2962 * store a sentinel value for an empty (i.e., NULL) parent
2963 * list to distinguish it from a not-yet-saved list, however.
2967 if (commit->parents)
2968 *pp = copy_commit_list(commit->parents);
2970 *pp = EMPTY_PARENT_LIST;
2973 static void free_saved_parents(struct rev_info *revs)
2975 if (revs->saved_parents_slab)
2976 clear_saved_parents(revs->saved_parents_slab);
2979 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
2981 struct commit_list *parents;
2983 if (!revs->saved_parents_slab)
2984 return commit->parents;
2986 parents = *saved_parents_at(revs->saved_parents_slab, commit);
2987 if (parents == EMPTY_PARENT_LIST)
2992 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
2994 enum commit_action action = get_commit_action(revs, commit);
2996 if (action == commit_show &&
2998 revs->prune && revs->dense && want_ancestry(revs)) {
3000 * --full-diff on simplified parents is no good: it
3001 * will show spurious changes from the commits that
3002 * were elided. So we save the parents on the side
3003 * when --full-diff is in effect.
3005 if (revs->full_diff)
3006 save_parents(revs, commit);
3007 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3008 return commit_error;
3013 static void track_linear(struct rev_info *revs, struct commit *commit)
3015 if (revs->track_first_time) {
3017 revs->track_first_time = 0;
3019 struct commit_list *p;
3020 for (p = revs->previous_parents; p; p = p->next)
3021 if (p->item == NULL || /* first commit */
3022 !hashcmp(p->item->object.sha1, commit->object.sha1))
3024 revs->linear = p != NULL;
3026 if (revs->reverse) {
3028 commit->object.flags |= TRACK_LINEAR;
3030 free_commit_list(revs->previous_parents);
3031 revs->previous_parents = copy_commit_list(commit->parents);
3034 static struct commit *get_revision_1(struct rev_info *revs)
3040 struct commit_list *entry = revs->commits;
3041 struct commit *commit = entry->item;
3043 revs->commits = entry->next;
3046 if (revs->reflog_info) {
3047 save_parents(revs, commit);
3048 fake_reflog_parent(revs->reflog_info, commit);
3049 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3053 * If we haven't done the list limiting, we need to look at
3054 * the parents here. We also need to do the date-based limiting
3055 * that we'd otherwise have done in limit_list().
3057 if (!revs->limited) {
3058 if (revs->max_age != -1 &&
3059 (commit->date < revs->max_age))
3061 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3062 if (!revs->ignore_missing_links)
3063 die("Failed to traverse parents of commit %s",
3064 sha1_to_hex(commit->object.sha1));
3068 switch (simplify_commit(revs, commit)) {
3072 die("Failed to simplify parents of commit %s",
3073 sha1_to_hex(commit->object.sha1));
3075 if (revs->track_linear)
3076 track_linear(revs, commit);
3079 } while (revs->commits);
3084 * Return true for entries that have not yet been shown. (This is an
3085 * object_array_each_func_t.)
3087 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3089 return !(entry->item->flags & SHOWN);
3093 * If array is on the verge of a realloc, garbage-collect any entries
3094 * that have already been shown to try to free up some space.
3096 static void gc_boundary(struct object_array *array)
3098 if (array->nr == array->alloc)
3099 object_array_filter(array, entry_unshown, NULL);
3102 static void create_boundary_commit_list(struct rev_info *revs)
3106 struct object_array *array = &revs->boundary_commits;
3107 struct object_array_entry *objects = array->objects;
3110 * If revs->commits is non-NULL at this point, an error occurred in
3111 * get_revision_1(). Ignore the error and continue printing the
3112 * boundary commits anyway. (This is what the code has always
3115 if (revs->commits) {
3116 free_commit_list(revs->commits);
3117 revs->commits = NULL;
3121 * Put all of the actual boundary commits from revs->boundary_commits
3122 * into revs->commits
3124 for (i = 0; i < array->nr; i++) {
3125 c = (struct commit *)(objects[i].item);
3128 if (!(c->object.flags & CHILD_SHOWN))
3130 if (c->object.flags & (SHOWN | BOUNDARY))
3132 c->object.flags |= BOUNDARY;
3133 commit_list_insert(c, &revs->commits);
3137 * If revs->topo_order is set, sort the boundary commits
3138 * in topological order
3140 sort_in_topological_order(&revs->commits, revs->sort_order);
3143 static struct commit *get_revision_internal(struct rev_info *revs)
3145 struct commit *c = NULL;
3146 struct commit_list *l;
3148 if (revs->boundary == 2) {
3150 * All of the normal commits have already been returned,
3151 * and we are now returning boundary commits.
3152 * create_boundary_commit_list() has populated
3153 * revs->commits with the remaining commits to return.
3155 c = pop_commit(&revs->commits);
3157 c->object.flags |= SHOWN;
3162 * If our max_count counter has reached zero, then we are done. We
3163 * don't simply return NULL because we still might need to show
3164 * boundary commits. But we want to avoid calling get_revision_1, which
3165 * might do a considerable amount of work finding the next commit only
3166 * for us to throw it away.
3168 * If it is non-zero, then either we don't have a max_count at all
3169 * (-1), or it is still counting, in which case we decrement.
3171 if (revs->max_count) {
3172 c = get_revision_1(revs);
3174 while (revs->skip_count > 0) {
3176 c = get_revision_1(revs);
3182 if (revs->max_count > 0)
3187 c->object.flags |= SHOWN;
3189 if (!revs->boundary)
3194 * get_revision_1() runs out the commits, and
3195 * we are done computing the boundaries.
3196 * switch to boundary commits output mode.
3201 * Update revs->commits to contain the list of
3204 create_boundary_commit_list(revs);
3206 return get_revision_internal(revs);
3210 * boundary commits are the commits that are parents of the
3211 * ones we got from get_revision_1() but they themselves are
3212 * not returned from get_revision_1(). Before returning
3213 * 'c', we need to mark its parents that they could be boundaries.
3216 for (l = c->parents; l; l = l->next) {
3218 p = &(l->item->object);
3219 if (p->flags & (CHILD_SHOWN | SHOWN))
3221 p->flags |= CHILD_SHOWN;
3222 gc_boundary(&revs->boundary_commits);
3223 add_object_array(p, NULL, &revs->boundary_commits);
3229 struct commit *get_revision(struct rev_info *revs)
3232 struct commit_list *reversed;
3234 if (revs->reverse) {
3236 while ((c = get_revision_internal(revs)))
3237 commit_list_insert(c, &reversed);
3238 revs->commits = reversed;
3240 revs->reverse_output_stage = 1;
3243 if (revs->reverse_output_stage) {
3244 c = pop_commit(&revs->commits);
3245 if (revs->track_linear)
3246 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3250 c = get_revision_internal(revs);
3251 if (c && revs->graph)
3252 graph_update(revs->graph, c);
3254 free_saved_parents(revs);
3255 if (revs->previous_parents) {
3256 free_commit_list(revs->previous_parents);
3257 revs->previous_parents = NULL;
3263 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3265 if (commit->object.flags & BOUNDARY)
3267 else if (commit->object.flags & UNINTERESTING)
3269 else if (commit->object.flags & PATCHSAME)
3271 else if (!revs || revs->left_right) {
3272 if (commit->object.flags & SYMMETRIC_LEFT)
3276 } else if (revs->graph)
3278 else if (revs->cherry_mark)
3283 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3285 char *mark = get_revision_mark(revs, commit);
3288 fputs(mark, stdout);