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 void add_pending_sha1(struct rev_info *revs, const char *name,
230 const unsigned char *sha1, unsigned int flags)
232 struct object *object = get_reference(revs, name, sha1, flags);
233 add_pending_object(revs, object, name);
236 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
238 unsigned long flags = object->flags;
241 * Tag object? Look what it points to..
243 while (object->type == OBJ_TAG) {
244 struct tag *tag = (struct tag *) object;
245 if (revs->tag_objects && !(flags & UNINTERESTING))
246 add_pending_object(revs, object, tag->tag);
249 object = parse_object(tag->tagged->sha1);
251 if (flags & UNINTERESTING)
253 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
258 * Commit object? Just return it, we'll do all the complex
261 if (object->type == OBJ_COMMIT) {
262 struct commit *commit = (struct commit *)object;
263 if (parse_commit(commit) < 0)
264 die("unable to parse commit %s", name);
265 if (flags & UNINTERESTING) {
266 commit->object.flags |= UNINTERESTING;
267 mark_parents_uninteresting(commit);
270 if (revs->show_source && !commit->util)
271 commit->util = (void *) name;
276 * Tree object? Either mark it uninteresting, or add it
277 * to the list of objects to look at later..
279 if (object->type == OBJ_TREE) {
280 struct tree *tree = (struct tree *)object;
281 if (!revs->tree_objects)
283 if (flags & UNINTERESTING) {
284 mark_tree_uninteresting(tree);
287 add_pending_object(revs, object, "");
292 * Blob object? You know the drill by now..
294 if (object->type == OBJ_BLOB) {
295 struct blob *blob = (struct blob *)object;
296 if (!revs->blob_objects)
298 if (flags & UNINTERESTING) {
299 mark_blob_uninteresting(blob);
302 add_pending_object(revs, object, "");
305 die("%s is unknown object", name);
308 static int everybody_uninteresting(struct commit_list *orig)
310 struct commit_list *list = orig;
312 struct commit *commit = list->item;
314 if (commit->object.flags & UNINTERESTING)
322 * The goal is to get REV_TREE_NEW as the result only if the
323 * diff consists of all '+' (and no other changes), REV_TREE_OLD
324 * if the whole diff is removal of old data, and otherwise
325 * REV_TREE_DIFFERENT (of course if the trees are the same we
326 * want REV_TREE_SAME).
327 * That means that once we get to REV_TREE_DIFFERENT, we do not
328 * have to look any further.
330 static int tree_difference = REV_TREE_SAME;
332 static void file_add_remove(struct diff_options *options,
333 int addremove, unsigned mode,
334 const unsigned char *sha1,
336 const char *fullpath, unsigned dirty_submodule)
338 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
340 tree_difference |= diff;
341 if (tree_difference == REV_TREE_DIFFERENT)
342 DIFF_OPT_SET(options, HAS_CHANGES);
345 static void file_change(struct diff_options *options,
346 unsigned old_mode, unsigned new_mode,
347 const unsigned char *old_sha1,
348 const unsigned char *new_sha1,
349 int old_sha1_valid, int new_sha1_valid,
350 const char *fullpath,
351 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
353 tree_difference = REV_TREE_DIFFERENT;
354 DIFF_OPT_SET(options, HAS_CHANGES);
357 static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
359 struct tree *t1 = parent->tree;
360 struct tree *t2 = commit->tree;
367 if (revs->simplify_by_decoration) {
369 * If we are simplifying by decoration, then the commit
370 * is worth showing if it has a tag pointing at it.
372 if (lookup_decoration(&name_decoration, &commit->object))
373 return REV_TREE_DIFFERENT;
375 * A commit that is not pointed by a tag is uninteresting
376 * if we are not limited by path. This means that you will
377 * see the usual "commits that touch the paths" plus any
378 * tagged commit by specifying both --simplify-by-decoration
381 if (!revs->prune_data.nr)
382 return REV_TREE_SAME;
385 tree_difference = REV_TREE_SAME;
386 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
387 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
389 return REV_TREE_DIFFERENT;
390 return tree_difference;
393 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
398 struct tree_desc empty, real;
399 struct tree *t1 = commit->tree;
404 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
407 init_tree_desc(&real, tree, size);
408 init_tree_desc(&empty, "", 0);
410 tree_difference = REV_TREE_SAME;
411 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
412 retval = diff_tree(&empty, &real, "", &revs->pruning);
415 return retval >= 0 && (tree_difference == REV_TREE_SAME);
418 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
420 struct commit_list **pp, *parent;
421 int tree_changed = 0, tree_same = 0, nth_parent = 0;
424 * If we don't do pruning, everything is interesting
432 if (!commit->parents) {
433 if (rev_same_tree_as_empty(revs, commit))
434 commit->object.flags |= TREESAME;
439 * Normal non-merge commit? If we don't want to make the
440 * history dense, we consider it always to be a change..
442 if (!revs->dense && !commit->parents->next)
445 pp = &commit->parents;
446 while ((parent = *pp) != NULL) {
447 struct commit *p = parent->item;
450 * Do not compare with later parents when we care only about
451 * the first parent chain, in order to avoid derailing the
452 * traversal to follow a side branch that brought everything
453 * in the path we are limited to by the pathspec.
455 if (revs->first_parent_only && nth_parent++)
457 if (parse_commit(p) < 0)
458 die("cannot simplify commit %s (because of %s)",
459 sha1_to_hex(commit->object.sha1),
460 sha1_to_hex(p->object.sha1));
461 switch (rev_compare_tree(revs, p, commit)) {
464 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
465 /* Even if a merge with an uninteresting
466 * side branch brought the entire change
467 * we are interested in, we do not want
468 * to lose the other branches of this
469 * merge, so we just keep going.
475 commit->parents = parent;
476 commit->object.flags |= TREESAME;
480 if (revs->remove_empty_trees &&
481 rev_same_tree_as_empty(revs, p)) {
482 /* We are adding all the specified
483 * paths from this parent, so the
484 * history beyond this parent is not
485 * interesting. Remove its parents
486 * (they are grandparents for us).
487 * IOW, we pretend this parent is a
490 if (parse_commit(p) < 0)
491 die("cannot simplify commit %s (invalid %s)",
492 sha1_to_hex(commit->object.sha1),
493 sha1_to_hex(p->object.sha1));
498 case REV_TREE_DIFFERENT:
503 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
505 if (tree_changed && !tree_same)
507 commit->object.flags |= TREESAME;
510 static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
511 struct commit_list *cached_base, struct commit_list **cache)
513 struct commit_list *new_entry;
515 if (cached_base && p->date < cached_base->item->date)
516 new_entry = commit_list_insert_by_date(p, &cached_base->next);
518 new_entry = commit_list_insert_by_date(p, head);
520 if (cache && (!*cache || p->date < (*cache)->item->date))
524 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
525 struct commit_list **list, struct commit_list **cache_ptr)
527 struct commit_list *parent = commit->parents;
529 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
531 if (commit->object.flags & ADDED)
533 commit->object.flags |= ADDED;
536 * If the commit is uninteresting, don't try to
537 * prune parents - we want the maximal uninteresting
540 * Normally we haven't parsed the parent
541 * yet, so we won't have a parent of a parent
542 * here. However, it may turn out that we've
543 * reached this commit some other way (where it
544 * wasn't uninteresting), in which case we need
545 * to mark its parents recursively too..
547 if (commit->object.flags & UNINTERESTING) {
549 struct commit *p = parent->item;
550 parent = parent->next;
552 p->object.flags |= UNINTERESTING;
553 if (parse_commit(p) < 0)
556 mark_parents_uninteresting(p);
557 if (p->object.flags & SEEN)
559 p->object.flags |= SEEN;
560 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
566 * Ok, the commit wasn't uninteresting. Try to
567 * simplify the commit history and find the parent
568 * that has no differences in the path set if one exists.
570 try_to_simplify_commit(revs, commit);
575 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
577 for (parent = commit->parents; parent; parent = parent->next) {
578 struct commit *p = parent->item;
580 if (parse_commit(p) < 0)
582 if (revs->show_source && !p->util)
583 p->util = commit->util;
584 p->object.flags |= left_flag;
585 if (!(p->object.flags & SEEN)) {
586 p->object.flags |= SEEN;
587 commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
589 if (revs->first_parent_only)
595 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
597 struct commit_list *p;
598 int left_count = 0, right_count = 0;
600 struct patch_ids ids;
601 unsigned cherry_flag;
603 /* First count the commits on the left and on the right */
604 for (p = list; p; p = p->next) {
605 struct commit *commit = p->item;
606 unsigned flags = commit->object.flags;
607 if (flags & BOUNDARY)
609 else if (flags & SYMMETRIC_LEFT)
615 if (!left_count || !right_count)
618 left_first = left_count < right_count;
619 init_patch_ids(&ids);
620 ids.diffopts.pathspec = revs->diffopt.pathspec;
622 /* Compute patch-ids for one side */
623 for (p = list; p; p = p->next) {
624 struct commit *commit = p->item;
625 unsigned flags = commit->object.flags;
627 if (flags & BOUNDARY)
630 * If we have fewer left, left_first is set and we omit
631 * commits on the right branch in this loop. If we have
632 * fewer right, we skip the left ones.
634 if (left_first != !!(flags & SYMMETRIC_LEFT))
636 commit->util = add_commit_patch_id(commit, &ids);
639 /* either cherry_mark or cherry_pick are true */
640 cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
642 /* Check the other side */
643 for (p = list; p; p = p->next) {
644 struct commit *commit = p->item;
646 unsigned flags = commit->object.flags;
648 if (flags & BOUNDARY)
651 * If we have fewer left, left_first is set and we omit
652 * commits on the left branch in this loop.
654 if (left_first == !!(flags & SYMMETRIC_LEFT))
658 * Have we seen the same patch id?
660 id = has_commit_patch_id(commit, &ids);
664 commit->object.flags |= cherry_flag;
667 /* Now check the original side for seen ones */
668 for (p = list; p; p = p->next) {
669 struct commit *commit = p->item;
670 struct patch_id *ent;
676 commit->object.flags |= cherry_flag;
680 free_patch_ids(&ids);
683 /* How many extra uninteresting commits we want to see.. */
686 static int still_interesting(struct commit_list *src, unsigned long date, int slop)
689 * No source list at all? We're definitely done..
695 * Does the destination list contain entries with a date
696 * before the source list? Definitely _not_ done.
698 if (date < src->item->date)
702 * Does the source list still have interesting commits in
703 * it? Definitely not done..
705 if (!everybody_uninteresting(src))
708 /* Ok, we're closing in.. */
713 * "rev-list --ancestry-path A..B" computes commits that are ancestors
714 * of B but not ancestors of A but further limits the result to those
715 * that are descendants of A. This takes the list of bottom commits and
716 * the result of "A..B" without --ancestry-path, and limits the latter
717 * further to the ones that can reach one of the commits in "bottom".
719 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
721 struct commit_list *p;
722 struct commit_list *rlist = NULL;
726 * Reverse the list so that it will be likely that we would
727 * process parents before children.
729 for (p = list; p; p = p->next)
730 commit_list_insert(p->item, &rlist);
732 for (p = bottom; p; p = p->next)
733 p->item->object.flags |= TMP_MARK;
736 * Mark the ones that can reach bottom commits in "list",
737 * in a bottom-up fashion.
741 for (p = rlist; p; p = p->next) {
742 struct commit *c = p->item;
743 struct commit_list *parents;
744 if (c->object.flags & (TMP_MARK | UNINTERESTING))
746 for (parents = c->parents;
748 parents = parents->next) {
749 if (!(parents->item->object.flags & TMP_MARK))
751 c->object.flags |= TMP_MARK;
756 } while (made_progress);
759 * NEEDSWORK: decide if we want to remove parents that are
760 * not marked with TMP_MARK from commit->parents for commits
761 * in the resulting list. We may not want to do that, though.
765 * The ones that are not marked with TMP_MARK are uninteresting
767 for (p = list; p; p = p->next) {
768 struct commit *c = p->item;
769 if (c->object.flags & TMP_MARK)
771 c->object.flags |= UNINTERESTING;
774 /* We are done with the TMP_MARK */
775 for (p = list; p; p = p->next)
776 p->item->object.flags &= ~TMP_MARK;
777 for (p = bottom; p; p = p->next)
778 p->item->object.flags &= ~TMP_MARK;
779 free_commit_list(rlist);
783 * Before walking the history, keep the set of "negative" refs the
784 * caller has asked to exclude.
786 * This is used to compute "rev-list --ancestry-path A..B", as we need
787 * to filter the result of "A..B" further to the ones that can actually
790 static struct commit_list *collect_bottom_commits(struct rev_info *revs)
792 struct commit_list *bottom = NULL;
794 for (i = 0; i < revs->cmdline.nr; i++) {
795 struct rev_cmdline_entry *elem = &revs->cmdline.rev[i];
796 if ((elem->flags & UNINTERESTING) &&
797 elem->item->type == OBJ_COMMIT)
798 commit_list_insert((struct commit *)elem->item, &bottom);
803 /* Assumes either left_only or right_only is set */
804 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
806 struct commit_list *p;
808 for (p = list; p; p = p->next) {
809 struct commit *commit = p->item;
811 if (revs->right_only) {
812 if (commit->object.flags & SYMMETRIC_LEFT)
813 commit->object.flags |= SHOWN;
814 } else /* revs->left_only is set */
815 if (!(commit->object.flags & SYMMETRIC_LEFT))
816 commit->object.flags |= SHOWN;
820 static int limit_list(struct rev_info *revs)
823 unsigned long date = ~0ul;
824 struct commit_list *list = revs->commits;
825 struct commit_list *newlist = NULL;
826 struct commit_list **p = &newlist;
827 struct commit_list *bottom = NULL;
829 if (revs->ancestry_path) {
830 bottom = collect_bottom_commits(revs);
832 die("--ancestry-path given but there are no bottom commits");
836 struct commit_list *entry = list;
837 struct commit *commit = list->item;
838 struct object *obj = &commit->object;
839 show_early_output_fn_t show;
844 if (revs->max_age != -1 && (commit->date < revs->max_age))
845 obj->flags |= UNINTERESTING;
846 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
848 if (obj->flags & UNINTERESTING) {
849 mark_parents_uninteresting(commit);
851 p = &commit_list_insert(commit, p)->next;
852 slop = still_interesting(list, date, slop);
855 /* If showing all, add the whole pending list to the end */
860 if (revs->min_age != -1 && (commit->date > revs->min_age))
863 p = &commit_list_insert(commit, p)->next;
865 show = show_early_output;
870 show_early_output = NULL;
872 if (revs->cherry_pick || revs->cherry_mark)
873 cherry_pick_list(newlist, revs);
875 if (revs->left_only || revs->right_only)
876 limit_left_right(newlist, revs);
879 limit_to_ancestry(bottom, newlist);
880 free_commit_list(bottom);
883 revs->commits = newlist;
887 static void add_rev_cmdline(struct rev_info *revs,
893 struct rev_cmdline_info *info = &revs->cmdline;
896 ALLOC_GROW(info->rev, nr + 1, info->alloc);
897 info->rev[nr].item = item;
898 info->rev[nr].name = name;
899 info->rev[nr].whence = whence;
900 info->rev[nr].flags = flags;
906 int warned_bad_reflog;
907 struct rev_info *all_revs;
908 const char *name_for_errormsg;
911 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
913 struct all_refs_cb *cb = cb_data;
914 struct object *object = get_reference(cb->all_revs, path, sha1,
916 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
917 add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags);
921 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
925 cb->all_flags = flags;
928 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
929 int (*for_each)(const char *, each_ref_fn, void *))
931 struct all_refs_cb cb;
932 init_all_refs_cb(&cb, revs, flags);
933 for_each(submodule, handle_one_ref, &cb);
936 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
938 struct all_refs_cb *cb = cb_data;
939 if (!is_null_sha1(sha1)) {
940 struct object *o = parse_object(sha1);
942 o->flags |= cb->all_flags;
943 /* ??? CMDLINEFLAGS ??? */
944 add_pending_object(cb->all_revs, o, "");
946 else if (!cb->warned_bad_reflog) {
947 warning("reflog of '%s' references pruned commits",
948 cb->name_for_errormsg);
949 cb->warned_bad_reflog = 1;
954 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
955 const char *email, unsigned long timestamp, int tz,
956 const char *message, void *cb_data)
958 handle_one_reflog_commit(osha1, cb_data);
959 handle_one_reflog_commit(nsha1, cb_data);
963 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
965 struct all_refs_cb *cb = cb_data;
966 cb->warned_bad_reflog = 0;
967 cb->name_for_errormsg = path;
968 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
972 static void handle_reflog(struct rev_info *revs, unsigned flags)
974 struct all_refs_cb cb;
976 cb.all_flags = flags;
977 for_each_reflog(handle_one_reflog, &cb);
980 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
982 unsigned char sha1[20];
984 struct commit *commit;
985 struct commit_list *parents;
986 const char *arg = arg_;
989 flags ^= UNINTERESTING;
992 if (get_sha1(arg, sha1))
995 it = get_reference(revs, arg, sha1, 0);
996 if (!it && revs->ignore_missing)
998 if (it->type != OBJ_TAG)
1000 if (!((struct tag*)it)->tagged)
1002 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
1004 if (it->type != OBJ_COMMIT)
1006 commit = (struct commit *)it;
1007 for (parents = commit->parents; parents; parents = parents->next) {
1008 it = &parents->item->object;
1010 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1011 add_pending_object(revs, it, arg);
1016 void init_revisions(struct rev_info *revs, const char *prefix)
1018 memset(revs, 0, sizeof(*revs));
1020 revs->abbrev = DEFAULT_ABBREV;
1021 revs->ignore_merges = 1;
1022 revs->simplify_history = 1;
1023 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1024 DIFF_OPT_SET(&revs->pruning, QUICK);
1025 revs->pruning.add_remove = file_add_remove;
1026 revs->pruning.change = file_change;
1029 revs->prefix = prefix;
1032 revs->skip_count = -1;
1033 revs->max_count = -1;
1034 revs->max_parents = -1;
1036 revs->commit_format = CMIT_FMT_DEFAULT;
1038 revs->grep_filter.status_only = 1;
1039 revs->grep_filter.pattern_tail = &(revs->grep_filter.pattern_list);
1040 revs->grep_filter.header_tail = &(revs->grep_filter.header_list);
1041 revs->grep_filter.regflags = REG_NEWLINE;
1043 diff_setup(&revs->diffopt);
1044 if (prefix && !revs->diffopt.prefix) {
1045 revs->diffopt.prefix = prefix;
1046 revs->diffopt.prefix_length = strlen(prefix);
1049 revs->notes_opt.use_default_notes = -1;
1052 static void add_pending_commit_list(struct rev_info *revs,
1053 struct commit_list *commit_list,
1056 while (commit_list) {
1057 struct object *object = &commit_list->item->object;
1058 object->flags |= flags;
1059 add_pending_object(revs, object, sha1_to_hex(object->sha1));
1060 commit_list = commit_list->next;
1064 static void prepare_show_merge(struct rev_info *revs)
1066 struct commit_list *bases;
1067 struct commit *head, *other;
1068 unsigned char sha1[20];
1069 const char **prune = NULL;
1070 int i, prune_num = 1; /* counting terminating NULL */
1072 if (get_sha1("HEAD", sha1))
1073 die("--merge without HEAD?");
1074 head = lookup_commit_or_die(sha1, "HEAD");
1075 if (get_sha1("MERGE_HEAD", sha1))
1076 die("--merge without MERGE_HEAD?");
1077 other = lookup_commit_or_die(sha1, "MERGE_HEAD");
1078 add_pending_object(revs, &head->object, "HEAD");
1079 add_pending_object(revs, &other->object, "MERGE_HEAD");
1080 bases = get_merge_bases(head, other, 1);
1081 add_pending_commit_list(revs, bases, UNINTERESTING);
1082 free_commit_list(bases);
1083 head->object.flags |= SYMMETRIC_LEFT;
1087 for (i = 0; i < active_nr; i++) {
1088 struct cache_entry *ce = active_cache[i];
1091 if (ce_path_match(ce, &revs->prune_data)) {
1093 prune = xrealloc(prune, sizeof(*prune) * prune_num);
1094 prune[prune_num-2] = ce->name;
1095 prune[prune_num-1] = NULL;
1097 while ((i+1 < active_nr) &&
1098 ce_same_name(ce, active_cache[i+1]))
1101 free_pathspec(&revs->prune_data);
1102 init_pathspec(&revs->prune_data, prune);
1106 int handle_revision_arg(const char *arg_, struct rev_info *revs,
1108 int cant_be_filename)
1112 struct object *object;
1113 unsigned char sha1[20];
1115 const char *arg = arg_;
1117 dotdot = strstr(arg, "..");
1119 unsigned char from_sha1[20];
1120 const char *next = dotdot + 2;
1121 const char *this = arg;
1122 int symmetric = *next == '.';
1123 unsigned int flags_exclude = flags ^ UNINTERESTING;
1124 unsigned int a_flags;
1133 if (!get_sha1(this, from_sha1) &&
1134 !get_sha1(next, sha1)) {
1135 struct commit *a, *b;
1136 struct commit_list *exclude;
1138 a = lookup_commit_reference(from_sha1);
1139 b = lookup_commit_reference(sha1);
1141 if (revs->ignore_missing)
1144 "Invalid symmetric difference expression %s...%s" :
1145 "Invalid revision range %s..%s",
1149 if (!cant_be_filename) {
1151 verify_non_filename(revs->prefix, arg);
1155 exclude = get_merge_bases(a, b, 1);
1156 add_pending_commit_list(revs, exclude,
1158 free_commit_list(exclude);
1159 a_flags = flags | SYMMETRIC_LEFT;
1161 a_flags = flags_exclude;
1162 a->object.flags |= a_flags;
1163 b->object.flags |= flags;
1164 add_rev_cmdline(revs, &a->object, this,
1165 REV_CMD_LEFT, a_flags);
1166 add_rev_cmdline(revs, &b->object, next,
1167 REV_CMD_RIGHT, flags);
1168 add_pending_object(revs, &a->object, this);
1169 add_pending_object(revs, &b->object, next);
1174 dotdot = strstr(arg, "^@");
1175 if (dotdot && !dotdot[2]) {
1177 if (add_parents_only(revs, arg, flags))
1181 dotdot = strstr(arg, "^!");
1182 if (dotdot && !dotdot[2]) {
1184 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
1190 local_flags = UNINTERESTING;
1193 if (get_sha1_with_mode(arg, sha1, &mode))
1194 return revs->ignore_missing ? 0 : -1;
1195 if (!cant_be_filename)
1196 verify_non_filename(revs->prefix, arg);
1197 object = get_reference(revs, arg, sha1, flags ^ local_flags);
1198 add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1199 add_pending_object_with_mode(revs, object, arg, mode);
1203 struct cmdline_pathspec {
1209 static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1212 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1213 prune->path[prune->nr++] = *(av++);
1217 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1218 struct cmdline_pathspec *prune)
1220 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1222 if (len && sb->buf[len - 1] == '\n')
1223 sb->buf[--len] = '\0';
1224 ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1225 prune->path[prune->nr++] = xstrdup(sb->buf);
1229 static void read_revisions_from_stdin(struct rev_info *revs,
1230 struct cmdline_pathspec *prune)
1233 int seen_dashdash = 0;
1235 strbuf_init(&sb, 1000);
1236 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1238 if (len && sb.buf[len - 1] == '\n')
1239 sb.buf[--len] = '\0';
1242 if (sb.buf[0] == '-') {
1243 if (len == 2 && sb.buf[1] == '-') {
1247 die("options not supported in --stdin mode");
1249 if (handle_revision_arg(sb.buf, revs, 0, 1))
1250 die("bad revision '%s'", sb.buf);
1253 read_pathspec_from_stdin(revs, &sb, prune);
1254 strbuf_release(&sb);
1257 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1259 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1262 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1264 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1267 static void add_message_grep(struct rev_info *revs, const char *pattern)
1269 add_grep(revs, pattern, GREP_PATTERN_BODY);
1272 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1273 int *unkc, const char **unkv)
1275 const char *arg = argv[0];
1279 /* pseudo revision arguments */
1280 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1281 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1282 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1283 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1284 !strcmp(arg, "--bisect") || !prefixcmp(arg, "--glob=") ||
1285 !prefixcmp(arg, "--branches=") || !prefixcmp(arg, "--tags=") ||
1286 !prefixcmp(arg, "--remotes="))
1288 unkv[(*unkc)++] = arg;
1292 if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1293 revs->max_count = atoi(optarg);
1296 } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1297 revs->skip_count = atoi(optarg);
1299 } else if ((*arg == '-') && isdigit(arg[1])) {
1300 /* accept -<digit>, like traditional "head" */
1301 revs->max_count = atoi(arg + 1);
1303 } else if (!strcmp(arg, "-n")) {
1305 return error("-n requires an argument");
1306 revs->max_count = atoi(argv[1]);
1309 } else if (!prefixcmp(arg, "-n")) {
1310 revs->max_count = atoi(arg + 2);
1312 } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1313 revs->max_age = atoi(optarg);
1315 } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1316 revs->max_age = approxidate(optarg);
1318 } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1319 revs->max_age = approxidate(optarg);
1321 } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1322 revs->min_age = atoi(optarg);
1324 } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1325 revs->min_age = approxidate(optarg);
1327 } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1328 revs->min_age = approxidate(optarg);
1330 } else if (!strcmp(arg, "--first-parent")) {
1331 revs->first_parent_only = 1;
1332 } else if (!strcmp(arg, "--ancestry-path")) {
1333 revs->ancestry_path = 1;
1334 revs->simplify_history = 0;
1336 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1337 init_reflog_walk(&revs->reflog_info);
1338 } else if (!strcmp(arg, "--default")) {
1340 return error("bad --default argument");
1341 revs->def = argv[1];
1343 } else if (!strcmp(arg, "--merge")) {
1344 revs->show_merge = 1;
1345 } else if (!strcmp(arg, "--topo-order")) {
1347 revs->topo_order = 1;
1348 } else if (!strcmp(arg, "--simplify-merges")) {
1349 revs->simplify_merges = 1;
1350 revs->rewrite_parents = 1;
1351 revs->simplify_history = 0;
1353 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1354 revs->simplify_merges = 1;
1355 revs->rewrite_parents = 1;
1356 revs->simplify_history = 0;
1357 revs->simplify_by_decoration = 1;
1360 load_ref_decorations(DECORATE_SHORT_REFS);
1361 } else if (!strcmp(arg, "--date-order")) {
1363 revs->topo_order = 1;
1364 } else if (!prefixcmp(arg, "--early-output")) {
1368 count = atoi(arg+15);
1371 revs->topo_order = 1;
1372 revs->early_output = count;
1374 } else if (!strcmp(arg, "--parents")) {
1375 revs->rewrite_parents = 1;
1376 revs->print_parents = 1;
1377 } else if (!strcmp(arg, "--dense")) {
1379 } else if (!strcmp(arg, "--sparse")) {
1381 } else if (!strcmp(arg, "--show-all")) {
1383 } else if (!strcmp(arg, "--remove-empty")) {
1384 revs->remove_empty_trees = 1;
1385 } else if (!strcmp(arg, "--merges")) {
1386 revs->min_parents = 2;
1387 } else if (!strcmp(arg, "--no-merges")) {
1388 revs->max_parents = 1;
1389 } else if (!prefixcmp(arg, "--min-parents=")) {
1390 revs->min_parents = atoi(arg+14);
1391 } else if (!prefixcmp(arg, "--no-min-parents")) {
1392 revs->min_parents = 0;
1393 } else if (!prefixcmp(arg, "--max-parents=")) {
1394 revs->max_parents = atoi(arg+14);
1395 } else if (!prefixcmp(arg, "--no-max-parents")) {
1396 revs->max_parents = -1;
1397 } else if (!strcmp(arg, "--boundary")) {
1399 } else if (!strcmp(arg, "--left-right")) {
1400 revs->left_right = 1;
1401 } else if (!strcmp(arg, "--left-only")) {
1402 if (revs->right_only)
1403 die("--left-only is incompatible with --right-only"
1405 revs->left_only = 1;
1406 } else if (!strcmp(arg, "--right-only")) {
1407 if (revs->left_only)
1408 die("--right-only is incompatible with --left-only");
1409 revs->right_only = 1;
1410 } else if (!strcmp(arg, "--cherry")) {
1411 if (revs->left_only)
1412 die("--cherry is incompatible with --left-only");
1413 revs->cherry_mark = 1;
1414 revs->right_only = 1;
1415 revs->max_parents = 1;
1417 } else if (!strcmp(arg, "--count")) {
1419 } else if (!strcmp(arg, "--cherry-mark")) {
1420 if (revs->cherry_pick)
1421 die("--cherry-mark is incompatible with --cherry-pick");
1422 revs->cherry_mark = 1;
1423 revs->limited = 1; /* needs limit_list() */
1424 } else if (!strcmp(arg, "--cherry-pick")) {
1425 if (revs->cherry_mark)
1426 die("--cherry-pick is incompatible with --cherry-mark");
1427 revs->cherry_pick = 1;
1429 } else if (!strcmp(arg, "--objects")) {
1430 revs->tag_objects = 1;
1431 revs->tree_objects = 1;
1432 revs->blob_objects = 1;
1433 } else if (!strcmp(arg, "--objects-edge")) {
1434 revs->tag_objects = 1;
1435 revs->tree_objects = 1;
1436 revs->blob_objects = 1;
1437 revs->edge_hint = 1;
1438 } else if (!strcmp(arg, "--verify-objects")) {
1439 revs->tag_objects = 1;
1440 revs->tree_objects = 1;
1441 revs->blob_objects = 1;
1442 revs->verify_objects = 1;
1443 } else if (!strcmp(arg, "--unpacked")) {
1445 } else if (!prefixcmp(arg, "--unpacked=")) {
1446 die("--unpacked=<packfile> no longer supported.");
1447 } else if (!strcmp(arg, "-r")) {
1449 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1450 } else if (!strcmp(arg, "-t")) {
1452 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1453 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1454 } else if (!strcmp(arg, "-m")) {
1455 revs->ignore_merges = 0;
1456 } else if (!strcmp(arg, "-c")) {
1458 revs->dense_combined_merges = 0;
1459 revs->combine_merges = 1;
1460 } else if (!strcmp(arg, "--cc")) {
1462 revs->dense_combined_merges = 1;
1463 revs->combine_merges = 1;
1464 } else if (!strcmp(arg, "-v")) {
1465 revs->verbose_header = 1;
1466 } else if (!strcmp(arg, "--pretty")) {
1467 revs->verbose_header = 1;
1468 revs->pretty_given = 1;
1469 get_commit_format(arg+8, revs);
1470 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1472 * Detached form ("--pretty X" as opposed to "--pretty=X")
1473 * not allowed, since the argument is optional.
1475 revs->verbose_header = 1;
1476 revs->pretty_given = 1;
1477 get_commit_format(arg+9, revs);
1478 } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1479 revs->show_notes = 1;
1480 revs->show_notes_given = 1;
1481 revs->notes_opt.use_default_notes = 1;
1482 } else if (!strcmp(arg, "--show-signature")) {
1483 revs->show_signature = 1;
1484 } else if (!prefixcmp(arg, "--show-notes=") ||
1485 !prefixcmp(arg, "--notes=")) {
1486 struct strbuf buf = STRBUF_INIT;
1487 revs->show_notes = 1;
1488 revs->show_notes_given = 1;
1489 if (!prefixcmp(arg, "--show-notes")) {
1490 if (revs->notes_opt.use_default_notes < 0)
1491 revs->notes_opt.use_default_notes = 1;
1492 strbuf_addstr(&buf, arg+13);
1495 strbuf_addstr(&buf, arg+8);
1496 expand_notes_ref(&buf);
1497 string_list_append(&revs->notes_opt.extra_notes_refs,
1498 strbuf_detach(&buf, NULL));
1499 } else if (!strcmp(arg, "--no-notes")) {
1500 revs->show_notes = 0;
1501 revs->show_notes_given = 1;
1502 revs->notes_opt.use_default_notes = -1;
1503 /* we have been strdup'ing ourselves, so trick
1504 * string_list into free()ing strings */
1505 revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1506 string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1507 revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1508 } else if (!strcmp(arg, "--standard-notes")) {
1509 revs->show_notes_given = 1;
1510 revs->notes_opt.use_default_notes = 1;
1511 } else if (!strcmp(arg, "--no-standard-notes")) {
1512 revs->notes_opt.use_default_notes = 0;
1513 } else if (!strcmp(arg, "--oneline")) {
1514 revs->verbose_header = 1;
1515 get_commit_format("oneline", revs);
1516 revs->pretty_given = 1;
1517 revs->abbrev_commit = 1;
1518 } else if (!strcmp(arg, "--graph")) {
1519 revs->topo_order = 1;
1520 revs->rewrite_parents = 1;
1521 revs->graph = graph_init(revs);
1522 } else if (!strcmp(arg, "--root")) {
1523 revs->show_root_diff = 1;
1524 } else if (!strcmp(arg, "--no-commit-id")) {
1525 revs->no_commit_id = 1;
1526 } else if (!strcmp(arg, "--always")) {
1527 revs->always_show_header = 1;
1528 } else if (!strcmp(arg, "--no-abbrev")) {
1530 } else if (!strcmp(arg, "--abbrev")) {
1531 revs->abbrev = DEFAULT_ABBREV;
1532 } else if (!prefixcmp(arg, "--abbrev=")) {
1533 revs->abbrev = strtoul(arg + 9, NULL, 10);
1534 if (revs->abbrev < MINIMUM_ABBREV)
1535 revs->abbrev = MINIMUM_ABBREV;
1536 else if (revs->abbrev > 40)
1538 } else if (!strcmp(arg, "--abbrev-commit")) {
1539 revs->abbrev_commit = 1;
1540 revs->abbrev_commit_given = 1;
1541 } else if (!strcmp(arg, "--no-abbrev-commit")) {
1542 revs->abbrev_commit = 0;
1543 } else if (!strcmp(arg, "--full-diff")) {
1545 revs->full_diff = 1;
1546 } else if (!strcmp(arg, "--full-history")) {
1547 revs->simplify_history = 0;
1548 } else if (!strcmp(arg, "--relative-date")) {
1549 revs->date_mode = DATE_RELATIVE;
1550 revs->date_mode_explicit = 1;
1551 } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1552 revs->date_mode = parse_date_format(optarg);
1553 revs->date_mode_explicit = 1;
1555 } else if (!strcmp(arg, "--log-size")) {
1556 revs->show_log_size = 1;
1559 * Grepping the commit log
1561 else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1562 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1564 } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1565 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1567 } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1568 add_message_grep(revs, optarg);
1570 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1571 revs->grep_filter.regflags |= REG_EXTENDED;
1572 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1573 revs->grep_filter.regflags |= REG_ICASE;
1574 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1575 revs->grep_filter.fixed = 1;
1576 } else if (!strcmp(arg, "--all-match")) {
1577 revs->grep_filter.all_match = 1;
1578 } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1579 if (strcmp(optarg, "none"))
1580 git_log_output_encoding = xstrdup(optarg);
1582 git_log_output_encoding = "";
1584 } else if (!strcmp(arg, "--reverse")) {
1586 } else if (!strcmp(arg, "--children")) {
1587 revs->children.name = "children";
1589 } else if (!strcmp(arg, "--ignore-missing")) {
1590 revs->ignore_missing = 1;
1592 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1594 unkv[(*unkc)++] = arg;
1601 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1602 const struct option *options,
1603 const char * const usagestr[])
1605 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1606 &ctx->cpidx, ctx->out);
1608 error("unknown option `%s'", ctx->argv[0]);
1609 usage_with_options(usagestr, options);
1615 static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1617 return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
1620 static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1622 return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
1625 static int handle_revision_pseudo_opt(const char *submodule,
1626 struct rev_info *revs,
1627 int argc, const char **argv, int *flags)
1629 const char *arg = argv[0];
1636 * Commands like "git shortlog" will not accept the options below
1637 * unless parse_revision_opt queues them (as opposed to erroring
1640 * When implementing your new pseudo-option, remember to
1641 * register it in the list at the top of handle_revision_opt.
1643 if (!strcmp(arg, "--all")) {
1644 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
1645 handle_refs(submodule, revs, *flags, head_ref_submodule);
1646 } else if (!strcmp(arg, "--branches")) {
1647 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
1648 } else if (!strcmp(arg, "--bisect")) {
1649 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
1650 handle_refs(submodule, revs, *flags ^ UNINTERESTING, for_each_good_bisect_ref);
1652 } else if (!strcmp(arg, "--tags")) {
1653 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
1654 } else if (!strcmp(arg, "--remotes")) {
1655 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
1656 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
1657 struct all_refs_cb cb;
1658 init_all_refs_cb(&cb, revs, *flags);
1659 for_each_glob_ref(handle_one_ref, optarg, &cb);
1661 } else if (!prefixcmp(arg, "--branches=")) {
1662 struct all_refs_cb cb;
1663 init_all_refs_cb(&cb, revs, *flags);
1664 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1665 } else if (!prefixcmp(arg, "--tags=")) {
1666 struct all_refs_cb cb;
1667 init_all_refs_cb(&cb, revs, *flags);
1668 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1669 } else if (!prefixcmp(arg, "--remotes=")) {
1670 struct all_refs_cb cb;
1671 init_all_refs_cb(&cb, revs, *flags);
1672 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1673 } else if (!strcmp(arg, "--reflog")) {
1674 handle_reflog(revs, *flags);
1675 } else if (!strcmp(arg, "--not")) {
1676 *flags ^= UNINTERESTING;
1677 } else if (!strcmp(arg, "--no-walk")) {
1679 } else if (!strcmp(arg, "--do-walk")) {
1689 * Parse revision information, filling in the "rev_info" structure,
1690 * and removing the used arguments from the argument list.
1692 * Returns the number of arguments left that weren't recognized
1693 * (which are also moved to the head of the argument list)
1695 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
1697 int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0;
1698 struct cmdline_pathspec prune_data;
1699 const char *submodule = NULL;
1701 memset(&prune_data, 0, sizeof(prune_data));
1703 submodule = opt->submodule;
1705 /* First, search for "--" */
1707 for (i = 1; i < argc; i++) {
1708 const char *arg = argv[i];
1709 if (strcmp(arg, "--"))
1714 append_prune_data(&prune_data, argv + i + 1);
1719 /* Second, deal with arguments and options */
1721 read_from_stdin = 0;
1722 for (left = i = 1; i < argc; i++) {
1723 const char *arg = argv[i];
1727 opts = handle_revision_pseudo_opt(submodule,
1728 revs, argc - i, argv + i,
1735 if (!strcmp(arg, "--stdin")) {
1736 if (revs->disable_stdin) {
1740 if (read_from_stdin++)
1741 die("--stdin given twice?");
1742 read_revisions_from_stdin(revs, &prune_data);
1746 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
1756 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1758 if (seen_dashdash || *arg == '^')
1759 die("bad revision '%s'", arg);
1761 /* If we didn't have a "--":
1762 * (1) all filenames must exist;
1763 * (2) all rev-args must not be interpretable
1764 * as a valid filename.
1765 * but the latter we have checked in the main loop.
1767 for (j = i; j < argc; j++)
1768 verify_filename(revs->prefix, argv[j]);
1770 append_prune_data(&prune_data, argv + i);
1777 if (prune_data.nr) {
1779 * If we need to introduce the magic "a lone ':' means no
1780 * pathspec whatsoever", here is the place to do so.
1782 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
1783 * prune_data.nr = 0;
1784 * prune_data.alloc = 0;
1785 * free(prune_data.path);
1786 * prune_data.path = NULL;
1788 * terminate prune_data.alloc with NULL and
1789 * call init_pathspec() to set revs->prune_data here.
1792 ALLOC_GROW(prune_data.path, prune_data.nr+1, prune_data.alloc);
1793 prune_data.path[prune_data.nr++] = NULL;
1794 init_pathspec(&revs->prune_data,
1795 get_pathspec(revs->prefix, prune_data.path));
1798 if (revs->def == NULL)
1799 revs->def = opt ? opt->def : NULL;
1800 if (opt && opt->tweak)
1801 opt->tweak(revs, opt);
1802 if (revs->show_merge)
1803 prepare_show_merge(revs);
1804 if (revs->def && !revs->pending.nr && !got_rev_arg) {
1805 unsigned char sha1[20];
1806 struct object *object;
1808 if (get_sha1_with_mode(revs->def, sha1, &mode))
1809 die("bad default revision '%s'", revs->def);
1810 object = get_reference(revs, revs->def, sha1, 0);
1811 add_pending_object_with_mode(revs, object, revs->def, mode);
1814 /* Did the user ask for any diff output? Run the diff! */
1815 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1818 /* Pickaxe, diff-filter and rename following need diffs */
1819 if (revs->diffopt.pickaxe ||
1820 revs->diffopt.filter ||
1821 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1824 if (revs->topo_order)
1827 if (revs->prune_data.nr) {
1828 diff_tree_setup_paths(revs->prune_data.raw, &revs->pruning);
1829 /* Can't prune commits with rename following: the paths change.. */
1830 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1832 if (!revs->full_diff)
1833 diff_tree_setup_paths(revs->prune_data.raw, &revs->diffopt);
1835 if (revs->combine_merges)
1836 revs->ignore_merges = 0;
1837 revs->diffopt.abbrev = revs->abbrev;
1838 if (diff_setup_done(&revs->diffopt) < 0)
1839 die("diff_setup_done failed");
1841 compile_grep_patterns(&revs->grep_filter);
1843 if (revs->reverse && revs->reflog_info)
1844 die("cannot combine --reverse with --walk-reflogs");
1845 if (revs->rewrite_parents && revs->children.name)
1846 die("cannot combine --parents and --children");
1849 * Limitations on the graph functionality
1851 if (revs->reverse && revs->graph)
1852 die("cannot combine --reverse with --graph");
1854 if (revs->reflog_info && revs->graph)
1855 die("cannot combine --walk-reflogs with --graph");
1860 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1862 struct commit_list *l = xcalloc(1, sizeof(*l));
1865 l->next = add_decoration(&revs->children, &parent->object, l);
1868 static int remove_duplicate_parents(struct commit *commit)
1870 struct commit_list **pp, *p;
1871 int surviving_parents;
1873 /* Examine existing parents while marking ones we have seen... */
1874 pp = &commit->parents;
1875 while ((p = *pp) != NULL) {
1876 struct commit *parent = p->item;
1877 if (parent->object.flags & TMP_MARK) {
1881 parent->object.flags |= TMP_MARK;
1884 /* count them while clearing the temporary mark */
1885 surviving_parents = 0;
1886 for (p = commit->parents; p; p = p->next) {
1887 p->item->object.flags &= ~TMP_MARK;
1888 surviving_parents++;
1890 return surviving_parents;
1893 struct merge_simplify_state {
1894 struct commit *simplified;
1897 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1899 struct merge_simplify_state *st;
1901 st = lookup_decoration(&revs->merge_simplification, &commit->object);
1903 st = xcalloc(1, sizeof(*st));
1904 add_decoration(&revs->merge_simplification, &commit->object, st);
1909 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
1911 struct commit_list *p;
1912 struct merge_simplify_state *st, *pst;
1915 st = locate_simplify_state(revs, commit);
1918 * Have we handled this one?
1924 * An UNINTERESTING commit simplifies to itself, so does a
1925 * root commit. We do not rewrite parents of such commit
1928 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
1929 st->simplified = commit;
1934 * Do we know what commit all of our parents should be rewritten to?
1935 * Otherwise we are not ready to rewrite this one yet.
1937 for (cnt = 0, p = commit->parents; p; p = p->next) {
1938 pst = locate_simplify_state(revs, p->item);
1939 if (!pst->simplified) {
1940 tail = &commit_list_insert(p->item, tail)->next;
1945 tail = &commit_list_insert(commit, tail)->next;
1950 * Rewrite our list of parents.
1952 for (p = commit->parents; p; p = p->next) {
1953 pst = locate_simplify_state(revs, p->item);
1954 p->item = pst->simplified;
1956 cnt = remove_duplicate_parents(commit);
1959 * It is possible that we are a merge and one side branch
1960 * does not have any commit that touches the given paths;
1961 * in such a case, the immediate parents will be rewritten
1962 * to different commits.
1964 * o----X X: the commit we are looking at;
1965 * / / o: a commit that touches the paths;
1968 * Further reduce the parents by removing redundant parents.
1971 struct commit_list *h = reduce_heads(commit->parents);
1972 cnt = commit_list_count(h);
1973 free_commit_list(commit->parents);
1974 commit->parents = h;
1978 * A commit simplifies to itself if it is a root, if it is
1979 * UNINTERESTING, if it touches the given paths, or if it is a
1980 * merge and its parents simplifies to more than one commits
1981 * (the first two cases are already handled at the beginning of
1984 * Otherwise, it simplifies to what its sole parent simplifies to.
1987 (commit->object.flags & UNINTERESTING) ||
1988 !(commit->object.flags & TREESAME) ||
1990 st->simplified = commit;
1992 pst = locate_simplify_state(revs, commit->parents->item);
1993 st->simplified = pst->simplified;
1998 static void simplify_merges(struct rev_info *revs)
2000 struct commit_list *list;
2001 struct commit_list *yet_to_do, **tail;
2003 if (!revs->topo_order)
2004 sort_in_topological_order(&revs->commits, revs->lifo);
2008 /* feed the list reversed */
2010 for (list = revs->commits; list; list = list->next)
2011 commit_list_insert(list->item, &yet_to_do);
2017 struct commit *commit = list->item;
2018 struct commit_list *next = list->next;
2021 tail = simplify_one(revs, commit, tail);
2025 /* clean up the result, removing the simplified ones */
2026 list = revs->commits;
2027 revs->commits = NULL;
2028 tail = &revs->commits;
2030 struct commit *commit = list->item;
2031 struct commit_list *next = list->next;
2032 struct merge_simplify_state *st;
2035 st = locate_simplify_state(revs, commit);
2036 if (st->simplified == commit)
2037 tail = &commit_list_insert(commit, tail)->next;
2041 static void set_children(struct rev_info *revs)
2043 struct commit_list *l;
2044 for (l = revs->commits; l; l = l->next) {
2045 struct commit *commit = l->item;
2046 struct commit_list *p;
2048 for (p = commit->parents; p; p = p->next)
2049 add_child(revs, p->item, commit);
2053 int prepare_revision_walk(struct rev_info *revs)
2055 int nr = revs->pending.nr;
2056 struct object_array_entry *e, *list;
2058 e = list = revs->pending.objects;
2059 revs->pending.nr = 0;
2060 revs->pending.alloc = 0;
2061 revs->pending.objects = NULL;
2063 struct commit *commit = handle_commit(revs, e->item, e->name);
2065 if (!(commit->object.flags & SEEN)) {
2066 commit->object.flags |= SEEN;
2067 commit_list_insert_by_date(commit, &revs->commits);
2072 if (!revs->leak_pending)
2078 if (limit_list(revs) < 0)
2080 if (revs->topo_order)
2081 sort_in_topological_order(&revs->commits, revs->lifo);
2082 if (revs->simplify_merges)
2083 simplify_merges(revs);
2084 if (revs->children.name)
2089 enum rewrite_result {
2091 rewrite_one_noparents,
2095 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2097 struct commit_list *cache = NULL;
2100 struct commit *p = *pp;
2102 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2103 return rewrite_one_error;
2104 if (p->parents && p->parents->next)
2105 return rewrite_one_ok;
2106 if (p->object.flags & UNINTERESTING)
2107 return rewrite_one_ok;
2108 if (!(p->object.flags & TREESAME))
2109 return rewrite_one_ok;
2111 return rewrite_one_noparents;
2112 *pp = p->parents->item;
2116 static int rewrite_parents(struct rev_info *revs, struct commit *commit)
2118 struct commit_list **pp = &commit->parents;
2120 struct commit_list *parent = *pp;
2121 switch (rewrite_one(revs, &parent->item)) {
2122 case rewrite_one_ok:
2124 case rewrite_one_noparents:
2127 case rewrite_one_error:
2132 remove_duplicate_parents(commit);
2136 static int commit_match(struct commit *commit, struct rev_info *opt)
2138 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2140 return grep_buffer(&opt->grep_filter,
2141 commit->buffer, strlen(commit->buffer));
2144 static inline int want_ancestry(struct rev_info *revs)
2146 return (revs->rewrite_parents || revs->children.name);
2149 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2151 if (commit->object.flags & SHOWN)
2152 return commit_ignore;
2153 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2154 return commit_ignore;
2157 if (commit->object.flags & UNINTERESTING)
2158 return commit_ignore;
2159 if (revs->min_age != -1 && (commit->date > revs->min_age))
2160 return commit_ignore;
2161 if (revs->min_parents || (revs->max_parents >= 0)) {
2163 struct commit_list *p;
2164 for (p = commit->parents; p; p = p->next)
2166 if ((n < revs->min_parents) ||
2167 ((revs->max_parents >= 0) && (n > revs->max_parents)))
2168 return commit_ignore;
2170 if (!commit_match(commit, revs))
2171 return commit_ignore;
2172 if (revs->prune && revs->dense) {
2173 /* Commit without changes? */
2174 if (commit->object.flags & TREESAME) {
2175 /* drop merges unless we want parenthood */
2176 if (!want_ancestry(revs))
2177 return commit_ignore;
2178 /* non-merge - always ignore it */
2179 if (!commit->parents || !commit->parents->next)
2180 return commit_ignore;
2186 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
2188 enum commit_action action = get_commit_action(revs, commit);
2190 if (action == commit_show &&
2192 revs->prune && revs->dense && want_ancestry(revs)) {
2193 if (rewrite_parents(revs, commit) < 0)
2194 return commit_error;
2199 static struct commit *get_revision_1(struct rev_info *revs)
2205 struct commit_list *entry = revs->commits;
2206 struct commit *commit = entry->item;
2208 revs->commits = entry->next;
2211 if (revs->reflog_info) {
2212 fake_reflog_parent(revs->reflog_info, commit);
2213 commit->object.flags &= ~(ADDED | SEEN | SHOWN);
2217 * If we haven't done the list limiting, we need to look at
2218 * the parents here. We also need to do the date-based limiting
2219 * that we'd otherwise have done in limit_list().
2221 if (!revs->limited) {
2222 if (revs->max_age != -1 &&
2223 (commit->date < revs->max_age))
2225 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
2226 die("Failed to traverse parents of commit %s",
2227 sha1_to_hex(commit->object.sha1));
2230 switch (simplify_commit(revs, commit)) {
2234 die("Failed to simplify parents of commit %s",
2235 sha1_to_hex(commit->object.sha1));
2239 } while (revs->commits);
2243 static void gc_boundary(struct object_array *array)
2245 unsigned nr = array->nr;
2246 unsigned alloc = array->alloc;
2247 struct object_array_entry *objects = array->objects;
2251 for (i = j = 0; i < nr; i++) {
2252 if (objects[i].item->flags & SHOWN)
2255 objects[j] = objects[i];
2258 for (i = j; i < nr; i++)
2259 objects[i].item = NULL;
2264 static void create_boundary_commit_list(struct rev_info *revs)
2268 struct object_array *array = &revs->boundary_commits;
2269 struct object_array_entry *objects = array->objects;
2272 * If revs->commits is non-NULL at this point, an error occurred in
2273 * get_revision_1(). Ignore the error and continue printing the
2274 * boundary commits anyway. (This is what the code has always
2277 if (revs->commits) {
2278 free_commit_list(revs->commits);
2279 revs->commits = NULL;
2283 * Put all of the actual boundary commits from revs->boundary_commits
2284 * into revs->commits
2286 for (i = 0; i < array->nr; i++) {
2287 c = (struct commit *)(objects[i].item);
2290 if (!(c->object.flags & CHILD_SHOWN))
2292 if (c->object.flags & (SHOWN | BOUNDARY))
2294 c->object.flags |= BOUNDARY;
2295 commit_list_insert(c, &revs->commits);
2299 * If revs->topo_order is set, sort the boundary commits
2300 * in topological order
2302 sort_in_topological_order(&revs->commits, revs->lifo);
2305 static struct commit *get_revision_internal(struct rev_info *revs)
2307 struct commit *c = NULL;
2308 struct commit_list *l;
2310 if (revs->boundary == 2) {
2312 * All of the normal commits have already been returned,
2313 * and we are now returning boundary commits.
2314 * create_boundary_commit_list() has populated
2315 * revs->commits with the remaining commits to return.
2317 c = pop_commit(&revs->commits);
2319 c->object.flags |= SHOWN;
2324 * Now pick up what they want to give us
2326 c = get_revision_1(revs);
2328 while (0 < revs->skip_count) {
2330 c = get_revision_1(revs);
2337 * Check the max_count.
2339 switch (revs->max_count) {
2350 c->object.flags |= SHOWN;
2352 if (!revs->boundary) {
2358 * get_revision_1() runs out the commits, and
2359 * we are done computing the boundaries.
2360 * switch to boundary commits output mode.
2365 * Update revs->commits to contain the list of
2368 create_boundary_commit_list(revs);
2370 return get_revision_internal(revs);
2374 * boundary commits are the commits that are parents of the
2375 * ones we got from get_revision_1() but they themselves are
2376 * not returned from get_revision_1(). Before returning
2377 * 'c', we need to mark its parents that they could be boundaries.
2380 for (l = c->parents; l; l = l->next) {
2382 p = &(l->item->object);
2383 if (p->flags & (CHILD_SHOWN | SHOWN))
2385 p->flags |= CHILD_SHOWN;
2386 gc_boundary(&revs->boundary_commits);
2387 add_object_array(p, NULL, &revs->boundary_commits);
2393 struct commit *get_revision(struct rev_info *revs)
2396 struct commit_list *reversed;
2398 if (revs->reverse) {
2400 while ((c = get_revision_internal(revs))) {
2401 commit_list_insert(c, &reversed);
2403 revs->commits = reversed;
2405 revs->reverse_output_stage = 1;
2408 if (revs->reverse_output_stage)
2409 return pop_commit(&revs->commits);
2411 c = get_revision_internal(revs);
2412 if (c && revs->graph)
2413 graph_update(revs->graph, c);
2417 char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
2419 if (commit->object.flags & BOUNDARY)
2421 else if (commit->object.flags & UNINTERESTING)
2423 else if (commit->object.flags & PATCHSAME)
2425 else if (!revs || revs->left_right) {
2426 if (commit->object.flags & SYMMETRIC_LEFT)
2430 } else if (revs->graph)
2432 else if (revs->cherry_mark)
2437 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
2439 char *mark = get_revision_mark(revs, commit);
2442 fputs(mark, stdout);