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 char *path_name(const struct name_path *path, const char *name)
26 const struct name_path *p;
28 int nlen = strlen(name);
31 for (p = path; p; p = p->up) {
33 len += p->elem_len + 1;
36 m = n + len - (nlen + 1);
38 for (p = path; p; p = p->up) {
41 memcpy(m, p->elem, p->elem_len);
48 static int show_path_component_truncated(FILE *out, const char *name, int len)
51 for (cnt = 0; cnt < len; cnt++) {
53 if (!ch || ch == '\n')
60 static int show_path_truncated(FILE *out, const struct name_path *path)
66 emitted = show_path_truncated(out, path->up);
71 ours = show_path_component_truncated(out, path->elem, path->elem_len);
74 return ours || emitted;
77 void show_object_with_name(FILE *out, struct object *obj,
78 const struct name_path *path, const char *component)
80 struct name_path leaf;
81 leaf.up = (struct name_path *)path;
82 leaf.elem = component;
83 leaf.elem_len = strlen(component);
85 fprintf(out, "%s ", sha1_to_hex(obj->sha1));
86 show_path_truncated(out, &leaf);
90 static void mark_blob_uninteresting(struct blob *blob)
94 if (blob->object.flags & UNINTERESTING)
96 blob->object.flags |= UNINTERESTING;
99 static void mark_tree_contents_uninteresting(struct tree *tree)
101 struct tree_desc desc;
102 struct name_entry entry;
103 struct object *obj = &tree->object;
105 if (!has_sha1_file(obj->sha1))
107 if (parse_tree(tree) < 0)
108 die("bad tree %s", sha1_to_hex(obj->sha1));
110 init_tree_desc(&desc, tree->buffer, tree->size);
111 while (tree_entry(&desc, &entry)) {
112 switch (object_type(entry.mode)) {
114 mark_tree_uninteresting(lookup_tree(entry.sha1));
117 mark_blob_uninteresting(lookup_blob(entry.sha1));
120 /* Subproject commit - not in this repository */
126 * We don't care about the tree any more
127 * after it has been marked uninteresting.
129 free_tree_buffer(tree);
132 void mark_tree_uninteresting(struct tree *tree)
140 if (obj->flags & UNINTERESTING)
142 obj->flags |= UNINTERESTING;
143 mark_tree_contents_uninteresting(tree);
146 void mark_parents_uninteresting(struct commit *commit)
148 struct commit_list *parents = NULL, *l;
150 for (l = commit->parents; l; l = l->next)
151 commit_list_insert(l->item, &parents);
154 struct commit *commit = parents->item;
156 parents = parents->next;
161 * A missing commit is ok iff its parent is marked
164 * We just mark such a thing parsed, so that when
165 * it is popped next time around, we won't be trying
166 * to parse it and get an error.
168 if (!has_sha1_file(commit->object.sha1))
169 commit->object.parsed = 1;
171 if (commit->object.flags & UNINTERESTING)
174 commit->object.flags |= UNINTERESTING;
177 * Normally we haven't parsed the parent
178 * yet, so we won't have a parent of a parent
179 * here. However, it may turn out that we've
180 * reached this commit some other way (where it
181 * wasn't uninteresting), in which case we need
182 * to mark its parents recursively too..
184 if (!commit->parents)
187 for (l = commit->parents->next; l; l = l->next)
188 commit_list_insert(l->item, &parents);
189 commit = commit->parents->item;
194 static void add_pending_object_with_path(struct rev_info *revs,
196 const char *name, unsigned mode,
201 if (revs->no_walk && (obj->flags & UNINTERESTING))
203 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
204 struct strbuf buf = STRBUF_INIT;
205 int len = interpret_branch_name(name, 0, &buf);
208 if (0 < len && name[len] && buf.len)
209 strbuf_addstr(&buf, name + len);
210 st = add_reflog_for_walk(revs->reflog_info,
211 (struct commit *)obj,
212 buf.buf[0] ? buf.buf: name);
213 strbuf_release(&buf);
217 add_object_array_with_path(obj, name, &revs->pending, mode, path);
220 static void add_pending_object_with_mode(struct rev_info *revs,
222 const char *name, unsigned mode)
224 add_pending_object_with_path(revs, obj, name, mode, NULL);
227 void add_pending_object(struct rev_info *revs,
228 struct object *obj, const char *name)
230 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
233 void add_head_to_pending(struct rev_info *revs)
235 unsigned char sha1[20];
237 if (get_sha1("HEAD", sha1))
239 obj = parse_object(sha1);
242 add_pending_object(revs, obj, "HEAD");
245 static struct object *get_reference(struct rev_info *revs, const char *name,
246 const unsigned char *sha1,
249 struct object *object;
251 object = parse_object(sha1);
253 if (revs->ignore_missing)
255 die("bad object %s", name);
257 object->flags |= flags;
261 void add_pending_sha1(struct rev_info *revs, const char *name,
262 const unsigned char *sha1, unsigned int flags)
264 struct object *object = get_reference(revs, name, sha1, flags);
265 add_pending_object(revs, object, name);
268 static struct commit *handle_commit(struct rev_info *revs,
269 struct object_array_entry *entry)
271 struct object *object = entry->item;
272 const char *name = entry->name;
273 const char *path = entry->path;
274 unsigned int mode = entry->mode;
275 unsigned long flags = object->flags;
278 * Tag object? Look what it points to..
280 while (object->type == OBJ_TAG) {
281 struct tag *tag = (struct tag *) object;
282 if (revs->tag_objects && !(flags & UNINTERESTING))
283 add_pending_object(revs, object, tag->tag);
286 object = parse_object(tag->tagged->sha1);
288 if (flags & UNINTERESTING)
290 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
292 object->flags |= flags;
294 * We'll handle the tagged object by looping or dropping
295 * through to the non-tag handlers below. Do not
296 * propagate data from the tag's pending entry.
304 * Commit object? Just return it, we'll do all the complex
307 if (object->type == OBJ_COMMIT) {
308 struct commit *commit = (struct commit *)object;
309 if (parse_commit(commit) < 0)
310 die("unable to parse commit %s", name);
311 if (flags & UNINTERESTING) {
312 mark_parents_uninteresting(commit);
315 if (revs->show_source && !commit->util)
316 commit->util = xstrdup(name);
321 * Tree object? Either mark it uninteresting, or add it
322 * to the list of objects to look at later..
324 if (object->type == OBJ_TREE) {
325 struct tree *tree = (struct tree *)object;
326 if (!revs->tree_objects)
328 if (flags & UNINTERESTING) {
329 mark_tree_contents_uninteresting(tree);
332 add_pending_object_with_path(revs, object, name, mode, path);
337 * Blob object? You know the drill by now..
339 if (object->type == OBJ_BLOB) {
340 if (!revs->blob_objects)
342 if (flags & UNINTERESTING)
344 add_pending_object_with_path(revs, object, name, mode, path);
347 die("%s is unknown object", name);
350 static int everybody_uninteresting(struct commit_list *orig,
351 struct commit **interesting_cache)
353 struct commit_list *list = orig;
355 if (*interesting_cache) {
356 struct commit *commit = *interesting_cache;
357 if (!(commit->object.flags & UNINTERESTING))
362 struct commit *commit = list->item;
364 if (commit->object.flags & UNINTERESTING)
367 *interesting_cache = commit;
374 * A definition of "relevant" commit that we can use to simplify limited graphs
375 * by eliminating side branches.
377 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
378 * in our list), or that is a specified BOTTOM commit. Then after computing
379 * a limited list, during processing we can generally ignore boundary merges
380 * coming from outside the graph, (ie from irrelevant parents), and treat
381 * those merges as if they were single-parent. TREESAME is defined to consider
382 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
383 * we don't care if we were !TREESAME to non-graph parents.
385 * Treating bottom commits as relevant ensures that a limited graph's
386 * connection to the actual bottom commit is not viewed as a side branch, but
387 * treated as part of the graph. For example:
389 * ....Z...A---X---o---o---B
393 * When computing "A..B", the A-X connection is at least as important as
394 * Y-X, despite A being flagged UNINTERESTING.
396 * And when computing --ancestry-path "A..B", the A-X connection is more
397 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
399 static inline int relevant_commit(struct commit *commit)
401 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
405 * Return a single relevant commit from a parent list. If we are a TREESAME
406 * commit, and this selects one of our parents, then we can safely simplify to
409 static struct commit *one_relevant_parent(const struct rev_info *revs,
410 struct commit_list *orig)
412 struct commit_list *list = orig;
413 struct commit *relevant = NULL;
419 * For 1-parent commits, or if first-parent-only, then return that
420 * first parent (even if not "relevant" by the above definition).
421 * TREESAME will have been set purely on that parent.
423 if (revs->first_parent_only || !orig->next)
427 * For multi-parent commits, identify a sole relevant parent, if any.
428 * If we have only one relevant parent, then TREESAME will be set purely
429 * with regard to that parent, and we can simplify accordingly.
431 * If we have more than one relevant parent, or no relevant parents
432 * (and multiple irrelevant ones), then we can't select a parent here
436 struct commit *commit = list->item;
438 if (relevant_commit(commit)) {
448 * The goal is to get REV_TREE_NEW as the result only if the
449 * diff consists of all '+' (and no other changes), REV_TREE_OLD
450 * if the whole diff is removal of old data, and otherwise
451 * REV_TREE_DIFFERENT (of course if the trees are the same we
452 * want REV_TREE_SAME).
453 * That means that once we get to REV_TREE_DIFFERENT, we do not
454 * have to look any further.
456 static int tree_difference = REV_TREE_SAME;
458 static void file_add_remove(struct diff_options *options,
459 int addremove, unsigned mode,
460 const unsigned char *sha1,
462 const char *fullpath, unsigned dirty_submodule)
464 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
466 tree_difference |= diff;
467 if (tree_difference == REV_TREE_DIFFERENT)
468 DIFF_OPT_SET(options, HAS_CHANGES);
471 static void file_change(struct diff_options *options,
472 unsigned old_mode, unsigned new_mode,
473 const unsigned char *old_sha1,
474 const unsigned char *new_sha1,
475 int old_sha1_valid, int new_sha1_valid,
476 const char *fullpath,
477 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
479 tree_difference = REV_TREE_DIFFERENT;
480 DIFF_OPT_SET(options, HAS_CHANGES);
483 static int rev_compare_tree(struct rev_info *revs,
484 struct commit *parent, struct commit *commit)
486 struct tree *t1 = parent->tree;
487 struct tree *t2 = commit->tree;
494 if (revs->simplify_by_decoration) {
496 * If we are simplifying by decoration, then the commit
497 * is worth showing if it has a tag pointing at it.
499 if (get_name_decoration(&commit->object))
500 return REV_TREE_DIFFERENT;
502 * A commit that is not pointed by a tag is uninteresting
503 * if we are not limited by path. This means that you will
504 * see the usual "commits that touch the paths" plus any
505 * tagged commit by specifying both --simplify-by-decoration
508 if (!revs->prune_data.nr)
509 return REV_TREE_SAME;
512 tree_difference = REV_TREE_SAME;
513 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
514 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
516 return REV_TREE_DIFFERENT;
517 return tree_difference;
520 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
523 struct tree *t1 = commit->tree;
528 tree_difference = REV_TREE_SAME;
529 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
530 retval = diff_tree_sha1(NULL, t1->object.sha1, "", &revs->pruning);
532 return retval >= 0 && (tree_difference == REV_TREE_SAME);
535 struct treesame_state {
536 unsigned int nparents;
537 unsigned char treesame[FLEX_ARRAY];
540 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
542 unsigned n = commit_list_count(commit->parents);
543 struct treesame_state *st = xcalloc(1, sizeof(*st) + n);
545 add_decoration(&revs->treesame, &commit->object, st);
550 * Must be called immediately after removing the nth_parent from a commit's
551 * parent list, if we are maintaining the per-parent treesame[] decoration.
552 * This does not recalculate the master TREESAME flag - update_treesame()
553 * should be called to update it after a sequence of treesame[] modifications
554 * that may have affected it.
556 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
558 struct treesame_state *st;
561 if (!commit->parents) {
563 * Have just removed the only parent from a non-merge.
564 * Different handling, as we lack decoration.
567 die("compact_treesame %u", nth_parent);
568 old_same = !!(commit->object.flags & TREESAME);
569 if (rev_same_tree_as_empty(revs, commit))
570 commit->object.flags |= TREESAME;
572 commit->object.flags &= ~TREESAME;
576 st = lookup_decoration(&revs->treesame, &commit->object);
577 if (!st || nth_parent >= st->nparents)
578 die("compact_treesame %u", nth_parent);
580 old_same = st->treesame[nth_parent];
581 memmove(st->treesame + nth_parent,
582 st->treesame + nth_parent + 1,
583 st->nparents - nth_parent - 1);
586 * If we've just become a non-merge commit, update TREESAME
587 * immediately, and remove the no-longer-needed decoration.
588 * If still a merge, defer update until update_treesame().
590 if (--st->nparents == 1) {
591 if (commit->parents->next)
592 die("compact_treesame parents mismatch");
593 if (st->treesame[0] && revs->dense)
594 commit->object.flags |= TREESAME;
596 commit->object.flags &= ~TREESAME;
597 free(add_decoration(&revs->treesame, &commit->object, NULL));
603 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
605 if (commit->parents && commit->parents->next) {
607 struct treesame_state *st;
608 struct commit_list *p;
609 unsigned relevant_parents;
610 unsigned relevant_change, irrelevant_change;
612 st = lookup_decoration(&revs->treesame, &commit->object);
614 die("update_treesame %s", sha1_to_hex(commit->object.sha1));
615 relevant_parents = 0;
616 relevant_change = irrelevant_change = 0;
617 for (p = commit->parents, n = 0; p; n++, p = p->next) {
618 if (relevant_commit(p->item)) {
619 relevant_change |= !st->treesame[n];
622 irrelevant_change |= !st->treesame[n];
624 if (relevant_parents ? relevant_change : irrelevant_change)
625 commit->object.flags &= ~TREESAME;
627 commit->object.flags |= TREESAME;
630 return commit->object.flags & TREESAME;
633 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
636 * TREESAME is irrelevant unless prune && dense;
637 * if simplify_history is set, we can't have a mixture of TREESAME and
638 * !TREESAME INTERESTING parents (and we don't have treesame[]
639 * decoration anyway);
640 * if first_parent_only is set, then the TREESAME flag is locked
641 * against the first parent (and again we lack treesame[] decoration).
643 return revs->prune && revs->dense &&
644 !revs->simplify_history &&
645 !revs->first_parent_only;
648 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
650 struct commit_list **pp, *parent;
651 struct treesame_state *ts = NULL;
652 int relevant_change = 0, irrelevant_change = 0;
653 int relevant_parents, nth_parent;
656 * If we don't do pruning, everything is interesting
664 if (!commit->parents) {
665 if (rev_same_tree_as_empty(revs, commit))
666 commit->object.flags |= TREESAME;
671 * Normal non-merge commit? If we don't want to make the
672 * history dense, we consider it always to be a change..
674 if (!revs->dense && !commit->parents->next)
677 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
678 (parent = *pp) != NULL;
679 pp = &parent->next, nth_parent++) {
680 struct commit *p = parent->item;
681 if (relevant_commit(p))
684 if (nth_parent == 1) {
686 * This our second loop iteration - so we now know
687 * we're dealing with a merge.
689 * Do not compare with later parents when we care only about
690 * the first parent chain, in order to avoid derailing the
691 * traversal to follow a side branch that brought everything
692 * in the path we are limited to by the pathspec.
694 if (revs->first_parent_only)
697 * If this will remain a potentially-simplifiable
698 * merge, remember per-parent treesame if needed.
699 * Initialise the array with the comparison from our
702 if (revs->treesame.name &&
703 !revs->simplify_history &&
704 !(commit->object.flags & UNINTERESTING)) {
705 ts = initialise_treesame(revs, commit);
706 if (!(irrelevant_change || relevant_change))
710 if (parse_commit(p) < 0)
711 die("cannot simplify commit %s (because of %s)",
712 sha1_to_hex(commit->object.sha1),
713 sha1_to_hex(p->object.sha1));
714 switch (rev_compare_tree(revs, p, commit)) {
716 if (!revs->simplify_history || !relevant_commit(p)) {
717 /* Even if a merge with an uninteresting
718 * side branch brought the entire change
719 * we are interested in, we do not want
720 * to lose the other branches of this
721 * merge, so we just keep going.
724 ts->treesame[nth_parent] = 1;
728 commit->parents = parent;
729 commit->object.flags |= TREESAME;
733 if (revs->remove_empty_trees &&
734 rev_same_tree_as_empty(revs, p)) {
735 /* We are adding all the specified
736 * paths from this parent, so the
737 * history beyond this parent is not
738 * interesting. Remove its parents
739 * (they are grandparents for us).
740 * IOW, we pretend this parent is a
743 if (parse_commit(p) < 0)
744 die("cannot simplify commit %s (invalid %s)",
745 sha1_to_hex(commit->object.sha1),
746 sha1_to_hex(p->object.sha1));
751 case REV_TREE_DIFFERENT:
752 if (relevant_commit(p))
755 irrelevant_change = 1;
758 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
762 * TREESAME is straightforward for single-parent commits. For merge
763 * commits, it is most useful to define it so that "irrelevant"
764 * parents cannot make us !TREESAME - if we have any relevant
765 * parents, then we only consider TREESAMEness with respect to them,
766 * allowing irrelevant merges from uninteresting branches to be
767 * simplified away. Only if we have only irrelevant parents do we
768 * base TREESAME on them. Note that this logic is replicated in
769 * update_treesame, which should be kept in sync.
771 if (relevant_parents ? !relevant_change : !irrelevant_change)
772 commit->object.flags |= TREESAME;
775 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
776 struct commit_list *cached_base, struct commit_list **cache)
778 struct commit_list *new_entry;
780 if (cached_base && p->date < cached_base->item->date)
781 new_entry = commit_list_insert_by_date(p, &cached_base->next);
783 new_entry = commit_list_insert_by_date(p, head);
785 if (cache && (!*cache || p->date < (*cache)->item->date))
789 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
790 struct commit_list **list, struct commit_list **cache_ptr)
792 struct commit_list *parent = commit->parents;
794 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
796 if (commit->object.flags & ADDED)
798 commit->object.flags |= ADDED;
800 if (revs->include_check &&
801 !revs->include_check(commit, revs->include_check_data))
805 * If the commit is uninteresting, don't try to
806 * prune parents - we want the maximal uninteresting
809 * Normally we haven't parsed the parent
810 * yet, so we won't have a parent of a parent
811 * here. However, it may turn out that we've
812 * reached this commit some other way (where it
813 * wasn't uninteresting), in which case we need
814 * to mark its parents recursively too..
816 if (commit->object.flags & UNINTERESTING) {
818 struct commit *p = parent->item;
819 parent = parent->next;
821 p->object.flags |= UNINTERESTING;
822 if (parse_commit_gently(p, 1) < 0)
825 mark_parents_uninteresting(p);
826 if (p->object.flags & SEEN)
828 p->object.flags |= SEEN;
829 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
835 * Ok, the commit wasn't uninteresting. Try to
836 * simplify the commit history and find the parent
837 * that has no differences in the path set if one exists.
839 try_to_simplify_commit(revs, commit);
844 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
846 for (parent = commit->parents; parent; parent = parent->next) {
847 struct commit *p = parent->item;
849 if (parse_commit_gently(p, revs->ignore_missing_links) < 0)
851 if (revs->show_source && !p->util)
852 p->util = commit->util;
853 p->object.flags |= left_flag;
854 if (!(p->object.flags & SEEN)) {
855 p->object.flags |= SEEN;
856 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
858 if (revs->first_parent_only)
864 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
866 struct commit_list *p;
867 int left_count = 0, right_count = 0;
869 struct patch_ids ids;
870 unsigned cherry_flag;
872 /* First count the commits on the left and on the right */
873 for (p = list; p; p = p->next) {
874 struct commit *commit = p->item;
875 unsigned flags = commit->object.flags;
876 if (flags & BOUNDARY)
878 else if (flags & SYMMETRIC_LEFT)
884 if (!left_count || !right_count)
887 left_first = left_count < right_count;
888 init_patch_ids(&ids);
889 ids.diffopts.pathspec = revs->diffopt.pathspec;
891 /* Compute patch-ids for one side */
892 for (p = list; p; p = p->next) {
893 struct commit *commit = p->item;
894 unsigned flags = commit->object.flags;
896 if (flags & BOUNDARY)
899 * If we have fewer left, left_first is set and we omit
900 * commits on the right branch in this loop. If we have
901 * fewer right, we skip the left ones.
903 if (left_first != !!(flags & SYMMETRIC_LEFT))
905 commit->util = add_commit_patch_id(commit, &ids);
908 /* either cherry_mark or cherry_pick are true */
909 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
911 /* Check the other side */
912 for (p = list; p; p = p->next) {
913 struct commit *commit = p->item;
915 unsigned flags = commit->object.flags;
917 if (flags & BOUNDARY)
920 * If we have fewer left, left_first is set and we omit
921 * commits on the left branch in this loop.
923 if (left_first == !!(flags & SYMMETRIC_LEFT))
927 * Have we seen the same patch id?
929 id = has_commit_patch_id(commit, &ids);
933 commit->object.flags |= cherry_flag;
936 /* Now check the original side for seen ones */
937 for (p = list; p; p = p->next) {
938 struct commit *commit = p->item;
939 struct patch_id *ent;
945 commit->object.flags |= cherry_flag;
949 free_patch_ids(&ids);
952 /* How many extra uninteresting commits we want to see.. */
955 static int still_interesting(struct commit_list *src, unsigned long date, int slop,
956 struct commit **interesting_cache)
959 * No source list at all? We're definitely done..
965 * Does the destination list contain entries with a date
966 * before the source list? Definitely _not_ done.
968 if (date <= src->item->date)
972 * Does the source list still have interesting commits in
973 * it? Definitely not done..
975 if (!everybody_uninteresting(src, interesting_cache))
978 /* Ok, we're closing in.. */
983 * "rev-list --ancestry-path A..B" computes commits that are ancestors
984 * of B but not ancestors of A but further limits the result to those
985 * that are descendants of A. This takes the list of bottom commits and
986 * the result of "A..B" without --ancestry-path, and limits the latter
987 * further to the ones that can reach one of the commits in "bottom".
989 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
991 struct commit_list *p;
992 struct commit_list *rlist = NULL;
996 * Reverse the list so that it will be likely that we would
997 * process parents before children.
999 for (p = list; p; p = p->next)
1000 commit_list_insert(p->item, &rlist);
1002 for (p = bottom; p; p = p->next)
1003 p->item->object.flags |= TMP_MARK;
1006 * Mark the ones that can reach bottom commits in "list",
1007 * in a bottom-up fashion.
1011 for (p = rlist; p; p = p->next) {
1012 struct commit *c = p->item;
1013 struct commit_list *parents;
1014 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1016 for (parents = c->parents;
1018 parents = parents->next) {
1019 if (!(parents->item->object.flags & TMP_MARK))
1021 c->object.flags |= TMP_MARK;
1026 } while (made_progress);
1029 * NEEDSWORK: decide if we want to remove parents that are
1030 * not marked with TMP_MARK from commit->parents for commits
1031 * in the resulting list. We may not want to do that, though.
1035 * The ones that are not marked with TMP_MARK are uninteresting
1037 for (p = list; p; p = p->next) {
1038 struct commit *c = p->item;
1039 if (c->object.flags & TMP_MARK)
1041 c->object.flags |= UNINTERESTING;
1044 /* We are done with the TMP_MARK */
1045 for (p = list; p; p = p->next)
1046 p->item->object.flags &= ~TMP_MARK;
1047 for (p = bottom; p; p = p->next)
1048 p->item->object.flags &= ~TMP_MARK;
1049 free_commit_list(rlist);
1053 * Before walking the history, keep the set of "negative" refs the
1054 * caller has asked to exclude.
1056 * This is used to compute "rev-list --ancestry-path A..B", as we need
1057 * to filter the result of "A..B" further to the ones that can actually
1060 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1062 struct commit_list *elem, *bottom = NULL;
1063 for (elem = list; elem; elem = elem->next)
1064 if (elem->item->object.flags & BOTTOM)
1065 commit_list_insert(elem->item, &bottom);
1069 /* Assumes either left_only or right_only is set */
1070 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1072 struct commit_list *p;
1074 for (p = list; p; p = p->next) {
1075 struct commit *commit = p->item;
1077 if (revs->right_only) {
1078 if (commit->object.flags & SYMMETRIC_LEFT)
1079 commit->object.flags |= SHOWN;
1080 } else /* revs->left_only is set */
1081 if (!(commit->object.flags & SYMMETRIC_LEFT))
1082 commit->object.flags |= SHOWN;
1086 static int limit_list(struct rev_info *revs)
1089 unsigned long date = ~0ul;
1090 struct commit_list *list = revs->commits;
1091 struct commit_list *newlist = NULL;
1092 struct commit_list **p = &newlist;
1093 struct commit_list *bottom = NULL;
1094 struct commit *interesting_cache = NULL;
1096 if (revs->ancestry_path) {
1097 bottom = collect_bottom_commits(list);
1099 die("--ancestry-path given but there are no bottom commits");
1103 struct commit_list *entry = list;
1104 struct commit *commit = list->item;
1105 struct object *obj = &commit->object;
1106 show_early_output_fn_t show;
1111 if (commit == interesting_cache)
1112 interesting_cache = NULL;
1114 if (revs->max_age != -1 && (commit->date < revs->max_age))
1115 obj->flags |= UNINTERESTING;
1116 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1118 if (obj->flags & UNINTERESTING) {
1119 mark_parents_uninteresting(commit);
1121 p = &commit_list_insert(commit, p)->next;
1122 slop = still_interesting(list, date, slop, &interesting_cache);
1125 /* If showing all, add the whole pending list to the end */
1130 if (revs->min_age != -1 && (commit->date > revs->min_age))
1132 date = commit->date;
1133 p = &commit_list_insert(commit, p)->next;
1135 show = show_early_output;
1139 show(revs, newlist);
1140 show_early_output = NULL;
1142 if (revs->cherry_pick || revs->cherry_mark)
1143 cherry_pick_list(newlist, revs);
1145 if (revs->left_only || revs->right_only)
1146 limit_left_right(newlist, revs);
1149 limit_to_ancestry(bottom, newlist);
1150 free_commit_list(bottom);
1154 * Check if any commits have become TREESAME by some of their parents
1155 * becoming UNINTERESTING.
1157 if (limiting_can_increase_treesame(revs))
1158 for (list = newlist; list; list = list->next) {
1159 struct commit *c = list->item;
1160 if (c->object.flags & (UNINTERESTING | TREESAME))
1162 update_treesame(revs, c);
1165 revs->commits = newlist;
1170 * Add an entry to refs->cmdline with the specified information.
1173 static void add_rev_cmdline(struct rev_info *revs,
1174 struct object *item,
1179 struct rev_cmdline_info *info = &revs->cmdline;
1182 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1183 info->rev[nr].item = item;
1184 info->rev[nr].name = xstrdup(name);
1185 info->rev[nr].whence = whence;
1186 info->rev[nr].flags = flags;
1190 static void add_rev_cmdline_list(struct rev_info *revs,
1191 struct commit_list *commit_list,
1195 while (commit_list) {
1196 struct object *object = &commit_list->item->object;
1197 add_rev_cmdline(revs, object, sha1_to_hex(object->sha1),
1199 commit_list = commit_list->next;
1203 struct all_refs_cb {
1205 int warned_bad_reflog;
1206 struct rev_info *all_revs;
1207 const char *name_for_errormsg;
1210 int ref_excluded(struct string_list *ref_excludes, const char *path)
1212 struct string_list_item *item;
1216 for_each_string_list_item(item, ref_excludes) {
1217 if (!wildmatch(item->string, path, 0, NULL))
1223 static int handle_one_ref(const char *path, const struct object_id *oid,
1224 int flag, void *cb_data)
1226 struct all_refs_cb *cb = cb_data;
1227 struct object *object;
1229 if (ref_excluded(cb->all_revs->ref_excludes, path))
1232 object = get_reference(cb->all_revs, path, oid->hash, cb->all_flags);
1233 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1234 add_pending_sha1(cb->all_revs, path, oid->hash, cb->all_flags);
1238 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1241 cb->all_revs = revs;
1242 cb->all_flags = flags;
1245 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1247 if (*ref_excludes_p) {
1248 string_list_clear(*ref_excludes_p, 0);
1249 free(*ref_excludes_p);
1251 *ref_excludes_p = NULL;
1254 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1256 if (!*ref_excludes_p) {
1257 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1258 (*ref_excludes_p)->strdup_strings = 1;
1260 string_list_append(*ref_excludes_p, exclude);
1263 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
1264 int (*for_each)(const char *, each_ref_fn, void *))
1266 struct all_refs_cb cb;
1267 init_all_refs_cb(&cb, revs, flags);
1268 for_each(submodule, handle_one_ref, &cb);
1271 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
1273 struct all_refs_cb *cb = cb_data;
1274 if (!is_null_sha1(sha1)) {
1275 struct object *o = parse_object(sha1);
1277 o->flags |= cb->all_flags;
1278 /* ??? CMDLINEFLAGS ??? */
1279 add_pending_object(cb->all_revs, o, "");
1281 else if (!cb->warned_bad_reflog) {
1282 warning("reflog of '%s' references pruned commits",
1283 cb->name_for_errormsg);
1284 cb->warned_bad_reflog = 1;
1289 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
1290 const char *email, unsigned long timestamp, int tz,
1291 const char *message, void *cb_data)
1293 handle_one_reflog_commit(osha1, cb_data);
1294 handle_one_reflog_commit(nsha1, cb_data);
1298 static int handle_one_reflog(const char *path, const struct object_id *oid,
1299 int flag, void *cb_data)
1301 struct all_refs_cb *cb = cb_data;
1302 cb->warned_bad_reflog = 0;
1303 cb->name_for_errormsg = path;
1304 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
1308 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1310 struct all_refs_cb cb;
1313 cb.all_flags = flags;
1314 for_each_reflog(handle_one_reflog, &cb);
1317 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1318 struct strbuf *path)
1320 size_t baselen = path->len;
1323 if (it->entry_count >= 0) {
1324 struct tree *tree = lookup_tree(it->sha1);
1325 add_pending_object_with_path(revs, &tree->object, "",
1329 for (i = 0; i < it->subtree_nr; i++) {
1330 struct cache_tree_sub *sub = it->down[i];
1331 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1332 add_cache_tree(sub->cache_tree, revs, path);
1333 strbuf_setlen(path, baselen);
1338 void add_index_objects_to_pending(struct rev_info *revs, unsigned flags)
1343 for (i = 0; i < active_nr; i++) {
1344 struct cache_entry *ce = active_cache[i];
1347 if (S_ISGITLINK(ce->ce_mode))
1350 blob = lookup_blob(ce->sha1);
1352 die("unable to add index blob to traversal");
1353 add_pending_object_with_path(revs, &blob->object, "",
1354 ce->ce_mode, ce->name);
1357 if (active_cache_tree) {
1358 struct strbuf path = STRBUF_INIT;
1359 add_cache_tree(active_cache_tree, revs, &path);
1360 strbuf_release(&path);
1364 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
1366 unsigned char sha1[20];
1368 struct commit *commit;
1369 struct commit_list *parents;
1370 const char *arg = arg_;
1373 flags ^= UNINTERESTING | BOTTOM;
1376 if (get_sha1_committish(arg, sha1))
1379 it = get_reference(revs, arg, sha1, 0);
1380 if (!it && revs->ignore_missing)
1382 if (it->type != OBJ_TAG)
1384 if (!((struct tag*)it)->tagged)
1386 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
1388 if (it->type != OBJ_COMMIT)
1390 commit = (struct commit *)it;
1391 for (parents = commit->parents; parents; parents = parents->next) {
1392 it = &parents->item->object;
1394 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1395 add_pending_object(revs, it, arg);
1400 void init_revisions(struct rev_info *revs, const char *prefix)
1402 memset(revs, 0, sizeof(*revs));
1404 revs->abbrev = DEFAULT_ABBREV;
1405 revs->ignore_merges = 1;
1406 revs->simplify_history = 1;
1407 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1408 DIFF_OPT_SET(&revs->pruning, QUICK);
1409 revs->pruning.add_remove = file_add_remove;
1410 revs->pruning.change = file_change;
1411 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1413 revs->prefix = prefix;
1416 revs->skip_count = -1;
1417 revs->max_count = -1;
1418 revs->max_parents = -1;
1420 revs->commit_format = CMIT_FMT_DEFAULT;
1422 init_grep_defaults();
1423 grep_init(&revs->grep_filter, prefix);
1424 revs->grep_filter.status_only = 1;
1425 revs->grep_filter.regflags = REG_NEWLINE;
1427 diff_setup(&revs->diffopt);
1428 if (prefix && !revs->diffopt.prefix) {
1429 revs->diffopt.prefix = prefix;
1430 revs->diffopt.prefix_length = strlen(prefix);
1433 revs->notes_opt.use_default_notes = -1;
1436 static void add_pending_commit_list(struct rev_info *revs,
1437 struct commit_list *commit_list,
1440 while (commit_list) {
1441 struct object *object = &commit_list->item->object;
1442 object->flags |= flags;
1443 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1444 commit_list = commit_list->next;
1448 static void prepare_show_merge(struct rev_info *revs)
1450 struct commit_list *bases;
1451 struct commit *head, *other;
1452 unsigned char sha1[20];
1453 const char **prune = NULL;
1454 int i, prune_num = 1; /* counting terminating NULL */
1456 if (get_sha1("HEAD", sha1))
1457 die("--merge without HEAD?");
1458 head = lookup_commit_or_die(sha1, "HEAD");
1459 if (get_sha1("MERGE_HEAD", sha1))
1460 die("--merge without MERGE_HEAD?");
1461 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
1462 add_pending_object(revs, &head->object, "HEAD");
1463 add_pending_object(revs, &other->object, "MERGE_HEAD");
1464 bases = get_merge_bases(head, other);
1465 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1466 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1467 free_commit_list(bases);
1468 head->object.flags |= SYMMETRIC_LEFT;
1472 for (i = 0; i < active_nr; i++) {
1473 const struct cache_entry *ce = active_cache[i];
1476 if (ce_path_match(ce, &revs->prune_data, NULL)) {
1478 REALLOC_ARRAY(prune, prune_num);
1479 prune[prune_num-2] = ce->name;
1480 prune[prune_num-1] = NULL;
1482 while ((i+1 < active_nr) &&
1483 ce_same_name(ce, active_cache[i+1]))
1486 free_pathspec(&revs->prune_data);
1487 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1488 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1492 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1494 struct object_context oc;
1496 struct object *object;
1497 unsigned char sha1[20];
1499 const char *arg = arg_;
1500 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1501 unsigned get_sha1_flags = 0;
1503 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1505 dotdot = strstr(arg, "..");
1507 unsigned char from_sha1[20];
1508 const char *next = dotdot + 2;
1509 const char *this = arg;
1510 int symmetric = *next == '.';
1511 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1512 static const char head_by_default[] = "HEAD";
1513 unsigned int a_flags;
1519 next = head_by_default;
1521 this = head_by_default;
1522 if (this == head_by_default && next == head_by_default &&
1525 * Just ".."? That is not a range but the
1526 * pathspec for the parent directory.
1528 if (!cant_be_filename) {
1533 if (!get_sha1_committish(this, from_sha1) &&
1534 !get_sha1_committish(next, sha1)) {
1535 struct object *a_obj, *b_obj;
1537 if (!cant_be_filename) {
1539 verify_non_filename(revs->prefix, arg);
1542 a_obj = parse_object(from_sha1);
1543 b_obj = parse_object(sha1);
1544 if (!a_obj || !b_obj) {
1546 if (revs->ignore_missing)
1549 ? "Invalid symmetric difference expression %s"
1550 : "Invalid revision range %s", arg);
1555 a_flags = flags_exclude;
1557 /* A...B -- find merge bases between the two */
1558 struct commit *a, *b;
1559 struct commit_list *exclude;
1561 a = (a_obj->type == OBJ_COMMIT
1562 ? (struct commit *)a_obj
1563 : lookup_commit_reference(a_obj->sha1));
1564 b = (b_obj->type == OBJ_COMMIT
1565 ? (struct commit *)b_obj
1566 : lookup_commit_reference(b_obj->sha1));
1569 exclude = get_merge_bases(a, b);
1570 add_rev_cmdline_list(revs, exclude,
1573 add_pending_commit_list(revs, exclude,
1575 free_commit_list(exclude);
1577 a_flags = flags | SYMMETRIC_LEFT;
1580 a_obj->flags |= a_flags;
1581 b_obj->flags |= flags;
1582 add_rev_cmdline(revs, a_obj, this,
1583 REV_CMD_LEFT, a_flags);
1584 add_rev_cmdline(revs, b_obj, next,
1585 REV_CMD_RIGHT, flags);
1586 add_pending_object(revs, a_obj, this);
1587 add_pending_object(revs, b_obj, next);
1592 dotdot = strstr(arg, "^@");
1593 if (dotdot && !dotdot[2]) {
1595 if (add_parents_only(revs, arg, flags))
1599 dotdot = strstr(arg, "^!");
1600 if (dotdot && !dotdot[2]) {
1602 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM)))
1608 local_flags = UNINTERESTING | BOTTOM;
1612 if (revarg_opt & REVARG_COMMITTISH)
1613 get_sha1_flags = GET_SHA1_COMMITTISH;
1615 if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
1616 return revs->ignore_missing ? 0 : -1;
1617 if (!cant_be_filename)
1618 verify_non_filename(revs->prefix, arg);
1619 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1620 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1621 add_pending_object_with_mode(revs, object, arg, oc.mode);
1625 struct cmdline_pathspec {
1631 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1634 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1635 prune->path[prune->nr++] = *(av++);
1639 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1640 struct cmdline_pathspec *prune)
1642 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1644 if (len && sb->buf[len - 1] == '\n')
1645 sb->buf[--len] = '\0';
1646 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1647 prune->path[prune->nr++] = xstrdup(sb->buf);
1651 static void read_revisions_from_stdin(struct rev_info *revs,
1652 struct cmdline_pathspec *prune)
1655 int seen_dashdash = 0;
1658 save_warning = warn_on_object_refname_ambiguity;
1659 warn_on_object_refname_ambiguity = 0;
1661 strbuf_init(&sb, 1000);
1662 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1664 if (len && sb.buf[len - 1] == '\n')
1665 sb.buf[--len] = '\0';
1668 if (sb.buf[0] == '-') {
1669 if (len == 2 && sb.buf[1] == '-') {
1673 die("options not supported in --stdin mode");
1675 if (handle_revision_arg(sb.buf, revs, 0,
1676 REVARG_CANNOT_BE_FILENAME))
1677 die("bad revision '%s'", sb.buf);
1680 read_pathspec_from_stdin(revs, &sb, prune);
1682 strbuf_release(&sb);
1683 warn_on_object_refname_ambiguity = save_warning;
1686 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1688 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1691 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1693 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1696 static void add_message_grep(struct rev_info *revs, const char *pattern)
1698 add_grep(revs, pattern, GREP_PATTERN_BODY);
1701 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1702 int *unkc, const char **unkv)
1704 const char *arg = argv[0];
1708 /* pseudo revision arguments */
1709 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1710 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1711 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1712 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1713 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1714 !strcmp(arg, "--indexed-objects") ||
1715 starts_with(arg, "--exclude=") ||
1716 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1717 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1719 unkv[(*unkc)++] = arg;
1723 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1724 revs->max_count = atoi(optarg);
1727 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1728 revs->skip_count = atoi(optarg);
1730 } else if ((*arg == '-') && isdigit(arg[1])) {
1731 /* accept -<digit>, like traditional "head" */
1732 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1733 revs->max_count < 0)
1734 die("'%s': not a non-negative integer", arg + 1);
1736 } else if (!strcmp(arg, "-n")) {
1738 return error("-n requires an argument");
1739 revs->max_count = atoi(argv[1]);
1742 } else if (starts_with(arg, "-n")) {
1743 revs->max_count = atoi(arg + 2);
1745 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1746 revs->max_age = atoi(optarg);
1748 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1749 revs->max_age = approxidate(optarg);
1751 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1752 revs->max_age = approxidate(optarg);
1754 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1755 revs->min_age = atoi(optarg);
1757 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1758 revs->min_age = approxidate(optarg);
1760 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1761 revs->min_age = approxidate(optarg);
1763 } else if (!strcmp(arg, "--first-parent")) {
1764 revs->first_parent_only = 1;
1765 } else if (!strcmp(arg, "--ancestry-path")) {
1766 revs->ancestry_path = 1;
1767 revs->simplify_history = 0;
1769 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1770 init_reflog_walk(&revs->reflog_info);
1771 } else if (!strcmp(arg, "--default")) {
1773 return error("bad --default argument");
1774 revs->def = argv[1];
1776 } else if (!strcmp(arg, "--merge")) {
1777 revs->show_merge = 1;
1778 } else if (!strcmp(arg, "--topo-order")) {
1779 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1780 revs->topo_order = 1;
1781 } else if (!strcmp(arg, "--simplify-merges")) {
1782 revs->simplify_merges = 1;
1783 revs->topo_order = 1;
1784 revs->rewrite_parents = 1;
1785 revs->simplify_history = 0;
1787 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1788 revs->simplify_merges = 1;
1789 revs->topo_order = 1;
1790 revs->rewrite_parents = 1;
1791 revs->simplify_history = 0;
1792 revs->simplify_by_decoration = 1;
1795 load_ref_decorations(DECORATE_SHORT_REFS);
1796 } else if (!strcmp(arg, "--date-order")) {
1797 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1798 revs->topo_order = 1;
1799 } else if (!strcmp(arg, "--author-date-order")) {
1800 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1801 revs->topo_order = 1;
1802 } else if (starts_with(arg, "--early-output")) {
1806 count = atoi(arg+15);
1809 revs->topo_order = 1;
1810 revs->early_output = count;
1812 } else if (!strcmp(arg, "--parents")) {
1813 revs->rewrite_parents = 1;
1814 revs->print_parents = 1;
1815 } else if (!strcmp(arg, "--dense")) {
1817 } else if (!strcmp(arg, "--sparse")) {
1819 } else if (!strcmp(arg, "--show-all")) {
1821 } else if (!strcmp(arg, "--remove-empty")) {
1822 revs->remove_empty_trees = 1;
1823 } else if (!strcmp(arg, "--merges")) {
1824 revs->min_parents = 2;
1825 } else if (!strcmp(arg, "--no-merges")) {
1826 revs->max_parents = 1;
1827 } else if (starts_with(arg, "--min-parents=")) {
1828 revs->min_parents = atoi(arg+14);
1829 } else if (starts_with(arg, "--no-min-parents")) {
1830 revs->min_parents = 0;
1831 } else if (starts_with(arg, "--max-parents=")) {
1832 revs->max_parents = atoi(arg+14);
1833 } else if (starts_with(arg, "--no-max-parents")) {
1834 revs->max_parents = -1;
1835 } else if (!strcmp(arg, "--boundary")) {
1837 } else if (!strcmp(arg, "--left-right")) {
1838 revs->left_right = 1;
1839 } else if (!strcmp(arg, "--left-only")) {
1840 if (revs->right_only)
1841 die("--left-only is incompatible with --right-only"
1843 revs->left_only = 1;
1844 } else if (!strcmp(arg, "--right-only")) {
1845 if (revs->left_only)
1846 die("--right-only is incompatible with --left-only");
1847 revs->right_only = 1;
1848 } else if (!strcmp(arg, "--cherry")) {
1849 if (revs->left_only)
1850 die("--cherry is incompatible with --left-only");
1851 revs->cherry_mark = 1;
1852 revs->right_only = 1;
1853 revs->max_parents = 1;
1855 } else if (!strcmp(arg, "--count")) {
1857 } else if (!strcmp(arg, "--cherry-mark")) {
1858 if (revs->cherry_pick)
1859 die("--cherry-mark is incompatible with --cherry-pick");
1860 revs->cherry_mark = 1;
1861 revs->limited = 1; /* needs limit_list() */
1862 } else if (!strcmp(arg, "--cherry-pick")) {
1863 if (revs->cherry_mark)
1864 die("--cherry-pick is incompatible with --cherry-mark");
1865 revs->cherry_pick = 1;
1867 } else if (!strcmp(arg, "--objects")) {
1868 revs->tag_objects = 1;
1869 revs->tree_objects = 1;
1870 revs->blob_objects = 1;
1871 } else if (!strcmp(arg, "--objects-edge")) {
1872 revs->tag_objects = 1;
1873 revs->tree_objects = 1;
1874 revs->blob_objects = 1;
1875 revs->edge_hint = 1;
1876 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1877 revs->tag_objects = 1;
1878 revs->tree_objects = 1;
1879 revs->blob_objects = 1;
1880 revs->edge_hint = 1;
1881 revs->edge_hint_aggressive = 1;
1882 } else if (!strcmp(arg, "--verify-objects")) {
1883 revs->tag_objects = 1;
1884 revs->tree_objects = 1;
1885 revs->blob_objects = 1;
1886 revs->verify_objects = 1;
1887 } else if (!strcmp(arg, "--unpacked")) {
1889 } else if (starts_with(arg, "--unpacked=")) {
1890 die("--unpacked=<packfile> no longer supported.");
1891 } else if (!strcmp(arg, "-r")) {
1893 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1894 } else if (!strcmp(arg, "-t")) {
1896 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1897 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1898 } else if (!strcmp(arg, "-m")) {
1899 revs->ignore_merges = 0;
1900 } else if (!strcmp(arg, "-c")) {
1902 revs->dense_combined_merges = 0;
1903 revs->combine_merges = 1;
1904 } else if (!strcmp(arg, "--cc")) {
1906 revs->dense_combined_merges = 1;
1907 revs->combine_merges = 1;
1908 } else if (!strcmp(arg, "-v")) {
1909 revs->verbose_header = 1;
1910 } else if (!strcmp(arg, "--pretty")) {
1911 revs->verbose_header = 1;
1912 revs->pretty_given = 1;
1913 get_commit_format(NULL, revs);
1914 } else if (starts_with(arg, "--pretty=") || starts_with(arg, "--format=")) {
1916 * Detached form ("--pretty X" as opposed to "--pretty=X")
1917 * not allowed, since the argument is optional.
1919 revs->verbose_header = 1;
1920 revs->pretty_given = 1;
1921 get_commit_format(arg+9, revs);
1922 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1923 revs->show_notes = 1;
1924 revs->show_notes_given = 1;
1925 revs->notes_opt.use_default_notes = 1;
1926 } else if (!strcmp(arg, "--show-signature")) {
1927 revs->show_signature = 1;
1928 } else if (!strcmp(arg, "--show-linear-break") ||
1929 starts_with(arg, "--show-linear-break=")) {
1930 if (starts_with(arg, "--show-linear-break="))
1931 revs->break_bar = xstrdup(arg + 20);
1933 revs->break_bar = " ..........";
1934 revs->track_linear = 1;
1935 revs->track_first_time = 1;
1936 } else if (starts_with(arg, "--show-notes=") ||
1937 starts_with(arg, "--notes=")) {
1938 struct strbuf buf = STRBUF_INIT;
1939 revs->show_notes = 1;
1940 revs->show_notes_given = 1;
1941 if (starts_with(arg, "--show-notes")) {
1942 if (revs->notes_opt.use_default_notes < 0)
1943 revs->notes_opt.use_default_notes = 1;
1944 strbuf_addstr(&buf, arg+13);
1947 strbuf_addstr(&buf, arg+8);
1948 expand_notes_ref(&buf);
1949 string_list_append(&revs->notes_opt.extra_notes_refs,
1950 strbuf_detach(&buf, NULL));
1951 } else if (!strcmp(arg, "--no-notes")) {
1952 revs->show_notes = 0;
1953 revs->show_notes_given = 1;
1954 revs->notes_opt.use_default_notes = -1;
1955 /* we have been strdup'ing ourselves, so trick
1956 * string_list into free()ing strings */
1957 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1958 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1959 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1960 } else if (!strcmp(arg, "--standard-notes")) {
1961 revs->show_notes_given = 1;
1962 revs->notes_opt.use_default_notes = 1;
1963 } else if (!strcmp(arg, "--no-standard-notes")) {
1964 revs->notes_opt.use_default_notes = 0;
1965 } else if (!strcmp(arg, "--oneline")) {
1966 revs->verbose_header = 1;
1967 get_commit_format("oneline", revs);
1968 revs->pretty_given = 1;
1969 revs->abbrev_commit = 1;
1970 } else if (!strcmp(arg, "--graph")) {
1971 revs->topo_order = 1;
1972 revs->rewrite_parents = 1;
1973 revs->graph = graph_init(revs);
1974 } else if (!strcmp(arg, "--root")) {
1975 revs->show_root_diff = 1;
1976 } else if (!strcmp(arg, "--no-commit-id")) {
1977 revs->no_commit_id = 1;
1978 } else if (!strcmp(arg, "--always")) {
1979 revs->always_show_header = 1;
1980 } else if (!strcmp(arg, "--no-abbrev")) {
1982 } else if (!strcmp(arg, "--abbrev")) {
1983 revs->abbrev = DEFAULT_ABBREV;
1984 } else if (starts_with(arg, "--abbrev=")) {
1985 revs->abbrev = strtoul(arg + 9, NULL, 10);
1986 if (revs->abbrev < MINIMUM_ABBREV)
1987 revs->abbrev = MINIMUM_ABBREV;
1988 else if (revs->abbrev > 40)
1990 } else if (!strcmp(arg, "--abbrev-commit")) {
1991 revs->abbrev_commit = 1;
1992 revs->abbrev_commit_given = 1;
1993 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1994 revs->abbrev_commit = 0;
1995 } else if (!strcmp(arg, "--full-diff")) {
1997 revs->full_diff = 1;
1998 } else if (!strcmp(arg, "--full-history")) {
1999 revs->simplify_history = 0;
2000 } else if (!strcmp(arg, "--relative-date")) {
2001 revs->date_mode = DATE_RELATIVE;
2002 revs->date_mode_explicit = 1;
2003 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2004 revs->date_mode = parse_date_format(optarg);
2005 revs->date_mode_explicit = 1;
2007 } else if (!strcmp(arg, "--log-size")) {
2008 revs->show_log_size = 1;
2011 * Grepping the commit log
2013 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2014 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2016 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2017 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2019 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2020 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2022 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2023 add_message_grep(revs, optarg);
2025 } else if (!strcmp(arg, "--grep-debug")) {
2026 revs->grep_filter.debug = 1;
2027 } else if (!strcmp(arg, "--basic-regexp")) {
2028 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter);
2029 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2030 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter);
2031 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2032 revs->grep_filter.regflags |= REG_ICASE;
2033 DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
2034 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2035 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter);
2036 } else if (!strcmp(arg, "--perl-regexp")) {
2037 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
2038 } else if (!strcmp(arg, "--all-match")) {
2039 revs->grep_filter.all_match = 1;
2040 } else if (!strcmp(arg, "--invert-grep")) {
2041 revs->invert_grep = 1;
2042 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2043 if (strcmp(optarg, "none"))
2044 git_log_output_encoding = xstrdup(optarg);
2046 git_log_output_encoding = "";
2048 } else if (!strcmp(arg, "--reverse")) {
2050 } else if (!strcmp(arg, "--children")) {
2051 revs->children.name = "children";
2053 } else if (!strcmp(arg, "--ignore-missing")) {
2054 revs->ignore_missing = 1;
2056 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
2058 unkv[(*unkc)++] = arg;
2061 if (revs->graph && revs->track_linear)
2062 die("--show-linear-break and --graph are incompatible");
2067 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2068 const struct option *options,
2069 const char * const usagestr[])
2071 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2072 &ctx->cpidx, ctx->out);
2074 error("unknown option `%s'", ctx->argv[0]);
2075 usage_with_options(usagestr, options);
2081 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2083 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
2086 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2088 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
2091 static int handle_revision_pseudo_opt(const char *submodule,
2092 struct rev_info *revs,
2093 int argc, const char **argv, int *flags)
2095 const char *arg = argv[0];
2102 * Commands like "git shortlog" will not accept the options below
2103 * unless parse_revision_opt queues them (as opposed to erroring
2106 * When implementing your new pseudo-option, remember to
2107 * register it in the list at the top of handle_revision_opt.
2109 if (!strcmp(arg, "--all")) {
2110 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
2111 handle_refs(submodule, revs, *flags, head_ref_submodule);
2112 clear_ref_exclusion(&revs->ref_excludes);
2113 } else if (!strcmp(arg, "--branches")) {
2114 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
2115 clear_ref_exclusion(&revs->ref_excludes);
2116 } else if (!strcmp(arg, "--bisect")) {
2117 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
2118 handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
2120 } else if (!strcmp(arg, "--tags")) {
2121 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
2122 clear_ref_exclusion(&revs->ref_excludes);
2123 } else if (!strcmp(arg, "--remotes")) {
2124 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
2125 clear_ref_exclusion(&revs->ref_excludes);
2126 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2127 struct all_refs_cb cb;
2128 init_all_refs_cb(&cb, revs, *flags);
2129 for_each_glob_ref(handle_one_ref, optarg, &cb);
2130 clear_ref_exclusion(&revs->ref_excludes);
2132 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2133 add_ref_exclusion(&revs->ref_excludes, optarg);
2135 } else if (starts_with(arg, "--branches=")) {
2136 struct all_refs_cb cb;
2137 init_all_refs_cb(&cb, revs, *flags);
2138 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
2139 clear_ref_exclusion(&revs->ref_excludes);
2140 } else if (starts_with(arg, "--tags=")) {
2141 struct all_refs_cb cb;
2142 init_all_refs_cb(&cb, revs, *flags);
2143 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
2144 clear_ref_exclusion(&revs->ref_excludes);
2145 } else if (starts_with(arg, "--remotes=")) {
2146 struct all_refs_cb cb;
2147 init_all_refs_cb(&cb, revs, *flags);
2148 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
2149 clear_ref_exclusion(&revs->ref_excludes);
2150 } else if (!strcmp(arg, "--reflog")) {
2151 add_reflogs_to_pending(revs, *flags);
2152 } else if (!strcmp(arg, "--indexed-objects")) {
2153 add_index_objects_to_pending(revs, *flags);
2154 } else if (!strcmp(arg, "--not")) {
2155 *flags ^= UNINTERESTING | BOTTOM;
2156 } else if (!strcmp(arg, "--no-walk")) {
2157 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2158 } else if (starts_with(arg, "--no-walk=")) {
2160 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2161 * not allowed, since the argument is optional.
2163 if (!strcmp(arg + 10, "sorted"))
2164 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2165 else if (!strcmp(arg + 10, "unsorted"))
2166 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2168 return error("invalid argument to --no-walk");
2169 } else if (!strcmp(arg, "--do-walk")) {
2178 static void NORETURN diagnose_missing_default(const char *def)
2180 unsigned char sha1[20];
2182 const char *refname;
2184 refname = resolve_ref_unsafe(def, 0, sha1, &flags);
2185 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2186 die(_("your current branch appears to be broken"));
2188 skip_prefix(refname, "refs/heads/", &refname);
2189 die(_("your current branch '%s' does not have any commits yet"),
2194 * Parse revision information, filling in the "rev_info" structure,
2195 * and removing the used arguments from the argument list.
2197 * Returns the number of arguments left that weren't recognized
2198 * (which are also moved to the head of the argument list)
2200 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2202 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2203 struct cmdline_pathspec prune_data;
2204 const char *submodule = NULL;
2206 memset(&prune_data, 0, sizeof(prune_data));
2208 submodule = opt->submodule;
2210 /* First, search for "--" */
2211 if (opt && opt->assume_dashdash) {
2215 for (i = 1; i < argc; i++) {
2216 const char *arg = argv[i];
2217 if (strcmp(arg, "--"))
2222 append_prune_data(&prune_data, argv + i + 1);
2228 /* Second, deal with arguments and options */
2230 revarg_opt = opt ? opt->revarg_opt : 0;
2232 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2233 read_from_stdin = 0;
2234 for (left = i = 1; i < argc; i++) {
2235 const char *arg = argv[i];
2239 opts = handle_revision_pseudo_opt(submodule,
2240 revs, argc - i, argv + i,
2247 if (!strcmp(arg, "--stdin")) {
2248 if (revs->disable_stdin) {
2252 if (read_from_stdin++)
2253 die("--stdin given twice?");
2254 read_revisions_from_stdin(revs, &prune_data);
2258 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2269 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2271 if (seen_dashdash || *arg == '^')
2272 die("bad revision '%s'", arg);
2274 /* If we didn't have a "--":
2275 * (1) all filenames must exist;
2276 * (2) all rev-args must not be interpretable
2277 * as a valid filename.
2278 * but the latter we have checked in the main loop.
2280 for (j = i; j < argc; j++)
2281 verify_filename(revs->prefix, argv[j], j == i);
2283 append_prune_data(&prune_data, argv + i);
2290 if (prune_data.nr) {
2292 * If we need to introduce the magic "a lone ':' means no
2293 * pathspec whatsoever", here is the place to do so.
2295 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2296 * prune_data.nr = 0;
2297 * prune_data.alloc = 0;
2298 * free(prune_data.path);
2299 * prune_data.path = NULL;
2301 * terminate prune_data.alloc with NULL and
2302 * call init_pathspec() to set revs->prune_data here.
2305 ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc);
2306 prune_data.path[prune_data.nr++] = NULL;
2307 parse_pathspec(&revs->prune_data, 0, 0,
2308 revs->prefix, prune_data.path);
2311 if (revs->def == NULL)
2312 revs->def = opt ? opt->def : NULL;
2313 if (opt && opt->tweak)
2314 opt->tweak(revs, opt);
2315 if (revs->show_merge)
2316 prepare_show_merge(revs);
2317 if (revs->def && !revs->pending.nr && !got_rev_arg) {
2318 unsigned char sha1[20];
2319 struct object *object;
2320 struct object_context oc;
2321 if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2322 diagnose_missing_default(revs->def);
2323 object = get_reference(revs, revs->def, sha1, 0);
2324 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2327 /* Did the user ask for any diff output? Run the diff! */
2328 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2331 /* Pickaxe, diff-filter and rename following need diffs */
2332 if (revs->diffopt.pickaxe ||
2333 revs->diffopt.filter ||
2334 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2337 if (revs->topo_order)
2340 if (revs->prune_data.nr) {
2341 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2342 /* Can't prune commits with rename following: the paths change.. */
2343 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2345 if (!revs->full_diff)
2346 copy_pathspec(&revs->diffopt.pathspec,
2349 if (revs->combine_merges)
2350 revs->ignore_merges = 0;
2351 revs->diffopt.abbrev = revs->abbrev;
2353 if (revs->line_level_traverse) {
2355 revs->topo_order = 1;
2358 diff_setup_done(&revs->diffopt);
2360 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2361 &revs->grep_filter);
2362 compile_grep_patterns(&revs->grep_filter);
2364 if (revs->reverse && revs->reflog_info)
2365 die("cannot combine --reverse with --walk-reflogs");
2366 if (revs->rewrite_parents && revs->children.name)
2367 die("cannot combine --parents and --children");
2370 * Limitations on the graph functionality
2372 if (revs->reverse && revs->graph)
2373 die("cannot combine --reverse with --graph");
2375 if (revs->reflog_info && revs->graph)
2376 die("cannot combine --walk-reflogs with --graph");
2377 if (revs->no_walk && revs->graph)
2378 die("cannot combine --no-walk with --graph");
2379 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2380 die("cannot use --grep-reflog without --walk-reflogs");
2382 if (revs->first_parent_only && revs->bisect)
2383 die(_("--first-parent is incompatible with --bisect"));
2388 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2390 struct commit_list *l = xcalloc(1, sizeof(*l));
2393 l->next = add_decoration(&revs->children, &parent->object, l);
2396 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2398 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2399 struct commit_list **pp, *p;
2400 int surviving_parents;
2402 /* Examine existing parents while marking ones we have seen... */
2403 pp = &commit->parents;
2404 surviving_parents = 0;
2405 while ((p = *pp) != NULL) {
2406 struct commit *parent = p->item;
2407 if (parent->object.flags & TMP_MARK) {
2410 compact_treesame(revs, commit, surviving_parents);
2413 parent->object.flags |= TMP_MARK;
2414 surviving_parents++;
2417 /* clear the temporary mark */
2418 for (p = commit->parents; p; p = p->next) {
2419 p->item->object.flags &= ~TMP_MARK;
2421 /* no update_treesame() - removing duplicates can't affect TREESAME */
2422 return surviving_parents;
2425 struct merge_simplify_state {
2426 struct commit *simplified;
2429 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2431 struct merge_simplify_state *st;
2433 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2435 st = xcalloc(1, sizeof(*st));
2436 add_decoration(&revs->merge_simplification, &commit->object, st);
2441 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2443 struct commit_list *h = reduce_heads(commit->parents);
2444 int i = 0, marked = 0;
2445 struct commit_list *po, *pn;
2447 /* Want these for sanity-checking only */
2448 int orig_cnt = commit_list_count(commit->parents);
2449 int cnt = commit_list_count(h);
2452 * Not ready to remove items yet, just mark them for now, based
2453 * on the output of reduce_heads(). reduce_heads outputs the reduced
2454 * set in its original order, so this isn't too hard.
2456 po = commit->parents;
2459 if (pn && po->item == pn->item) {
2463 po->item->object.flags |= TMP_MARK;
2469 if (i != cnt || cnt+marked != orig_cnt)
2470 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2472 free_commit_list(h);
2477 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2479 struct commit_list *p;
2482 for (p = commit->parents; p; p = p->next) {
2483 struct commit *parent = p->item;
2484 if (!parent->parents && (parent->object.flags & TREESAME)) {
2485 parent->object.flags |= TMP_MARK;
2494 * Awkward naming - this means one parent we are TREESAME to.
2495 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2496 * empty tree). Better name suggestions?
2498 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2500 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2501 struct commit *unmarked = NULL, *marked = NULL;
2502 struct commit_list *p;
2505 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2506 if (ts->treesame[n]) {
2507 if (p->item->object.flags & TMP_MARK) {
2520 * If we are TREESAME to a marked-for-deletion parent, but not to any
2521 * unmarked parents, unmark the first TREESAME parent. This is the
2522 * parent that the default simplify_history==1 scan would have followed,
2523 * and it doesn't make sense to omit that path when asking for a
2524 * simplified full history. Retaining it improves the chances of
2525 * understanding odd missed merges that took an old version of a file.
2529 * I--------*X A modified the file, but mainline merge X used
2530 * \ / "-s ours", so took the version from I. X is
2531 * `-*A--' TREESAME to I and !TREESAME to A.
2533 * Default log from X would produce "I". Without this check,
2534 * --full-history --simplify-merges would produce "I-A-X", showing
2535 * the merge commit X and that it changed A, but not making clear that
2536 * it had just taken the I version. With this check, the topology above
2539 * Note that it is possible that the simplification chooses a different
2540 * TREESAME parent from the default, in which case this test doesn't
2541 * activate, and we _do_ drop the default parent. Example:
2543 * I------X A modified the file, but it was reverted in B,
2544 * \ / meaning mainline merge X is TREESAME to both
2547 * Default log would produce "I" by following the first parent;
2548 * --full-history --simplify-merges will produce "I-A-B". But this is a
2549 * reasonable result - it presents a logical full history leading from
2550 * I to X, and X is not an important merge.
2552 if (!unmarked && marked) {
2553 marked->object.flags &= ~TMP_MARK;
2560 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2562 struct commit_list **pp, *p;
2563 int nth_parent, removed = 0;
2565 pp = &commit->parents;
2567 while ((p = *pp) != NULL) {
2568 struct commit *parent = p->item;
2569 if (parent->object.flags & TMP_MARK) {
2570 parent->object.flags &= ~TMP_MARK;
2574 compact_treesame(revs, commit, nth_parent);
2581 /* Removing parents can only increase TREESAMEness */
2582 if (removed && !(commit->object.flags & TREESAME))
2583 update_treesame(revs, commit);
2588 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2590 struct commit_list *p;
2591 struct commit *parent;
2592 struct merge_simplify_state *st, *pst;
2595 st = locate_simplify_state(revs, commit);
2598 * Have we handled this one?
2604 * An UNINTERESTING commit simplifies to itself, so does a
2605 * root commit. We do not rewrite parents of such commit
2608 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2609 st->simplified = commit;
2614 * Do we know what commit all of our parents that matter
2615 * should be rewritten to? Otherwise we are not ready to
2616 * rewrite this one yet.
2618 for (cnt = 0, p = commit->parents; p; p = p->next) {
2619 pst = locate_simplify_state(revs, p->item);
2620 if (!pst->simplified) {
2621 tail = &commit_list_insert(p->item, tail)->next;
2624 if (revs->first_parent_only)
2628 tail = &commit_list_insert(commit, tail)->next;
2633 * Rewrite our list of parents. Note that this cannot
2634 * affect our TREESAME flags in any way - a commit is
2635 * always TREESAME to its simplification.
2637 for (p = commit->parents; p; p = p->next) {
2638 pst = locate_simplify_state(revs, p->item);
2639 p->item = pst->simplified;
2640 if (revs->first_parent_only)
2644 if (revs->first_parent_only)
2647 cnt = remove_duplicate_parents(revs, commit);
2650 * It is possible that we are a merge and one side branch
2651 * does not have any commit that touches the given paths;
2652 * in such a case, the immediate parent from that branch
2653 * will be rewritten to be the merge base.
2655 * o----X X: the commit we are looking at;
2656 * / / o: a commit that touches the paths;
2659 * Further, a merge of an independent branch that doesn't
2660 * touch the path will reduce to a treesame root parent:
2662 * ----o----X X: the commit we are looking at;
2663 * / o: a commit that touches the paths;
2664 * r r: a root commit not touching the paths
2666 * Detect and simplify both cases.
2669 int marked = mark_redundant_parents(revs, commit);
2670 marked += mark_treesame_root_parents(revs, commit);
2672 marked -= leave_one_treesame_to_parent(revs, commit);
2674 cnt = remove_marked_parents(revs, commit);
2678 * A commit simplifies to itself if it is a root, if it is
2679 * UNINTERESTING, if it touches the given paths, or if it is a
2680 * merge and its parents don't simplify to one relevant commit
2681 * (the first two cases are already handled at the beginning of
2684 * Otherwise, it simplifies to what its sole relevant parent
2688 (commit->object.flags & UNINTERESTING) ||
2689 !(commit->object.flags & TREESAME) ||
2690 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2691 st->simplified = commit;
2693 pst = locate_simplify_state(revs, parent);
2694 st->simplified = pst->simplified;
2699 static void simplify_merges(struct rev_info *revs)
2701 struct commit_list *list, *next;
2702 struct commit_list *yet_to_do, **tail;
2703 struct commit *commit;
2708 /* feed the list reversed */
2710 for (list = revs->commits; list; list = next) {
2711 commit = list->item;
2714 * Do not free(list) here yet; the original list
2715 * is used later in this function.
2717 commit_list_insert(commit, &yet_to_do);
2724 commit = list->item;
2728 tail = simplify_one(revs, commit, tail);
2732 /* clean up the result, removing the simplified ones */
2733 list = revs->commits;
2734 revs->commits = NULL;
2735 tail = &revs->commits;
2737 struct merge_simplify_state *st;
2739 commit = list->item;
2743 st = locate_simplify_state(revs, commit);
2744 if (st->simplified == commit)
2745 tail = &commit_list_insert(commit, tail)->next;
2749 static void set_children(struct rev_info *revs)
2751 struct commit_list *l;
2752 for (l = revs->commits; l; l = l->next) {
2753 struct commit *commit = l->item;
2754 struct commit_list *p;
2756 for (p = commit->parents; p; p = p->next)
2757 add_child(revs, p->item, commit);
2761 void reset_revision_walk(void)
2763 clear_object_flags(SEEN | ADDED | SHOWN);
2766 int prepare_revision_walk(struct rev_info *revs)
2769 struct object_array old_pending;
2770 struct commit_list **next = &revs->commits;
2772 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2773 revs->pending.nr = 0;
2774 revs->pending.alloc = 0;
2775 revs->pending.objects = NULL;
2776 for (i = 0; i < old_pending.nr; i++) {
2777 struct object_array_entry *e = old_pending.objects + i;
2778 struct commit *commit = handle_commit(revs, e);
2780 if (!(commit->object.flags & SEEN)) {
2781 commit->object.flags |= SEEN;
2782 next = commit_list_append(commit, next);
2786 if (!revs->leak_pending)
2787 object_array_clear(&old_pending);
2789 /* Signal whether we need per-parent treesame decoration */
2790 if (revs->simplify_merges ||
2791 (revs->limited && limiting_can_increase_treesame(revs)))
2792 revs->treesame.name = "treesame";
2794 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2795 commit_list_sort_by_date(&revs->commits);
2799 if (limit_list(revs) < 0)
2801 if (revs->topo_order)
2802 sort_in_topological_order(&revs->commits, revs->sort_order);
2803 if (revs->line_level_traverse)
2804 line_log_filter(revs);
2805 if (revs->simplify_merges)
2806 simplify_merges(revs);
2807 if (revs->children.name)
2812 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2814 struct commit_list *cache = NULL;
2817 struct commit *p = *pp;
2819 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2820 return rewrite_one_error;
2821 if (p->object.flags & UNINTERESTING)
2822 return rewrite_one_ok;
2823 if (!(p->object.flags & TREESAME))
2824 return rewrite_one_ok;
2826 return rewrite_one_noparents;
2827 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2828 return rewrite_one_ok;
2833 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2834 rewrite_parent_fn_t rewrite_parent)
2836 struct commit_list **pp = &commit->parents;
2838 struct commit_list *parent = *pp;
2839 switch (rewrite_parent(revs, &parent->item)) {
2840 case rewrite_one_ok:
2842 case rewrite_one_noparents:
2845 case rewrite_one_error:
2850 remove_duplicate_parents(revs, commit);
2854 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2856 char *person, *endp;
2857 size_t len, namelen, maillen;
2860 struct ident_split ident;
2862 person = strstr(buf->buf, what);
2866 person += strlen(what);
2867 endp = strchr(person, '\n');
2871 len = endp - person;
2873 if (split_ident_line(&ident, person, len))
2876 mail = ident.mail_begin;
2877 maillen = ident.mail_end - ident.mail_begin;
2878 name = ident.name_begin;
2879 namelen = ident.name_end - ident.name_begin;
2881 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2882 struct strbuf namemail = STRBUF_INIT;
2884 strbuf_addf(&namemail, "%.*s <%.*s>",
2885 (int)namelen, name, (int)maillen, mail);
2887 strbuf_splice(buf, ident.name_begin - buf->buf,
2888 ident.mail_end - ident.name_begin + 1,
2889 namemail.buf, namemail.len);
2891 strbuf_release(&namemail);
2899 static int commit_match(struct commit *commit, struct rev_info *opt)
2902 const char *encoding;
2903 const char *message;
2904 struct strbuf buf = STRBUF_INIT;
2906 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2909 /* Prepend "fake" headers as needed */
2910 if (opt->grep_filter.use_reflog_filter) {
2911 strbuf_addstr(&buf, "reflog ");
2912 get_reflog_message(&buf, opt->reflog_info);
2913 strbuf_addch(&buf, '\n');
2917 * We grep in the user's output encoding, under the assumption that it
2918 * is the encoding they are most likely to write their grep pattern
2919 * for. In addition, it means we will match the "notes" encoding below,
2920 * so we will not end up with a buffer that has two different encodings
2923 encoding = get_log_output_encoding();
2924 message = logmsg_reencode(commit, NULL, encoding);
2926 /* Copy the commit to temporary if we are using "fake" headers */
2928 strbuf_addstr(&buf, message);
2930 if (opt->grep_filter.header_list && opt->mailmap) {
2932 strbuf_addstr(&buf, message);
2934 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2935 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2938 /* Append "fake" message parts as needed */
2939 if (opt->show_notes) {
2941 strbuf_addstr(&buf, message);
2942 format_display_notes(commit->object.sha1, &buf, encoding, 1);
2946 * Find either in the original commit message, or in the temporary.
2947 * Note that we cast away the constness of "message" here. It is
2948 * const because it may come from the cached commit buffer. That's OK,
2949 * because we know that it is modifiable heap memory, and that while
2950 * grep_buffer may modify it for speed, it will restore any
2951 * changes before returning.
2954 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2956 retval = grep_buffer(&opt->grep_filter,
2957 (char *)message, strlen(message));
2958 strbuf_release(&buf);
2959 unuse_commit_buffer(commit, message);
2960 return opt->invert_grep ? !retval : retval;
2963 static inline int want_ancestry(const struct rev_info *revs)
2965 return (revs->rewrite_parents || revs->children.name);
2968 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2970 if (commit->object.flags & SHOWN)
2971 return commit_ignore;
2972 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2973 return commit_ignore;
2976 if (commit->object.flags & UNINTERESTING)
2977 return commit_ignore;
2978 if (revs->min_age != -1 && (commit->date > revs->min_age))
2979 return commit_ignore;
2980 if (revs->min_parents || (revs->max_parents >= 0)) {
2981 int n = commit_list_count(commit->parents);
2982 if ((n < revs->min_parents) ||
2983 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2984 return commit_ignore;
2986 if (!commit_match(commit, revs))
2987 return commit_ignore;
2988 if (revs->prune && revs->dense) {
2989 /* Commit without changes? */
2990 if (commit->object.flags & TREESAME) {
2992 struct commit_list *p;
2993 /* drop merges unless we want parenthood */
2994 if (!want_ancestry(revs))
2995 return commit_ignore;
2997 * If we want ancestry, then need to keep any merges
2998 * between relevant commits to tie together topology.
2999 * For consistency with TREESAME and simplification
3000 * use "relevant" here rather than just INTERESTING,
3001 * to treat bottom commit(s) as part of the topology.
3003 for (n = 0, p = commit->parents; p; p = p->next)
3004 if (relevant_commit(p->item))
3007 return commit_ignore;
3013 define_commit_slab(saved_parents, struct commit_list *);
3015 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3018 * You may only call save_parents() once per commit (this is checked
3019 * for non-root commits).
3021 static void save_parents(struct rev_info *revs, struct commit *commit)
3023 struct commit_list **pp;
3025 if (!revs->saved_parents_slab) {
3026 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3027 init_saved_parents(revs->saved_parents_slab);
3030 pp = saved_parents_at(revs->saved_parents_slab, commit);
3033 * When walking with reflogs, we may visit the same commit
3034 * several times: once for each appearance in the reflog.
3036 * In this case, save_parents() will be called multiple times.
3037 * We want to keep only the first set of parents. We need to
3038 * store a sentinel value for an empty (i.e., NULL) parent
3039 * list to distinguish it from a not-yet-saved list, however.
3043 if (commit->parents)
3044 *pp = copy_commit_list(commit->parents);
3046 *pp = EMPTY_PARENT_LIST;
3049 static void free_saved_parents(struct rev_info *revs)
3051 if (revs->saved_parents_slab)
3052 clear_saved_parents(revs->saved_parents_slab);
3055 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3057 struct commit_list *parents;
3059 if (!revs->saved_parents_slab)
3060 return commit->parents;
3062 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3063 if (parents == EMPTY_PARENT_LIST)
3068 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3070 enum commit_action action = get_commit_action(revs, commit);
3072 if (action == commit_show &&
3074 revs->prune && revs->dense && want_ancestry(revs)) {
3076 * --full-diff on simplified parents is no good: it
3077 * will show spurious changes from the commits that
3078 * were elided. So we save the parents on the side
3079 * when --full-diff is in effect.
3081 if (revs->full_diff)
3082 save_parents(revs, commit);
3083 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3084 return commit_error;
3089 static void track_linear(struct rev_info *revs, struct commit *commit)
3091 if (revs->track_first_time) {
3093 revs->track_first_time = 0;
3095 struct commit_list *p;
3096 for (p = revs->previous_parents; p; p = p->next)
3097 if (p->item == NULL || /* first commit */
3098 !hashcmp(p->item->object.sha1, commit->object.sha1))
3100 revs->linear = p != NULL;
3102 if (revs->reverse) {
3104 commit->object.flags |= TRACK_LINEAR;
3106 free_commit_list(revs->previous_parents);
3107 revs->previous_parents = copy_commit_list(commit->parents);
3110 static struct commit *get_revision_1(struct rev_info *revs)
3116 struct commit_list *entry = revs->commits;
3117 struct commit *commit = entry->item;
3119 revs->commits = entry->next;
3122 if (revs->reflog_info) {
3123 save_parents(revs, commit);
3124 fake_reflog_parent(revs->reflog_info, commit);
3125 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3129 * If we haven't done the list limiting, we need to look at
3130 * the parents here. We also need to do the date-based limiting
3131 * that we'd otherwise have done in limit_list().
3133 if (!revs->limited) {
3134 if (revs->max_age != -1 &&
3135 (commit->date < revs->max_age))
3137 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3138 if (!revs->ignore_missing_links)
3139 die("Failed to traverse parents of commit %s",
3140 sha1_to_hex(commit->object.sha1));
3144 switch (simplify_commit(revs, commit)) {
3148 die("Failed to simplify parents of commit %s",
3149 sha1_to_hex(commit->object.sha1));
3151 if (revs->track_linear)
3152 track_linear(revs, commit);
3155 } while (revs->commits);
3160 * Return true for entries that have not yet been shown. (This is an
3161 * object_array_each_func_t.)
3163 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3165 return !(entry->item->flags & SHOWN);
3169 * If array is on the verge of a realloc, garbage-collect any entries
3170 * that have already been shown to try to free up some space.
3172 static void gc_boundary(struct object_array *array)
3174 if (array->nr == array->alloc)
3175 object_array_filter(array, entry_unshown, NULL);
3178 static void create_boundary_commit_list(struct rev_info *revs)
3182 struct object_array *array = &revs->boundary_commits;
3183 struct object_array_entry *objects = array->objects;
3186 * If revs->commits is non-NULL at this point, an error occurred in
3187 * get_revision_1(). Ignore the error and continue printing the
3188 * boundary commits anyway. (This is what the code has always
3191 if (revs->commits) {
3192 free_commit_list(revs->commits);
3193 revs->commits = NULL;
3197 * Put all of the actual boundary commits from revs->boundary_commits
3198 * into revs->commits
3200 for (i = 0; i < array->nr; i++) {
3201 c = (struct commit *)(objects[i].item);
3204 if (!(c->object.flags & CHILD_SHOWN))
3206 if (c->object.flags & (SHOWN | BOUNDARY))
3208 c->object.flags |= BOUNDARY;
3209 commit_list_insert(c, &revs->commits);
3213 * If revs->topo_order is set, sort the boundary commits
3214 * in topological order
3216 sort_in_topological_order(&revs->commits, revs->sort_order);
3219 static struct commit *get_revision_internal(struct rev_info *revs)
3221 struct commit *c = NULL;
3222 struct commit_list *l;
3224 if (revs->boundary == 2) {
3226 * All of the normal commits have already been returned,
3227 * and we are now returning boundary commits.
3228 * create_boundary_commit_list() has populated
3229 * revs->commits with the remaining commits to return.
3231 c = pop_commit(&revs->commits);
3233 c->object.flags |= SHOWN;
3238 * If our max_count counter has reached zero, then we are done. We
3239 * don't simply return NULL because we still might need to show
3240 * boundary commits. But we want to avoid calling get_revision_1, which
3241 * might do a considerable amount of work finding the next commit only
3242 * for us to throw it away.
3244 * If it is non-zero, then either we don't have a max_count at all
3245 * (-1), or it is still counting, in which case we decrement.
3247 if (revs->max_count) {
3248 c = get_revision_1(revs);
3250 while (revs->skip_count > 0) {
3252 c = get_revision_1(revs);
3258 if (revs->max_count > 0)
3263 c->object.flags |= SHOWN;
3265 if (!revs->boundary)
3270 * get_revision_1() runs out the commits, and
3271 * we are done computing the boundaries.
3272 * switch to boundary commits output mode.
3277 * Update revs->commits to contain the list of
3280 create_boundary_commit_list(revs);
3282 return get_revision_internal(revs);
3286 * boundary commits are the commits that are parents of the
3287 * ones we got from get_revision_1() but they themselves are
3288 * not returned from get_revision_1(). Before returning
3289 * 'c', we need to mark its parents that they could be boundaries.
3292 for (l = c->parents; l; l = l->next) {
3294 p = &(l->item->object);
3295 if (p->flags & (CHILD_SHOWN | SHOWN))
3297 p->flags |= CHILD_SHOWN;
3298 gc_boundary(&revs->boundary_commits);
3299 add_object_array(p, NULL, &revs->boundary_commits);
3305 struct commit *get_revision(struct rev_info *revs)
3308 struct commit_list *reversed;
3310 if (revs->reverse) {
3312 while ((c = get_revision_internal(revs)))
3313 commit_list_insert(c, &reversed);
3314 revs->commits = reversed;
3316 revs->reverse_output_stage = 1;
3319 if (revs->reverse_output_stage) {
3320 c = pop_commit(&revs->commits);
3321 if (revs->track_linear)
3322 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3326 c = get_revision_internal(revs);
3327 if (c && revs->graph)
3328 graph_update(revs->graph, c);
3330 free_saved_parents(revs);
3331 if (revs->previous_parents) {
3332 free_commit_list(revs->previous_parents);
3333 revs->previous_parents = NULL;
3339 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3341 if (commit->object.flags & BOUNDARY)
3343 else if (commit->object.flags & UNINTERESTING)
3345 else if (commit->object.flags & PATCHSAME)
3347 else if (!revs || revs->left_right) {
3348 if (commit->object.flags & SYMMETRIC_LEFT)
3352 } else if (revs->graph)
3354 else if (revs->cherry_mark)
3359 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3361 char *mark = get_revision_mark(revs, commit);
3364 fputs(mark, stdout);