11 #include "reflog-walk.h"
12 #include "patch-ids.h"
15 #include "string-list.h"
19 volatile show_early_output_fn_t show_early_output;
21 char *path_name(const struct name_path *path, const char *name)
23 const struct name_path *p;
25 int nlen = strlen(name);
28 for (p = path; p; p = p->up) {
30 len += p->elem_len + 1;
33 m = n + len - (nlen + 1);
35 for (p = path; p; p = p->up) {
38 memcpy(m, p->elem, p->elem_len);
45 static int show_path_component_truncated(FILE *out, const char *name, int len)
48 for (cnt = 0; cnt < len; cnt++) {
50 if (!ch || ch == '\n')
57 static int show_path_truncated(FILE *out, const struct name_path *path)
63 emitted = show_path_truncated(out, path->up);
68 ours = show_path_component_truncated(out, path->elem, path->elem_len);
71 return ours || emitted;
74 void show_object_with_name(FILE *out, struct object *obj,
75 const struct name_path *path, const char *component)
77 struct name_path leaf;
78 leaf.up = (struct name_path *)path;
79 leaf.elem = component;
80 leaf.elem_len = strlen(component);
82 fprintf(out, "%s ", sha1_to_hex(obj->sha1));
83 show_path_truncated(out, &leaf);
87 void add_object(struct object *obj,
88 struct object_array *p,
89 struct name_path *path,
92 char *pn = path_name(path, name);
93 add_object_array(obj, pn, p);
97 static void mark_blob_uninteresting(struct blob *blob)
101 if (blob->object.flags & UNINTERESTING)
103 blob->object.flags |= UNINTERESTING;
106 void mark_tree_uninteresting(struct tree *tree)
108 struct tree_desc desc;
109 struct name_entry entry;
110 struct object *obj = &tree->object;
114 if (obj->flags & UNINTERESTING)
116 obj->flags |= UNINTERESTING;
117 if (!has_sha1_file(obj->sha1))
119 if (parse_tree(tree) < 0)
120 die("bad tree %s", sha1_to_hex(obj->sha1));
122 init_tree_desc(&desc, tree->buffer, tree->size);
123 while (tree_entry(&desc, &entry)) {
124 switch (object_type(entry.mode)) {
126 mark_tree_uninteresting(lookup_tree(entry.sha1));
129 mark_blob_uninteresting(lookup_blob(entry.sha1));
132 /* Subproject commit - not in this repository */
138 * We don't care about the tree any more
139 * after it has been marked uninteresting.
145 void mark_parents_uninteresting(struct commit *commit)
147 struct commit_list *parents = NULL, *l;
149 for (l = commit->parents; l; l = l->next)
150 commit_list_insert(l->item, &parents);
153 struct commit *commit = parents->item;
155 parents = parents->next;
160 * A missing commit is ok iff its parent is marked
163 * We just mark such a thing parsed, so that when
164 * it is popped next time around, we won't be trying
165 * to parse it and get an error.
167 if (!has_sha1_file(commit->object.sha1))
168 commit->object.parsed = 1;
170 if (commit->object.flags & UNINTERESTING)
173 commit->object.flags |= UNINTERESTING;
176 * Normally we haven't parsed the parent
177 * yet, so we won't have a parent of a parent
178 * here. However, it may turn out that we've
179 * reached this commit some other way (where it
180 * wasn't uninteresting), in which case we need
181 * to mark its parents recursively too..
183 if (!commit->parents)
186 for (l = commit->parents->next; l; l = l->next)
187 commit_list_insert(l->item, &parents);
188 commit = commit->parents->item;
193 static void add_pending_object_with_mode(struct rev_info *revs,
195 const char *name, unsigned mode)
199 if (revs->no_walk && (obj->flags & UNINTERESTING))
201 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
202 struct strbuf buf = STRBUF_INIT;
203 int len = interpret_branch_name(name, &buf);
206 if (0 < len && name[len] && buf.len)
207 strbuf_addstr(&buf, name + len);
208 st = add_reflog_for_walk(revs->reflog_info,
209 (struct commit *)obj,
210 buf.buf[0] ? buf.buf: name);
211 strbuf_release(&buf);
215 add_object_array_with_mode(obj, name, &revs->pending, mode);
218 void add_pending_object(struct rev_info *revs,
219 struct object *obj, const char *name)
221 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
224 void add_head_to_pending(struct rev_info *revs)
226 unsigned char sha1[20];
228 if (get_sha1("HEAD", sha1))
230 obj = parse_object(sha1);
233 add_pending_object(revs, obj, "HEAD");
236 static struct object *get_reference(struct rev_info *revs, const char *name,
237 const unsigned char *sha1,
240 struct object *object;
242 object = parse_object(sha1);
244 if (revs->ignore_missing)
246 die("bad object %s", name);
248 object->flags |= flags;
252 void add_pending_sha1(struct rev_info *revs, const char *name,
253 const unsigned char *sha1, unsigned int flags)
255 struct object *object = get_reference(revs, name, sha1, flags);
256 add_pending_object(revs, object, name);
259 static struct commit *handle_commit(struct rev_info *revs,
260 struct object *object, const char *name)
262 unsigned long flags = object->flags;
265 * Tag object? Look what it points to..
267 while (object->type == OBJ_TAG) {
268 struct tag *tag = (struct tag *) object;
269 if (revs->tag_objects && !(flags & UNINTERESTING))
270 add_pending_object(revs, object, tag->tag);
273 object = parse_object(tag->tagged->sha1);
275 if (flags & UNINTERESTING)
277 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
282 * Commit object? Just return it, we'll do all the complex
285 if (object->type == OBJ_COMMIT) {
286 struct commit *commit = (struct commit *)object;
287 if (parse_commit(commit) < 0)
288 die("unable to parse commit %s", name);
289 if (flags & UNINTERESTING) {
290 commit->object.flags |= UNINTERESTING;
291 mark_parents_uninteresting(commit);
294 if (revs->show_source && !commit->util)
295 commit->util = (void *) name;
300 * Tree object? Either mark it uninteresting, or add it
301 * to the list of objects to look at later..
303 if (object->type == OBJ_TREE) {
304 struct tree *tree = (struct tree *)object;
305 if (!revs->tree_objects)
307 if (flags & UNINTERESTING) {
308 mark_tree_uninteresting(tree);
311 add_pending_object(revs, object, "");
316 * Blob object? You know the drill by now..
318 if (object->type == OBJ_BLOB) {
319 struct blob *blob = (struct blob *)object;
320 if (!revs->blob_objects)
322 if (flags & UNINTERESTING) {
323 mark_blob_uninteresting(blob);
326 add_pending_object(revs, object, "");
329 die("%s is unknown object", name);
332 static int everybody_uninteresting(struct commit_list *orig)
334 struct commit_list *list = orig;
336 struct commit *commit = list->item;
338 if (commit->object.flags & UNINTERESTING)
346 * A definition of "relevant" commit that we can use to simplify limited graphs
347 * by eliminating side branches.
349 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
350 * in our list), or that is a specified BOTTOM commit. Then after computing
351 * a limited list, during processing we can generally ignore boundary merges
352 * coming from outside the graph, (ie from irrelevant parents), and treat
353 * those merges as if they were single-parent. TREESAME is defined to consider
354 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
355 * we don't care if we were !TREESAME to non-graph parents.
357 * Treating bottom commits as relevant ensures that a limited graph's
358 * connection to the actual bottom commit is not viewed as a side branch, but
359 * treated as part of the graph. For example:
361 * ....Z...A---X---o---o---B
365 * When computing "A..B", the A-X connection is at least as important as
366 * Y-X, despite A being flagged UNINTERESTING.
368 * And when computing --ancestry-path "A..B", the A-X connection is more
369 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
371 static inline int relevant_commit(struct commit *commit)
373 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
377 * Return a single relevant commit from a parent list. If we are a TREESAME
378 * commit, and this selects one of our parents, then we can safely simplify to
381 static struct commit *one_relevant_parent(const struct rev_info *revs,
382 struct commit_list *orig)
384 struct commit_list *list = orig;
385 struct commit *relevant = NULL;
391 * For 1-parent commits, or if first-parent-only, then return that
392 * first parent (even if not "relevant" by the above definition).
393 * TREESAME will have been set purely on that parent.
395 if (revs->first_parent_only || !orig->next)
399 * For multi-parent commits, identify a sole relevant parent, if any.
400 * If we have only one relevant parent, then TREESAME will be set purely
401 * with regard to that parent, and we can simplify accordingly.
403 * If we have more than one relevant parent, or no relevant parents
404 * (and multiple irrelevant ones), then we can't select a parent here
408 struct commit *commit = list->item;
410 if (relevant_commit(commit)) {
420 * The goal is to get REV_TREE_NEW as the result only if the
421 * diff consists of all '+' (and no other changes), REV_TREE_OLD
422 * if the whole diff is removal of old data, and otherwise
423 * REV_TREE_DIFFERENT (of course if the trees are the same we
424 * want REV_TREE_SAME).
425 * That means that once we get to REV_TREE_DIFFERENT, we do not
426 * have to look any further.
428 static int tree_difference = REV_TREE_SAME;
430 static void file_add_remove(struct diff_options *options,
431 int addremove, unsigned mode,
432 const unsigned char *sha1,
434 const char *fullpath, unsigned dirty_submodule)
436 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
438 tree_difference |= diff;
439 if (tree_difference == REV_TREE_DIFFERENT)
440 DIFF_OPT_SET(options, HAS_CHANGES);
443 static void file_change(struct diff_options *options,
444 unsigned old_mode, unsigned new_mode,
445 const unsigned char *old_sha1,
446 const unsigned char *new_sha1,
447 int old_sha1_valid, int new_sha1_valid,
448 const char *fullpath,
449 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
451 tree_difference = REV_TREE_DIFFERENT;
452 DIFF_OPT_SET(options, HAS_CHANGES);
455 static int rev_compare_tree(struct rev_info *revs,
456 struct commit *parent, struct commit *commit)
458 struct tree *t1 = parent->tree;
459 struct tree *t2 = commit->tree;
466 if (revs->simplify_by_decoration) {
468 * If we are simplifying by decoration, then the commit
469 * is worth showing if it has a tag pointing at it.
471 if (lookup_decoration(&name_decoration, &commit->object))
472 return REV_TREE_DIFFERENT;
474 * A commit that is not pointed by a tag is uninteresting
475 * if we are not limited by path. This means that you will
476 * see the usual "commits that touch the paths" plus any
477 * tagged commit by specifying both --simplify-by-decoration
480 if (!revs->prune_data.nr)
481 return REV_TREE_SAME;
484 tree_difference = REV_TREE_SAME;
485 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
486 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
488 return REV_TREE_DIFFERENT;
489 return tree_difference;
492 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
497 struct tree_desc empty, real;
498 struct tree *t1 = commit->tree;
503 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
506 init_tree_desc(&real, tree, size);
507 init_tree_desc(&empty, "", 0);
509 tree_difference = REV_TREE_SAME;
510 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
511 retval = diff_tree(&empty, &real, "", &revs->pruning);
514 return retval >= 0 && (tree_difference == REV_TREE_SAME);
517 struct treesame_state {
518 unsigned int nparents;
519 unsigned char treesame[FLEX_ARRAY];
522 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
524 unsigned n = commit_list_count(commit->parents);
525 struct treesame_state *st = xcalloc(1, sizeof(*st) + n);
527 add_decoration(&revs->treesame, &commit->object, st);
532 * Must be called immediately after removing the nth_parent from a commit's
533 * parent list, if we are maintaining the per-parent treesame[] decoration.
534 * This does not recalculate the master TREESAME flag - update_treesame()
535 * should be called to update it after a sequence of treesame[] modifications
536 * that may have affected it.
538 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
540 struct treesame_state *st;
543 if (!commit->parents) {
545 * Have just removed the only parent from a non-merge.
546 * Different handling, as we lack decoration.
549 die("compact_treesame %u", nth_parent);
550 old_same = !!(commit->object.flags & TREESAME);
551 if (rev_same_tree_as_empty(revs, commit))
552 commit->object.flags |= TREESAME;
554 commit->object.flags &= ~TREESAME;
558 st = lookup_decoration(&revs->treesame, &commit->object);
559 if (!st || nth_parent >= st->nparents)
560 die("compact_treesame %u", nth_parent);
562 old_same = st->treesame[nth_parent];
563 memmove(st->treesame + nth_parent,
564 st->treesame + nth_parent + 1,
565 st->nparents - nth_parent - 1);
568 * If we've just become a non-merge commit, update TREESAME
569 * immediately, and remove the no-longer-needed decoration.
570 * If still a merge, defer update until update_treesame().
572 if (--st->nparents == 1) {
573 if (commit->parents->next)
574 die("compact_treesame parents mismatch");
575 if (st->treesame[0] && revs->dense)
576 commit->object.flags |= TREESAME;
578 commit->object.flags &= ~TREESAME;
579 free(add_decoration(&revs->treesame, &commit->object, NULL));
585 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
587 if (commit->parents && commit->parents->next) {
589 struct treesame_state *st;
590 struct commit_list *p;
591 unsigned relevant_parents;
592 unsigned relevant_change, irrelevant_change;
594 st = lookup_decoration(&revs->treesame, &commit->object);
596 die("update_treesame %s", sha1_to_hex(commit->object.sha1));
597 relevant_parents = 0;
598 relevant_change = irrelevant_change = 0;
599 for (p = commit->parents, n = 0; p; n++, p = p->next) {
600 if (relevant_commit(p->item)) {
601 relevant_change |= !st->treesame[n];
604 irrelevant_change |= !st->treesame[n];
606 if (relevant_parents ? relevant_change : irrelevant_change)
607 commit->object.flags &= ~TREESAME;
609 commit->object.flags |= TREESAME;
612 return commit->object.flags & TREESAME;
615 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
618 * TREESAME is irrelevant unless prune && dense;
619 * if simplify_history is set, we can't have a mixture of TREESAME and
620 * !TREESAME INTERESTING parents (and we don't have treesame[]
621 * decoration anyway);
622 * if first_parent_only is set, then the TREESAME flag is locked
623 * against the first parent (and again we lack treesame[] decoration).
625 return revs->prune && revs->dense &&
626 !revs->simplify_history &&
627 !revs->first_parent_only;
630 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
632 struct commit_list **pp, *parent;
633 struct treesame_state *ts = NULL;
634 int relevant_change = 0, irrelevant_change = 0;
635 int relevant_parents, nth_parent;
638 * If we don't do pruning, everything is interesting
646 if (!commit->parents) {
647 if (rev_same_tree_as_empty(revs, commit))
648 commit->object.flags |= TREESAME;
653 * Normal non-merge commit? If we don't want to make the
654 * history dense, we consider it always to be a change..
656 if (!revs->dense && !commit->parents->next)
659 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
660 (parent = *pp) != NULL;
661 pp = &parent->next, nth_parent++) {
662 struct commit *p = parent->item;
663 if (relevant_commit(p))
666 if (nth_parent == 1) {
668 * This our second loop iteration - so we now know
669 * we're dealing with a merge.
671 * Do not compare with later parents when we care only about
672 * the first parent chain, in order to avoid derailing the
673 * traversal to follow a side branch that brought everything
674 * in the path we are limited to by the pathspec.
676 if (revs->first_parent_only)
679 * If this will remain a potentially-simplifiable
680 * merge, remember per-parent treesame if needed.
681 * Initialise the array with the comparison from our
684 if (revs->treesame.name &&
685 !revs->simplify_history &&
686 !(commit->object.flags & UNINTERESTING)) {
687 ts = initialise_treesame(revs, commit);
688 if (!(irrelevant_change || relevant_change))
692 if (parse_commit(p) < 0)
693 die("cannot simplify commit %s (because of %s)",
694 sha1_to_hex(commit->object.sha1),
695 sha1_to_hex(p->object.sha1));
696 switch (rev_compare_tree(revs, p, commit)) {
698 if (!revs->simplify_history || !relevant_commit(p)) {
699 /* Even if a merge with an uninteresting
700 * side branch brought the entire change
701 * we are interested in, we do not want
702 * to lose the other branches of this
703 * merge, so we just keep going.
706 ts->treesame[nth_parent] = 1;
710 commit->parents = parent;
711 commit->object.flags |= TREESAME;
715 if (revs->remove_empty_trees &&
716 rev_same_tree_as_empty(revs, p)) {
717 /* We are adding all the specified
718 * paths from this parent, so the
719 * history beyond this parent is not
720 * interesting. Remove its parents
721 * (they are grandparents for us).
722 * IOW, we pretend this parent is a
725 if (parse_commit(p) < 0)
726 die("cannot simplify commit %s (invalid %s)",
727 sha1_to_hex(commit->object.sha1),
728 sha1_to_hex(p->object.sha1));
733 case REV_TREE_DIFFERENT:
734 if (relevant_commit(p))
737 irrelevant_change = 1;
740 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
744 * TREESAME is straightforward for single-parent commits. For merge
745 * commits, it is most useful to define it so that "irrelevant"
746 * parents cannot make us !TREESAME - if we have any relevant
747 * parents, then we only consider TREESAMEness with respect to them,
748 * allowing irrelevant merges from uninteresting branches to be
749 * simplified away. Only if we have only irrelevant parents do we
750 * base TREESAME on them. Note that this logic is replicated in
751 * update_treesame, which should be kept in sync.
753 if (relevant_parents ? !relevant_change : !irrelevant_change)
754 commit->object.flags |= TREESAME;
757 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
758 struct commit_list *cached_base, struct commit_list **cache)
760 struct commit_list *new_entry;
762 if (cached_base && p->date < cached_base->item->date)
763 new_entry = commit_list_insert_by_date(p, &cached_base->next);
765 new_entry = commit_list_insert_by_date(p, head);
767 if (cache && (!*cache || p->date < (*cache)->item->date))
771 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
772 struct commit_list **list, struct commit_list **cache_ptr)
774 struct commit_list *parent = commit->parents;
776 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
778 if (commit->object.flags & ADDED)
780 commit->object.flags |= ADDED;
783 * If the commit is uninteresting, don't try to
784 * prune parents - we want the maximal uninteresting
787 * Normally we haven't parsed the parent
788 * yet, so we won't have a parent of a parent
789 * here. However, it may turn out that we've
790 * reached this commit some other way (where it
791 * wasn't uninteresting), in which case we need
792 * to mark its parents recursively too..
794 if (commit->object.flags & UNINTERESTING) {
796 struct commit *p = parent->item;
797 parent = parent->next;
799 p->object.flags |= UNINTERESTING;
800 if (parse_commit(p) < 0)
803 mark_parents_uninteresting(p);
804 if (p->object.flags & SEEN)
806 p->object.flags |= SEEN;
807 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
813 * Ok, the commit wasn't uninteresting. Try to
814 * simplify the commit history and find the parent
815 * that has no differences in the path set if one exists.
817 try_to_simplify_commit(revs, commit);
822 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
824 for (parent = commit->parents; parent; parent = parent->next) {
825 struct commit *p = parent->item;
827 if (parse_commit(p) < 0)
829 if (revs->show_source && !p->util)
830 p->util = commit->util;
831 p->object.flags |= left_flag;
832 if (!(p->object.flags & SEEN)) {
833 p->object.flags |= SEEN;
834 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
836 if (revs->first_parent_only)
842 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
844 struct commit_list *p;
845 int left_count = 0, right_count = 0;
847 struct patch_ids ids;
848 unsigned cherry_flag;
850 /* First count the commits on the left and on the right */
851 for (p = list; p; p = p->next) {
852 struct commit *commit = p->item;
853 unsigned flags = commit->object.flags;
854 if (flags & BOUNDARY)
856 else if (flags & SYMMETRIC_LEFT)
862 if (!left_count || !right_count)
865 left_first = left_count < right_count;
866 init_patch_ids(&ids);
867 ids.diffopts.pathspec = revs->diffopt.pathspec;
869 /* Compute patch-ids for one side */
870 for (p = list; p; p = p->next) {
871 struct commit *commit = p->item;
872 unsigned flags = commit->object.flags;
874 if (flags & BOUNDARY)
877 * If we have fewer left, left_first is set and we omit
878 * commits on the right branch in this loop. If we have
879 * fewer right, we skip the left ones.
881 if (left_first != !!(flags & SYMMETRIC_LEFT))
883 commit->util = add_commit_patch_id(commit, &ids);
886 /* either cherry_mark or cherry_pick are true */
887 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
889 /* Check the other side */
890 for (p = list; p; p = p->next) {
891 struct commit *commit = p->item;
893 unsigned flags = commit->object.flags;
895 if (flags & BOUNDARY)
898 * If we have fewer left, left_first is set and we omit
899 * commits on the left branch in this loop.
901 if (left_first == !!(flags & SYMMETRIC_LEFT))
905 * Have we seen the same patch id?
907 id = has_commit_patch_id(commit, &ids);
911 commit->object.flags |= cherry_flag;
914 /* Now check the original side for seen ones */
915 for (p = list; p; p = p->next) {
916 struct commit *commit = p->item;
917 struct patch_id *ent;
923 commit->object.flags |= cherry_flag;
927 free_patch_ids(&ids);
930 /* How many extra uninteresting commits we want to see.. */
933 static int still_interesting(struct commit_list *src, unsigned long date, int slop)
936 * No source list at all? We're definitely done..
942 * Does the destination list contain entries with a date
943 * before the source list? Definitely _not_ done.
945 if (date <= src->item->date)
949 * Does the source list still have interesting commits in
950 * it? Definitely not done..
952 if (!everybody_uninteresting(src))
955 /* Ok, we're closing in.. */
960 * "rev-list --ancestry-path A..B" computes commits that are ancestors
961 * of B but not ancestors of A but further limits the result to those
962 * that are descendants of A. This takes the list of bottom commits and
963 * the result of "A..B" without --ancestry-path, and limits the latter
964 * further to the ones that can reach one of the commits in "bottom".
966 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
968 struct commit_list *p;
969 struct commit_list *rlist = NULL;
973 * Reverse the list so that it will be likely that we would
974 * process parents before children.
976 for (p = list; p; p = p->next)
977 commit_list_insert(p->item, &rlist);
979 for (p = bottom; p; p = p->next)
980 p->item->object.flags |= TMP_MARK;
983 * Mark the ones that can reach bottom commits in "list",
984 * in a bottom-up fashion.
988 for (p = rlist; p; p = p->next) {
989 struct commit *c = p->item;
990 struct commit_list *parents;
991 if (c->object.flags & (TMP_MARK | UNINTERESTING))
993 for (parents = c->parents;
995 parents = parents->next) {
996 if (!(parents->item->object.flags & TMP_MARK))
998 c->object.flags |= TMP_MARK;
1003 } while (made_progress);
1006 * NEEDSWORK: decide if we want to remove parents that are
1007 * not marked with TMP_MARK from commit->parents for commits
1008 * in the resulting list. We may not want to do that, though.
1012 * The ones that are not marked with TMP_MARK are uninteresting
1014 for (p = list; p; p = p->next) {
1015 struct commit *c = p->item;
1016 if (c->object.flags & TMP_MARK)
1018 c->object.flags |= UNINTERESTING;
1021 /* We are done with the TMP_MARK */
1022 for (p = list; p; p = p->next)
1023 p->item->object.flags &= ~TMP_MARK;
1024 for (p = bottom; p; p = p->next)
1025 p->item->object.flags &= ~TMP_MARK;
1026 free_commit_list(rlist);
1030 * Before walking the history, keep the set of "negative" refs the
1031 * caller has asked to exclude.
1033 * This is used to compute "rev-list --ancestry-path A..B", as we need
1034 * to filter the result of "A..B" further to the ones that can actually
1037 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1039 struct commit_list *elem, *bottom = NULL;
1040 for (elem = list; elem; elem = elem->next)
1041 if (elem->item->object.flags & BOTTOM)
1042 commit_list_insert(elem->item, &bottom);
1046 /* Assumes either left_only or right_only is set */
1047 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1049 struct commit_list *p;
1051 for (p = list; p; p = p->next) {
1052 struct commit *commit = p->item;
1054 if (revs->right_only) {
1055 if (commit->object.flags & SYMMETRIC_LEFT)
1056 commit->object.flags |= SHOWN;
1057 } else /* revs->left_only is set */
1058 if (!(commit->object.flags & SYMMETRIC_LEFT))
1059 commit->object.flags |= SHOWN;
1063 static int limit_list(struct rev_info *revs)
1066 unsigned long date = ~0ul;
1067 struct commit_list *list = revs->commits;
1068 struct commit_list *newlist = NULL;
1069 struct commit_list **p = &newlist;
1070 struct commit_list *bottom = NULL;
1072 if (revs->ancestry_path) {
1073 bottom = collect_bottom_commits(list);
1075 die("--ancestry-path given but there are no bottom commits");
1079 struct commit_list *entry = list;
1080 struct commit *commit = list->item;
1081 struct object *obj = &commit->object;
1082 show_early_output_fn_t show;
1087 if (revs->max_age != -1 && (commit->date < revs->max_age))
1088 obj->flags |= UNINTERESTING;
1089 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1091 if (obj->flags & UNINTERESTING) {
1092 mark_parents_uninteresting(commit);
1094 p = &commit_list_insert(commit, p)->next;
1095 slop = still_interesting(list, date, slop);
1098 /* If showing all, add the whole pending list to the end */
1103 if (revs->min_age != -1 && (commit->date > revs->min_age))
1105 date = commit->date;
1106 p = &commit_list_insert(commit, p)->next;
1108 show = show_early_output;
1112 show(revs, newlist);
1113 show_early_output = NULL;
1115 if (revs->cherry_pick || revs->cherry_mark)
1116 cherry_pick_list(newlist, revs);
1118 if (revs->left_only || revs->right_only)
1119 limit_left_right(newlist, revs);
1122 limit_to_ancestry(bottom, newlist);
1123 free_commit_list(bottom);
1127 * Check if any commits have become TREESAME by some of their parents
1128 * becoming UNINTERESTING.
1130 if (limiting_can_increase_treesame(revs))
1131 for (list = newlist; list; list = list->next) {
1132 struct commit *c = list->item;
1133 if (c->object.flags & (UNINTERESTING | TREESAME))
1135 update_treesame(revs, c);
1138 revs->commits = newlist;
1143 * Add an entry to refs->cmdline with the specified information.
1146 static void add_rev_cmdline(struct rev_info *revs,
1147 struct object *item,
1152 struct rev_cmdline_info *info = &revs->cmdline;
1155 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1156 info->rev[nr].item = item;
1157 info->rev[nr].name = xstrdup(name);
1158 info->rev[nr].whence = whence;
1159 info->rev[nr].flags = flags;
1163 static void add_rev_cmdline_list(struct rev_info *revs,
1164 struct commit_list *commit_list,
1168 while (commit_list) {
1169 struct object *object = &commit_list->item->object;
1170 add_rev_cmdline(revs, object, sha1_to_hex(object->sha1),
1172 commit_list = commit_list->next;
1176 struct all_refs_cb {
1178 int warned_bad_reflog;
1179 struct rev_info *all_revs;
1180 const char *name_for_errormsg;
1183 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
1185 struct all_refs_cb *cb = cb_data;
1186 struct object *object = get_reference(cb->all_revs, path, sha1,
1188 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1189 add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags);
1193 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1196 cb->all_revs = revs;
1197 cb->all_flags = flags;
1200 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
1201 int (*for_each)(const char *, each_ref_fn, void *))
1203 struct all_refs_cb cb;
1204 init_all_refs_cb(&cb, revs, flags);
1205 for_each(submodule, handle_one_ref, &cb);
1208 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
1210 struct all_refs_cb *cb = cb_data;
1211 if (!is_null_sha1(sha1)) {
1212 struct object *o = parse_object(sha1);
1214 o->flags |= cb->all_flags;
1215 /* ??? CMDLINEFLAGS ??? */
1216 add_pending_object(cb->all_revs, o, "");
1218 else if (!cb->warned_bad_reflog) {
1219 warning("reflog of '%s' references pruned commits",
1220 cb->name_for_errormsg);
1221 cb->warned_bad_reflog = 1;
1226 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
1227 const char *email, unsigned long timestamp, int tz,
1228 const char *message, void *cb_data)
1230 handle_one_reflog_commit(osha1, cb_data);
1231 handle_one_reflog_commit(nsha1, cb_data);
1235 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
1237 struct all_refs_cb *cb = cb_data;
1238 cb->warned_bad_reflog = 0;
1239 cb->name_for_errormsg = path;
1240 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
1244 static void handle_reflog(struct rev_info *revs, unsigned flags)
1246 struct all_refs_cb cb;
1248 cb.all_flags = flags;
1249 for_each_reflog(handle_one_reflog, &cb);
1252 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
1254 unsigned char sha1[20];
1256 struct commit *commit;
1257 struct commit_list *parents;
1258 const char *arg = arg_;
1261 flags ^= UNINTERESTING | BOTTOM;
1264 if (get_sha1_committish(arg, sha1))
1267 it = get_reference(revs, arg, sha1, 0);
1268 if (!it && revs->ignore_missing)
1270 if (it->type != OBJ_TAG)
1272 if (!((struct tag*)it)->tagged)
1274 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
1276 if (it->type != OBJ_COMMIT)
1278 commit = (struct commit *)it;
1279 for (parents = commit->parents; parents; parents = parents->next) {
1280 it = &parents->item->object;
1282 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1283 add_pending_object(revs, it, arg);
1288 void init_revisions(struct rev_info *revs, const char *prefix)
1290 memset(revs, 0, sizeof(*revs));
1292 revs->abbrev = DEFAULT_ABBREV;
1293 revs->ignore_merges = 1;
1294 revs->simplify_history = 1;
1295 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1296 DIFF_OPT_SET(&revs->pruning, QUICK);
1297 revs->pruning.add_remove = file_add_remove;
1298 revs->pruning.change = file_change;
1301 revs->prefix = prefix;
1304 revs->skip_count = -1;
1305 revs->max_count = -1;
1306 revs->max_parents = -1;
1308 revs->commit_format = CMIT_FMT_DEFAULT;
1310 init_grep_defaults();
1311 grep_init(&revs->grep_filter, prefix);
1312 revs->grep_filter.status_only = 1;
1313 revs->grep_filter.regflags = REG_NEWLINE;
1315 diff_setup(&revs->diffopt);
1316 if (prefix && !revs->diffopt.prefix) {
1317 revs->diffopt.prefix = prefix;
1318 revs->diffopt.prefix_length = strlen(prefix);
1321 revs->notes_opt.use_default_notes = -1;
1324 static void add_pending_commit_list(struct rev_info *revs,
1325 struct commit_list *commit_list,
1328 while (commit_list) {
1329 struct object *object = &commit_list->item->object;
1330 object->flags |= flags;
1331 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1332 commit_list = commit_list->next;
1336 static void prepare_show_merge(struct rev_info *revs)
1338 struct commit_list *bases;
1339 struct commit *head, *other;
1340 unsigned char sha1[20];
1341 const char **prune = NULL;
1342 int i, prune_num = 1; /* counting terminating NULL */
1344 if (get_sha1("HEAD", sha1))
1345 die("--merge without HEAD?");
1346 head = lookup_commit_or_die(sha1, "HEAD");
1347 if (get_sha1("MERGE_HEAD", sha1))
1348 die("--merge without MERGE_HEAD?");
1349 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
1350 add_pending_object(revs, &head->object, "HEAD");
1351 add_pending_object(revs, &other->object, "MERGE_HEAD");
1352 bases = get_merge_bases(head, other, 1);
1353 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1354 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1355 free_commit_list(bases);
1356 head->object.flags |= SYMMETRIC_LEFT;
1360 for (i = 0; i < active_nr; i++) {
1361 struct cache_entry *ce = active_cache[i];
1364 if (ce_path_match(ce, &revs->prune_data)) {
1366 prune = xrealloc(prune, sizeof(*prune) * prune_num);
1367 prune[prune_num-2] = ce->name;
1368 prune[prune_num-1] = NULL;
1370 while ((i+1 < active_nr) &&
1371 ce_same_name(ce, active_cache[i+1]))
1374 free_pathspec(&revs->prune_data);
1375 init_pathspec(&revs->prune_data, prune);
1379 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1381 struct object_context oc;
1383 struct object *object;
1384 unsigned char sha1[20];
1386 const char *arg = arg_;
1387 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1388 unsigned get_sha1_flags = 0;
1390 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1392 dotdot = strstr(arg, "..");
1394 unsigned char from_sha1[20];
1395 const char *next = dotdot + 2;
1396 const char *this = arg;
1397 int symmetric = *next == '.';
1398 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1399 static const char head_by_default[] = "HEAD";
1400 unsigned int a_flags;
1406 next = head_by_default;
1408 this = head_by_default;
1409 if (this == head_by_default && next == head_by_default &&
1412 * Just ".."? That is not a range but the
1413 * pathspec for the parent directory.
1415 if (!cant_be_filename) {
1420 if (!get_sha1_committish(this, from_sha1) &&
1421 !get_sha1_committish(next, sha1)) {
1422 struct commit *a, *b;
1423 struct commit_list *exclude;
1425 a = lookup_commit_reference(from_sha1);
1426 b = lookup_commit_reference(sha1);
1428 if (revs->ignore_missing)
1431 "Invalid symmetric difference expression %s...%s" :
1432 "Invalid revision range %s..%s",
1436 if (!cant_be_filename) {
1438 verify_non_filename(revs->prefix, arg);
1442 exclude = get_merge_bases(a, b, 1);
1443 add_rev_cmdline_list(revs, exclude,
1446 add_pending_commit_list(revs, exclude,
1448 free_commit_list(exclude);
1449 a_flags = flags | SYMMETRIC_LEFT;
1451 a_flags = flags_exclude;
1452 a->object.flags |= a_flags;
1453 b->object.flags |= flags;
1454 add_rev_cmdline(revs, &a->object, this,
1455 REV_CMD_LEFT, a_flags);
1456 add_rev_cmdline(revs, &b->object, next,
1457 REV_CMD_RIGHT, flags);
1458 add_pending_object(revs, &a->object, this);
1459 add_pending_object(revs, &b->object, next);
1464 dotdot = strstr(arg, "^@");
1465 if (dotdot && !dotdot[2]) {
1467 if (add_parents_only(revs, arg, flags))
1471 dotdot = strstr(arg, "^!");
1472 if (dotdot && !dotdot[2]) {
1474 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM)))
1480 local_flags = UNINTERESTING | BOTTOM;
1484 if (revarg_opt & REVARG_COMMITTISH)
1485 get_sha1_flags = GET_SHA1_COMMITTISH;
1487 if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
1488 return revs->ignore_missing ? 0 : -1;
1489 if (!cant_be_filename)
1490 verify_non_filename(revs->prefix, arg);
1491 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1492 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1493 add_pending_object_with_mode(revs, object, arg, oc.mode);
1497 struct cmdline_pathspec {
1503 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1506 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1507 prune->path[prune->nr++] = *(av++);
1511 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1512 struct cmdline_pathspec *prune)
1514 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1516 if (len && sb->buf[len - 1] == '\n')
1517 sb->buf[--len] = '\0';
1518 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1519 prune->path[prune->nr++] = xstrdup(sb->buf);
1523 static void read_revisions_from_stdin(struct rev_info *revs,
1524 struct cmdline_pathspec *prune)
1527 int seen_dashdash = 0;
1529 strbuf_init(&sb, 1000);
1530 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1532 if (len && sb.buf[len - 1] == '\n')
1533 sb.buf[--len] = '\0';
1536 if (sb.buf[0] == '-') {
1537 if (len == 2 && sb.buf[1] == '-') {
1541 die("options not supported in --stdin mode");
1543 if (handle_revision_arg(sb.buf, revs, 0,
1544 REVARG_CANNOT_BE_FILENAME))
1545 die("bad revision '%s'", sb.buf);
1548 read_pathspec_from_stdin(revs, &sb, prune);
1549 strbuf_release(&sb);
1552 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1554 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1557 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1559 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1562 static void add_message_grep(struct rev_info *revs, const char *pattern)
1564 add_grep(revs, pattern, GREP_PATTERN_BODY);
1567 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1568 int *unkc, const char **unkv)
1570 const char *arg = argv[0];
1574 /* pseudo revision arguments */
1575 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1576 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1577 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1578 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1579 !strcmp(arg, "--bisect") || !prefixcmp(arg, "--glob=") ||
1580 !prefixcmp(arg, "--branches=") || !prefixcmp(arg, "--tags=") ||
1581 !prefixcmp(arg, "--remotes=") || !prefixcmp(arg, "--no-walk="))
1583 unkv[(*unkc)++] = arg;
1587 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1588 revs->max_count = atoi(optarg);
1591 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1592 revs->skip_count = atoi(optarg);
1594 } else if ((*arg == '-') && isdigit(arg[1])) {
1595 /* accept -<digit>, like traditional "head" */
1596 revs->max_count = atoi(arg + 1);
1598 } else if (!strcmp(arg, "-n")) {
1600 return error("-n requires an argument");
1601 revs->max_count = atoi(argv[1]);
1604 } else if (!prefixcmp(arg, "-n")) {
1605 revs->max_count = atoi(arg + 2);
1607 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1608 revs->max_age = atoi(optarg);
1610 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1611 revs->max_age = approxidate(optarg);
1613 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1614 revs->max_age = approxidate(optarg);
1616 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1617 revs->min_age = atoi(optarg);
1619 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1620 revs->min_age = approxidate(optarg);
1622 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1623 revs->min_age = approxidate(optarg);
1625 } else if (!strcmp(arg, "--first-parent")) {
1626 revs->first_parent_only = 1;
1627 } else if (!strcmp(arg, "--ancestry-path")) {
1628 revs->ancestry_path = 1;
1629 revs->simplify_history = 0;
1631 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1632 init_reflog_walk(&revs->reflog_info);
1633 } else if (!strcmp(arg, "--default")) {
1635 return error("bad --default argument");
1636 revs->def = argv[1];
1638 } else if (!strcmp(arg, "--merge")) {
1639 revs->show_merge = 1;
1640 } else if (!strcmp(arg, "--topo-order")) {
1642 revs->topo_order = 1;
1643 } else if (!strcmp(arg, "--simplify-merges")) {
1644 revs->simplify_merges = 1;
1645 revs->topo_order = 1;
1646 revs->rewrite_parents = 1;
1647 revs->simplify_history = 0;
1649 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1650 revs->simplify_merges = 1;
1651 revs->topo_order = 1;
1652 revs->rewrite_parents = 1;
1653 revs->simplify_history = 0;
1654 revs->simplify_by_decoration = 1;
1657 load_ref_decorations(DECORATE_SHORT_REFS);
1658 } else if (!strcmp(arg, "--date-order")) {
1660 revs->topo_order = 1;
1661 } else if (!prefixcmp(arg, "--early-output")) {
1665 count = atoi(arg+15);
1668 revs->topo_order = 1;
1669 revs->early_output = count;
1671 } else if (!strcmp(arg, "--parents")) {
1672 revs->rewrite_parents = 1;
1673 revs->print_parents = 1;
1674 } else if (!strcmp(arg, "--dense")) {
1676 } else if (!strcmp(arg, "--sparse")) {
1678 } else if (!strcmp(arg, "--show-all")) {
1680 } else if (!strcmp(arg, "--remove-empty")) {
1681 revs->remove_empty_trees = 1;
1682 } else if (!strcmp(arg, "--merges")) {
1683 revs->min_parents = 2;
1684 } else if (!strcmp(arg, "--no-merges")) {
1685 revs->max_parents = 1;
1686 } else if (!prefixcmp(arg, "--min-parents=")) {
1687 revs->min_parents = atoi(arg+14);
1688 } else if (!prefixcmp(arg, "--no-min-parents")) {
1689 revs->min_parents = 0;
1690 } else if (!prefixcmp(arg, "--max-parents=")) {
1691 revs->max_parents = atoi(arg+14);
1692 } else if (!prefixcmp(arg, "--no-max-parents")) {
1693 revs->max_parents = -1;
1694 } else if (!strcmp(arg, "--boundary")) {
1696 } else if (!strcmp(arg, "--left-right")) {
1697 revs->left_right = 1;
1698 } else if (!strcmp(arg, "--left-only")) {
1699 if (revs->right_only)
1700 die("--left-only is incompatible with --right-only"
1702 revs->left_only = 1;
1703 } else if (!strcmp(arg, "--right-only")) {
1704 if (revs->left_only)
1705 die("--right-only is incompatible with --left-only");
1706 revs->right_only = 1;
1707 } else if (!strcmp(arg, "--cherry")) {
1708 if (revs->left_only)
1709 die("--cherry is incompatible with --left-only");
1710 revs->cherry_mark = 1;
1711 revs->right_only = 1;
1712 revs->max_parents = 1;
1714 } else if (!strcmp(arg, "--count")) {
1716 } else if (!strcmp(arg, "--cherry-mark")) {
1717 if (revs->cherry_pick)
1718 die("--cherry-mark is incompatible with --cherry-pick");
1719 revs->cherry_mark = 1;
1720 revs->limited = 1; /* needs limit_list() */
1721 } else if (!strcmp(arg, "--cherry-pick")) {
1722 if (revs->cherry_mark)
1723 die("--cherry-pick is incompatible with --cherry-mark");
1724 revs->cherry_pick = 1;
1726 } else if (!strcmp(arg, "--objects")) {
1727 revs->tag_objects = 1;
1728 revs->tree_objects = 1;
1729 revs->blob_objects = 1;
1730 } else if (!strcmp(arg, "--objects-edge")) {
1731 revs->tag_objects = 1;
1732 revs->tree_objects = 1;
1733 revs->blob_objects = 1;
1734 revs->edge_hint = 1;
1735 } else if (!strcmp(arg, "--verify-objects")) {
1736 revs->tag_objects = 1;
1737 revs->tree_objects = 1;
1738 revs->blob_objects = 1;
1739 revs->verify_objects = 1;
1740 } else if (!strcmp(arg, "--unpacked")) {
1742 } else if (!prefixcmp(arg, "--unpacked=")) {
1743 die("--unpacked=<packfile> no longer supported.");
1744 } else if (!strcmp(arg, "-r")) {
1746 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1747 } else if (!strcmp(arg, "-t")) {
1749 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1750 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1751 } else if (!strcmp(arg, "-m")) {
1752 revs->ignore_merges = 0;
1753 } else if (!strcmp(arg, "-c")) {
1755 revs->dense_combined_merges = 0;
1756 revs->combine_merges = 1;
1757 } else if (!strcmp(arg, "--cc")) {
1759 revs->dense_combined_merges = 1;
1760 revs->combine_merges = 1;
1761 } else if (!strcmp(arg, "-v")) {
1762 revs->verbose_header = 1;
1763 } else if (!strcmp(arg, "--pretty")) {
1764 revs->verbose_header = 1;
1765 revs->pretty_given = 1;
1766 get_commit_format(arg+8, revs);
1767 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1769 * Detached form ("--pretty X" as opposed to "--pretty=X")
1770 * not allowed, since the argument is optional.
1772 revs->verbose_header = 1;
1773 revs->pretty_given = 1;
1774 get_commit_format(arg+9, revs);
1775 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1776 revs->show_notes = 1;
1777 revs->show_notes_given = 1;
1778 revs->notes_opt.use_default_notes = 1;
1779 } else if (!strcmp(arg, "--show-signature")) {
1780 revs->show_signature = 1;
1781 } else if (!prefixcmp(arg, "--show-notes=") ||
1782 !prefixcmp(arg, "--notes=")) {
1783 struct strbuf buf = STRBUF_INIT;
1784 revs->show_notes = 1;
1785 revs->show_notes_given = 1;
1786 if (!prefixcmp(arg, "--show-notes")) {
1787 if (revs->notes_opt.use_default_notes < 0)
1788 revs->notes_opt.use_default_notes = 1;
1789 strbuf_addstr(&buf, arg+13);
1792 strbuf_addstr(&buf, arg+8);
1793 expand_notes_ref(&buf);
1794 string_list_append(&revs->notes_opt.extra_notes_refs,
1795 strbuf_detach(&buf, NULL));
1796 } else if (!strcmp(arg, "--no-notes")) {
1797 revs->show_notes = 0;
1798 revs->show_notes_given = 1;
1799 revs->notes_opt.use_default_notes = -1;
1800 /* we have been strdup'ing ourselves, so trick
1801 * string_list into free()ing strings */
1802 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1803 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1804 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1805 } else if (!strcmp(arg, "--standard-notes")) {
1806 revs->show_notes_given = 1;
1807 revs->notes_opt.use_default_notes = 1;
1808 } else if (!strcmp(arg, "--no-standard-notes")) {
1809 revs->notes_opt.use_default_notes = 0;
1810 } else if (!strcmp(arg, "--oneline")) {
1811 revs->verbose_header = 1;
1812 get_commit_format("oneline", revs);
1813 revs->pretty_given = 1;
1814 revs->abbrev_commit = 1;
1815 } else if (!strcmp(arg, "--graph")) {
1816 revs->topo_order = 1;
1817 revs->rewrite_parents = 1;
1818 revs->graph = graph_init(revs);
1819 } else if (!strcmp(arg, "--root")) {
1820 revs->show_root_diff = 1;
1821 } else if (!strcmp(arg, "--no-commit-id")) {
1822 revs->no_commit_id = 1;
1823 } else if (!strcmp(arg, "--always")) {
1824 revs->always_show_header = 1;
1825 } else if (!strcmp(arg, "--no-abbrev")) {
1827 } else if (!strcmp(arg, "--abbrev")) {
1828 revs->abbrev = DEFAULT_ABBREV;
1829 } else if (!prefixcmp(arg, "--abbrev=")) {
1830 revs->abbrev = strtoul(arg + 9, NULL, 10);
1831 if (revs->abbrev < MINIMUM_ABBREV)
1832 revs->abbrev = MINIMUM_ABBREV;
1833 else if (revs->abbrev > 40)
1835 } else if (!strcmp(arg, "--abbrev-commit")) {
1836 revs->abbrev_commit = 1;
1837 revs->abbrev_commit_given = 1;
1838 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1839 revs->abbrev_commit = 0;
1840 } else if (!strcmp(arg, "--full-diff")) {
1842 revs->full_diff = 1;
1843 } else if (!strcmp(arg, "--full-history")) {
1844 revs->simplify_history = 0;
1845 } else if (!strcmp(arg, "--relative-date")) {
1846 revs->date_mode = DATE_RELATIVE;
1847 revs->date_mode_explicit = 1;
1848 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1849 revs->date_mode = parse_date_format(optarg);
1850 revs->date_mode_explicit = 1;
1852 } else if (!strcmp(arg, "--log-size")) {
1853 revs->show_log_size = 1;
1856 * Grepping the commit log
1858 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1859 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1861 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1862 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1864 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
1865 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
1867 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1868 add_message_grep(revs, optarg);
1870 } else if (!strcmp(arg, "--grep-debug")) {
1871 revs->grep_filter.debug = 1;
1872 } else if (!strcmp(arg, "--basic-regexp")) {
1873 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter);
1874 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1875 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter);
1876 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1877 revs->grep_filter.regflags |= REG_ICASE;
1878 DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
1879 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1880 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter);
1881 } else if (!strcmp(arg, "--perl-regexp")) {
1882 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
1883 } else if (!strcmp(arg, "--all-match")) {
1884 revs->grep_filter.all_match = 1;
1885 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1886 if (strcmp(optarg, "none"))
1887 git_log_output_encoding = xstrdup(optarg);
1889 git_log_output_encoding = "";
1891 } else if (!strcmp(arg, "--reverse")) {
1893 } else if (!strcmp(arg, "--children")) {
1894 revs->children.name = "children";
1896 } else if (!strcmp(arg, "--ignore-missing")) {
1897 revs->ignore_missing = 1;
1899 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1901 unkv[(*unkc)++] = arg;
1908 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1909 const struct option *options,
1910 const char * const usagestr[])
1912 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1913 &ctx->cpidx, ctx->out);
1915 error("unknown option `%s'", ctx->argv[0]);
1916 usage_with_options(usagestr, options);
1922 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1924 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
1927 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1929 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
1932 static int handle_revision_pseudo_opt(const char *submodule,
1933 struct rev_info *revs,
1934 int argc, const char **argv, int *flags)
1936 const char *arg = argv[0];
1943 * Commands like "git shortlog" will not accept the options below
1944 * unless parse_revision_opt queues them (as opposed to erroring
1947 * When implementing your new pseudo-option, remember to
1948 * register it in the list at the top of handle_revision_opt.
1950 if (!strcmp(arg, "--all")) {
1951 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
1952 handle_refs(submodule, revs, *flags, head_ref_submodule);
1953 } else if (!strcmp(arg, "--branches")) {
1954 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
1955 } else if (!strcmp(arg, "--bisect")) {
1956 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
1957 handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
1959 } else if (!strcmp(arg, "--tags")) {
1960 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
1961 } else if (!strcmp(arg, "--remotes")) {
1962 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
1963 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
1964 struct all_refs_cb cb;
1965 init_all_refs_cb(&cb, revs, *flags);
1966 for_each_glob_ref(handle_one_ref, optarg, &cb);
1968 } else if (!prefixcmp(arg, "--branches=")) {
1969 struct all_refs_cb cb;
1970 init_all_refs_cb(&cb, revs, *flags);
1971 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1972 } else if (!prefixcmp(arg, "--tags=")) {
1973 struct all_refs_cb cb;
1974 init_all_refs_cb(&cb, revs, *flags);
1975 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1976 } else if (!prefixcmp(arg, "--remotes=")) {
1977 struct all_refs_cb cb;
1978 init_all_refs_cb(&cb, revs, *flags);
1979 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1980 } else if (!strcmp(arg, "--reflog")) {
1981 handle_reflog(revs, *flags);
1982 } else if (!strcmp(arg, "--not")) {
1983 *flags ^= UNINTERESTING | BOTTOM;
1984 } else if (!strcmp(arg, "--no-walk")) {
1985 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
1986 } else if (!prefixcmp(arg, "--no-walk=")) {
1988 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
1989 * not allowed, since the argument is optional.
1991 if (!strcmp(arg + 10, "sorted"))
1992 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
1993 else if (!strcmp(arg + 10, "unsorted"))
1994 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
1996 return error("invalid argument to --no-walk");
1997 } else if (!strcmp(arg, "--do-walk")) {
2007 * Parse revision information, filling in the "rev_info" structure,
2008 * and removing the used arguments from the argument list.
2010 * Returns the number of arguments left that weren't recognized
2011 * (which are also moved to the head of the argument list)
2013 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2015 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2016 struct cmdline_pathspec prune_data;
2017 const char *submodule = NULL;
2019 memset(&prune_data, 0, sizeof(prune_data));
2021 submodule = opt->submodule;
2023 /* First, search for "--" */
2024 if (opt && opt->assume_dashdash) {
2028 for (i = 1; i < argc; i++) {
2029 const char *arg = argv[i];
2030 if (strcmp(arg, "--"))
2035 append_prune_data(&prune_data, argv + i + 1);
2041 /* Second, deal with arguments and options */
2043 revarg_opt = opt ? opt->revarg_opt : 0;
2045 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2046 read_from_stdin = 0;
2047 for (left = i = 1; i < argc; i++) {
2048 const char *arg = argv[i];
2052 opts = handle_revision_pseudo_opt(submodule,
2053 revs, argc - i, argv + i,
2060 if (!strcmp(arg, "--stdin")) {
2061 if (revs->disable_stdin) {
2065 if (read_from_stdin++)
2066 die("--stdin given twice?");
2067 read_revisions_from_stdin(revs, &prune_data);
2071 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2082 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2084 if (seen_dashdash || *arg == '^')
2085 die("bad revision '%s'", arg);
2087 /* If we didn't have a "--":
2088 * (1) all filenames must exist;
2089 * (2) all rev-args must not be interpretable
2090 * as a valid filename.
2091 * but the latter we have checked in the main loop.
2093 for (j = i; j < argc; j++)
2094 verify_filename(revs->prefix, argv[j], j == i);
2096 append_prune_data(&prune_data, argv + i);
2103 if (prune_data.nr) {
2105 * If we need to introduce the magic "a lone ':' means no
2106 * pathspec whatsoever", here is the place to do so.
2108 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2109 * prune_data.nr = 0;
2110 * prune_data.alloc = 0;
2111 * free(prune_data.path);
2112 * prune_data.path = NULL;
2114 * terminate prune_data.alloc with NULL and
2115 * call init_pathspec() to set revs->prune_data here.
2118 ALLOC_GROW(prune_data.path, prune_data.nr+1, prune_data.alloc);
2119 prune_data.path[prune_data.nr++] = NULL;
2120 init_pathspec(&revs->prune_data,
2121 get_pathspec(revs->prefix, prune_data.path));
2124 if (revs->def == NULL)
2125 revs->def = opt ? opt->def : NULL;
2126 if (opt && opt->tweak)
2127 opt->tweak(revs, opt);
2128 if (revs->show_merge)
2129 prepare_show_merge(revs);
2130 if (revs->def && !revs->pending.nr && !got_rev_arg) {
2131 unsigned char sha1[20];
2132 struct object *object;
2133 struct object_context oc;
2134 if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2135 die("bad default revision '%s'", revs->def);
2136 object = get_reference(revs, revs->def, sha1, 0);
2137 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2140 /* Did the user ask for any diff output? Run the diff! */
2141 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2144 /* Pickaxe, diff-filter and rename following need diffs */
2145 if (revs->diffopt.pickaxe ||
2146 revs->diffopt.filter ||
2147 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2150 if (revs->topo_order)
2153 if (revs->prune_data.nr) {
2154 diff_tree_setup_paths(revs->prune_data.raw, &revs->pruning);
2155 /* Can't prune commits with rename following: the paths change.. */
2156 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2158 if (!revs->full_diff)
2159 diff_tree_setup_paths(revs->prune_data.raw, &revs->diffopt);
2161 if (revs->combine_merges)
2162 revs->ignore_merges = 0;
2163 revs->diffopt.abbrev = revs->abbrev;
2165 if (revs->line_level_traverse) {
2167 revs->topo_order = 1;
2170 diff_setup_done(&revs->diffopt);
2172 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2173 &revs->grep_filter);
2174 compile_grep_patterns(&revs->grep_filter);
2176 if (revs->reverse && revs->reflog_info)
2177 die("cannot combine --reverse with --walk-reflogs");
2178 if (revs->rewrite_parents && revs->children.name)
2179 die("cannot combine --parents and --children");
2182 * Limitations on the graph functionality
2184 if (revs->reverse && revs->graph)
2185 die("cannot combine --reverse with --graph");
2187 if (revs->reflog_info && revs->graph)
2188 die("cannot combine --walk-reflogs with --graph");
2189 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2190 die("cannot use --grep-reflog without --walk-reflogs");
2195 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2197 struct commit_list *l = xcalloc(1, sizeof(*l));
2200 l->next = add_decoration(&revs->children, &parent->object, l);
2203 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2205 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2206 struct commit_list **pp, *p;
2207 int surviving_parents;
2209 /* Examine existing parents while marking ones we have seen... */
2210 pp = &commit->parents;
2211 surviving_parents = 0;
2212 while ((p = *pp) != NULL) {
2213 struct commit *parent = p->item;
2214 if (parent->object.flags & TMP_MARK) {
2217 compact_treesame(revs, commit, surviving_parents);
2220 parent->object.flags |= TMP_MARK;
2221 surviving_parents++;
2224 /* clear the temporary mark */
2225 for (p = commit->parents; p; p = p->next) {
2226 p->item->object.flags &= ~TMP_MARK;
2228 /* no update_treesame() - removing duplicates can't affect TREESAME */
2229 return surviving_parents;
2232 struct merge_simplify_state {
2233 struct commit *simplified;
2236 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2238 struct merge_simplify_state *st;
2240 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2242 st = xcalloc(1, sizeof(*st));
2243 add_decoration(&revs->merge_simplification, &commit->object, st);
2248 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2250 struct commit_list *h = reduce_heads(commit->parents);
2251 int i = 0, marked = 0;
2252 struct commit_list *po, *pn;
2254 /* Want these for sanity-checking only */
2255 int orig_cnt = commit_list_count(commit->parents);
2256 int cnt = commit_list_count(h);
2259 * Not ready to remove items yet, just mark them for now, based
2260 * on the output of reduce_heads(). reduce_heads outputs the reduced
2261 * set in its original order, so this isn't too hard.
2263 po = commit->parents;
2266 if (pn && po->item == pn->item) {
2270 po->item->object.flags |= TMP_MARK;
2276 if (i != cnt || cnt+marked != orig_cnt)
2277 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2279 free_commit_list(h);
2284 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2286 struct commit_list *p;
2289 for (p = commit->parents; p; p = p->next) {
2290 struct commit *parent = p->item;
2291 if (!parent->parents && (parent->object.flags & TREESAME)) {
2292 parent->object.flags |= TMP_MARK;
2301 * Awkward naming - this means one parent we are TREESAME to.
2302 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2303 * empty tree). Better name suggestions?
2305 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2307 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2308 struct commit *unmarked = NULL, *marked = NULL;
2309 struct commit_list *p;
2312 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2313 if (ts->treesame[n]) {
2314 if (p->item->object.flags & TMP_MARK) {
2327 * If we are TREESAME to a marked-for-deletion parent, but not to any
2328 * unmarked parents, unmark the first TREESAME parent. This is the
2329 * parent that the default simplify_history==1 scan would have followed,
2330 * and it doesn't make sense to omit that path when asking for a
2331 * simplified full history. Retaining it improves the chances of
2332 * understanding odd missed merges that took an old version of a file.
2336 * I--------*X A modified the file, but mainline merge X used
2337 * \ / "-s ours", so took the version from I. X is
2338 * `-*A--' TREESAME to I and !TREESAME to A.
2340 * Default log from X would produce "I". Without this check,
2341 * --full-history --simplify-merges would produce "I-A-X", showing
2342 * the merge commit X and that it changed A, but not making clear that
2343 * it had just taken the I version. With this check, the topology above
2346 * Note that it is possible that the simplification chooses a different
2347 * TREESAME parent from the default, in which case this test doesn't
2348 * activate, and we _do_ drop the default parent. Example:
2350 * I------X A modified the file, but it was reverted in B,
2351 * \ / meaning mainline merge X is TREESAME to both
2354 * Default log would produce "I" by following the first parent;
2355 * --full-history --simplify-merges will produce "I-A-B". But this is a
2356 * reasonable result - it presents a logical full history leading from
2357 * I to X, and X is not an important merge.
2359 if (!unmarked && marked) {
2360 marked->object.flags &= ~TMP_MARK;
2367 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2369 struct commit_list **pp, *p;
2370 int nth_parent, removed = 0;
2372 pp = &commit->parents;
2374 while ((p = *pp) != NULL) {
2375 struct commit *parent = p->item;
2376 if (parent->object.flags & TMP_MARK) {
2377 parent->object.flags &= ~TMP_MARK;
2381 compact_treesame(revs, commit, nth_parent);
2388 /* Removing parents can only increase TREESAMEness */
2389 if (removed && !(commit->object.flags & TREESAME))
2390 update_treesame(revs, commit);
2395 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2397 struct commit_list *p;
2398 struct commit *parent;
2399 struct merge_simplify_state *st, *pst;
2402 st = locate_simplify_state(revs, commit);
2405 * Have we handled this one?
2411 * An UNINTERESTING commit simplifies to itself, so does a
2412 * root commit. We do not rewrite parents of such commit
2415 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2416 st->simplified = commit;
2421 * Do we know what commit all of our parents that matter
2422 * should be rewritten to? Otherwise we are not ready to
2423 * rewrite this one yet.
2425 for (cnt = 0, p = commit->parents; p; p = p->next) {
2426 pst = locate_simplify_state(revs, p->item);
2427 if (!pst->simplified) {
2428 tail = &commit_list_insert(p->item, tail)->next;
2431 if (revs->first_parent_only)
2435 tail = &commit_list_insert(commit, tail)->next;
2440 * Rewrite our list of parents. Note that this cannot
2441 * affect our TREESAME flags in any way - a commit is
2442 * always TREESAME to its simplification.
2444 for (p = commit->parents; p; p = p->next) {
2445 pst = locate_simplify_state(revs, p->item);
2446 p->item = pst->simplified;
2447 if (revs->first_parent_only)
2451 if (revs->first_parent_only)
2454 cnt = remove_duplicate_parents(revs, commit);
2457 * It is possible that we are a merge and one side branch
2458 * does not have any commit that touches the given paths;
2459 * in such a case, the immediate parent from that branch
2460 * will be rewritten to be the merge base.
2462 * o----X X: the commit we are looking at;
2463 * / / o: a commit that touches the paths;
2466 * Further, a merge of an independent branch that doesn't
2467 * touch the path will reduce to a treesame root parent:
2469 * ----o----X X: the commit we are looking at;
2470 * / o: a commit that touches the paths;
2471 * r r: a root commit not touching the paths
2473 * Detect and simplify both cases.
2476 int marked = mark_redundant_parents(revs, commit);
2477 marked += mark_treesame_root_parents(revs, commit);
2479 marked -= leave_one_treesame_to_parent(revs, commit);
2481 cnt = remove_marked_parents(revs, commit);
2485 * A commit simplifies to itself if it is a root, if it is
2486 * UNINTERESTING, if it touches the given paths, or if it is a
2487 * merge and its parents don't simplify to one relevant commit
2488 * (the first two cases are already handled at the beginning of
2491 * Otherwise, it simplifies to what its sole relevant parent
2495 (commit->object.flags & UNINTERESTING) ||
2496 !(commit->object.flags & TREESAME) ||
2497 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2498 st->simplified = commit;
2500 pst = locate_simplify_state(revs, parent);
2501 st->simplified = pst->simplified;
2506 static void simplify_merges(struct rev_info *revs)
2508 struct commit_list *list, *next;
2509 struct commit_list *yet_to_do, **tail;
2510 struct commit *commit;
2515 /* feed the list reversed */
2517 for (list = revs->commits; list; list = next) {
2518 commit = list->item;
2521 * Do not free(list) here yet; the original list
2522 * is used later in this function.
2524 commit_list_insert(commit, &yet_to_do);
2531 commit = list->item;
2535 tail = simplify_one(revs, commit, tail);
2539 /* clean up the result, removing the simplified ones */
2540 list = revs->commits;
2541 revs->commits = NULL;
2542 tail = &revs->commits;
2544 struct merge_simplify_state *st;
2546 commit = list->item;
2550 st = locate_simplify_state(revs, commit);
2551 if (st->simplified == commit)
2552 tail = &commit_list_insert(commit, tail)->next;
2556 static void set_children(struct rev_info *revs)
2558 struct commit_list *l;
2559 for (l = revs->commits; l; l = l->next) {
2560 struct commit *commit = l->item;
2561 struct commit_list *p;
2563 for (p = commit->parents; p; p = p->next)
2564 add_child(revs, p->item, commit);
2568 void reset_revision_walk(void)
2570 clear_object_flags(SEEN | ADDED | SHOWN);
2573 int prepare_revision_walk(struct rev_info *revs)
2575 int nr = revs->pending.nr;
2576 struct object_array_entry *e, *list;
2577 struct commit_list **next = &revs->commits;
2579 e = list = revs->pending.objects;
2580 revs->pending.nr = 0;
2581 revs->pending.alloc = 0;
2582 revs->pending.objects = NULL;
2584 struct commit *commit = handle_commit(revs, e->item, e->name);
2586 if (!(commit->object.flags & SEEN)) {
2587 commit->object.flags |= SEEN;
2588 next = commit_list_append(commit, next);
2593 if (!revs->leak_pending)
2596 /* Signal whether we need per-parent treesame decoration */
2597 if (revs->simplify_merges ||
2598 (revs->limited && limiting_can_increase_treesame(revs)))
2599 revs->treesame.name = "treesame";
2601 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2602 commit_list_sort_by_date(&revs->commits);
2606 if (limit_list(revs) < 0)
2608 if (revs->topo_order)
2609 sort_in_topological_order(&revs->commits, revs->lifo);
2610 if (revs->line_level_traverse)
2611 line_log_filter(revs);
2612 if (revs->simplify_merges)
2613 simplify_merges(revs);
2614 if (revs->children.name)
2619 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2621 struct commit_list *cache = NULL;
2624 struct commit *p = *pp;
2626 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2627 return rewrite_one_error;
2628 if (p->object.flags & UNINTERESTING)
2629 return rewrite_one_ok;
2630 if (!(p->object.flags & TREESAME))
2631 return rewrite_one_ok;
2633 return rewrite_one_noparents;
2634 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2635 return rewrite_one_ok;
2640 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2641 rewrite_parent_fn_t rewrite_parent)
2643 struct commit_list **pp = &commit->parents;
2645 struct commit_list *parent = *pp;
2646 switch (rewrite_parent(revs, &parent->item)) {
2647 case rewrite_one_ok:
2649 case rewrite_one_noparents:
2652 case rewrite_one_error:
2657 remove_duplicate_parents(revs, commit);
2661 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2663 char *person, *endp;
2664 size_t len, namelen, maillen;
2667 struct ident_split ident;
2669 person = strstr(buf->buf, what);
2673 person += strlen(what);
2674 endp = strchr(person, '\n');
2678 len = endp - person;
2680 if (split_ident_line(&ident, person, len))
2683 mail = ident.mail_begin;
2684 maillen = ident.mail_end - ident.mail_begin;
2685 name = ident.name_begin;
2686 namelen = ident.name_end - ident.name_begin;
2688 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2689 struct strbuf namemail = STRBUF_INIT;
2691 strbuf_addf(&namemail, "%.*s <%.*s>",
2692 (int)namelen, name, (int)maillen, mail);
2694 strbuf_splice(buf, ident.name_begin - buf->buf,
2695 ident.mail_end - ident.name_begin + 1,
2696 namemail.buf, namemail.len);
2698 strbuf_release(&namemail);
2706 static int commit_match(struct commit *commit, struct rev_info *opt)
2709 const char *encoding;
2711 struct strbuf buf = STRBUF_INIT;
2713 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2716 /* Prepend "fake" headers as needed */
2717 if (opt->grep_filter.use_reflog_filter) {
2718 strbuf_addstr(&buf, "reflog ");
2719 get_reflog_message(&buf, opt->reflog_info);
2720 strbuf_addch(&buf, '\n');
2724 * We grep in the user's output encoding, under the assumption that it
2725 * is the encoding they are most likely to write their grep pattern
2726 * for. In addition, it means we will match the "notes" encoding below,
2727 * so we will not end up with a buffer that has two different encodings
2730 encoding = get_log_output_encoding();
2731 message = logmsg_reencode(commit, NULL, encoding);
2733 /* Copy the commit to temporary if we are using "fake" headers */
2735 strbuf_addstr(&buf, message);
2737 if (opt->grep_filter.header_list && opt->mailmap) {
2739 strbuf_addstr(&buf, message);
2741 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2742 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2745 /* Append "fake" message parts as needed */
2746 if (opt->show_notes) {
2748 strbuf_addstr(&buf, message);
2749 format_display_notes(commit->object.sha1, &buf, encoding, 1);
2752 /* Find either in the original commit message, or in the temporary */
2754 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2756 retval = grep_buffer(&opt->grep_filter,
2757 message, strlen(message));
2758 strbuf_release(&buf);
2759 logmsg_free(message, commit);
2763 static inline int want_ancestry(struct rev_info *revs)
2765 return (revs->rewrite_parents || revs->children.name);
2768 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2770 if (commit->object.flags & SHOWN)
2771 return commit_ignore;
2772 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2773 return commit_ignore;
2776 if (commit->object.flags & UNINTERESTING)
2777 return commit_ignore;
2778 if (revs->min_age != -1 && (commit->date > revs->min_age))
2779 return commit_ignore;
2780 if (revs->min_parents || (revs->max_parents >= 0)) {
2781 int n = commit_list_count(commit->parents);
2782 if ((n < revs->min_parents) ||
2783 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2784 return commit_ignore;
2786 if (!commit_match(commit, revs))
2787 return commit_ignore;
2788 if (revs->prune && revs->dense) {
2789 /* Commit without changes? */
2790 if (commit->object.flags & TREESAME) {
2792 struct commit_list *p;
2793 /* drop merges unless we want parenthood */
2794 if (!want_ancestry(revs))
2795 return commit_ignore;
2797 * If we want ancestry, then need to keep any merges
2798 * between relevant commits to tie together topology.
2799 * For consistency with TREESAME and simplification
2800 * use "relevant" here rather than just INTERESTING,
2801 * to treat bottom commit(s) as part of the topology.
2803 for (n = 0, p = commit->parents; p; p = p->next)
2804 if (relevant_commit(p->item))
2807 return commit_ignore;
2813 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
2815 enum commit_action action = get_commit_action(revs, commit);
2817 if (action == commit_show &&
2819 revs->prune && revs->dense && want_ancestry(revs)) {
2820 if (rewrite_parents(revs, commit, rewrite_one) < 0)
2821 return commit_error;
2826 static struct commit *get_revision_1(struct rev_info *revs)
2832 struct commit_list *entry = revs->commits;
2833 struct commit *commit = entry->item;
2835 revs->commits = entry->next;
2838 if (revs->reflog_info) {
2839 fake_reflog_parent(revs->reflog_info, commit);
2840 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
2844 * If we haven't done the list limiting, we need to look at
2845 * the parents here. We also need to do the date-based limiting
2846 * that we'd otherwise have done in limit_list().
2848 if (!revs->limited) {
2849 if (revs->max_age != -1 &&
2850 (commit->date < revs->max_age))
2852 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
2853 die("Failed to traverse parents of commit %s",
2854 sha1_to_hex(commit->object.sha1));
2857 switch (simplify_commit(revs, commit)) {
2861 die("Failed to simplify parents of commit %s",
2862 sha1_to_hex(commit->object.sha1));
2866 } while (revs->commits);
2871 * Return true for entries that have not yet been shown. (This is an
2872 * object_array_each_func_t.)
2874 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
2876 return !(entry->item->flags & SHOWN);
2880 * If array is on the verge of a realloc, garbage-collect any entries
2881 * that have already been shown to try to free up some space.
2883 static void gc_boundary(struct object_array *array)
2885 if (array->nr == array->alloc)
2886 object_array_filter(array, entry_unshown, NULL);
2889 static void create_boundary_commit_list(struct rev_info *revs)
2893 struct object_array *array = &revs->boundary_commits;
2894 struct object_array_entry *objects = array->objects;
2897 * If revs->commits is non-NULL at this point, an error occurred in
2898 * get_revision_1(). Ignore the error and continue printing the
2899 * boundary commits anyway. (This is what the code has always
2902 if (revs->commits) {
2903 free_commit_list(revs->commits);
2904 revs->commits = NULL;
2908 * Put all of the actual boundary commits from revs->boundary_commits
2909 * into revs->commits
2911 for (i = 0; i < array->nr; i++) {
2912 c = (struct commit *)(objects[i].item);
2915 if (!(c->object.flags & CHILD_SHOWN))
2917 if (c->object.flags & (SHOWN | BOUNDARY))
2919 c->object.flags |= BOUNDARY;
2920 commit_list_insert(c, &revs->commits);
2924 * If revs->topo_order is set, sort the boundary commits
2925 * in topological order
2927 sort_in_topological_order(&revs->commits, revs->lifo);
2930 static struct commit *get_revision_internal(struct rev_info *revs)
2932 struct commit *c = NULL;
2933 struct commit_list *l;
2935 if (revs->boundary == 2) {
2937 * All of the normal commits have already been returned,
2938 * and we are now returning boundary commits.
2939 * create_boundary_commit_list() has populated
2940 * revs->commits with the remaining commits to return.
2942 c = pop_commit(&revs->commits);
2944 c->object.flags |= SHOWN;
2949 * If our max_count counter has reached zero, then we are done. We
2950 * don't simply return NULL because we still might need to show
2951 * boundary commits. But we want to avoid calling get_revision_1, which
2952 * might do a considerable amount of work finding the next commit only
2953 * for us to throw it away.
2955 * If it is non-zero, then either we don't have a max_count at all
2956 * (-1), or it is still counting, in which case we decrement.
2958 if (revs->max_count) {
2959 c = get_revision_1(revs);
2961 while (0 < revs->skip_count) {
2963 c = get_revision_1(revs);
2969 if (revs->max_count > 0)
2974 c->object.flags |= SHOWN;
2976 if (!revs->boundary) {
2982 * get_revision_1() runs out the commits, and
2983 * we are done computing the boundaries.
2984 * switch to boundary commits output mode.
2989 * Update revs->commits to contain the list of
2992 create_boundary_commit_list(revs);
2994 return get_revision_internal(revs);
2998 * boundary commits are the commits that are parents of the
2999 * ones we got from get_revision_1() but they themselves are
3000 * not returned from get_revision_1(). Before returning
3001 * 'c', we need to mark its parents that they could be boundaries.
3004 for (l = c->parents; l; l = l->next) {
3006 p = &(l->item->object);
3007 if (p->flags & (CHILD_SHOWN | SHOWN))
3009 p->flags |= CHILD_SHOWN;
3010 gc_boundary(&revs->boundary_commits);
3011 add_object_array(p, NULL, &revs->boundary_commits);
3017 struct commit *get_revision(struct rev_info *revs)
3020 struct commit_list *reversed;
3022 if (revs->reverse) {
3024 while ((c = get_revision_internal(revs))) {
3025 commit_list_insert(c, &reversed);
3027 revs->commits = reversed;
3029 revs->reverse_output_stage = 1;
3032 if (revs->reverse_output_stage)
3033 return pop_commit(&revs->commits);
3035 c = get_revision_internal(revs);
3036 if (c && revs->graph)
3037 graph_update(revs->graph, c);
3041 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3043 if (commit->object.flags & BOUNDARY)
3045 else if (commit->object.flags & UNINTERESTING)
3047 else if (commit->object.flags & PATCHSAME)
3049 else if (!revs || revs->left_right) {
3050 if (commit->object.flags & SYMMETRIC_LEFT)
3054 } else if (revs->graph)
3056 else if (revs->cherry_mark)
3061 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3063 char *mark = get_revision_mark(revs, commit);
3066 fputs(mark, stdout);