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"
23 volatile show_early_output_fn_t show_early_output;
25 static const char *term_bad;
26 static const char *term_good;
28 char *path_name(const struct name_path *path, const char *name)
30 const struct name_path *p;
32 int nlen = strlen(name);
35 for (p = path; p; p = p->up) {
37 len += p->elem_len + 1;
40 m = n + len - (nlen + 1);
41 memcpy(m, name, nlen + 1);
42 for (p = path; p; p = p->up) {
45 memcpy(m, p->elem, p->elem_len);
52 static int show_path_component_truncated(FILE *out, const char *name, int len)
55 for (cnt = 0; cnt < len; cnt++) {
57 if (!ch || ch == '\n')
64 static int show_path_truncated(FILE *out, const struct name_path *path)
70 emitted = show_path_truncated(out, path->up);
75 ours = show_path_component_truncated(out, path->elem, path->elem_len);
78 return ours || emitted;
81 void show_object_with_name(FILE *out, struct object *obj,
82 const struct name_path *path, const char *component)
84 struct name_path leaf;
85 leaf.up = (struct name_path *)path;
86 leaf.elem = component;
87 leaf.elem_len = strlen(component);
89 fprintf(out, "%s ", oid_to_hex(&obj->oid));
90 show_path_truncated(out, &leaf);
94 static void mark_blob_uninteresting(struct blob *blob)
98 if (blob->object.flags & UNINTERESTING)
100 blob->object.flags |= UNINTERESTING;
103 static void mark_tree_contents_uninteresting(struct tree *tree)
105 struct tree_desc desc;
106 struct name_entry entry;
107 struct object *obj = &tree->object;
109 if (!has_object_file(&obj->oid))
111 if (parse_tree(tree) < 0)
112 die("bad tree %s", oid_to_hex(&obj->oid));
114 init_tree_desc(&desc, tree->buffer, tree->size);
115 while (tree_entry(&desc, &entry)) {
116 switch (object_type(entry.mode)) {
118 mark_tree_uninteresting(lookup_tree(entry.sha1));
121 mark_blob_uninteresting(lookup_blob(entry.sha1));
124 /* Subproject commit - not in this repository */
130 * We don't care about the tree any more
131 * after it has been marked uninteresting.
133 free_tree_buffer(tree);
136 void mark_tree_uninteresting(struct tree *tree)
144 if (obj->flags & UNINTERESTING)
146 obj->flags |= UNINTERESTING;
147 mark_tree_contents_uninteresting(tree);
150 void mark_parents_uninteresting(struct commit *commit)
152 struct commit_list *parents = NULL, *l;
154 for (l = commit->parents; l; l = l->next)
155 commit_list_insert(l->item, &parents);
158 struct commit *commit = pop_commit(&parents);
162 * A missing commit is ok iff its parent is marked
165 * We just mark such a thing parsed, so that when
166 * it is popped next time around, we won't be trying
167 * to parse it and get an error.
169 if (!has_object_file(&commit->object.oid))
170 commit->object.parsed = 1;
172 if (commit->object.flags & UNINTERESTING)
175 commit->object.flags |= UNINTERESTING;
178 * Normally we haven't parsed the parent
179 * yet, so we won't have a parent of a parent
180 * here. However, it may turn out that we've
181 * reached this commit some other way (where it
182 * wasn't uninteresting), in which case we need
183 * to mark its parents recursively too..
185 if (!commit->parents)
188 for (l = commit->parents->next; l; l = l->next)
189 commit_list_insert(l->item, &parents);
190 commit = commit->parents->item;
195 static void add_pending_object_with_path(struct rev_info *revs,
197 const char *name, unsigned mode,
202 if (revs->no_walk && (obj->flags & UNINTERESTING))
204 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
205 struct strbuf buf = STRBUF_INIT;
206 int len = interpret_branch_name(name, 0, &buf);
209 if (0 < len && name[len] && buf.len)
210 strbuf_addstr(&buf, name + len);
211 st = add_reflog_for_walk(revs->reflog_info,
212 (struct commit *)obj,
213 buf.buf[0] ? buf.buf: name);
214 strbuf_release(&buf);
218 add_object_array_with_path(obj, name, &revs->pending, mode, path);
221 static void add_pending_object_with_mode(struct rev_info *revs,
223 const char *name, unsigned mode)
225 add_pending_object_with_path(revs, obj, name, mode, NULL);
228 void add_pending_object(struct rev_info *revs,
229 struct object *obj, const char *name)
231 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
234 void add_head_to_pending(struct rev_info *revs)
236 unsigned char sha1[20];
238 if (get_sha1("HEAD", sha1))
240 obj = parse_object(sha1);
243 add_pending_object(revs, obj, "HEAD");
246 static struct object *get_reference(struct rev_info *revs, const char *name,
247 const unsigned char *sha1,
250 struct object *object;
252 object = parse_object(sha1);
254 if (revs->ignore_missing)
256 die("bad object %s", name);
258 object->flags |= flags;
262 void add_pending_sha1(struct rev_info *revs, const char *name,
263 const unsigned char *sha1, unsigned int flags)
265 struct object *object = get_reference(revs, name, sha1, flags);
266 add_pending_object(revs, object, name);
269 static struct commit *handle_commit(struct rev_info *revs,
270 struct object_array_entry *entry)
272 struct object *object = entry->item;
273 const char *name = entry->name;
274 const char *path = entry->path;
275 unsigned int mode = entry->mode;
276 unsigned long flags = object->flags;
279 * Tag object? Look what it points to..
281 while (object->type == OBJ_TAG) {
282 struct tag *tag = (struct tag *) object;
283 if (revs->tag_objects && !(flags & UNINTERESTING))
284 add_pending_object(revs, object, tag->tag);
287 object = parse_object(tag->tagged->oid.hash);
289 if (flags & UNINTERESTING)
291 die("bad object %s", oid_to_hex(&tag->tagged->oid));
293 object->flags |= flags;
295 * We'll handle the tagged object by looping or dropping
296 * through to the non-tag handlers below. Do not
297 * propagate data from the tag's pending entry.
305 * Commit object? Just return it, we'll do all the complex
308 if (object->type == OBJ_COMMIT) {
309 struct commit *commit = (struct commit *)object;
310 if (parse_commit(commit) < 0)
311 die("unable to parse commit %s", name);
312 if (flags & UNINTERESTING) {
313 mark_parents_uninteresting(commit);
316 if (revs->show_source && !commit->util)
317 commit->util = xstrdup(name);
322 * Tree object? Either mark it uninteresting, or add it
323 * to the list of objects to look at later..
325 if (object->type == OBJ_TREE) {
326 struct tree *tree = (struct tree *)object;
327 if (!revs->tree_objects)
329 if (flags & UNINTERESTING) {
330 mark_tree_contents_uninteresting(tree);
333 add_pending_object_with_path(revs, object, name, mode, path);
338 * Blob object? You know the drill by now..
340 if (object->type == OBJ_BLOB) {
341 if (!revs->blob_objects)
343 if (flags & UNINTERESTING)
345 add_pending_object_with_path(revs, object, name, mode, path);
348 die("%s is unknown object", name);
351 static int everybody_uninteresting(struct commit_list *orig,
352 struct commit **interesting_cache)
354 struct commit_list *list = orig;
356 if (*interesting_cache) {
357 struct commit *commit = *interesting_cache;
358 if (!(commit->object.flags & UNINTERESTING))
363 struct commit *commit = list->item;
365 if (commit->object.flags & UNINTERESTING)
368 *interesting_cache = commit;
375 * A definition of "relevant" commit that we can use to simplify limited graphs
376 * by eliminating side branches.
378 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
379 * in our list), or that is a specified BOTTOM commit. Then after computing
380 * a limited list, during processing we can generally ignore boundary merges
381 * coming from outside the graph, (ie from irrelevant parents), and treat
382 * those merges as if they were single-parent. TREESAME is defined to consider
383 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
384 * we don't care if we were !TREESAME to non-graph parents.
386 * Treating bottom commits as relevant ensures that a limited graph's
387 * connection to the actual bottom commit is not viewed as a side branch, but
388 * treated as part of the graph. For example:
390 * ....Z...A---X---o---o---B
394 * When computing "A..B", the A-X connection is at least as important as
395 * Y-X, despite A being flagged UNINTERESTING.
397 * And when computing --ancestry-path "A..B", the A-X connection is more
398 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
400 static inline int relevant_commit(struct commit *commit)
402 return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
406 * Return a single relevant commit from a parent list. If we are a TREESAME
407 * commit, and this selects one of our parents, then we can safely simplify to
410 static struct commit *one_relevant_parent(const struct rev_info *revs,
411 struct commit_list *orig)
413 struct commit_list *list = orig;
414 struct commit *relevant = NULL;
420 * For 1-parent commits, or if first-parent-only, then return that
421 * first parent (even if not "relevant" by the above definition).
422 * TREESAME will have been set purely on that parent.
424 if (revs->first_parent_only || !orig->next)
428 * For multi-parent commits, identify a sole relevant parent, if any.
429 * If we have only one relevant parent, then TREESAME will be set purely
430 * with regard to that parent, and we can simplify accordingly.
432 * If we have more than one relevant parent, or no relevant parents
433 * (and multiple irrelevant ones), then we can't select a parent here
437 struct commit *commit = list->item;
439 if (relevant_commit(commit)) {
449 * The goal is to get REV_TREE_NEW as the result only if the
450 * diff consists of all '+' (and no other changes), REV_TREE_OLD
451 * if the whole diff is removal of old data, and otherwise
452 * REV_TREE_DIFFERENT (of course if the trees are the same we
453 * want REV_TREE_SAME).
454 * That means that once we get to REV_TREE_DIFFERENT, we do not
455 * have to look any further.
457 static int tree_difference = REV_TREE_SAME;
459 static void file_add_remove(struct diff_options *options,
460 int addremove, unsigned mode,
461 const unsigned char *sha1,
463 const char *fullpath, unsigned dirty_submodule)
465 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
467 tree_difference |= diff;
468 if (tree_difference == REV_TREE_DIFFERENT)
469 DIFF_OPT_SET(options, HAS_CHANGES);
472 static void file_change(struct diff_options *options,
473 unsigned old_mode, unsigned new_mode,
474 const unsigned char *old_sha1,
475 const unsigned char *new_sha1,
476 int old_sha1_valid, int new_sha1_valid,
477 const char *fullpath,
478 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
480 tree_difference = REV_TREE_DIFFERENT;
481 DIFF_OPT_SET(options, HAS_CHANGES);
484 static int rev_compare_tree(struct rev_info *revs,
485 struct commit *parent, struct commit *commit)
487 struct tree *t1 = parent->tree;
488 struct tree *t2 = commit->tree;
495 if (revs->simplify_by_decoration) {
497 * If we are simplifying by decoration, then the commit
498 * is worth showing if it has a tag pointing at it.
500 if (get_name_decoration(&commit->object))
501 return REV_TREE_DIFFERENT;
503 * A commit that is not pointed by a tag is uninteresting
504 * if we are not limited by path. This means that you will
505 * see the usual "commits that touch the paths" plus any
506 * tagged commit by specifying both --simplify-by-decoration
509 if (!revs->prune_data.nr)
510 return REV_TREE_SAME;
513 tree_difference = REV_TREE_SAME;
514 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
515 if (diff_tree_sha1(t1->object.oid.hash, t2->object.oid.hash, "",
517 return REV_TREE_DIFFERENT;
518 return tree_difference;
521 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
524 struct tree *t1 = commit->tree;
529 tree_difference = REV_TREE_SAME;
530 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
531 retval = diff_tree_sha1(NULL, t1->object.oid.hash, "", &revs->pruning);
533 return retval >= 0 && (tree_difference == REV_TREE_SAME);
536 struct treesame_state {
537 unsigned int nparents;
538 unsigned char treesame[FLEX_ARRAY];
541 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
543 unsigned n = commit_list_count(commit->parents);
544 struct treesame_state *st = xcalloc(1, sizeof(*st) + n);
546 add_decoration(&revs->treesame, &commit->object, st);
551 * Must be called immediately after removing the nth_parent from a commit's
552 * parent list, if we are maintaining the per-parent treesame[] decoration.
553 * This does not recalculate the master TREESAME flag - update_treesame()
554 * should be called to update it after a sequence of treesame[] modifications
555 * that may have affected it.
557 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
559 struct treesame_state *st;
562 if (!commit->parents) {
564 * Have just removed the only parent from a non-merge.
565 * Different handling, as we lack decoration.
568 die("compact_treesame %u", nth_parent);
569 old_same = !!(commit->object.flags & TREESAME);
570 if (rev_same_tree_as_empty(revs, commit))
571 commit->object.flags |= TREESAME;
573 commit->object.flags &= ~TREESAME;
577 st = lookup_decoration(&revs->treesame, &commit->object);
578 if (!st || nth_parent >= st->nparents)
579 die("compact_treesame %u", nth_parent);
581 old_same = st->treesame[nth_parent];
582 memmove(st->treesame + nth_parent,
583 st->treesame + nth_parent + 1,
584 st->nparents - nth_parent - 1);
587 * If we've just become a non-merge commit, update TREESAME
588 * immediately, and remove the no-longer-needed decoration.
589 * If still a merge, defer update until update_treesame().
591 if (--st->nparents == 1) {
592 if (commit->parents->next)
593 die("compact_treesame parents mismatch");
594 if (st->treesame[0] && revs->dense)
595 commit->object.flags |= TREESAME;
597 commit->object.flags &= ~TREESAME;
598 free(add_decoration(&revs->treesame, &commit->object, NULL));
604 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
606 if (commit->parents && commit->parents->next) {
608 struct treesame_state *st;
609 struct commit_list *p;
610 unsigned relevant_parents;
611 unsigned relevant_change, irrelevant_change;
613 st = lookup_decoration(&revs->treesame, &commit->object);
615 die("update_treesame %s", oid_to_hex(&commit->object.oid));
616 relevant_parents = 0;
617 relevant_change = irrelevant_change = 0;
618 for (p = commit->parents, n = 0; p; n++, p = p->next) {
619 if (relevant_commit(p->item)) {
620 relevant_change |= !st->treesame[n];
623 irrelevant_change |= !st->treesame[n];
625 if (relevant_parents ? relevant_change : irrelevant_change)
626 commit->object.flags &= ~TREESAME;
628 commit->object.flags |= TREESAME;
631 return commit->object.flags & TREESAME;
634 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
637 * TREESAME is irrelevant unless prune && dense;
638 * if simplify_history is set, we can't have a mixture of TREESAME and
639 * !TREESAME INTERESTING parents (and we don't have treesame[]
640 * decoration anyway);
641 * if first_parent_only is set, then the TREESAME flag is locked
642 * against the first parent (and again we lack treesame[] decoration).
644 return revs->prune && revs->dense &&
645 !revs->simplify_history &&
646 !revs->first_parent_only;
649 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
651 struct commit_list **pp, *parent;
652 struct treesame_state *ts = NULL;
653 int relevant_change = 0, irrelevant_change = 0;
654 int relevant_parents, nth_parent;
657 * If we don't do pruning, everything is interesting
665 if (!commit->parents) {
666 if (rev_same_tree_as_empty(revs, commit))
667 commit->object.flags |= TREESAME;
672 * Normal non-merge commit? If we don't want to make the
673 * history dense, we consider it always to be a change..
675 if (!revs->dense && !commit->parents->next)
678 for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
679 (parent = *pp) != NULL;
680 pp = &parent->next, nth_parent++) {
681 struct commit *p = parent->item;
682 if (relevant_commit(p))
685 if (nth_parent == 1) {
687 * This our second loop iteration - so we now know
688 * we're dealing with a merge.
690 * Do not compare with later parents when we care only about
691 * the first parent chain, in order to avoid derailing the
692 * traversal to follow a side branch that brought everything
693 * in the path we are limited to by the pathspec.
695 if (revs->first_parent_only)
698 * If this will remain a potentially-simplifiable
699 * merge, remember per-parent treesame if needed.
700 * Initialise the array with the comparison from our
703 if (revs->treesame.name &&
704 !revs->simplify_history &&
705 !(commit->object.flags & UNINTERESTING)) {
706 ts = initialise_treesame(revs, commit);
707 if (!(irrelevant_change || relevant_change))
711 if (parse_commit(p) < 0)
712 die("cannot simplify commit %s (because of %s)",
713 oid_to_hex(&commit->object.oid),
714 oid_to_hex(&p->object.oid));
715 switch (rev_compare_tree(revs, p, commit)) {
717 if (!revs->simplify_history || !relevant_commit(p)) {
718 /* Even if a merge with an uninteresting
719 * side branch brought the entire change
720 * we are interested in, we do not want
721 * to lose the other branches of this
722 * merge, so we just keep going.
725 ts->treesame[nth_parent] = 1;
729 commit->parents = parent;
730 commit->object.flags |= TREESAME;
734 if (revs->remove_empty_trees &&
735 rev_same_tree_as_empty(revs, p)) {
736 /* We are adding all the specified
737 * paths from this parent, so the
738 * history beyond this parent is not
739 * interesting. Remove its parents
740 * (they are grandparents for us).
741 * IOW, we pretend this parent is a
744 if (parse_commit(p) < 0)
745 die("cannot simplify commit %s (invalid %s)",
746 oid_to_hex(&commit->object.oid),
747 oid_to_hex(&p->object.oid));
752 case REV_TREE_DIFFERENT:
753 if (relevant_commit(p))
756 irrelevant_change = 1;
759 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
763 * TREESAME is straightforward for single-parent commits. For merge
764 * commits, it is most useful to define it so that "irrelevant"
765 * parents cannot make us !TREESAME - if we have any relevant
766 * parents, then we only consider TREESAMEness with respect to them,
767 * allowing irrelevant merges from uninteresting branches to be
768 * simplified away. Only if we have only irrelevant parents do we
769 * base TREESAME on them. Note that this logic is replicated in
770 * update_treesame, which should be kept in sync.
772 if (relevant_parents ? !relevant_change : !irrelevant_change)
773 commit->object.flags |= TREESAME;
776 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
777 struct commit_list *cached_base, struct commit_list **cache)
779 struct commit_list *new_entry;
781 if (cached_base && p->date < cached_base->item->date)
782 new_entry = commit_list_insert_by_date(p, &cached_base->next);
784 new_entry = commit_list_insert_by_date(p, head);
786 if (cache && (!*cache || p->date < (*cache)->item->date))
790 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
791 struct commit_list **list, struct commit_list **cache_ptr)
793 struct commit_list *parent = commit->parents;
795 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
797 if (commit->object.flags & ADDED)
799 commit->object.flags |= ADDED;
801 if (revs->include_check &&
802 !revs->include_check(commit, revs->include_check_data))
806 * If the commit is uninteresting, don't try to
807 * prune parents - we want the maximal uninteresting
810 * Normally we haven't parsed the parent
811 * yet, so we won't have a parent of a parent
812 * here. However, it may turn out that we've
813 * reached this commit some other way (where it
814 * wasn't uninteresting), in which case we need
815 * to mark its parents recursively too..
817 if (commit->object.flags & UNINTERESTING) {
819 struct commit *p = parent->item;
820 parent = parent->next;
822 p->object.flags |= UNINTERESTING;
823 if (parse_commit_gently(p, 1) < 0)
826 mark_parents_uninteresting(p);
827 if (p->object.flags & SEEN)
829 p->object.flags |= SEEN;
830 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
836 * Ok, the commit wasn't uninteresting. Try to
837 * simplify the commit history and find the parent
838 * that has no differences in the path set if one exists.
840 try_to_simplify_commit(revs, commit);
845 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
847 for (parent = commit->parents; parent; parent = parent->next) {
848 struct commit *p = parent->item;
850 if (parse_commit_gently(p, revs->ignore_missing_links) < 0)
852 if (revs->show_source && !p->util)
853 p->util = commit->util;
854 p->object.flags |= left_flag;
855 if (!(p->object.flags & SEEN)) {
856 p->object.flags |= SEEN;
857 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
859 if (revs->first_parent_only)
865 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
867 struct commit_list *p;
868 int left_count = 0, right_count = 0;
870 struct patch_ids ids;
871 unsigned cherry_flag;
873 /* First count the commits on the left and on the right */
874 for (p = list; p; p = p->next) {
875 struct commit *commit = p->item;
876 unsigned flags = commit->object.flags;
877 if (flags & BOUNDARY)
879 else if (flags & SYMMETRIC_LEFT)
885 if (!left_count || !right_count)
888 left_first = left_count < right_count;
889 init_patch_ids(&ids);
890 ids.diffopts.pathspec = revs->diffopt.pathspec;
892 /* Compute patch-ids for one side */
893 for (p = list; p; p = p->next) {
894 struct commit *commit = p->item;
895 unsigned flags = commit->object.flags;
897 if (flags & BOUNDARY)
900 * If we have fewer left, left_first is set and we omit
901 * commits on the right branch in this loop. If we have
902 * fewer right, we skip the left ones.
904 if (left_first != !!(flags & SYMMETRIC_LEFT))
906 commit->util = add_commit_patch_id(commit, &ids);
909 /* either cherry_mark or cherry_pick are true */
910 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
912 /* Check the other side */
913 for (p = list; p; p = p->next) {
914 struct commit *commit = p->item;
916 unsigned flags = commit->object.flags;
918 if (flags & BOUNDARY)
921 * If we have fewer left, left_first is set and we omit
922 * commits on the left branch in this loop.
924 if (left_first == !!(flags & SYMMETRIC_LEFT))
928 * Have we seen the same patch id?
930 id = has_commit_patch_id(commit, &ids);
934 commit->object.flags |= cherry_flag;
937 /* Now check the original side for seen ones */
938 for (p = list; p; p = p->next) {
939 struct commit *commit = p->item;
940 struct patch_id *ent;
946 commit->object.flags |= cherry_flag;
950 free_patch_ids(&ids);
953 /* How many extra uninteresting commits we want to see.. */
956 static int still_interesting(struct commit_list *src, unsigned long date, int slop,
957 struct commit **interesting_cache)
960 * No source list at all? We're definitely done..
966 * Does the destination list contain entries with a date
967 * before the source list? Definitely _not_ done.
969 if (date <= src->item->date)
973 * Does the source list still have interesting commits in
974 * it? Definitely not done..
976 if (!everybody_uninteresting(src, interesting_cache))
979 /* Ok, we're closing in.. */
984 * "rev-list --ancestry-path A..B" computes commits that are ancestors
985 * of B but not ancestors of A but further limits the result to those
986 * that are descendants of A. This takes the list of bottom commits and
987 * the result of "A..B" without --ancestry-path, and limits the latter
988 * further to the ones that can reach one of the commits in "bottom".
990 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
992 struct commit_list *p;
993 struct commit_list *rlist = NULL;
997 * Reverse the list so that it will be likely that we would
998 * process parents before children.
1000 for (p = list; p; p = p->next)
1001 commit_list_insert(p->item, &rlist);
1003 for (p = bottom; p; p = p->next)
1004 p->item->object.flags |= TMP_MARK;
1007 * Mark the ones that can reach bottom commits in "list",
1008 * in a bottom-up fashion.
1012 for (p = rlist; p; p = p->next) {
1013 struct commit *c = p->item;
1014 struct commit_list *parents;
1015 if (c->object.flags & (TMP_MARK | UNINTERESTING))
1017 for (parents = c->parents;
1019 parents = parents->next) {
1020 if (!(parents->item->object.flags & TMP_MARK))
1022 c->object.flags |= TMP_MARK;
1027 } while (made_progress);
1030 * NEEDSWORK: decide if we want to remove parents that are
1031 * not marked with TMP_MARK from commit->parents for commits
1032 * in the resulting list. We may not want to do that, though.
1036 * The ones that are not marked with TMP_MARK are uninteresting
1038 for (p = list; p; p = p->next) {
1039 struct commit *c = p->item;
1040 if (c->object.flags & TMP_MARK)
1042 c->object.flags |= UNINTERESTING;
1045 /* We are done with the TMP_MARK */
1046 for (p = list; p; p = p->next)
1047 p->item->object.flags &= ~TMP_MARK;
1048 for (p = bottom; p; p = p->next)
1049 p->item->object.flags &= ~TMP_MARK;
1050 free_commit_list(rlist);
1054 * Before walking the history, keep the set of "negative" refs the
1055 * caller has asked to exclude.
1057 * This is used to compute "rev-list --ancestry-path A..B", as we need
1058 * to filter the result of "A..B" further to the ones that can actually
1061 static struct commit_list *collect_bottom_commits(struct commit_list *list)
1063 struct commit_list *elem, *bottom = NULL;
1064 for (elem = list; elem; elem = elem->next)
1065 if (elem->item->object.flags & BOTTOM)
1066 commit_list_insert(elem->item, &bottom);
1070 /* Assumes either left_only or right_only is set */
1071 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1073 struct commit_list *p;
1075 for (p = list; p; p = p->next) {
1076 struct commit *commit = p->item;
1078 if (revs->right_only) {
1079 if (commit->object.flags & SYMMETRIC_LEFT)
1080 commit->object.flags |= SHOWN;
1081 } else /* revs->left_only is set */
1082 if (!(commit->object.flags & SYMMETRIC_LEFT))
1083 commit->object.flags |= SHOWN;
1087 static int limit_list(struct rev_info *revs)
1090 unsigned long date = ~0ul;
1091 struct commit_list *list = revs->commits;
1092 struct commit_list *newlist = NULL;
1093 struct commit_list **p = &newlist;
1094 struct commit_list *bottom = NULL;
1095 struct commit *interesting_cache = NULL;
1097 if (revs->ancestry_path) {
1098 bottom = collect_bottom_commits(list);
1100 die("--ancestry-path given but there are no bottom commits");
1104 struct commit *commit = pop_commit(&list);
1105 struct object *obj = &commit->object;
1106 show_early_output_fn_t show;
1108 if (commit == interesting_cache)
1109 interesting_cache = NULL;
1111 if (revs->max_age != -1 && (commit->date < revs->max_age))
1112 obj->flags |= UNINTERESTING;
1113 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1115 if (obj->flags & UNINTERESTING) {
1116 mark_parents_uninteresting(commit);
1118 p = &commit_list_insert(commit, p)->next;
1119 slop = still_interesting(list, date, slop, &interesting_cache);
1122 /* If showing all, add the whole pending list to the end */
1127 if (revs->min_age != -1 && (commit->date > revs->min_age))
1129 date = commit->date;
1130 p = &commit_list_insert(commit, p)->next;
1132 show = show_early_output;
1136 show(revs, newlist);
1137 show_early_output = NULL;
1139 if (revs->cherry_pick || revs->cherry_mark)
1140 cherry_pick_list(newlist, revs);
1142 if (revs->left_only || revs->right_only)
1143 limit_left_right(newlist, revs);
1146 limit_to_ancestry(bottom, newlist);
1147 free_commit_list(bottom);
1151 * Check if any commits have become TREESAME by some of their parents
1152 * becoming UNINTERESTING.
1154 if (limiting_can_increase_treesame(revs))
1155 for (list = newlist; list; list = list->next) {
1156 struct commit *c = list->item;
1157 if (c->object.flags & (UNINTERESTING | TREESAME))
1159 update_treesame(revs, c);
1162 revs->commits = newlist;
1167 * Add an entry to refs->cmdline with the specified information.
1170 static void add_rev_cmdline(struct rev_info *revs,
1171 struct object *item,
1176 struct rev_cmdline_info *info = &revs->cmdline;
1179 ALLOC_GROW(info->rev, nr + 1, info->alloc);
1180 info->rev[nr].item = item;
1181 info->rev[nr].name = xstrdup(name);
1182 info->rev[nr].whence = whence;
1183 info->rev[nr].flags = flags;
1187 static void add_rev_cmdline_list(struct rev_info *revs,
1188 struct commit_list *commit_list,
1192 while (commit_list) {
1193 struct object *object = &commit_list->item->object;
1194 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1196 commit_list = commit_list->next;
1200 struct all_refs_cb {
1202 int warned_bad_reflog;
1203 struct rev_info *all_revs;
1204 const char *name_for_errormsg;
1207 int ref_excluded(struct string_list *ref_excludes, const char *path)
1209 struct string_list_item *item;
1213 for_each_string_list_item(item, ref_excludes) {
1214 if (!wildmatch(item->string, path, 0, NULL))
1220 static int handle_one_ref(const char *path, const struct object_id *oid,
1221 int flag, void *cb_data)
1223 struct all_refs_cb *cb = cb_data;
1224 struct object *object;
1226 if (ref_excluded(cb->all_revs->ref_excludes, path))
1229 object = get_reference(cb->all_revs, path, oid->hash, cb->all_flags);
1230 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1231 add_pending_sha1(cb->all_revs, path, oid->hash, cb->all_flags);
1235 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1238 cb->all_revs = revs;
1239 cb->all_flags = flags;
1242 void clear_ref_exclusion(struct string_list **ref_excludes_p)
1244 if (*ref_excludes_p) {
1245 string_list_clear(*ref_excludes_p, 0);
1246 free(*ref_excludes_p);
1248 *ref_excludes_p = NULL;
1251 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1253 if (!*ref_excludes_p) {
1254 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1255 (*ref_excludes_p)->strdup_strings = 1;
1257 string_list_append(*ref_excludes_p, exclude);
1260 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
1261 int (*for_each)(const char *, each_ref_fn, void *))
1263 struct all_refs_cb cb;
1264 init_all_refs_cb(&cb, revs, flags);
1265 for_each(submodule, handle_one_ref, &cb);
1268 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
1270 struct all_refs_cb *cb = cb_data;
1271 if (!is_null_sha1(sha1)) {
1272 struct object *o = parse_object(sha1);
1274 o->flags |= cb->all_flags;
1275 /* ??? CMDLINEFLAGS ??? */
1276 add_pending_object(cb->all_revs, o, "");
1278 else if (!cb->warned_bad_reflog) {
1279 warning("reflog of '%s' references pruned commits",
1280 cb->name_for_errormsg);
1281 cb->warned_bad_reflog = 1;
1286 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
1287 const char *email, unsigned long timestamp, int tz,
1288 const char *message, void *cb_data)
1290 handle_one_reflog_commit(osha1, cb_data);
1291 handle_one_reflog_commit(nsha1, cb_data);
1295 static int handle_one_reflog(const char *path, const struct object_id *oid,
1296 int flag, void *cb_data)
1298 struct all_refs_cb *cb = cb_data;
1299 cb->warned_bad_reflog = 0;
1300 cb->name_for_errormsg = path;
1301 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
1305 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1307 struct all_refs_cb cb;
1310 cb.all_flags = flags;
1311 for_each_reflog(handle_one_reflog, &cb);
1314 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1315 struct strbuf *path)
1317 size_t baselen = path->len;
1320 if (it->entry_count >= 0) {
1321 struct tree *tree = lookup_tree(it->sha1);
1322 add_pending_object_with_path(revs, &tree->object, "",
1326 for (i = 0; i < it->subtree_nr; i++) {
1327 struct cache_tree_sub *sub = it->down[i];
1328 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1329 add_cache_tree(sub->cache_tree, revs, path);
1330 strbuf_setlen(path, baselen);
1335 void add_index_objects_to_pending(struct rev_info *revs, unsigned flags)
1340 for (i = 0; i < active_nr; i++) {
1341 struct cache_entry *ce = active_cache[i];
1344 if (S_ISGITLINK(ce->ce_mode))
1347 blob = lookup_blob(ce->sha1);
1349 die("unable to add index blob to traversal");
1350 add_pending_object_with_path(revs, &blob->object, "",
1351 ce->ce_mode, ce->name);
1354 if (active_cache_tree) {
1355 struct strbuf path = STRBUF_INIT;
1356 add_cache_tree(active_cache_tree, revs, &path);
1357 strbuf_release(&path);
1361 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
1363 unsigned char sha1[20];
1365 struct commit *commit;
1366 struct commit_list *parents;
1367 const char *arg = arg_;
1370 flags ^= UNINTERESTING | BOTTOM;
1373 if (get_sha1_committish(arg, sha1))
1376 it = get_reference(revs, arg, sha1, 0);
1377 if (!it && revs->ignore_missing)
1379 if (it->type != OBJ_TAG)
1381 if (!((struct tag*)it)->tagged)
1383 hashcpy(sha1, ((struct tag*)it)->tagged->oid.hash);
1385 if (it->type != OBJ_COMMIT)
1387 commit = (struct commit *)it;
1388 for (parents = commit->parents; parents; parents = parents->next) {
1389 it = &parents->item->object;
1391 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1392 add_pending_object(revs, it, arg);
1397 void init_revisions(struct rev_info *revs, const char *prefix)
1399 memset(revs, 0, sizeof(*revs));
1401 revs->abbrev = DEFAULT_ABBREV;
1402 revs->ignore_merges = 1;
1403 revs->simplify_history = 1;
1404 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1405 DIFF_OPT_SET(&revs->pruning, QUICK);
1406 revs->pruning.add_remove = file_add_remove;
1407 revs->pruning.change = file_change;
1408 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1410 revs->prefix = prefix;
1413 revs->skip_count = -1;
1414 revs->max_count = -1;
1415 revs->max_parents = -1;
1417 revs->commit_format = CMIT_FMT_DEFAULT;
1419 init_grep_defaults();
1420 grep_init(&revs->grep_filter, prefix);
1421 revs->grep_filter.status_only = 1;
1422 revs->grep_filter.regflags = REG_NEWLINE;
1424 diff_setup(&revs->diffopt);
1425 if (prefix && !revs->diffopt.prefix) {
1426 revs->diffopt.prefix = prefix;
1427 revs->diffopt.prefix_length = strlen(prefix);
1430 revs->notes_opt.use_default_notes = -1;
1433 static void add_pending_commit_list(struct rev_info *revs,
1434 struct commit_list *commit_list,
1437 while (commit_list) {
1438 struct object *object = &commit_list->item->object;
1439 object->flags |= flags;
1440 add_pending_object(revs, object, oid_to_hex(&object->oid));
1441 commit_list = commit_list->next;
1445 static void prepare_show_merge(struct rev_info *revs)
1447 struct commit_list *bases;
1448 struct commit *head, *other;
1449 unsigned char sha1[20];
1450 const char **prune = NULL;
1451 int i, prune_num = 1; /* counting terminating NULL */
1453 if (get_sha1("HEAD", sha1))
1454 die("--merge without HEAD?");
1455 head = lookup_commit_or_die(sha1, "HEAD");
1456 if (get_sha1("MERGE_HEAD", sha1))
1457 die("--merge without MERGE_HEAD?");
1458 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
1459 add_pending_object(revs, &head->object, "HEAD");
1460 add_pending_object(revs, &other->object, "MERGE_HEAD");
1461 bases = get_merge_bases(head, other);
1462 add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1463 add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1464 free_commit_list(bases);
1465 head->object.flags |= SYMMETRIC_LEFT;
1469 for (i = 0; i < active_nr; i++) {
1470 const struct cache_entry *ce = active_cache[i];
1473 if (ce_path_match(ce, &revs->prune_data, NULL)) {
1475 REALLOC_ARRAY(prune, prune_num);
1476 prune[prune_num-2] = ce->name;
1477 prune[prune_num-1] = NULL;
1479 while ((i+1 < active_nr) &&
1480 ce_same_name(ce, active_cache[i+1]))
1483 free_pathspec(&revs->prune_data);
1484 parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1485 PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1489 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1491 struct object_context oc;
1493 struct object *object;
1494 unsigned char sha1[20];
1496 const char *arg = arg_;
1497 int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1498 unsigned get_sha1_flags = 0;
1500 flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1502 dotdot = strstr(arg, "..");
1504 unsigned char from_sha1[20];
1505 const char *next = dotdot + 2;
1506 const char *this = arg;
1507 int symmetric = *next == '.';
1508 unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1509 static const char head_by_default[] = "HEAD";
1510 unsigned int a_flags;
1516 next = head_by_default;
1518 this = head_by_default;
1519 if (this == head_by_default && next == head_by_default &&
1522 * Just ".."? That is not a range but the
1523 * pathspec for the parent directory.
1525 if (!cant_be_filename) {
1530 if (!get_sha1_committish(this, from_sha1) &&
1531 !get_sha1_committish(next, sha1)) {
1532 struct object *a_obj, *b_obj;
1534 if (!cant_be_filename) {
1536 verify_non_filename(revs->prefix, arg);
1539 a_obj = parse_object(from_sha1);
1540 b_obj = parse_object(sha1);
1541 if (!a_obj || !b_obj) {
1543 if (revs->ignore_missing)
1546 ? "Invalid symmetric difference expression %s"
1547 : "Invalid revision range %s", arg);
1552 a_flags = flags_exclude;
1554 /* A...B -- find merge bases between the two */
1555 struct commit *a, *b;
1556 struct commit_list *exclude;
1558 a = (a_obj->type == OBJ_COMMIT
1559 ? (struct commit *)a_obj
1560 : lookup_commit_reference(a_obj->oid.hash));
1561 b = (b_obj->type == OBJ_COMMIT
1562 ? (struct commit *)b_obj
1563 : lookup_commit_reference(b_obj->oid.hash));
1566 exclude = get_merge_bases(a, b);
1567 add_rev_cmdline_list(revs, exclude,
1570 add_pending_commit_list(revs, exclude,
1572 free_commit_list(exclude);
1574 a_flags = flags | SYMMETRIC_LEFT;
1577 a_obj->flags |= a_flags;
1578 b_obj->flags |= flags;
1579 add_rev_cmdline(revs, a_obj, this,
1580 REV_CMD_LEFT, a_flags);
1581 add_rev_cmdline(revs, b_obj, next,
1582 REV_CMD_RIGHT, flags);
1583 add_pending_object(revs, a_obj, this);
1584 add_pending_object(revs, b_obj, next);
1589 dotdot = strstr(arg, "^@");
1590 if (dotdot && !dotdot[2]) {
1592 if (add_parents_only(revs, arg, flags))
1596 dotdot = strstr(arg, "^!");
1597 if (dotdot && !dotdot[2]) {
1599 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM)))
1605 local_flags = UNINTERESTING | BOTTOM;
1609 if (revarg_opt & REVARG_COMMITTISH)
1610 get_sha1_flags = GET_SHA1_COMMITTISH;
1612 if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
1613 return revs->ignore_missing ? 0 : -1;
1614 if (!cant_be_filename)
1615 verify_non_filename(revs->prefix, arg);
1616 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1617 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1618 add_pending_object_with_mode(revs, object, arg, oc.mode);
1622 struct cmdline_pathspec {
1628 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1631 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1632 prune->path[prune->nr++] = *(av++);
1636 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1637 struct cmdline_pathspec *prune)
1639 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1641 if (len && sb->buf[len - 1] == '\n')
1642 sb->buf[--len] = '\0';
1643 ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1644 prune->path[prune->nr++] = xstrdup(sb->buf);
1648 static void read_revisions_from_stdin(struct rev_info *revs,
1649 struct cmdline_pathspec *prune)
1652 int seen_dashdash = 0;
1655 save_warning = warn_on_object_refname_ambiguity;
1656 warn_on_object_refname_ambiguity = 0;
1658 strbuf_init(&sb, 1000);
1659 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1661 if (len && sb.buf[len - 1] == '\n')
1662 sb.buf[--len] = '\0';
1665 if (sb.buf[0] == '-') {
1666 if (len == 2 && sb.buf[1] == '-') {
1670 die("options not supported in --stdin mode");
1672 if (handle_revision_arg(sb.buf, revs, 0,
1673 REVARG_CANNOT_BE_FILENAME))
1674 die("bad revision '%s'", sb.buf);
1677 read_pathspec_from_stdin(revs, &sb, prune);
1679 strbuf_release(&sb);
1680 warn_on_object_refname_ambiguity = save_warning;
1683 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1685 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1688 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1690 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1693 static void add_message_grep(struct rev_info *revs, const char *pattern)
1695 add_grep(revs, pattern, GREP_PATTERN_BODY);
1698 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1699 int *unkc, const char **unkv)
1701 const char *arg = argv[0];
1705 /* pseudo revision arguments */
1706 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1707 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1708 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1709 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1710 !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1711 !strcmp(arg, "--indexed-objects") ||
1712 starts_with(arg, "--exclude=") ||
1713 starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1714 starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1716 unkv[(*unkc)++] = arg;
1720 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1721 revs->max_count = atoi(optarg);
1724 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1725 revs->skip_count = atoi(optarg);
1727 } else if ((*arg == '-') && isdigit(arg[1])) {
1728 /* accept -<digit>, like traditional "head" */
1729 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1730 revs->max_count < 0)
1731 die("'%s': not a non-negative integer", arg + 1);
1733 } else if (!strcmp(arg, "-n")) {
1735 return error("-n requires an argument");
1736 revs->max_count = atoi(argv[1]);
1739 } else if (starts_with(arg, "-n")) {
1740 revs->max_count = atoi(arg + 2);
1742 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1743 revs->max_age = atoi(optarg);
1745 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1746 revs->max_age = approxidate(optarg);
1748 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1749 revs->max_age = approxidate(optarg);
1751 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1752 revs->min_age = atoi(optarg);
1754 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1755 revs->min_age = approxidate(optarg);
1757 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1758 revs->min_age = approxidate(optarg);
1760 } else if (!strcmp(arg, "--first-parent")) {
1761 revs->first_parent_only = 1;
1762 } else if (!strcmp(arg, "--ancestry-path")) {
1763 revs->ancestry_path = 1;
1764 revs->simplify_history = 0;
1766 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1767 init_reflog_walk(&revs->reflog_info);
1768 } else if (!strcmp(arg, "--default")) {
1770 return error("bad --default argument");
1771 revs->def = argv[1];
1773 } else if (!strcmp(arg, "--merge")) {
1774 revs->show_merge = 1;
1775 } else if (!strcmp(arg, "--topo-order")) {
1776 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1777 revs->topo_order = 1;
1778 } else if (!strcmp(arg, "--simplify-merges")) {
1779 revs->simplify_merges = 1;
1780 revs->topo_order = 1;
1781 revs->rewrite_parents = 1;
1782 revs->simplify_history = 0;
1784 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1785 revs->simplify_merges = 1;
1786 revs->topo_order = 1;
1787 revs->rewrite_parents = 1;
1788 revs->simplify_history = 0;
1789 revs->simplify_by_decoration = 1;
1792 load_ref_decorations(DECORATE_SHORT_REFS);
1793 } else if (!strcmp(arg, "--date-order")) {
1794 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1795 revs->topo_order = 1;
1796 } else if (!strcmp(arg, "--author-date-order")) {
1797 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1798 revs->topo_order = 1;
1799 } else if (starts_with(arg, "--early-output")) {
1803 count = atoi(arg+15);
1806 revs->topo_order = 1;
1807 revs->early_output = count;
1809 } else if (!strcmp(arg, "--parents")) {
1810 revs->rewrite_parents = 1;
1811 revs->print_parents = 1;
1812 } else if (!strcmp(arg, "--dense")) {
1814 } else if (!strcmp(arg, "--sparse")) {
1816 } else if (!strcmp(arg, "--show-all")) {
1818 } else if (!strcmp(arg, "--remove-empty")) {
1819 revs->remove_empty_trees = 1;
1820 } else if (!strcmp(arg, "--merges")) {
1821 revs->min_parents = 2;
1822 } else if (!strcmp(arg, "--no-merges")) {
1823 revs->max_parents = 1;
1824 } else if (starts_with(arg, "--min-parents=")) {
1825 revs->min_parents = atoi(arg+14);
1826 } else if (starts_with(arg, "--no-min-parents")) {
1827 revs->min_parents = 0;
1828 } else if (starts_with(arg, "--max-parents=")) {
1829 revs->max_parents = atoi(arg+14);
1830 } else if (starts_with(arg, "--no-max-parents")) {
1831 revs->max_parents = -1;
1832 } else if (!strcmp(arg, "--boundary")) {
1834 } else if (!strcmp(arg, "--left-right")) {
1835 revs->left_right = 1;
1836 } else if (!strcmp(arg, "--left-only")) {
1837 if (revs->right_only)
1838 die("--left-only is incompatible with --right-only"
1840 revs->left_only = 1;
1841 } else if (!strcmp(arg, "--right-only")) {
1842 if (revs->left_only)
1843 die("--right-only is incompatible with --left-only");
1844 revs->right_only = 1;
1845 } else if (!strcmp(arg, "--cherry")) {
1846 if (revs->left_only)
1847 die("--cherry is incompatible with --left-only");
1848 revs->cherry_mark = 1;
1849 revs->right_only = 1;
1850 revs->max_parents = 1;
1852 } else if (!strcmp(arg, "--count")) {
1854 } else if (!strcmp(arg, "--cherry-mark")) {
1855 if (revs->cherry_pick)
1856 die("--cherry-mark is incompatible with --cherry-pick");
1857 revs->cherry_mark = 1;
1858 revs->limited = 1; /* needs limit_list() */
1859 } else if (!strcmp(arg, "--cherry-pick")) {
1860 if (revs->cherry_mark)
1861 die("--cherry-pick is incompatible with --cherry-mark");
1862 revs->cherry_pick = 1;
1864 } else if (!strcmp(arg, "--objects")) {
1865 revs->tag_objects = 1;
1866 revs->tree_objects = 1;
1867 revs->blob_objects = 1;
1868 } else if (!strcmp(arg, "--objects-edge")) {
1869 revs->tag_objects = 1;
1870 revs->tree_objects = 1;
1871 revs->blob_objects = 1;
1872 revs->edge_hint = 1;
1873 } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1874 revs->tag_objects = 1;
1875 revs->tree_objects = 1;
1876 revs->blob_objects = 1;
1877 revs->edge_hint = 1;
1878 revs->edge_hint_aggressive = 1;
1879 } else if (!strcmp(arg, "--verify-objects")) {
1880 revs->tag_objects = 1;
1881 revs->tree_objects = 1;
1882 revs->blob_objects = 1;
1883 revs->verify_objects = 1;
1884 } else if (!strcmp(arg, "--unpacked")) {
1886 } else if (starts_with(arg, "--unpacked=")) {
1887 die("--unpacked=<packfile> no longer supported.");
1888 } else if (!strcmp(arg, "-r")) {
1890 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1891 } else if (!strcmp(arg, "-t")) {
1893 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1894 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1895 } else if (!strcmp(arg, "-m")) {
1896 revs->ignore_merges = 0;
1897 } else if (!strcmp(arg, "-c")) {
1899 revs->dense_combined_merges = 0;
1900 revs->combine_merges = 1;
1901 } else if (!strcmp(arg, "--cc")) {
1903 revs->dense_combined_merges = 1;
1904 revs->combine_merges = 1;
1905 } else if (!strcmp(arg, "-v")) {
1906 revs->verbose_header = 1;
1907 } else if (!strcmp(arg, "--pretty")) {
1908 revs->verbose_header = 1;
1909 revs->pretty_given = 1;
1910 get_commit_format(NULL, revs);
1911 } else if (starts_with(arg, "--pretty=") || starts_with(arg, "--format=")) {
1913 * Detached form ("--pretty X" as opposed to "--pretty=X")
1914 * not allowed, since the argument is optional.
1916 revs->verbose_header = 1;
1917 revs->pretty_given = 1;
1918 get_commit_format(arg+9, revs);
1919 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1920 revs->show_notes = 1;
1921 revs->show_notes_given = 1;
1922 revs->notes_opt.use_default_notes = 1;
1923 } else if (!strcmp(arg, "--show-signature")) {
1924 revs->show_signature = 1;
1925 } else if (!strcmp(arg, "--show-linear-break") ||
1926 starts_with(arg, "--show-linear-break=")) {
1927 if (starts_with(arg, "--show-linear-break="))
1928 revs->break_bar = xstrdup(arg + 20);
1930 revs->break_bar = " ..........";
1931 revs->track_linear = 1;
1932 revs->track_first_time = 1;
1933 } else if (starts_with(arg, "--show-notes=") ||
1934 starts_with(arg, "--notes=")) {
1935 struct strbuf buf = STRBUF_INIT;
1936 revs->show_notes = 1;
1937 revs->show_notes_given = 1;
1938 if (starts_with(arg, "--show-notes")) {
1939 if (revs->notes_opt.use_default_notes < 0)
1940 revs->notes_opt.use_default_notes = 1;
1941 strbuf_addstr(&buf, arg+13);
1944 strbuf_addstr(&buf, arg+8);
1945 expand_notes_ref(&buf);
1946 string_list_append(&revs->notes_opt.extra_notes_refs,
1947 strbuf_detach(&buf, NULL));
1948 } else if (!strcmp(arg, "--no-notes")) {
1949 revs->show_notes = 0;
1950 revs->show_notes_given = 1;
1951 revs->notes_opt.use_default_notes = -1;
1952 /* we have been strdup'ing ourselves, so trick
1953 * string_list into free()ing strings */
1954 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1955 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1956 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1957 } else if (!strcmp(arg, "--standard-notes")) {
1958 revs->show_notes_given = 1;
1959 revs->notes_opt.use_default_notes = 1;
1960 } else if (!strcmp(arg, "--no-standard-notes")) {
1961 revs->notes_opt.use_default_notes = 0;
1962 } else if (!strcmp(arg, "--oneline")) {
1963 revs->verbose_header = 1;
1964 get_commit_format("oneline", revs);
1965 revs->pretty_given = 1;
1966 revs->abbrev_commit = 1;
1967 } else if (!strcmp(arg, "--graph")) {
1968 revs->topo_order = 1;
1969 revs->rewrite_parents = 1;
1970 revs->graph = graph_init(revs);
1971 } else if (!strcmp(arg, "--root")) {
1972 revs->show_root_diff = 1;
1973 } else if (!strcmp(arg, "--no-commit-id")) {
1974 revs->no_commit_id = 1;
1975 } else if (!strcmp(arg, "--always")) {
1976 revs->always_show_header = 1;
1977 } else if (!strcmp(arg, "--no-abbrev")) {
1979 } else if (!strcmp(arg, "--abbrev")) {
1980 revs->abbrev = DEFAULT_ABBREV;
1981 } else if (starts_with(arg, "--abbrev=")) {
1982 revs->abbrev = strtoul(arg + 9, NULL, 10);
1983 if (revs->abbrev < MINIMUM_ABBREV)
1984 revs->abbrev = MINIMUM_ABBREV;
1985 else if (revs->abbrev > 40)
1987 } else if (!strcmp(arg, "--abbrev-commit")) {
1988 revs->abbrev_commit = 1;
1989 revs->abbrev_commit_given = 1;
1990 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1991 revs->abbrev_commit = 0;
1992 } else if (!strcmp(arg, "--full-diff")) {
1994 revs->full_diff = 1;
1995 } else if (!strcmp(arg, "--full-history")) {
1996 revs->simplify_history = 0;
1997 } else if (!strcmp(arg, "--relative-date")) {
1998 revs->date_mode.type = DATE_RELATIVE;
1999 revs->date_mode_explicit = 1;
2000 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
2001 parse_date_format(optarg, &revs->date_mode);
2002 revs->date_mode_explicit = 1;
2004 } else if (!strcmp(arg, "--log-size")) {
2005 revs->show_log_size = 1;
2008 * Grepping the commit log
2010 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
2011 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
2013 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
2014 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
2016 } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
2017 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
2019 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
2020 add_message_grep(revs, optarg);
2022 } else if (!strcmp(arg, "--grep-debug")) {
2023 revs->grep_filter.debug = 1;
2024 } else if (!strcmp(arg, "--basic-regexp")) {
2025 grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter);
2026 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2027 grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter);
2028 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2029 revs->grep_filter.regflags |= REG_ICASE;
2030 DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
2031 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2032 grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter);
2033 } else if (!strcmp(arg, "--perl-regexp")) {
2034 grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
2035 } else if (!strcmp(arg, "--all-match")) {
2036 revs->grep_filter.all_match = 1;
2037 } else if (!strcmp(arg, "--invert-grep")) {
2038 revs->invert_grep = 1;
2039 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2040 if (strcmp(optarg, "none"))
2041 git_log_output_encoding = xstrdup(optarg);
2043 git_log_output_encoding = "";
2045 } else if (!strcmp(arg, "--reverse")) {
2047 } else if (!strcmp(arg, "--children")) {
2048 revs->children.name = "children";
2050 } else if (!strcmp(arg, "--ignore-missing")) {
2051 revs->ignore_missing = 1;
2053 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
2055 unkv[(*unkc)++] = arg;
2058 if (revs->graph && revs->track_linear)
2059 die("--show-linear-break and --graph are incompatible");
2064 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2065 const struct option *options,
2066 const char * const usagestr[])
2068 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2069 &ctx->cpidx, ctx->out);
2071 error("unknown option `%s'", ctx->argv[0]);
2072 usage_with_options(usagestr, options);
2078 static int for_each_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data, const char *term) {
2079 struct strbuf bisect_refs = STRBUF_INIT;
2081 strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2082 status = for_each_ref_in_submodule(submodule, bisect_refs.buf, fn, cb_data);
2083 strbuf_release(&bisect_refs);
2087 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2089 return for_each_bisect_ref(submodule, fn, cb_data, term_bad);
2092 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2094 return for_each_bisect_ref(submodule, fn, cb_data, term_good);
2097 static int handle_revision_pseudo_opt(const char *submodule,
2098 struct rev_info *revs,
2099 int argc, const char **argv, int *flags)
2101 const char *arg = argv[0];
2108 * Commands like "git shortlog" will not accept the options below
2109 * unless parse_revision_opt queues them (as opposed to erroring
2112 * When implementing your new pseudo-option, remember to
2113 * register it in the list at the top of handle_revision_opt.
2115 if (!strcmp(arg, "--all")) {
2116 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
2117 handle_refs(submodule, revs, *flags, head_ref_submodule);
2118 clear_ref_exclusion(&revs->ref_excludes);
2119 } else if (!strcmp(arg, "--branches")) {
2120 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
2121 clear_ref_exclusion(&revs->ref_excludes);
2122 } else if (!strcmp(arg, "--bisect")) {
2123 read_bisect_terms(&term_bad, &term_good);
2124 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
2125 handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
2127 } else if (!strcmp(arg, "--tags")) {
2128 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
2129 clear_ref_exclusion(&revs->ref_excludes);
2130 } else if (!strcmp(arg, "--remotes")) {
2131 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
2132 clear_ref_exclusion(&revs->ref_excludes);
2133 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2134 struct all_refs_cb cb;
2135 init_all_refs_cb(&cb, revs, *flags);
2136 for_each_glob_ref(handle_one_ref, optarg, &cb);
2137 clear_ref_exclusion(&revs->ref_excludes);
2139 } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2140 add_ref_exclusion(&revs->ref_excludes, optarg);
2142 } else if (starts_with(arg, "--branches=")) {
2143 struct all_refs_cb cb;
2144 init_all_refs_cb(&cb, revs, *flags);
2145 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
2146 clear_ref_exclusion(&revs->ref_excludes);
2147 } else if (starts_with(arg, "--tags=")) {
2148 struct all_refs_cb cb;
2149 init_all_refs_cb(&cb, revs, *flags);
2150 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
2151 clear_ref_exclusion(&revs->ref_excludes);
2152 } else if (starts_with(arg, "--remotes=")) {
2153 struct all_refs_cb cb;
2154 init_all_refs_cb(&cb, revs, *flags);
2155 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
2156 clear_ref_exclusion(&revs->ref_excludes);
2157 } else if (!strcmp(arg, "--reflog")) {
2158 add_reflogs_to_pending(revs, *flags);
2159 } else if (!strcmp(arg, "--indexed-objects")) {
2160 add_index_objects_to_pending(revs, *flags);
2161 } else if (!strcmp(arg, "--not")) {
2162 *flags ^= UNINTERESTING | BOTTOM;
2163 } else if (!strcmp(arg, "--no-walk")) {
2164 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2165 } else if (starts_with(arg, "--no-walk=")) {
2167 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2168 * not allowed, since the argument is optional.
2170 if (!strcmp(arg + 10, "sorted"))
2171 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2172 else if (!strcmp(arg + 10, "unsorted"))
2173 revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2175 return error("invalid argument to --no-walk");
2176 } else if (!strcmp(arg, "--do-walk")) {
2185 static void NORETURN diagnose_missing_default(const char *def)
2187 unsigned char sha1[20];
2189 const char *refname;
2191 refname = resolve_ref_unsafe(def, 0, sha1, &flags);
2192 if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2193 die(_("your current branch appears to be broken"));
2195 skip_prefix(refname, "refs/heads/", &refname);
2196 die(_("your current branch '%s' does not have any commits yet"),
2201 * Parse revision information, filling in the "rev_info" structure,
2202 * and removing the used arguments from the argument list.
2204 * Returns the number of arguments left that weren't recognized
2205 * (which are also moved to the head of the argument list)
2207 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2209 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2210 struct cmdline_pathspec prune_data;
2211 const char *submodule = NULL;
2213 memset(&prune_data, 0, sizeof(prune_data));
2215 submodule = opt->submodule;
2217 /* First, search for "--" */
2218 if (opt && opt->assume_dashdash) {
2222 for (i = 1; i < argc; i++) {
2223 const char *arg = argv[i];
2224 if (strcmp(arg, "--"))
2229 append_prune_data(&prune_data, argv + i + 1);
2235 /* Second, deal with arguments and options */
2237 revarg_opt = opt ? opt->revarg_opt : 0;
2239 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2240 read_from_stdin = 0;
2241 for (left = i = 1; i < argc; i++) {
2242 const char *arg = argv[i];
2246 opts = handle_revision_pseudo_opt(submodule,
2247 revs, argc - i, argv + i,
2254 if (!strcmp(arg, "--stdin")) {
2255 if (revs->disable_stdin) {
2259 if (read_from_stdin++)
2260 die("--stdin given twice?");
2261 read_revisions_from_stdin(revs, &prune_data);
2265 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2276 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2278 if (seen_dashdash || *arg == '^')
2279 die("bad revision '%s'", arg);
2281 /* If we didn't have a "--":
2282 * (1) all filenames must exist;
2283 * (2) all rev-args must not be interpretable
2284 * as a valid filename.
2285 * but the latter we have checked in the main loop.
2287 for (j = i; j < argc; j++)
2288 verify_filename(revs->prefix, argv[j], j == i);
2290 append_prune_data(&prune_data, argv + i);
2297 if (prune_data.nr) {
2299 * If we need to introduce the magic "a lone ':' means no
2300 * pathspec whatsoever", here is the place to do so.
2302 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2303 * prune_data.nr = 0;
2304 * prune_data.alloc = 0;
2305 * free(prune_data.path);
2306 * prune_data.path = NULL;
2308 * terminate prune_data.alloc with NULL and
2309 * call init_pathspec() to set revs->prune_data here.
2312 ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc);
2313 prune_data.path[prune_data.nr++] = NULL;
2314 parse_pathspec(&revs->prune_data, 0, 0,
2315 revs->prefix, prune_data.path);
2318 if (revs->def == NULL)
2319 revs->def = opt ? opt->def : NULL;
2320 if (opt && opt->tweak)
2321 opt->tweak(revs, opt);
2322 if (revs->show_merge)
2323 prepare_show_merge(revs);
2324 if (revs->def && !revs->pending.nr && !got_rev_arg) {
2325 unsigned char sha1[20];
2326 struct object *object;
2327 struct object_context oc;
2328 if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2329 diagnose_missing_default(revs->def);
2330 object = get_reference(revs, revs->def, sha1, 0);
2331 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2334 /* Did the user ask for any diff output? Run the diff! */
2335 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2338 /* Pickaxe, diff-filter and rename following need diffs */
2339 if (revs->diffopt.pickaxe ||
2340 revs->diffopt.filter ||
2341 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2344 if (revs->topo_order)
2347 if (revs->prune_data.nr) {
2348 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2349 /* Can't prune commits with rename following: the paths change.. */
2350 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2352 if (!revs->full_diff)
2353 copy_pathspec(&revs->diffopt.pathspec,
2356 if (revs->combine_merges)
2357 revs->ignore_merges = 0;
2358 revs->diffopt.abbrev = revs->abbrev;
2360 if (revs->line_level_traverse) {
2362 revs->topo_order = 1;
2365 diff_setup_done(&revs->diffopt);
2367 grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2368 &revs->grep_filter);
2369 compile_grep_patterns(&revs->grep_filter);
2371 if (revs->reverse && revs->reflog_info)
2372 die("cannot combine --reverse with --walk-reflogs");
2373 if (revs->rewrite_parents && revs->children.name)
2374 die("cannot combine --parents and --children");
2377 * Limitations on the graph functionality
2379 if (revs->reverse && revs->graph)
2380 die("cannot combine --reverse with --graph");
2382 if (revs->reflog_info && revs->graph)
2383 die("cannot combine --walk-reflogs with --graph");
2384 if (revs->no_walk && revs->graph)
2385 die("cannot combine --no-walk with --graph");
2386 if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2387 die("cannot use --grep-reflog without --walk-reflogs");
2389 if (revs->first_parent_only && revs->bisect)
2390 die(_("--first-parent is incompatible with --bisect"));
2395 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2397 struct commit_list *l = xcalloc(1, sizeof(*l));
2400 l->next = add_decoration(&revs->children, &parent->object, l);
2403 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2405 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2406 struct commit_list **pp, *p;
2407 int surviving_parents;
2409 /* Examine existing parents while marking ones we have seen... */
2410 pp = &commit->parents;
2411 surviving_parents = 0;
2412 while ((p = *pp) != NULL) {
2413 struct commit *parent = p->item;
2414 if (parent->object.flags & TMP_MARK) {
2417 compact_treesame(revs, commit, surviving_parents);
2420 parent->object.flags |= TMP_MARK;
2421 surviving_parents++;
2424 /* clear the temporary mark */
2425 for (p = commit->parents; p; p = p->next) {
2426 p->item->object.flags &= ~TMP_MARK;
2428 /* no update_treesame() - removing duplicates can't affect TREESAME */
2429 return surviving_parents;
2432 struct merge_simplify_state {
2433 struct commit *simplified;
2436 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2438 struct merge_simplify_state *st;
2440 st = lookup_decoration(&revs->merge_simplification, &commit->object);
2442 st = xcalloc(1, sizeof(*st));
2443 add_decoration(&revs->merge_simplification, &commit->object, st);
2448 static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2450 struct commit_list *h = reduce_heads(commit->parents);
2451 int i = 0, marked = 0;
2452 struct commit_list *po, *pn;
2454 /* Want these for sanity-checking only */
2455 int orig_cnt = commit_list_count(commit->parents);
2456 int cnt = commit_list_count(h);
2459 * Not ready to remove items yet, just mark them for now, based
2460 * on the output of reduce_heads(). reduce_heads outputs the reduced
2461 * set in its original order, so this isn't too hard.
2463 po = commit->parents;
2466 if (pn && po->item == pn->item) {
2470 po->item->object.flags |= TMP_MARK;
2476 if (i != cnt || cnt+marked != orig_cnt)
2477 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2479 free_commit_list(h);
2484 static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2486 struct commit_list *p;
2489 for (p = commit->parents; p; p = p->next) {
2490 struct commit *parent = p->item;
2491 if (!parent->parents && (parent->object.flags & TREESAME)) {
2492 parent->object.flags |= TMP_MARK;
2501 * Awkward naming - this means one parent we are TREESAME to.
2502 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2503 * empty tree). Better name suggestions?
2505 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2507 struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2508 struct commit *unmarked = NULL, *marked = NULL;
2509 struct commit_list *p;
2512 for (p = commit->parents, n = 0; p; p = p->next, n++) {
2513 if (ts->treesame[n]) {
2514 if (p->item->object.flags & TMP_MARK) {
2527 * If we are TREESAME to a marked-for-deletion parent, but not to any
2528 * unmarked parents, unmark the first TREESAME parent. This is the
2529 * parent that the default simplify_history==1 scan would have followed,
2530 * and it doesn't make sense to omit that path when asking for a
2531 * simplified full history. Retaining it improves the chances of
2532 * understanding odd missed merges that took an old version of a file.
2536 * I--------*X A modified the file, but mainline merge X used
2537 * \ / "-s ours", so took the version from I. X is
2538 * `-*A--' TREESAME to I and !TREESAME to A.
2540 * Default log from X would produce "I". Without this check,
2541 * --full-history --simplify-merges would produce "I-A-X", showing
2542 * the merge commit X and that it changed A, but not making clear that
2543 * it had just taken the I version. With this check, the topology above
2546 * Note that it is possible that the simplification chooses a different
2547 * TREESAME parent from the default, in which case this test doesn't
2548 * activate, and we _do_ drop the default parent. Example:
2550 * I------X A modified the file, but it was reverted in B,
2551 * \ / meaning mainline merge X is TREESAME to both
2554 * Default log would produce "I" by following the first parent;
2555 * --full-history --simplify-merges will produce "I-A-B". But this is a
2556 * reasonable result - it presents a logical full history leading from
2557 * I to X, and X is not an important merge.
2559 if (!unmarked && marked) {
2560 marked->object.flags &= ~TMP_MARK;
2567 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2569 struct commit_list **pp, *p;
2570 int nth_parent, removed = 0;
2572 pp = &commit->parents;
2574 while ((p = *pp) != NULL) {
2575 struct commit *parent = p->item;
2576 if (parent->object.flags & TMP_MARK) {
2577 parent->object.flags &= ~TMP_MARK;
2581 compact_treesame(revs, commit, nth_parent);
2588 /* Removing parents can only increase TREESAMEness */
2589 if (removed && !(commit->object.flags & TREESAME))
2590 update_treesame(revs, commit);
2595 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2597 struct commit_list *p;
2598 struct commit *parent;
2599 struct merge_simplify_state *st, *pst;
2602 st = locate_simplify_state(revs, commit);
2605 * Have we handled this one?
2611 * An UNINTERESTING commit simplifies to itself, so does a
2612 * root commit. We do not rewrite parents of such commit
2615 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2616 st->simplified = commit;
2621 * Do we know what commit all of our parents that matter
2622 * should be rewritten to? Otherwise we are not ready to
2623 * rewrite this one yet.
2625 for (cnt = 0, p = commit->parents; p; p = p->next) {
2626 pst = locate_simplify_state(revs, p->item);
2627 if (!pst->simplified) {
2628 tail = &commit_list_insert(p->item, tail)->next;
2631 if (revs->first_parent_only)
2635 tail = &commit_list_insert(commit, tail)->next;
2640 * Rewrite our list of parents. Note that this cannot
2641 * affect our TREESAME flags in any way - a commit is
2642 * always TREESAME to its simplification.
2644 for (p = commit->parents; p; p = p->next) {
2645 pst = locate_simplify_state(revs, p->item);
2646 p->item = pst->simplified;
2647 if (revs->first_parent_only)
2651 if (revs->first_parent_only)
2654 cnt = remove_duplicate_parents(revs, commit);
2657 * It is possible that we are a merge and one side branch
2658 * does not have any commit that touches the given paths;
2659 * in such a case, the immediate parent from that branch
2660 * will be rewritten to be the merge base.
2662 * o----X X: the commit we are looking at;
2663 * / / o: a commit that touches the paths;
2666 * Further, a merge of an independent branch that doesn't
2667 * touch the path will reduce to a treesame root parent:
2669 * ----o----X X: the commit we are looking at;
2670 * / o: a commit that touches the paths;
2671 * r r: a root commit not touching the paths
2673 * Detect and simplify both cases.
2676 int marked = mark_redundant_parents(revs, commit);
2677 marked += mark_treesame_root_parents(revs, commit);
2679 marked -= leave_one_treesame_to_parent(revs, commit);
2681 cnt = remove_marked_parents(revs, commit);
2685 * A commit simplifies to itself if it is a root, if it is
2686 * UNINTERESTING, if it touches the given paths, or if it is a
2687 * merge and its parents don't simplify to one relevant commit
2688 * (the first two cases are already handled at the beginning of
2691 * Otherwise, it simplifies to what its sole relevant parent
2695 (commit->object.flags & UNINTERESTING) ||
2696 !(commit->object.flags & TREESAME) ||
2697 (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2698 st->simplified = commit;
2700 pst = locate_simplify_state(revs, parent);
2701 st->simplified = pst->simplified;
2706 static void simplify_merges(struct rev_info *revs)
2708 struct commit_list *list, *next;
2709 struct commit_list *yet_to_do, **tail;
2710 struct commit *commit;
2715 /* feed the list reversed */
2717 for (list = revs->commits; list; list = next) {
2718 commit = list->item;
2721 * Do not free(list) here yet; the original list
2722 * is used later in this function.
2724 commit_list_insert(commit, &yet_to_do);
2731 commit = pop_commit(&list);
2732 tail = simplify_one(revs, commit, tail);
2736 /* clean up the result, removing the simplified ones */
2737 list = revs->commits;
2738 revs->commits = NULL;
2739 tail = &revs->commits;
2741 struct merge_simplify_state *st;
2743 commit = pop_commit(&list);
2744 st = locate_simplify_state(revs, commit);
2745 if (st->simplified == commit)
2746 tail = &commit_list_insert(commit, tail)->next;
2750 static void set_children(struct rev_info *revs)
2752 struct commit_list *l;
2753 for (l = revs->commits; l; l = l->next) {
2754 struct commit *commit = l->item;
2755 struct commit_list *p;
2757 for (p = commit->parents; p; p = p->next)
2758 add_child(revs, p->item, commit);
2762 void reset_revision_walk(void)
2764 clear_object_flags(SEEN | ADDED | SHOWN);
2767 int prepare_revision_walk(struct rev_info *revs)
2770 struct object_array old_pending;
2771 struct commit_list **next = &revs->commits;
2773 memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2774 revs->pending.nr = 0;
2775 revs->pending.alloc = 0;
2776 revs->pending.objects = NULL;
2777 for (i = 0; i < old_pending.nr; i++) {
2778 struct object_array_entry *e = old_pending.objects + i;
2779 struct commit *commit = handle_commit(revs, e);
2781 if (!(commit->object.flags & SEEN)) {
2782 commit->object.flags |= SEEN;
2783 next = commit_list_append(commit, next);
2787 if (!revs->leak_pending)
2788 object_array_clear(&old_pending);
2790 /* Signal whether we need per-parent treesame decoration */
2791 if (revs->simplify_merges ||
2792 (revs->limited && limiting_can_increase_treesame(revs)))
2793 revs->treesame.name = "treesame";
2795 if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2796 commit_list_sort_by_date(&revs->commits);
2800 if (limit_list(revs) < 0)
2802 if (revs->topo_order)
2803 sort_in_topological_order(&revs->commits, revs->sort_order);
2804 if (revs->line_level_traverse)
2805 line_log_filter(revs);
2806 if (revs->simplify_merges)
2807 simplify_merges(revs);
2808 if (revs->children.name)
2813 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2815 struct commit_list *cache = NULL;
2818 struct commit *p = *pp;
2820 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2821 return rewrite_one_error;
2822 if (p->object.flags & UNINTERESTING)
2823 return rewrite_one_ok;
2824 if (!(p->object.flags & TREESAME))
2825 return rewrite_one_ok;
2827 return rewrite_one_noparents;
2828 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2829 return rewrite_one_ok;
2834 int rewrite_parents(struct rev_info *revs, struct commit *commit,
2835 rewrite_parent_fn_t rewrite_parent)
2837 struct commit_list **pp = &commit->parents;
2839 struct commit_list *parent = *pp;
2840 switch (rewrite_parent(revs, &parent->item)) {
2841 case rewrite_one_ok:
2843 case rewrite_one_noparents:
2846 case rewrite_one_error:
2851 remove_duplicate_parents(revs, commit);
2855 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2857 char *person, *endp;
2858 size_t len, namelen, maillen;
2861 struct ident_split ident;
2863 person = strstr(buf->buf, what);
2867 person += strlen(what);
2868 endp = strchr(person, '\n');
2872 len = endp - person;
2874 if (split_ident_line(&ident, person, len))
2877 mail = ident.mail_begin;
2878 maillen = ident.mail_end - ident.mail_begin;
2879 name = ident.name_begin;
2880 namelen = ident.name_end - ident.name_begin;
2882 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2883 struct strbuf namemail = STRBUF_INIT;
2885 strbuf_addf(&namemail, "%.*s <%.*s>",
2886 (int)namelen, name, (int)maillen, mail);
2888 strbuf_splice(buf, ident.name_begin - buf->buf,
2889 ident.mail_end - ident.name_begin + 1,
2890 namemail.buf, namemail.len);
2892 strbuf_release(&namemail);
2900 static int commit_match(struct commit *commit, struct rev_info *opt)
2903 const char *encoding;
2904 const char *message;
2905 struct strbuf buf = STRBUF_INIT;
2907 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2910 /* Prepend "fake" headers as needed */
2911 if (opt->grep_filter.use_reflog_filter) {
2912 strbuf_addstr(&buf, "reflog ");
2913 get_reflog_message(&buf, opt->reflog_info);
2914 strbuf_addch(&buf, '\n');
2918 * We grep in the user's output encoding, under the assumption that it
2919 * is the encoding they are most likely to write their grep pattern
2920 * for. In addition, it means we will match the "notes" encoding below,
2921 * so we will not end up with a buffer that has two different encodings
2924 encoding = get_log_output_encoding();
2925 message = logmsg_reencode(commit, NULL, encoding);
2927 /* Copy the commit to temporary if we are using "fake" headers */
2929 strbuf_addstr(&buf, message);
2931 if (opt->grep_filter.header_list && opt->mailmap) {
2933 strbuf_addstr(&buf, message);
2935 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2936 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2939 /* Append "fake" message parts as needed */
2940 if (opt->show_notes) {
2942 strbuf_addstr(&buf, message);
2943 format_display_notes(commit->object.oid.hash, &buf, encoding, 1);
2947 * Find either in the original commit message, or in the temporary.
2948 * Note that we cast away the constness of "message" here. It is
2949 * const because it may come from the cached commit buffer. That's OK,
2950 * because we know that it is modifiable heap memory, and that while
2951 * grep_buffer may modify it for speed, it will restore any
2952 * changes before returning.
2955 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2957 retval = grep_buffer(&opt->grep_filter,
2958 (char *)message, strlen(message));
2959 strbuf_release(&buf);
2960 unuse_commit_buffer(commit, message);
2961 return opt->invert_grep ? !retval : retval;
2964 static inline int want_ancestry(const struct rev_info *revs)
2966 return (revs->rewrite_parents || revs->children.name);
2969 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2971 if (commit->object.flags & SHOWN)
2972 return commit_ignore;
2973 if (revs->unpacked && has_sha1_pack(commit->object.oid.hash))
2974 return commit_ignore;
2977 if (commit->object.flags & UNINTERESTING)
2978 return commit_ignore;
2979 if (revs->min_age != -1 && (commit->date > revs->min_age))
2980 return commit_ignore;
2981 if (revs->min_parents || (revs->max_parents >= 0)) {
2982 int n = commit_list_count(commit->parents);
2983 if ((n < revs->min_parents) ||
2984 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2985 return commit_ignore;
2987 if (!commit_match(commit, revs))
2988 return commit_ignore;
2989 if (revs->prune && revs->dense) {
2990 /* Commit without changes? */
2991 if (commit->object.flags & TREESAME) {
2993 struct commit_list *p;
2994 /* drop merges unless we want parenthood */
2995 if (!want_ancestry(revs))
2996 return commit_ignore;
2998 * If we want ancestry, then need to keep any merges
2999 * between relevant commits to tie together topology.
3000 * For consistency with TREESAME and simplification
3001 * use "relevant" here rather than just INTERESTING,
3002 * to treat bottom commit(s) as part of the topology.
3004 for (n = 0, p = commit->parents; p; p = p->next)
3005 if (relevant_commit(p->item))
3008 return commit_ignore;
3014 define_commit_slab(saved_parents, struct commit_list *);
3016 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
3019 * You may only call save_parents() once per commit (this is checked
3020 * for non-root commits).
3022 static void save_parents(struct rev_info *revs, struct commit *commit)
3024 struct commit_list **pp;
3026 if (!revs->saved_parents_slab) {
3027 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3028 init_saved_parents(revs->saved_parents_slab);
3031 pp = saved_parents_at(revs->saved_parents_slab, commit);
3034 * When walking with reflogs, we may visit the same commit
3035 * several times: once for each appearance in the reflog.
3037 * In this case, save_parents() will be called multiple times.
3038 * We want to keep only the first set of parents. We need to
3039 * store a sentinel value for an empty (i.e., NULL) parent
3040 * list to distinguish it from a not-yet-saved list, however.
3044 if (commit->parents)
3045 *pp = copy_commit_list(commit->parents);
3047 *pp = EMPTY_PARENT_LIST;
3050 static void free_saved_parents(struct rev_info *revs)
3052 if (revs->saved_parents_slab)
3053 clear_saved_parents(revs->saved_parents_slab);
3056 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3058 struct commit_list *parents;
3060 if (!revs->saved_parents_slab)
3061 return commit->parents;
3063 parents = *saved_parents_at(revs->saved_parents_slab, commit);
3064 if (parents == EMPTY_PARENT_LIST)
3069 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3071 enum commit_action action = get_commit_action(revs, commit);
3073 if (action == commit_show &&
3075 revs->prune && revs->dense && want_ancestry(revs)) {
3077 * --full-diff on simplified parents is no good: it
3078 * will show spurious changes from the commits that
3079 * were elided. So we save the parents on the side
3080 * when --full-diff is in effect.
3082 if (revs->full_diff)
3083 save_parents(revs, commit);
3084 if (rewrite_parents(revs, commit, rewrite_one) < 0)
3085 return commit_error;
3090 static void track_linear(struct rev_info *revs, struct commit *commit)
3092 if (revs->track_first_time) {
3094 revs->track_first_time = 0;
3096 struct commit_list *p;
3097 for (p = revs->previous_parents; p; p = p->next)
3098 if (p->item == NULL || /* first commit */
3099 !oidcmp(&p->item->object.oid, &commit->object.oid))
3101 revs->linear = p != NULL;
3103 if (revs->reverse) {
3105 commit->object.flags |= TRACK_LINEAR;
3107 free_commit_list(revs->previous_parents);
3108 revs->previous_parents = copy_commit_list(commit->parents);
3111 static struct commit *get_revision_1(struct rev_info *revs)
3117 struct commit *commit = pop_commit(&revs->commits);
3119 if (revs->reflog_info) {
3120 save_parents(revs, commit);
3121 fake_reflog_parent(revs->reflog_info, commit);
3122 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3126 * If we haven't done the list limiting, we need to look at
3127 * the parents here. We also need to do the date-based limiting
3128 * that we'd otherwise have done in limit_list().
3130 if (!revs->limited) {
3131 if (revs->max_age != -1 &&
3132 (commit->date < revs->max_age))
3134 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3135 if (!revs->ignore_missing_links)
3136 die("Failed to traverse parents of commit %s",
3137 oid_to_hex(&commit->object.oid));
3141 switch (simplify_commit(revs, commit)) {
3145 die("Failed to simplify parents of commit %s",
3146 oid_to_hex(&commit->object.oid));
3148 if (revs->track_linear)
3149 track_linear(revs, commit);
3152 } while (revs->commits);
3157 * Return true for entries that have not yet been shown. (This is an
3158 * object_array_each_func_t.)
3160 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3162 return !(entry->item->flags & SHOWN);
3166 * If array is on the verge of a realloc, garbage-collect any entries
3167 * that have already been shown to try to free up some space.
3169 static void gc_boundary(struct object_array *array)
3171 if (array->nr == array->alloc)
3172 object_array_filter(array, entry_unshown, NULL);
3175 static void create_boundary_commit_list(struct rev_info *revs)
3179 struct object_array *array = &revs->boundary_commits;
3180 struct object_array_entry *objects = array->objects;
3183 * If revs->commits is non-NULL at this point, an error occurred in
3184 * get_revision_1(). Ignore the error and continue printing the
3185 * boundary commits anyway. (This is what the code has always
3188 if (revs->commits) {
3189 free_commit_list(revs->commits);
3190 revs->commits = NULL;
3194 * Put all of the actual boundary commits from revs->boundary_commits
3195 * into revs->commits
3197 for (i = 0; i < array->nr; i++) {
3198 c = (struct commit *)(objects[i].item);
3201 if (!(c->object.flags & CHILD_SHOWN))
3203 if (c->object.flags & (SHOWN | BOUNDARY))
3205 c->object.flags |= BOUNDARY;
3206 commit_list_insert(c, &revs->commits);
3210 * If revs->topo_order is set, sort the boundary commits
3211 * in topological order
3213 sort_in_topological_order(&revs->commits, revs->sort_order);
3216 static struct commit *get_revision_internal(struct rev_info *revs)
3218 struct commit *c = NULL;
3219 struct commit_list *l;
3221 if (revs->boundary == 2) {
3223 * All of the normal commits have already been returned,
3224 * and we are now returning boundary commits.
3225 * create_boundary_commit_list() has populated
3226 * revs->commits with the remaining commits to return.
3228 c = pop_commit(&revs->commits);
3230 c->object.flags |= SHOWN;
3235 * If our max_count counter has reached zero, then we are done. We
3236 * don't simply return NULL because we still might need to show
3237 * boundary commits. But we want to avoid calling get_revision_1, which
3238 * might do a considerable amount of work finding the next commit only
3239 * for us to throw it away.
3241 * If it is non-zero, then either we don't have a max_count at all
3242 * (-1), or it is still counting, in which case we decrement.
3244 if (revs->max_count) {
3245 c = get_revision_1(revs);
3247 while (revs->skip_count > 0) {
3249 c = get_revision_1(revs);
3255 if (revs->max_count > 0)
3260 c->object.flags |= SHOWN;
3262 if (!revs->boundary)
3267 * get_revision_1() runs out the commits, and
3268 * we are done computing the boundaries.
3269 * switch to boundary commits output mode.
3274 * Update revs->commits to contain the list of
3277 create_boundary_commit_list(revs);
3279 return get_revision_internal(revs);
3283 * boundary commits are the commits that are parents of the
3284 * ones we got from get_revision_1() but they themselves are
3285 * not returned from get_revision_1(). Before returning
3286 * 'c', we need to mark its parents that they could be boundaries.
3289 for (l = c->parents; l; l = l->next) {
3291 p = &(l->item->object);
3292 if (p->flags & (CHILD_SHOWN | SHOWN))
3294 p->flags |= CHILD_SHOWN;
3295 gc_boundary(&revs->boundary_commits);
3296 add_object_array(p, NULL, &revs->boundary_commits);
3302 struct commit *get_revision(struct rev_info *revs)
3305 struct commit_list *reversed;
3307 if (revs->reverse) {
3309 while ((c = get_revision_internal(revs)))
3310 commit_list_insert(c, &reversed);
3311 revs->commits = reversed;
3313 revs->reverse_output_stage = 1;
3316 if (revs->reverse_output_stage) {
3317 c = pop_commit(&revs->commits);
3318 if (revs->track_linear)
3319 revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3323 c = get_revision_internal(revs);
3324 if (c && revs->graph)
3325 graph_update(revs->graph, c);
3327 free_saved_parents(revs);
3328 if (revs->previous_parents) {
3329 free_commit_list(revs->previous_parents);
3330 revs->previous_parents = NULL;
3336 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3338 if (commit->object.flags & BOUNDARY)
3340 else if (commit->object.flags & UNINTERESTING)
3342 else if (commit->object.flags & PATCHSAME)
3344 else if (!revs || revs->left_right) {
3345 if (commit->object.flags & SYMMETRIC_LEFT)
3349 } else if (revs->graph)
3351 else if (revs->cherry_mark)
3356 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3358 char *mark = get_revision_mark(revs, commit);
3361 fputs(mark, stdout);