11 #include "reflog-walk.h"
12 #include "patch-ids.h"
15 #include "string-list.h"
17 volatile show_early_output_fn_t show_early_output;
19 char *path_name(const struct name_path *path, const char *name)
21 const struct name_path *p;
23 int nlen = strlen(name);
26 for (p = path; p; p = p->up) {
28 len += p->elem_len + 1;
31 m = n + len - (nlen + 1);
33 for (p = path; p; p = p->up) {
36 memcpy(m, p->elem, p->elem_len);
43 static int show_path_component_truncated(FILE *out, const char *name, int len)
46 for (cnt = 0; cnt < len; cnt++) {
48 if (!ch || ch == '\n')
55 static int show_path_truncated(FILE *out, const struct name_path *path)
61 emitted = show_path_truncated(out, path->up);
66 ours = show_path_component_truncated(out, path->elem, path->elem_len);
69 return ours || emitted;
72 void show_object_with_name(FILE *out, struct object *obj, const struct name_path *path, const char *component)
74 struct name_path leaf;
75 leaf.up = (struct name_path *)path;
76 leaf.elem = component;
77 leaf.elem_len = strlen(component);
79 fprintf(out, "%s ", sha1_to_hex(obj->sha1));
80 show_path_truncated(out, &leaf);
84 void add_object(struct object *obj,
85 struct object_array *p,
86 struct name_path *path,
89 add_object_array(obj, path_name(path, name), p);
92 static void mark_blob_uninteresting(struct blob *blob)
96 if (blob->object.flags & UNINTERESTING)
98 blob->object.flags |= UNINTERESTING;
101 void mark_tree_uninteresting(struct tree *tree)
103 struct tree_desc desc;
104 struct name_entry entry;
105 struct object *obj = &tree->object;
109 if (obj->flags & UNINTERESTING)
111 obj->flags |= UNINTERESTING;
112 if (!has_sha1_file(obj->sha1))
114 if (parse_tree(tree) < 0)
115 die("bad tree %s", sha1_to_hex(obj->sha1));
117 init_tree_desc(&desc, tree->buffer, tree->size);
118 while (tree_entry(&desc, &entry)) {
119 switch (object_type(entry.mode)) {
121 mark_tree_uninteresting(lookup_tree(entry.sha1));
124 mark_blob_uninteresting(lookup_blob(entry.sha1));
127 /* Subproject commit - not in this repository */
133 * We don't care about the tree any more
134 * after it has been marked uninteresting.
140 void mark_parents_uninteresting(struct commit *commit)
142 struct commit_list *parents = commit->parents;
145 struct commit *commit = parents->item;
146 if (!(commit->object.flags & UNINTERESTING)) {
147 commit->object.flags |= UNINTERESTING;
150 * Normally we haven't parsed the parent
151 * yet, so we won't have a parent of a parent
152 * here. However, it may turn out that we've
153 * reached this commit some other way (where it
154 * wasn't uninteresting), in which case we need
155 * to mark its parents recursively too..
158 mark_parents_uninteresting(commit);
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_sha1_file(commit->object.sha1))
170 commit->object.parsed = 1;
171 parents = parents->next;
175 static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
179 if (revs->no_walk && (obj->flags & UNINTERESTING))
181 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
182 struct strbuf buf = STRBUF_INIT;
183 int len = interpret_branch_name(name, &buf);
186 if (0 < len && name[len] && buf.len)
187 strbuf_addstr(&buf, name + len);
188 st = add_reflog_for_walk(revs->reflog_info,
189 (struct commit *)obj,
190 buf.buf[0] ? buf.buf: name);
191 strbuf_release(&buf);
195 add_object_array_with_mode(obj, name, &revs->pending, mode);
198 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
200 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
203 void add_head_to_pending(struct rev_info *revs)
205 unsigned char sha1[20];
207 if (get_sha1("HEAD", sha1))
209 obj = parse_object(sha1);
212 add_pending_object(revs, obj, "HEAD");
215 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
217 struct object *object;
219 object = parse_object(sha1);
221 if (revs->ignore_missing)
223 die("bad object %s", name);
225 object->flags |= flags;
229 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
231 unsigned long flags = object->flags;
234 * Tag object? Look what it points to..
236 while (object->type == OBJ_TAG) {
237 struct tag *tag = (struct tag *) object;
238 if (revs->tag_objects && !(flags & UNINTERESTING))
239 add_pending_object(revs, object, tag->tag);
242 object = parse_object(tag->tagged->sha1);
244 if (flags & UNINTERESTING)
246 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
251 * Commit object? Just return it, we'll do all the complex
254 if (object->type == OBJ_COMMIT) {
255 struct commit *commit = (struct commit *)object;
256 if (parse_commit(commit) < 0)
257 die("unable to parse commit %s", name);
258 if (flags & UNINTERESTING) {
259 commit->object.flags |= UNINTERESTING;
260 mark_parents_uninteresting(commit);
263 if (revs->show_source && !commit->util)
264 commit->util = (void *) name;
269 * Tree object? Either mark it uninteresting, or add it
270 * to the list of objects to look at later..
272 if (object->type == OBJ_TREE) {
273 struct tree *tree = (struct tree *)object;
274 if (!revs->tree_objects)
276 if (flags & UNINTERESTING) {
277 mark_tree_uninteresting(tree);
280 add_pending_object(revs, object, "");
285 * Blob object? You know the drill by now..
287 if (object->type == OBJ_BLOB) {
288 struct blob *blob = (struct blob *)object;
289 if (!revs->blob_objects)
291 if (flags & UNINTERESTING) {
292 mark_blob_uninteresting(blob);
295 add_pending_object(revs, object, "");
298 die("%s is unknown object", name);
301 static int everybody_uninteresting(struct commit_list *orig)
303 struct commit_list *list = orig;
305 struct commit *commit = list->item;
307 if (commit->object.flags & UNINTERESTING)
315 * The goal is to get REV_TREE_NEW as the result only if the
316 * diff consists of all '+' (and no other changes), REV_TREE_OLD
317 * if the whole diff is removal of old data, and otherwise
318 * REV_TREE_DIFFERENT (of course if the trees are the same we
319 * want REV_TREE_SAME).
320 * That means that once we get to REV_TREE_DIFFERENT, we do not
321 * have to look any further.
323 static int tree_difference = REV_TREE_SAME;
325 static void file_add_remove(struct diff_options *options,
326 int addremove, unsigned mode,
327 const unsigned char *sha1,
328 const char *fullpath, unsigned dirty_submodule)
330 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
332 tree_difference |= diff;
333 if (tree_difference == REV_TREE_DIFFERENT)
334 DIFF_OPT_SET(options, HAS_CHANGES);
337 static void file_change(struct diff_options *options,
338 unsigned old_mode, unsigned new_mode,
339 const unsigned char *old_sha1,
340 const unsigned char *new_sha1,
341 const char *fullpath,
342 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
344 tree_difference = REV_TREE_DIFFERENT;
345 DIFF_OPT_SET(options, HAS_CHANGES);
348 static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
350 struct tree *t1 = parent->tree;
351 struct tree *t2 = commit->tree;
358 if (revs->simplify_by_decoration) {
360 * If we are simplifying by decoration, then the commit
361 * is worth showing if it has a tag pointing at it.
363 if (lookup_decoration(&name_decoration, &commit->object))
364 return REV_TREE_DIFFERENT;
366 * A commit that is not pointed by a tag is uninteresting
367 * if we are not limited by path. This means that you will
368 * see the usual "commits that touch the paths" plus any
369 * tagged commit by specifying both --simplify-by-decoration
372 if (!revs->prune_data.nr)
373 return REV_TREE_SAME;
376 tree_difference = REV_TREE_SAME;
377 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
378 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
380 return REV_TREE_DIFFERENT;
381 return tree_difference;
384 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
389 struct tree_desc empty, real;
390 struct tree *t1 = commit->tree;
395 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
398 init_tree_desc(&real, tree, size);
399 init_tree_desc(&empty, "", 0);
401 tree_difference = REV_TREE_SAME;
402 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
403 retval = diff_tree(&empty, &real, "", &revs->pruning);
406 return retval >= 0 && (tree_difference == REV_TREE_SAME);
409 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
411 struct commit_list **pp, *parent;
412 int tree_changed = 0, tree_same = 0;
415 * If we don't do pruning, everything is interesting
423 if (!commit->parents) {
424 if (rev_same_tree_as_empty(revs, commit))
425 commit->object.flags |= TREESAME;
430 * Normal non-merge commit? If we don't want to make the
431 * history dense, we consider it always to be a change..
433 if (!revs->dense && !commit->parents->next)
436 pp = &commit->parents;
437 while ((parent = *pp) != NULL) {
438 struct commit *p = parent->item;
440 if (parse_commit(p) < 0)
441 die("cannot simplify commit %s (because of %s)",
442 sha1_to_hex(commit->object.sha1),
443 sha1_to_hex(p->object.sha1));
444 switch (rev_compare_tree(revs, p, commit)) {
447 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
448 /* Even if a merge with an uninteresting
449 * side branch brought the entire change
450 * we are interested in, we do not want
451 * to lose the other branches of this
452 * merge, so we just keep going.
458 commit->parents = parent;
459 commit->object.flags |= TREESAME;
463 if (revs->remove_empty_trees &&
464 rev_same_tree_as_empty(revs, p)) {
465 /* We are adding all the specified
466 * paths from this parent, so the
467 * history beyond this parent is not
468 * interesting. Remove its parents
469 * (they are grandparents for us).
470 * IOW, we pretend this parent is a
473 if (parse_commit(p) < 0)
474 die("cannot simplify commit %s (invalid %s)",
475 sha1_to_hex(commit->object.sha1),
476 sha1_to_hex(p->object.sha1));
481 case REV_TREE_DIFFERENT:
486 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
488 if (tree_changed && !tree_same)
490 commit->object.flags |= TREESAME;
493 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
494 struct commit_list *cached_base, struct commit_list **cache)
496 struct commit_list *new_entry;
498 if (cached_base && p->date < cached_base->item->date)
499 new_entry = commit_list_insert_by_date(p, &cached_base->next);
501 new_entry = commit_list_insert_by_date(p, head);
503 if (cache && (!*cache || p->date < (*cache)->item->date))
507 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
508 struct commit_list **list, struct commit_list **cache_ptr)
510 struct commit_list *parent = commit->parents;
512 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
514 if (commit->object.flags & ADDED)
516 commit->object.flags |= ADDED;
519 * If the commit is uninteresting, don't try to
520 * prune parents - we want the maximal uninteresting
523 * Normally we haven't parsed the parent
524 * yet, so we won't have a parent of a parent
525 * here. However, it may turn out that we've
526 * reached this commit some other way (where it
527 * wasn't uninteresting), in which case we need
528 * to mark its parents recursively too..
530 if (commit->object.flags & UNINTERESTING) {
532 struct commit *p = parent->item;
533 parent = parent->next;
535 p->object.flags |= UNINTERESTING;
536 if (parse_commit(p) < 0)
539 mark_parents_uninteresting(p);
540 if (p->object.flags & SEEN)
542 p->object.flags |= SEEN;
543 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
549 * Ok, the commit wasn't uninteresting. Try to
550 * simplify the commit history and find the parent
551 * that has no differences in the path set if one exists.
553 try_to_simplify_commit(revs, commit);
558 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
560 for (parent = commit->parents; parent; parent = parent->next) {
561 struct commit *p = parent->item;
563 if (parse_commit(p) < 0)
565 if (revs->show_source && !p->util)
566 p->util = commit->util;
567 p->object.flags |= left_flag;
568 if (!(p->object.flags & SEEN)) {
569 p->object.flags |= SEEN;
570 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
572 if (revs->first_parent_only)
578 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
580 struct commit_list *p;
581 int left_count = 0, right_count = 0;
583 struct patch_ids ids;
584 unsigned cherry_flag;
586 /* First count the commits on the left and on the right */
587 for (p = list; p; p = p->next) {
588 struct commit *commit = p->item;
589 unsigned flags = commit->object.flags;
590 if (flags & BOUNDARY)
592 else if (flags & SYMMETRIC_LEFT)
598 if (!left_count || !right_count)
601 left_first = left_count < right_count;
602 init_patch_ids(&ids);
603 ids.diffopts.pathspec = revs->diffopt.pathspec;
605 /* Compute patch-ids for one side */
606 for (p = list; p; p = p->next) {
607 struct commit *commit = p->item;
608 unsigned flags = commit->object.flags;
610 if (flags & BOUNDARY)
613 * If we have fewer left, left_first is set and we omit
614 * commits on the right branch in this loop. If we have
615 * fewer right, we skip the left ones.
617 if (left_first != !!(flags & SYMMETRIC_LEFT))
619 commit->util = add_commit_patch_id(commit, &ids);
622 /* either cherry_mark or cherry_pick are true */
623 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
625 /* Check the other side */
626 for (p = list; p; p = p->next) {
627 struct commit *commit = p->item;
629 unsigned flags = commit->object.flags;
631 if (flags & BOUNDARY)
634 * If we have fewer left, left_first is set and we omit
635 * commits on the left branch in this loop.
637 if (left_first == !!(flags & SYMMETRIC_LEFT))
641 * Have we seen the same patch id?
643 id = has_commit_patch_id(commit, &ids);
647 commit->object.flags |= cherry_flag;
650 /* Now check the original side for seen ones */
651 for (p = list; p; p = p->next) {
652 struct commit *commit = p->item;
653 struct patch_id *ent;
659 commit->object.flags |= cherry_flag;
663 free_patch_ids(&ids);
666 /* How many extra uninteresting commits we want to see.. */
669 static int still_interesting(struct commit_list *src, unsigned long date, int slop)
672 * No source list at all? We're definitely done..
678 * Does the destination list contain entries with a date
679 * before the source list? Definitely _not_ done.
681 if (date < src->item->date)
685 * Does the source list still have interesting commits in
686 * it? Definitely not done..
688 if (!everybody_uninteresting(src))
691 /* Ok, we're closing in.. */
696 * "rev-list --ancestry-path A..B" computes commits that are ancestors
697 * of B but not ancestors of A but further limits the result to those
698 * that are descendants of A. This takes the list of bottom commits and
699 * the result of "A..B" without --ancestry-path, and limits the latter
700 * further to the ones that can reach one of the commits in "bottom".
702 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
704 struct commit_list *p;
705 struct commit_list *rlist = NULL;
709 * Reverse the list so that it will be likely that we would
710 * process parents before children.
712 for (p = list; p; p = p->next)
713 commit_list_insert(p->item, &rlist);
715 for (p = bottom; p; p = p->next)
716 p->item->object.flags |= TMP_MARK;
719 * Mark the ones that can reach bottom commits in "list",
720 * in a bottom-up fashion.
724 for (p = rlist; p; p = p->next) {
725 struct commit *c = p->item;
726 struct commit_list *parents;
727 if (c->object.flags & (TMP_MARK | UNINTERESTING))
729 for (parents = c->parents;
731 parents = parents->next) {
732 if (!(parents->item->object.flags & TMP_MARK))
734 c->object.flags |= TMP_MARK;
739 } while (made_progress);
742 * NEEDSWORK: decide if we want to remove parents that are
743 * not marked with TMP_MARK from commit->parents for commits
744 * in the resulting list. We may not want to do that, though.
748 * The ones that are not marked with TMP_MARK are uninteresting
750 for (p = list; p; p = p->next) {
751 struct commit *c = p->item;
752 if (c->object.flags & TMP_MARK)
754 c->object.flags |= UNINTERESTING;
757 /* We are done with the TMP_MARK */
758 for (p = list; p; p = p->next)
759 p->item->object.flags &= ~TMP_MARK;
760 for (p = bottom; p; p = p->next)
761 p->item->object.flags &= ~TMP_MARK;
762 free_commit_list(rlist);
766 * Before walking the history, keep the set of "negative" refs the
767 * caller has asked to exclude.
769 * This is used to compute "rev-list --ancestry-path A..B", as we need
770 * to filter the result of "A..B" further to the ones that can actually
773 static struct commit_list *collect_bottom_commits(struct rev_info *revs)
775 struct commit_list *bottom = NULL;
777 for (i = 0; i < revs->cmdline.nr; i++) {
778 struct rev_cmdline_entry *elem = &revs->cmdline.rev[i];
779 if ((elem->flags & UNINTERESTING) &&
780 elem->item->type == OBJ_COMMIT)
781 commit_list_insert((struct commit *)elem->item, &bottom);
786 /* Assumes either left_only or right_only is set */
787 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
789 struct commit_list *p;
791 for (p = list; p; p = p->next) {
792 struct commit *commit = p->item;
794 if (revs->right_only) {
795 if (commit->object.flags & SYMMETRIC_LEFT)
796 commit->object.flags |= SHOWN;
797 } else /* revs->left_only is set */
798 if (!(commit->object.flags & SYMMETRIC_LEFT))
799 commit->object.flags |= SHOWN;
803 static int limit_list(struct rev_info *revs)
806 unsigned long date = ~0ul;
807 struct commit_list *list = revs->commits;
808 struct commit_list *newlist = NULL;
809 struct commit_list **p = &newlist;
810 struct commit_list *bottom = NULL;
812 if (revs->ancestry_path) {
813 bottom = collect_bottom_commits(revs);
815 die("--ancestry-path given but there are no bottom commits");
819 struct commit_list *entry = list;
820 struct commit *commit = list->item;
821 struct object *obj = &commit->object;
822 show_early_output_fn_t show;
827 if (revs->max_age != -1 && (commit->date < revs->max_age))
828 obj->flags |= UNINTERESTING;
829 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
831 if (obj->flags & UNINTERESTING) {
832 mark_parents_uninteresting(commit);
834 p = &commit_list_insert(commit, p)->next;
835 slop = still_interesting(list, date, slop);
838 /* If showing all, add the whole pending list to the end */
843 if (revs->min_age != -1 && (commit->date > revs->min_age))
846 p = &commit_list_insert(commit, p)->next;
848 show = show_early_output;
853 show_early_output = NULL;
855 if (revs->cherry_pick || revs->cherry_mark)
856 cherry_pick_list(newlist, revs);
858 if (revs->left_only || revs->right_only)
859 limit_left_right(newlist, revs);
862 limit_to_ancestry(bottom, newlist);
863 free_commit_list(bottom);
866 revs->commits = newlist;
870 static void add_rev_cmdline(struct rev_info *revs,
876 struct rev_cmdline_info *info = &revs->cmdline;
879 ALLOC_GROW(info->rev, nr + 1, info->alloc);
880 info->rev[nr].item = item;
881 info->rev[nr].name = name;
882 info->rev[nr].whence = whence;
883 info->rev[nr].flags = flags;
889 int warned_bad_reflog;
890 struct rev_info *all_revs;
891 const char *name_for_errormsg;
894 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
896 struct all_refs_cb *cb = cb_data;
897 struct object *object = get_reference(cb->all_revs, path, sha1,
899 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
900 add_pending_object(cb->all_revs, object, path);
904 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
908 cb->all_flags = flags;
911 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
912 int (*for_each)(const char *, each_ref_fn, void *))
914 struct all_refs_cb cb;
915 init_all_refs_cb(&cb, revs, flags);
916 for_each(submodule, handle_one_ref, &cb);
919 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
921 struct all_refs_cb *cb = cb_data;
922 if (!is_null_sha1(sha1)) {
923 struct object *o = parse_object(sha1);
925 o->flags |= cb->all_flags;
926 /* ??? CMDLINEFLAGS ??? */
927 add_pending_object(cb->all_revs, o, "");
929 else if (!cb->warned_bad_reflog) {
930 warning("reflog of '%s' references pruned commits",
931 cb->name_for_errormsg);
932 cb->warned_bad_reflog = 1;
937 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
938 const char *email, unsigned long timestamp, int tz,
939 const char *message, void *cb_data)
941 handle_one_reflog_commit(osha1, cb_data);
942 handle_one_reflog_commit(nsha1, cb_data);
946 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
948 struct all_refs_cb *cb = cb_data;
949 cb->warned_bad_reflog = 0;
950 cb->name_for_errormsg = path;
951 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
955 static void handle_reflog(struct rev_info *revs, unsigned flags)
957 struct all_refs_cb cb;
959 cb.all_flags = flags;
960 for_each_reflog(handle_one_reflog, &cb);
963 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
965 unsigned char sha1[20];
967 struct commit *commit;
968 struct commit_list *parents;
969 const char *arg = arg_;
972 flags ^= UNINTERESTING;
975 if (get_sha1(arg, sha1))
978 it = get_reference(revs, arg, sha1, 0);
979 if (!it && revs->ignore_missing)
981 if (it->type != OBJ_TAG)
983 if (!((struct tag*)it)->tagged)
985 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
987 if (it->type != OBJ_COMMIT)
989 commit = (struct commit *)it;
990 for (parents = commit->parents; parents; parents = parents->next) {
991 it = &parents->item->object;
993 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
994 add_pending_object(revs, it, arg);
999 void init_revisions(struct rev_info *revs, const char *prefix)
1001 memset(revs, 0, sizeof(*revs));
1003 revs->abbrev = DEFAULT_ABBREV;
1004 revs->ignore_merges = 1;
1005 revs->simplify_history = 1;
1006 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1007 DIFF_OPT_SET(&revs->pruning, QUICK);
1008 revs->pruning.add_remove = file_add_remove;
1009 revs->pruning.change = file_change;
1012 revs->prefix = prefix;
1015 revs->skip_count = -1;
1016 revs->max_count = -1;
1017 revs->max_parents = -1;
1019 revs->commit_format = CMIT_FMT_DEFAULT;
1021 revs->grep_filter.status_only = 1;
1022 revs->grep_filter.pattern_tail = &(revs->grep_filter.pattern_list);
1023 revs->grep_filter.header_tail = &(revs->grep_filter.header_list);
1024 revs->grep_filter.regflags = REG_NEWLINE;
1026 diff_setup(&revs->diffopt);
1027 if (prefix && !revs->diffopt.prefix) {
1028 revs->diffopt.prefix = prefix;
1029 revs->diffopt.prefix_length = strlen(prefix);
1032 revs->notes_opt.use_default_notes = -1;
1035 static void add_pending_commit_list(struct rev_info *revs,
1036 struct commit_list *commit_list,
1039 while (commit_list) {
1040 struct object *object = &commit_list->item->object;
1041 object->flags |= flags;
1042 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1043 commit_list = commit_list->next;
1047 static void prepare_show_merge(struct rev_info *revs)
1049 struct commit_list *bases;
1050 struct commit *head, *other;
1051 unsigned char sha1[20];
1052 const char **prune = NULL;
1053 int i, prune_num = 1; /* counting terminating NULL */
1055 if (get_sha1("HEAD", sha1))
1056 die("--merge without HEAD?");
1057 head = lookup_commit_or_die(sha1, "HEAD");
1058 if (get_sha1("MERGE_HEAD", sha1))
1059 die("--merge without MERGE_HEAD?");
1060 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
1061 add_pending_object(revs, &head->object, "HEAD");
1062 add_pending_object(revs, &other->object, "MERGE_HEAD");
1063 bases = get_merge_bases(head, other, 1);
1064 add_pending_commit_list(revs, bases, UNINTERESTING);
1065 free_commit_list(bases);
1066 head->object.flags |= SYMMETRIC_LEFT;
1070 for (i = 0; i < active_nr; i++) {
1071 struct cache_entry *ce = active_cache[i];
1074 if (ce_path_match(ce, &revs->prune_data)) {
1076 prune = xrealloc(prune, sizeof(*prune) * prune_num);
1077 prune[prune_num-2] = ce->name;
1078 prune[prune_num-1] = NULL;
1080 while ((i+1 < active_nr) &&
1081 ce_same_name(ce, active_cache[i+1]))
1084 free_pathspec(&revs->prune_data);
1085 init_pathspec(&revs->prune_data, prune);
1089 int handle_revision_arg(const char *arg_, struct rev_info *revs,
1091 int cant_be_filename)
1095 struct object *object;
1096 unsigned char sha1[20];
1098 const char *arg = arg_;
1100 dotdot = strstr(arg, "..");
1102 unsigned char from_sha1[20];
1103 const char *next = dotdot + 2;
1104 const char *this = arg;
1105 int symmetric = *next == '.';
1106 unsigned int flags_exclude = flags ^ UNINTERESTING;
1107 unsigned int a_flags;
1116 if (!get_sha1(this, from_sha1) &&
1117 !get_sha1(next, sha1)) {
1118 struct commit *a, *b;
1119 struct commit_list *exclude;
1121 a = lookup_commit_reference(from_sha1);
1122 b = lookup_commit_reference(sha1);
1124 if (revs->ignore_missing)
1127 "Invalid symmetric difference expression %s...%s" :
1128 "Invalid revision range %s..%s",
1132 if (!cant_be_filename) {
1134 verify_non_filename(revs->prefix, arg);
1138 exclude = get_merge_bases(a, b, 1);
1139 add_pending_commit_list(revs, exclude,
1141 free_commit_list(exclude);
1142 a_flags = flags | SYMMETRIC_LEFT;
1144 a_flags = flags_exclude;
1145 a->object.flags |= a_flags;
1146 b->object.flags |= flags;
1147 add_rev_cmdline(revs, &a->object, this,
1148 REV_CMD_LEFT, a_flags);
1149 add_rev_cmdline(revs, &b->object, next,
1150 REV_CMD_RIGHT, flags);
1151 add_pending_object(revs, &a->object, this);
1152 add_pending_object(revs, &b->object, next);
1157 dotdot = strstr(arg, "^@");
1158 if (dotdot && !dotdot[2]) {
1160 if (add_parents_only(revs, arg, flags))
1164 dotdot = strstr(arg, "^!");
1165 if (dotdot && !dotdot[2]) {
1167 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
1173 local_flags = UNINTERESTING;
1176 if (get_sha1_with_mode(arg, sha1, &mode))
1177 return revs->ignore_missing ? 0 : -1;
1178 if (!cant_be_filename)
1179 verify_non_filename(revs->prefix, arg);
1180 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1181 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1182 add_pending_object_with_mode(revs, object, arg, mode);
1186 struct cmdline_pathspec {
1192 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1195 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1196 prune->path[prune->nr++] = *(av++);
1200 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1201 struct cmdline_pathspec *prune)
1203 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1205 if (len && sb->buf[len - 1] == '\n')
1206 sb->buf[--len] = '\0';
1207 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1208 prune->path[prune->nr++] = xstrdup(sb->buf);
1212 static void read_revisions_from_stdin(struct rev_info *revs,
1213 struct cmdline_pathspec *prune)
1216 int seen_dashdash = 0;
1218 strbuf_init(&sb, 1000);
1219 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1221 if (len && sb.buf[len - 1] == '\n')
1222 sb.buf[--len] = '\0';
1225 if (sb.buf[0] == '-') {
1226 if (len == 2 && sb.buf[1] == '-') {
1230 die("options not supported in --stdin mode");
1232 if (handle_revision_arg(sb.buf, revs, 0, 1))
1233 die("bad revision '%s'", sb.buf);
1236 read_pathspec_from_stdin(revs, &sb, prune);
1237 strbuf_release(&sb);
1240 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1242 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1245 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1247 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1250 static void add_message_grep(struct rev_info *revs, const char *pattern)
1252 add_grep(revs, pattern, GREP_PATTERN_BODY);
1255 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1256 int *unkc, const char **unkv)
1258 const char *arg = argv[0];
1262 /* pseudo revision arguments */
1263 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1264 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1265 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1266 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1267 !strcmp(arg, "--bisect") || !prefixcmp(arg, "--glob=") ||
1268 !prefixcmp(arg, "--branches=") || !prefixcmp(arg, "--tags=") ||
1269 !prefixcmp(arg, "--remotes="))
1271 unkv[(*unkc)++] = arg;
1275 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1276 revs->max_count = atoi(optarg);
1279 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1280 revs->skip_count = atoi(optarg);
1282 } else if ((*arg == '-') && isdigit(arg[1])) {
1283 /* accept -<digit>, like traditional "head" */
1284 revs->max_count = atoi(arg + 1);
1286 } else if (!strcmp(arg, "-n")) {
1288 return error("-n requires an argument");
1289 revs->max_count = atoi(argv[1]);
1292 } else if (!prefixcmp(arg, "-n")) {
1293 revs->max_count = atoi(arg + 2);
1295 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1296 revs->max_age = atoi(optarg);
1298 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1299 revs->max_age = approxidate(optarg);
1301 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1302 revs->max_age = approxidate(optarg);
1304 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1305 revs->min_age = atoi(optarg);
1307 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1308 revs->min_age = approxidate(optarg);
1310 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1311 revs->min_age = approxidate(optarg);
1313 } else if (!strcmp(arg, "--first-parent")) {
1314 revs->first_parent_only = 1;
1315 } else if (!strcmp(arg, "--ancestry-path")) {
1316 revs->ancestry_path = 1;
1317 revs->simplify_history = 0;
1319 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1320 init_reflog_walk(&revs->reflog_info);
1321 } else if (!strcmp(arg, "--default")) {
1323 return error("bad --default argument");
1324 revs->def = argv[1];
1326 } else if (!strcmp(arg, "--merge")) {
1327 revs->show_merge = 1;
1328 } else if (!strcmp(arg, "--topo-order")) {
1330 revs->topo_order = 1;
1331 } else if (!strcmp(arg, "--simplify-merges")) {
1332 revs->simplify_merges = 1;
1333 revs->rewrite_parents = 1;
1334 revs->simplify_history = 0;
1336 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1337 revs->simplify_merges = 1;
1338 revs->rewrite_parents = 1;
1339 revs->simplify_history = 0;
1340 revs->simplify_by_decoration = 1;
1343 load_ref_decorations(DECORATE_SHORT_REFS);
1344 } else if (!strcmp(arg, "--date-order")) {
1346 revs->topo_order = 1;
1347 } else if (!prefixcmp(arg, "--early-output")) {
1351 count = atoi(arg+15);
1354 revs->topo_order = 1;
1355 revs->early_output = count;
1357 } else if (!strcmp(arg, "--parents")) {
1358 revs->rewrite_parents = 1;
1359 revs->print_parents = 1;
1360 } else if (!strcmp(arg, "--dense")) {
1362 } else if (!strcmp(arg, "--sparse")) {
1364 } else if (!strcmp(arg, "--show-all")) {
1366 } else if (!strcmp(arg, "--remove-empty")) {
1367 revs->remove_empty_trees = 1;
1368 } else if (!strcmp(arg, "--merges")) {
1369 revs->min_parents = 2;
1370 } else if (!strcmp(arg, "--no-merges")) {
1371 revs->max_parents = 1;
1372 } else if (!prefixcmp(arg, "--min-parents=")) {
1373 revs->min_parents = atoi(arg+14);
1374 } else if (!prefixcmp(arg, "--no-min-parents")) {
1375 revs->min_parents = 0;
1376 } else if (!prefixcmp(arg, "--max-parents=")) {
1377 revs->max_parents = atoi(arg+14);
1378 } else if (!prefixcmp(arg, "--no-max-parents")) {
1379 revs->max_parents = -1;
1380 } else if (!strcmp(arg, "--boundary")) {
1382 } else if (!strcmp(arg, "--left-right")) {
1383 revs->left_right = 1;
1384 } else if (!strcmp(arg, "--left-only")) {
1385 if (revs->right_only)
1386 die("--left-only is incompatible with --right-only"
1388 revs->left_only = 1;
1389 } else if (!strcmp(arg, "--right-only")) {
1390 if (revs->left_only)
1391 die("--right-only is incompatible with --left-only");
1392 revs->right_only = 1;
1393 } else if (!strcmp(arg, "--cherry")) {
1394 if (revs->left_only)
1395 die("--cherry is incompatible with --left-only");
1396 revs->cherry_mark = 1;
1397 revs->right_only = 1;
1398 revs->max_parents = 1;
1400 } else if (!strcmp(arg, "--count")) {
1402 } else if (!strcmp(arg, "--cherry-mark")) {
1403 if (revs->cherry_pick)
1404 die("--cherry-mark is incompatible with --cherry-pick");
1405 revs->cherry_mark = 1;
1406 revs->limited = 1; /* needs limit_list() */
1407 } else if (!strcmp(arg, "--cherry-pick")) {
1408 if (revs->cherry_mark)
1409 die("--cherry-pick is incompatible with --cherry-mark");
1410 revs->cherry_pick = 1;
1412 } else if (!strcmp(arg, "--objects")) {
1413 revs->tag_objects = 1;
1414 revs->tree_objects = 1;
1415 revs->blob_objects = 1;
1416 } else if (!strcmp(arg, "--objects-edge")) {
1417 revs->tag_objects = 1;
1418 revs->tree_objects = 1;
1419 revs->blob_objects = 1;
1420 revs->edge_hint = 1;
1421 } else if (!strcmp(arg, "--verify-objects")) {
1422 revs->tag_objects = 1;
1423 revs->tree_objects = 1;
1424 revs->blob_objects = 1;
1425 revs->verify_objects = 1;
1426 } else if (!strcmp(arg, "--unpacked")) {
1428 } else if (!prefixcmp(arg, "--unpacked=")) {
1429 die("--unpacked=<packfile> no longer supported.");
1430 } else if (!strcmp(arg, "-r")) {
1432 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1433 } else if (!strcmp(arg, "-t")) {
1435 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1436 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1437 } else if (!strcmp(arg, "-m")) {
1438 revs->ignore_merges = 0;
1439 } else if (!strcmp(arg, "-c")) {
1441 revs->dense_combined_merges = 0;
1442 revs->combine_merges = 1;
1443 } else if (!strcmp(arg, "--cc")) {
1445 revs->dense_combined_merges = 1;
1446 revs->combine_merges = 1;
1447 } else if (!strcmp(arg, "-v")) {
1448 revs->verbose_header = 1;
1449 } else if (!strcmp(arg, "--pretty")) {
1450 revs->verbose_header = 1;
1451 revs->pretty_given = 1;
1452 get_commit_format(arg+8, revs);
1453 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1455 * Detached form ("--pretty X" as opposed to "--pretty=X")
1456 * not allowed, since the argument is optional.
1458 revs->verbose_header = 1;
1459 revs->pretty_given = 1;
1460 get_commit_format(arg+9, revs);
1461 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1462 revs->show_notes = 1;
1463 revs->show_notes_given = 1;
1464 revs->notes_opt.use_default_notes = 1;
1465 } else if (!prefixcmp(arg, "--show-notes=") ||
1466 !prefixcmp(arg, "--notes=")) {
1467 struct strbuf buf = STRBUF_INIT;
1468 revs->show_notes = 1;
1469 revs->show_notes_given = 1;
1470 if (!prefixcmp(arg, "--show-notes")) {
1471 if (revs->notes_opt.use_default_notes < 0)
1472 revs->notes_opt.use_default_notes = 1;
1473 strbuf_addstr(&buf, arg+13);
1476 strbuf_addstr(&buf, arg+8);
1477 expand_notes_ref(&buf);
1478 string_list_append(&revs->notes_opt.extra_notes_refs,
1479 strbuf_detach(&buf, NULL));
1480 } else if (!strcmp(arg, "--no-notes")) {
1481 revs->show_notes = 0;
1482 revs->show_notes_given = 1;
1483 revs->notes_opt.use_default_notes = -1;
1484 /* we have been strdup'ing ourselves, so trick
1485 * string_list into free()ing strings */
1486 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1487 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1488 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1489 } else if (!strcmp(arg, "--standard-notes")) {
1490 revs->show_notes_given = 1;
1491 revs->notes_opt.use_default_notes = 1;
1492 } else if (!strcmp(arg, "--no-standard-notes")) {
1493 revs->notes_opt.use_default_notes = 0;
1494 } else if (!strcmp(arg, "--oneline")) {
1495 revs->verbose_header = 1;
1496 get_commit_format("oneline", revs);
1497 revs->pretty_given = 1;
1498 revs->abbrev_commit = 1;
1499 } else if (!strcmp(arg, "--graph")) {
1500 revs->topo_order = 1;
1501 revs->rewrite_parents = 1;
1502 revs->graph = graph_init(revs);
1503 } else if (!strcmp(arg, "--root")) {
1504 revs->show_root_diff = 1;
1505 } else if (!strcmp(arg, "--no-commit-id")) {
1506 revs->no_commit_id = 1;
1507 } else if (!strcmp(arg, "--always")) {
1508 revs->always_show_header = 1;
1509 } else if (!strcmp(arg, "--no-abbrev")) {
1511 } else if (!strcmp(arg, "--abbrev")) {
1512 revs->abbrev = DEFAULT_ABBREV;
1513 } else if (!prefixcmp(arg, "--abbrev=")) {
1514 revs->abbrev = strtoul(arg + 9, NULL, 10);
1515 if (revs->abbrev < MINIMUM_ABBREV)
1516 revs->abbrev = MINIMUM_ABBREV;
1517 else if (revs->abbrev > 40)
1519 } else if (!strcmp(arg, "--abbrev-commit")) {
1520 revs->abbrev_commit = 1;
1521 revs->abbrev_commit_given = 1;
1522 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1523 revs->abbrev_commit = 0;
1524 } else if (!strcmp(arg, "--full-diff")) {
1526 revs->full_diff = 1;
1527 } else if (!strcmp(arg, "--full-history")) {
1528 revs->simplify_history = 0;
1529 } else if (!strcmp(arg, "--relative-date")) {
1530 revs->date_mode = DATE_RELATIVE;
1531 revs->date_mode_explicit = 1;
1532 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1533 revs->date_mode = parse_date_format(optarg);
1534 revs->date_mode_explicit = 1;
1536 } else if (!strcmp(arg, "--log-size")) {
1537 revs->show_log_size = 1;
1540 * Grepping the commit log
1542 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1543 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1545 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1546 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1548 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1549 add_message_grep(revs, optarg);
1551 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1552 revs->grep_filter.regflags |= REG_EXTENDED;
1553 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1554 revs->grep_filter.regflags |= REG_ICASE;
1555 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1556 revs->grep_filter.fixed = 1;
1557 } else if (!strcmp(arg, "--all-match")) {
1558 revs->grep_filter.all_match = 1;
1559 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1560 if (strcmp(optarg, "none"))
1561 git_log_output_encoding = xstrdup(optarg);
1563 git_log_output_encoding = "";
1565 } else if (!strcmp(arg, "--reverse")) {
1567 } else if (!strcmp(arg, "--children")) {
1568 revs->children.name = "children";
1570 } else if (!strcmp(arg, "--ignore-missing")) {
1571 revs->ignore_missing = 1;
1573 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1575 unkv[(*unkc)++] = arg;
1582 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1583 const struct option *options,
1584 const char * const usagestr[])
1586 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1587 &ctx->cpidx, ctx->out);
1589 error("unknown option `%s'", ctx->argv[0]);
1590 usage_with_options(usagestr, options);
1596 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1598 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
1601 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1603 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
1606 static int handle_revision_pseudo_opt(const char *submodule,
1607 struct rev_info *revs,
1608 int argc, const char **argv, int *flags)
1610 const char *arg = argv[0];
1617 * Commands like "git shortlog" will not accept the options below
1618 * unless parse_revision_opt queues them (as opposed to erroring
1621 * When implementing your new pseudo-option, remember to
1622 * register it in the list at the top of handle_revision_opt.
1624 if (!strcmp(arg, "--all")) {
1625 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
1626 handle_refs(submodule, revs, *flags, head_ref_submodule);
1627 } else if (!strcmp(arg, "--branches")) {
1628 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
1629 } else if (!strcmp(arg, "--bisect")) {
1630 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
1631 handle_refs(submodule, revs, *flags ^ UNINTERESTING, for_each_good_bisect_ref);
1633 } else if (!strcmp(arg, "--tags")) {
1634 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
1635 } else if (!strcmp(arg, "--remotes")) {
1636 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
1637 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
1638 struct all_refs_cb cb;
1639 init_all_refs_cb(&cb, revs, *flags);
1640 for_each_glob_ref(handle_one_ref, optarg, &cb);
1642 } else if (!prefixcmp(arg, "--branches=")) {
1643 struct all_refs_cb cb;
1644 init_all_refs_cb(&cb, revs, *flags);
1645 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1646 } else if (!prefixcmp(arg, "--tags=")) {
1647 struct all_refs_cb cb;
1648 init_all_refs_cb(&cb, revs, *flags);
1649 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1650 } else if (!prefixcmp(arg, "--remotes=")) {
1651 struct all_refs_cb cb;
1652 init_all_refs_cb(&cb, revs, *flags);
1653 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1654 } else if (!strcmp(arg, "--reflog")) {
1655 handle_reflog(revs, *flags);
1656 } else if (!strcmp(arg, "--not")) {
1657 *flags ^= UNINTERESTING;
1658 } else if (!strcmp(arg, "--no-walk")) {
1660 } else if (!strcmp(arg, "--do-walk")) {
1670 * Parse revision information, filling in the "rev_info" structure,
1671 * and removing the used arguments from the argument list.
1673 * Returns the number of arguments left that weren't recognized
1674 * (which are also moved to the head of the argument list)
1676 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
1678 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0;
1679 struct cmdline_pathspec prune_data;
1680 const char *submodule = NULL;
1682 memset(&prune_data, 0, sizeof(prune_data));
1684 submodule = opt->submodule;
1686 /* First, search for "--" */
1688 for (i = 1; i < argc; i++) {
1689 const char *arg = argv[i];
1690 if (strcmp(arg, "--"))
1695 append_prune_data(&prune_data, argv + i + 1);
1700 /* Second, deal with arguments and options */
1702 read_from_stdin = 0;
1703 for (left = i = 1; i < argc; i++) {
1704 const char *arg = argv[i];
1708 opts = handle_revision_pseudo_opt(submodule,
1709 revs, argc - i, argv + i,
1716 if (!strcmp(arg, "--stdin")) {
1717 if (revs->disable_stdin) {
1721 if (read_from_stdin++)
1722 die("--stdin given twice?");
1723 read_revisions_from_stdin(revs, &prune_data);
1727 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
1737 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1739 if (seen_dashdash || *arg == '^')
1740 die("bad revision '%s'", arg);
1742 /* If we didn't have a "--":
1743 * (1) all filenames must exist;
1744 * (2) all rev-args must not be interpretable
1745 * as a valid filename.
1746 * but the latter we have checked in the main loop.
1748 for (j = i; j < argc; j++)
1749 verify_filename(revs->prefix, argv[j]);
1751 append_prune_data(&prune_data, argv + i);
1758 if (prune_data.nr) {
1760 * If we need to introduce the magic "a lone ':' means no
1761 * pathspec whatsoever", here is the place to do so.
1763 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
1764 * prune_data.nr = 0;
1765 * prune_data.alloc = 0;
1766 * free(prune_data.path);
1767 * prune_data.path = NULL;
1769 * terminate prune_data.alloc with NULL and
1770 * call init_pathspec() to set revs->prune_data here.
1773 ALLOC_GROW(prune_data.path, prune_data.nr+1, prune_data.alloc);
1774 prune_data.path[prune_data.nr++] = NULL;
1775 init_pathspec(&revs->prune_data,
1776 get_pathspec(revs->prefix, prune_data.path));
1779 if (revs->def == NULL)
1780 revs->def = opt ? opt->def : NULL;
1781 if (opt && opt->tweak)
1782 opt->tweak(revs, opt);
1783 if (revs->show_merge)
1784 prepare_show_merge(revs);
1785 if (revs->def && !revs->pending.nr && !got_rev_arg) {
1786 unsigned char sha1[20];
1787 struct object *object;
1789 if (get_sha1_with_mode(revs->def, sha1, &mode))
1790 die("bad default revision '%s'", revs->def);
1791 object = get_reference(revs, revs->def, sha1, 0);
1792 add_pending_object_with_mode(revs, object, revs->def, mode);
1795 /* Did the user ask for any diff output? Run the diff! */
1796 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1799 /* Pickaxe, diff-filter and rename following need diffs */
1800 if (revs->diffopt.pickaxe ||
1801 revs->diffopt.filter ||
1802 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1805 if (revs->topo_order)
1808 if (revs->prune_data.nr) {
1809 diff_tree_setup_paths(revs->prune_data.raw, &revs->pruning);
1810 /* Can't prune commits with rename following: the paths change.. */
1811 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1813 if (!revs->full_diff)
1814 diff_tree_setup_paths(revs->prune_data.raw, &revs->diffopt);
1816 if (revs->combine_merges)
1817 revs->ignore_merges = 0;
1818 revs->diffopt.abbrev = revs->abbrev;
1819 if (diff_setup_done(&revs->diffopt) < 0)
1820 die("diff_setup_done failed");
1822 compile_grep_patterns(&revs->grep_filter);
1824 if (revs->reverse && revs->reflog_info)
1825 die("cannot combine --reverse with --walk-reflogs");
1826 if (revs->rewrite_parents && revs->children.name)
1827 die("cannot combine --parents and --children");
1830 * Limitations on the graph functionality
1832 if (revs->reverse && revs->graph)
1833 die("cannot combine --reverse with --graph");
1835 if (revs->reflog_info && revs->graph)
1836 die("cannot combine --walk-reflogs with --graph");
1841 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1843 struct commit_list *l = xcalloc(1, sizeof(*l));
1846 l->next = add_decoration(&revs->children, &parent->object, l);
1849 static int remove_duplicate_parents(struct commit *commit)
1851 struct commit_list **pp, *p;
1852 int surviving_parents;
1854 /* Examine existing parents while marking ones we have seen... */
1855 pp = &commit->parents;
1856 while ((p = *pp) != NULL) {
1857 struct commit *parent = p->item;
1858 if (parent->object.flags & TMP_MARK) {
1862 parent->object.flags |= TMP_MARK;
1865 /* count them while clearing the temporary mark */
1866 surviving_parents = 0;
1867 for (p = commit->parents; p; p = p->next) {
1868 p->item->object.flags &= ~TMP_MARK;
1869 surviving_parents++;
1871 return surviving_parents;
1874 struct merge_simplify_state {
1875 struct commit *simplified;
1878 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1880 struct merge_simplify_state *st;
1882 st = lookup_decoration(&revs->merge_simplification, &commit->object);
1884 st = xcalloc(1, sizeof(*st));
1885 add_decoration(&revs->merge_simplification, &commit->object, st);
1890 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
1892 struct commit_list *p;
1893 struct merge_simplify_state *st, *pst;
1896 st = locate_simplify_state(revs, commit);
1899 * Have we handled this one?
1905 * An UNINTERESTING commit simplifies to itself, so does a
1906 * root commit. We do not rewrite parents of such commit
1909 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
1910 st->simplified = commit;
1915 * Do we know what commit all of our parents should be rewritten to?
1916 * Otherwise we are not ready to rewrite this one yet.
1918 for (cnt = 0, p = commit->parents; p; p = p->next) {
1919 pst = locate_simplify_state(revs, p->item);
1920 if (!pst->simplified) {
1921 tail = &commit_list_insert(p->item, tail)->next;
1926 tail = &commit_list_insert(commit, tail)->next;
1931 * Rewrite our list of parents.
1933 for (p = commit->parents; p; p = p->next) {
1934 pst = locate_simplify_state(revs, p->item);
1935 p->item = pst->simplified;
1937 cnt = remove_duplicate_parents(commit);
1940 * It is possible that we are a merge and one side branch
1941 * does not have any commit that touches the given paths;
1942 * in such a case, the immediate parents will be rewritten
1943 * to different commits.
1945 * o----X X: the commit we are looking at;
1946 * / / o: a commit that touches the paths;
1949 * Further reduce the parents by removing redundant parents.
1952 struct commit_list *h = reduce_heads(commit->parents);
1953 cnt = commit_list_count(h);
1954 free_commit_list(commit->parents);
1955 commit->parents = h;
1959 * A commit simplifies to itself if it is a root, if it is
1960 * UNINTERESTING, if it touches the given paths, or if it is a
1961 * merge and its parents simplifies to more than one commits
1962 * (the first two cases are already handled at the beginning of
1965 * Otherwise, it simplifies to what its sole parent simplifies to.
1968 (commit->object.flags & UNINTERESTING) ||
1969 !(commit->object.flags & TREESAME) ||
1971 st->simplified = commit;
1973 pst = locate_simplify_state(revs, commit->parents->item);
1974 st->simplified = pst->simplified;
1979 static void simplify_merges(struct rev_info *revs)
1981 struct commit_list *list;
1982 struct commit_list *yet_to_do, **tail;
1984 if (!revs->topo_order)
1985 sort_in_topological_order(&revs->commits, revs->lifo);
1989 /* feed the list reversed */
1991 for (list = revs->commits; list; list = list->next)
1992 commit_list_insert(list->item, &yet_to_do);
1998 struct commit *commit = list->item;
1999 struct commit_list *next = list->next;
2002 tail = simplify_one(revs, commit, tail);
2006 /* clean up the result, removing the simplified ones */
2007 list = revs->commits;
2008 revs->commits = NULL;
2009 tail = &revs->commits;
2011 struct commit *commit = list->item;
2012 struct commit_list *next = list->next;
2013 struct merge_simplify_state *st;
2016 st = locate_simplify_state(revs, commit);
2017 if (st->simplified == commit)
2018 tail = &commit_list_insert(commit, tail)->next;
2022 static void set_children(struct rev_info *revs)
2024 struct commit_list *l;
2025 for (l = revs->commits; l; l = l->next) {
2026 struct commit *commit = l->item;
2027 struct commit_list *p;
2029 for (p = commit->parents; p; p = p->next)
2030 add_child(revs, p->item, commit);
2034 int prepare_revision_walk(struct rev_info *revs)
2036 int nr = revs->pending.nr;
2037 struct object_array_entry *e, *list;
2039 e = list = revs->pending.objects;
2040 revs->pending.nr = 0;
2041 revs->pending.alloc = 0;
2042 revs->pending.objects = NULL;
2044 struct commit *commit = handle_commit(revs, e->item, e->name);
2046 if (!(commit->object.flags & SEEN)) {
2047 commit->object.flags |= SEEN;
2048 commit_list_insert_by_date(commit, &revs->commits);
2058 if (limit_list(revs) < 0)
2060 if (revs->topo_order)
2061 sort_in_topological_order(&revs->commits, revs->lifo);
2062 if (revs->simplify_merges)
2063 simplify_merges(revs);
2064 if (revs->children.name)
2069 enum rewrite_result {
2071 rewrite_one_noparents,
2075 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2077 struct commit_list *cache = NULL;
2080 struct commit *p = *pp;
2082 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2083 return rewrite_one_error;
2084 if (p->parents && p->parents->next)
2085 return rewrite_one_ok;
2086 if (p->object.flags & UNINTERESTING)
2087 return rewrite_one_ok;
2088 if (!(p->object.flags & TREESAME))
2089 return rewrite_one_ok;
2091 return rewrite_one_noparents;
2092 *pp = p->parents->item;
2096 static int rewrite_parents(struct rev_info *revs, struct commit *commit)
2098 struct commit_list **pp = &commit->parents;
2100 struct commit_list *parent = *pp;
2101 switch (rewrite_one(revs, &parent->item)) {
2102 case rewrite_one_ok:
2104 case rewrite_one_noparents:
2107 case rewrite_one_error:
2112 remove_duplicate_parents(commit);
2116 static int commit_match(struct commit *commit, struct rev_info *opt)
2118 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2120 return grep_buffer(&opt->grep_filter,
2121 NULL, /* we say nothing, not even filename */
2122 commit->buffer, strlen(commit->buffer));
2125 static inline int want_ancestry(struct rev_info *revs)
2127 return (revs->rewrite_parents || revs->children.name);
2130 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2132 if (commit->object.flags & SHOWN)
2133 return commit_ignore;
2134 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2135 return commit_ignore;
2138 if (commit->object.flags & UNINTERESTING)
2139 return commit_ignore;
2140 if (revs->min_age != -1 && (commit->date > revs->min_age))
2141 return commit_ignore;
2142 if (revs->min_parents || (revs->max_parents >= 0)) {
2144 struct commit_list *p;
2145 for (p = commit->parents; p; p = p->next)
2147 if ((n < revs->min_parents) ||
2148 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2149 return commit_ignore;
2151 if (!commit_match(commit, revs))
2152 return commit_ignore;
2153 if (revs->prune && revs->dense) {
2154 /* Commit without changes? */
2155 if (commit->object.flags & TREESAME) {
2156 /* drop merges unless we want parenthood */
2157 if (!want_ancestry(revs))
2158 return commit_ignore;
2159 /* non-merge - always ignore it */
2160 if (!commit->parents || !commit->parents->next)
2161 return commit_ignore;
2167 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
2169 enum commit_action action = get_commit_action(revs, commit);
2171 if (action == commit_show &&
2173 revs->prune && revs->dense && want_ancestry(revs)) {
2174 if (rewrite_parents(revs, commit) < 0)
2175 return commit_error;
2180 static struct commit *get_revision_1(struct rev_info *revs)
2186 struct commit_list *entry = revs->commits;
2187 struct commit *commit = entry->item;
2189 revs->commits = entry->next;
2192 if (revs->reflog_info) {
2193 fake_reflog_parent(revs->reflog_info, commit);
2194 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
2198 * If we haven't done the list limiting, we need to look at
2199 * the parents here. We also need to do the date-based limiting
2200 * that we'd otherwise have done in limit_list().
2202 if (!revs->limited) {
2203 if (revs->max_age != -1 &&
2204 (commit->date < revs->max_age))
2206 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
2207 die("Failed to traverse parents of commit %s",
2208 sha1_to_hex(commit->object.sha1));
2211 switch (simplify_commit(revs, commit)) {
2215 die("Failed to simplify parents of commit %s",
2216 sha1_to_hex(commit->object.sha1));
2220 } while (revs->commits);
2224 static void gc_boundary(struct object_array *array)
2226 unsigned nr = array->nr;
2227 unsigned alloc = array->alloc;
2228 struct object_array_entry *objects = array->objects;
2232 for (i = j = 0; i < nr; i++) {
2233 if (objects[i].item->flags & SHOWN)
2236 objects[j] = objects[i];
2239 for (i = j; i < nr; i++)
2240 objects[i].item = NULL;
2245 static void create_boundary_commit_list(struct rev_info *revs)
2249 struct object_array *array = &revs->boundary_commits;
2250 struct object_array_entry *objects = array->objects;
2253 * If revs->commits is non-NULL at this point, an error occurred in
2254 * get_revision_1(). Ignore the error and continue printing the
2255 * boundary commits anyway. (This is what the code has always
2258 if (revs->commits) {
2259 free_commit_list(revs->commits);
2260 revs->commits = NULL;
2264 * Put all of the actual boundary commits from revs->boundary_commits
2265 * into revs->commits
2267 for (i = 0; i < array->nr; i++) {
2268 c = (struct commit *)(objects[i].item);
2271 if (!(c->object.flags & CHILD_SHOWN))
2273 if (c->object.flags & (SHOWN | BOUNDARY))
2275 c->object.flags |= BOUNDARY;
2276 commit_list_insert(c, &revs->commits);
2280 * If revs->topo_order is set, sort the boundary commits
2281 * in topological order
2283 sort_in_topological_order(&revs->commits, revs->lifo);
2286 static struct commit *get_revision_internal(struct rev_info *revs)
2288 struct commit *c = NULL;
2289 struct commit_list *l;
2291 if (revs->boundary == 2) {
2293 * All of the normal commits have already been returned,
2294 * and we are now returning boundary commits.
2295 * create_boundary_commit_list() has populated
2296 * revs->commits with the remaining commits to return.
2298 c = pop_commit(&revs->commits);
2300 c->object.flags |= SHOWN;
2305 * Now pick up what they want to give us
2307 c = get_revision_1(revs);
2309 while (0 < revs->skip_count) {
2311 c = get_revision_1(revs);
2318 * Check the max_count.
2320 switch (revs->max_count) {
2331 c->object.flags |= SHOWN;
2333 if (!revs->boundary) {
2339 * get_revision_1() runs out the commits, and
2340 * we are done computing the boundaries.
2341 * switch to boundary commits output mode.
2346 * Update revs->commits to contain the list of
2349 create_boundary_commit_list(revs);
2351 return get_revision_internal(revs);
2355 * boundary commits are the commits that are parents of the
2356 * ones we got from get_revision_1() but they themselves are
2357 * not returned from get_revision_1(). Before returning
2358 * 'c', we need to mark its parents that they could be boundaries.
2361 for (l = c->parents; l; l = l->next) {
2363 p = &(l->item->object);
2364 if (p->flags & (CHILD_SHOWN | SHOWN))
2366 p->flags |= CHILD_SHOWN;
2367 gc_boundary(&revs->boundary_commits);
2368 add_object_array(p, NULL, &revs->boundary_commits);
2374 struct commit *get_revision(struct rev_info *revs)
2377 struct commit_list *reversed;
2379 if (revs->reverse) {
2381 while ((c = get_revision_internal(revs))) {
2382 commit_list_insert(c, &reversed);
2384 revs->commits = reversed;
2386 revs->reverse_output_stage = 1;
2389 if (revs->reverse_output_stage)
2390 return pop_commit(&revs->commits);
2392 c = get_revision_internal(revs);
2393 if (c && revs->graph)
2394 graph_update(revs->graph, c);
2398 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
2400 if (commit->object.flags & BOUNDARY)
2402 else if (commit->object.flags & UNINTERESTING)
2404 else if (commit->object.flags & PATCHSAME)
2406 else if (!revs || revs->left_right) {
2407 if (commit->object.flags & SYMMETRIC_LEFT)
2411 } else if (revs->graph)
2413 else if (revs->cherry_mark)
2418 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
2420 char *mark = get_revision_mark(revs, commit);
2423 fputs(mark, stdout);