11 #include "reflog-walk.h"
12 #include "patch-ids.h"
16 volatile show_early_output_fn_t show_early_output;
18 char *path_name(const struct name_path *path, const char *name)
20 const struct name_path *p;
22 int nlen = strlen(name);
25 for (p = path; p; p = p->up) {
27 len += p->elem_len + 1;
30 m = n + len - (nlen + 1);
32 for (p = path; p; p = p->up) {
35 memcpy(m, p->elem, p->elem_len);
42 void add_object(struct object *obj,
43 struct object_array *p,
44 struct name_path *path,
47 add_object_array(obj, path_name(path, name), p);
50 static void mark_blob_uninteresting(struct blob *blob)
54 if (blob->object.flags & UNINTERESTING)
56 blob->object.flags |= UNINTERESTING;
59 void mark_tree_uninteresting(struct tree *tree)
61 struct tree_desc desc;
62 struct name_entry entry;
63 struct object *obj = &tree->object;
67 if (obj->flags & UNINTERESTING)
69 obj->flags |= UNINTERESTING;
70 if (!has_sha1_file(obj->sha1))
72 if (parse_tree(tree) < 0)
73 die("bad tree %s", sha1_to_hex(obj->sha1));
75 init_tree_desc(&desc, tree->buffer, tree->size);
76 while (tree_entry(&desc, &entry)) {
77 switch (object_type(entry.mode)) {
79 mark_tree_uninteresting(lookup_tree(entry.sha1));
82 mark_blob_uninteresting(lookup_blob(entry.sha1));
85 /* Subproject commit - not in this repository */
91 * We don't care about the tree any more
92 * after it has been marked uninteresting.
98 void mark_parents_uninteresting(struct commit *commit)
100 struct commit_list *parents = commit->parents;
103 struct commit *commit = parents->item;
104 if (!(commit->object.flags & UNINTERESTING)) {
105 commit->object.flags |= UNINTERESTING;
108 * Normally we haven't parsed the parent
109 * yet, so we won't have a parent of a parent
110 * here. However, it may turn out that we've
111 * reached this commit some other way (where it
112 * wasn't uninteresting), in which case we need
113 * to mark its parents recursively too..
116 mark_parents_uninteresting(commit);
120 * A missing commit is ok iff its parent is marked
123 * We just mark such a thing parsed, so that when
124 * it is popped next time around, we won't be trying
125 * to parse it and get an error.
127 if (!has_sha1_file(commit->object.sha1))
128 commit->object.parsed = 1;
129 parents = parents->next;
133 static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
135 if (revs->no_walk && (obj->flags & UNINTERESTING))
137 if (revs->reflog_info && obj->type == OBJ_COMMIT) {
138 struct strbuf buf = STRBUF_INIT;
139 int len = interpret_branch_name(name, &buf);
142 if (0 < len && name[len] && buf.len)
143 strbuf_addstr(&buf, name + len);
144 st = add_reflog_for_walk(revs->reflog_info,
145 (struct commit *)obj,
146 buf.buf[0] ? buf.buf: name);
147 strbuf_release(&buf);
151 add_object_array_with_mode(obj, name, &revs->pending, mode);
154 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
156 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
159 void add_head_to_pending(struct rev_info *revs)
161 unsigned char sha1[20];
163 if (get_sha1("HEAD", sha1))
165 obj = parse_object(sha1);
168 add_pending_object(revs, obj, "HEAD");
171 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
173 struct object *object;
175 object = parse_object(sha1);
177 die("bad object %s", name);
178 object->flags |= flags;
182 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
184 unsigned long flags = object->flags;
187 * Tag object? Look what it points to..
189 while (object->type == OBJ_TAG) {
190 struct tag *tag = (struct tag *) object;
191 if (revs->tag_objects && !(flags & UNINTERESTING))
192 add_pending_object(revs, object, tag->tag);
195 object = parse_object(tag->tagged->sha1);
197 if (flags & UNINTERESTING)
199 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
204 * Commit object? Just return it, we'll do all the complex
207 if (object->type == OBJ_COMMIT) {
208 struct commit *commit = (struct commit *)object;
209 if (parse_commit(commit) < 0)
210 die("unable to parse commit %s", name);
211 if (flags & UNINTERESTING) {
212 commit->object.flags |= UNINTERESTING;
213 mark_parents_uninteresting(commit);
216 if (revs->show_source && !commit->util)
217 commit->util = (void *) name;
222 * Tree object? Either mark it uninteresting, or add it
223 * to the list of objects to look at later..
225 if (object->type == OBJ_TREE) {
226 struct tree *tree = (struct tree *)object;
227 if (!revs->tree_objects)
229 if (flags & UNINTERESTING) {
230 mark_tree_uninteresting(tree);
233 add_pending_object(revs, object, "");
238 * Blob object? You know the drill by now..
240 if (object->type == OBJ_BLOB) {
241 struct blob *blob = (struct blob *)object;
242 if (!revs->blob_objects)
244 if (flags & UNINTERESTING) {
245 mark_blob_uninteresting(blob);
248 add_pending_object(revs, object, "");
251 die("%s is unknown object", name);
254 static int everybody_uninteresting(struct commit_list *orig)
256 struct commit_list *list = orig;
258 struct commit *commit = list->item;
260 if (commit->object.flags & UNINTERESTING)
268 * The goal is to get REV_TREE_NEW as the result only if the
269 * diff consists of all '+' (and no other changes), REV_TREE_OLD
270 * if the whole diff is removal of old data, and otherwise
271 * REV_TREE_DIFFERENT (of course if the trees are the same we
272 * want REV_TREE_SAME).
273 * That means that once we get to REV_TREE_DIFFERENT, we do not
274 * have to look any further.
276 static int tree_difference = REV_TREE_SAME;
278 static void file_add_remove(struct diff_options *options,
279 int addremove, unsigned mode,
280 const unsigned char *sha1,
281 const char *fullpath, unsigned dirty_submodule)
283 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
285 tree_difference |= diff;
286 if (tree_difference == REV_TREE_DIFFERENT)
287 DIFF_OPT_SET(options, HAS_CHANGES);
290 static void file_change(struct diff_options *options,
291 unsigned old_mode, unsigned new_mode,
292 const unsigned char *old_sha1,
293 const unsigned char *new_sha1,
294 const char *fullpath,
295 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
297 tree_difference = REV_TREE_DIFFERENT;
298 DIFF_OPT_SET(options, HAS_CHANGES);
301 static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
303 struct tree *t1 = parent->tree;
304 struct tree *t2 = commit->tree;
311 if (revs->simplify_by_decoration) {
313 * If we are simplifying by decoration, then the commit
314 * is worth showing if it has a tag pointing at it.
316 if (lookup_decoration(&name_decoration, &commit->object))
317 return REV_TREE_DIFFERENT;
319 * A commit that is not pointed by a tag is uninteresting
320 * if we are not limited by path. This means that you will
321 * see the usual "commits that touch the paths" plus any
322 * tagged commit by specifying both --simplify-by-decoration
325 if (!revs->prune_data)
326 return REV_TREE_SAME;
329 tree_difference = REV_TREE_SAME;
330 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
331 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
333 return REV_TREE_DIFFERENT;
334 return tree_difference;
337 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
342 struct tree_desc empty, real;
343 struct tree *t1 = commit->tree;
348 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
351 init_tree_desc(&real, tree, size);
352 init_tree_desc(&empty, "", 0);
354 tree_difference = REV_TREE_SAME;
355 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
356 retval = diff_tree(&empty, &real, "", &revs->pruning);
359 return retval >= 0 && (tree_difference == REV_TREE_SAME);
362 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
364 struct commit_list **pp, *parent;
365 int tree_changed = 0, tree_same = 0;
368 * If we don't do pruning, everything is interesting
376 if (!commit->parents) {
377 if (rev_same_tree_as_empty(revs, commit))
378 commit->object.flags |= TREESAME;
383 * Normal non-merge commit? If we don't want to make the
384 * history dense, we consider it always to be a change..
386 if (!revs->dense && !commit->parents->next)
389 pp = &commit->parents;
390 while ((parent = *pp) != NULL) {
391 struct commit *p = parent->item;
393 if (parse_commit(p) < 0)
394 die("cannot simplify commit %s (because of %s)",
395 sha1_to_hex(commit->object.sha1),
396 sha1_to_hex(p->object.sha1));
397 switch (rev_compare_tree(revs, p, commit)) {
400 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
401 /* Even if a merge with an uninteresting
402 * side branch brought the entire change
403 * we are interested in, we do not want
404 * to lose the other branches of this
405 * merge, so we just keep going.
411 commit->parents = parent;
412 commit->object.flags |= TREESAME;
416 if (revs->remove_empty_trees &&
417 rev_same_tree_as_empty(revs, p)) {
418 /* We are adding all the specified
419 * paths from this parent, so the
420 * history beyond this parent is not
421 * interesting. Remove its parents
422 * (they are grandparents for us).
423 * IOW, we pretend this parent is a
426 if (parse_commit(p) < 0)
427 die("cannot simplify commit %s (invalid %s)",
428 sha1_to_hex(commit->object.sha1),
429 sha1_to_hex(p->object.sha1));
434 case REV_TREE_DIFFERENT:
439 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
441 if (tree_changed && !tree_same)
443 commit->object.flags |= TREESAME;
446 static void insert_by_date_cached(struct commit *p, struct commit_list **head,
447 struct commit_list *cached_base, struct commit_list **cache)
449 struct commit_list *new_entry;
451 if (cached_base && p->date < cached_base->item->date)
452 new_entry = insert_by_date(p, &cached_base->next);
454 new_entry = insert_by_date(p, head);
456 if (cache && (!*cache || p->date < (*cache)->item->date))
460 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
461 struct commit_list **list, struct commit_list **cache_ptr)
463 struct commit_list *parent = commit->parents;
465 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
467 if (commit->object.flags & ADDED)
469 commit->object.flags |= ADDED;
472 * If the commit is uninteresting, don't try to
473 * prune parents - we want the maximal uninteresting
476 * Normally we haven't parsed the parent
477 * yet, so we won't have a parent of a parent
478 * here. However, it may turn out that we've
479 * reached this commit some other way (where it
480 * wasn't uninteresting), in which case we need
481 * to mark its parents recursively too..
483 if (commit->object.flags & UNINTERESTING) {
485 struct commit *p = parent->item;
486 parent = parent->next;
488 p->object.flags |= UNINTERESTING;
489 if (parse_commit(p) < 0)
492 mark_parents_uninteresting(p);
493 if (p->object.flags & SEEN)
495 p->object.flags |= SEEN;
496 insert_by_date_cached(p, list, cached_base, cache_ptr);
502 * Ok, the commit wasn't uninteresting. Try to
503 * simplify the commit history and find the parent
504 * that has no differences in the path set if one exists.
506 try_to_simplify_commit(revs, commit);
511 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
513 for (parent = commit->parents; parent; parent = parent->next) {
514 struct commit *p = parent->item;
516 if (parse_commit(p) < 0)
518 if (revs->show_source && !p->util)
519 p->util = commit->util;
520 p->object.flags |= left_flag;
521 if (!(p->object.flags & SEEN)) {
522 p->object.flags |= SEEN;
523 insert_by_date_cached(p, list, cached_base, cache_ptr);
525 if (revs->first_parent_only)
531 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
533 struct commit_list *p;
534 int left_count = 0, right_count = 0;
536 struct patch_ids ids;
538 /* First count the commits on the left and on the right */
539 for (p = list; p; p = p->next) {
540 struct commit *commit = p->item;
541 unsigned flags = commit->object.flags;
542 if (flags & BOUNDARY)
544 else if (flags & SYMMETRIC_LEFT)
550 left_first = left_count < right_count;
551 init_patch_ids(&ids);
552 if (revs->diffopt.nr_paths) {
553 ids.diffopts.nr_paths = revs->diffopt.nr_paths;
554 ids.diffopts.paths = revs->diffopt.paths;
555 ids.diffopts.pathlens = revs->diffopt.pathlens;
558 /* Compute patch-ids for one side */
559 for (p = list; p; p = p->next) {
560 struct commit *commit = p->item;
561 unsigned flags = commit->object.flags;
563 if (flags & BOUNDARY)
566 * If we have fewer left, left_first is set and we omit
567 * commits on the right branch in this loop. If we have
568 * fewer right, we skip the left ones.
570 if (left_first != !!(flags & SYMMETRIC_LEFT))
572 commit->util = add_commit_patch_id(commit, &ids);
575 /* Check the other side */
576 for (p = list; p; p = p->next) {
577 struct commit *commit = p->item;
579 unsigned flags = commit->object.flags;
581 if (flags & BOUNDARY)
584 * If we have fewer left, left_first is set and we omit
585 * commits on the left branch in this loop.
587 if (left_first == !!(flags & SYMMETRIC_LEFT))
591 * Have we seen the same patch id?
593 id = has_commit_patch_id(commit, &ids);
597 commit->object.flags |= SHOWN;
600 /* Now check the original side for seen ones */
601 for (p = list; p; p = p->next) {
602 struct commit *commit = p->item;
603 struct patch_id *ent;
609 commit->object.flags |= SHOWN;
613 free_patch_ids(&ids);
616 /* How many extra uninteresting commits we want to see.. */
619 static int still_interesting(struct commit_list *src, unsigned long date, int slop)
622 * No source list at all? We're definitely done..
628 * Does the destination list contain entries with a date
629 * before the source list? Definitely _not_ done.
631 if (date < src->item->date)
635 * Does the source list still have interesting commits in
636 * it? Definitely not done..
638 if (!everybody_uninteresting(src))
641 /* Ok, we're closing in.. */
645 static int limit_list(struct rev_info *revs)
648 unsigned long date = ~0ul;
649 struct commit_list *list = revs->commits;
650 struct commit_list *newlist = NULL;
651 struct commit_list **p = &newlist;
654 struct commit_list *entry = list;
655 struct commit *commit = list->item;
656 struct object *obj = &commit->object;
657 show_early_output_fn_t show;
662 if (revs->max_age != -1 && (commit->date < revs->max_age))
663 obj->flags |= UNINTERESTING;
664 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
666 if (obj->flags & UNINTERESTING) {
667 mark_parents_uninteresting(commit);
669 p = &commit_list_insert(commit, p)->next;
670 slop = still_interesting(list, date, slop);
673 /* If showing all, add the whole pending list to the end */
678 if (revs->min_age != -1 && (commit->date > revs->min_age))
681 p = &commit_list_insert(commit, p)->next;
683 show = show_early_output;
688 show_early_output = NULL;
690 if (revs->cherry_pick)
691 cherry_pick_list(newlist, revs);
693 revs->commits = newlist;
699 int warned_bad_reflog;
700 struct rev_info *all_revs;
701 const char *name_for_errormsg;
704 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
706 struct all_refs_cb *cb = cb_data;
707 struct object *object = get_reference(cb->all_revs, path, sha1,
709 add_pending_object(cb->all_revs, object, path);
713 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
717 cb->all_flags = flags;
720 static void handle_refs(struct rev_info *revs, unsigned flags,
721 int (*for_each)(each_ref_fn, void *))
723 struct all_refs_cb cb;
724 init_all_refs_cb(&cb, revs, flags);
725 for_each(handle_one_ref, &cb);
728 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
730 struct all_refs_cb *cb = cb_data;
731 if (!is_null_sha1(sha1)) {
732 struct object *o = parse_object(sha1);
734 o->flags |= cb->all_flags;
735 add_pending_object(cb->all_revs, o, "");
737 else if (!cb->warned_bad_reflog) {
738 warning("reflog of '%s' references pruned commits",
739 cb->name_for_errormsg);
740 cb->warned_bad_reflog = 1;
745 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
746 const char *email, unsigned long timestamp, int tz,
747 const char *message, void *cb_data)
749 handle_one_reflog_commit(osha1, cb_data);
750 handle_one_reflog_commit(nsha1, cb_data);
754 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
756 struct all_refs_cb *cb = cb_data;
757 cb->warned_bad_reflog = 0;
758 cb->name_for_errormsg = path;
759 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
763 static void handle_reflog(struct rev_info *revs, unsigned flags)
765 struct all_refs_cb cb;
767 cb.all_flags = flags;
768 for_each_reflog(handle_one_reflog, &cb);
771 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
773 unsigned char sha1[20];
775 struct commit *commit;
776 struct commit_list *parents;
779 flags ^= UNINTERESTING;
782 if (get_sha1(arg, sha1))
785 it = get_reference(revs, arg, sha1, 0);
786 if (it->type != OBJ_TAG)
788 if (!((struct tag*)it)->tagged)
790 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
792 if (it->type != OBJ_COMMIT)
794 commit = (struct commit *)it;
795 for (parents = commit->parents; parents; parents = parents->next) {
796 it = &parents->item->object;
798 add_pending_object(revs, it, arg);
803 void init_revisions(struct rev_info *revs, const char *prefix)
805 memset(revs, 0, sizeof(*revs));
807 revs->abbrev = DEFAULT_ABBREV;
808 revs->ignore_merges = 1;
809 revs->simplify_history = 1;
810 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
811 DIFF_OPT_SET(&revs->pruning, QUICK);
812 revs->pruning.add_remove = file_add_remove;
813 revs->pruning.change = file_change;
816 revs->prefix = prefix;
819 revs->skip_count = -1;
820 revs->max_count = -1;
822 revs->commit_format = CMIT_FMT_DEFAULT;
824 revs->grep_filter.status_only = 1;
825 revs->grep_filter.pattern_tail = &(revs->grep_filter.pattern_list);
826 revs->grep_filter.header_tail = &(revs->grep_filter.header_list);
827 revs->grep_filter.regflags = REG_NEWLINE;
829 diff_setup(&revs->diffopt);
830 if (prefix && !revs->diffopt.prefix) {
831 revs->diffopt.prefix = prefix;
832 revs->diffopt.prefix_length = strlen(prefix);
836 static void add_pending_commit_list(struct rev_info *revs,
837 struct commit_list *commit_list,
840 while (commit_list) {
841 struct object *object = &commit_list->item->object;
842 object->flags |= flags;
843 add_pending_object(revs, object, sha1_to_hex(object->sha1));
844 commit_list = commit_list->next;
848 static void prepare_show_merge(struct rev_info *revs)
850 struct commit_list *bases;
851 struct commit *head, *other;
852 unsigned char sha1[20];
853 const char **prune = NULL;
854 int i, prune_num = 1; /* counting terminating NULL */
856 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
857 die("--merge without HEAD?");
858 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
859 die("--merge without MERGE_HEAD?");
860 add_pending_object(revs, &head->object, "HEAD");
861 add_pending_object(revs, &other->object, "MERGE_HEAD");
862 bases = get_merge_bases(head, other, 1);
863 add_pending_commit_list(revs, bases, UNINTERESTING);
864 free_commit_list(bases);
865 head->object.flags |= SYMMETRIC_LEFT;
869 for (i = 0; i < active_nr; i++) {
870 struct cache_entry *ce = active_cache[i];
873 if (ce_path_match(ce, revs->prune_data)) {
875 prune = xrealloc(prune, sizeof(*prune) * prune_num);
876 prune[prune_num-2] = ce->name;
877 prune[prune_num-1] = NULL;
879 while ((i+1 < active_nr) &&
880 ce_same_name(ce, active_cache[i+1]))
883 revs->prune_data = prune;
887 int handle_revision_arg(const char *arg, struct rev_info *revs,
889 int cant_be_filename)
893 struct object *object;
894 unsigned char sha1[20];
897 dotdot = strstr(arg, "..");
899 unsigned char from_sha1[20];
900 const char *next = dotdot + 2;
901 const char *this = arg;
902 int symmetric = *next == '.';
903 unsigned int flags_exclude = flags ^ UNINTERESTING;
912 if (!get_sha1(this, from_sha1) &&
913 !get_sha1(next, sha1)) {
914 struct commit *a, *b;
915 struct commit_list *exclude;
917 a = lookup_commit_reference(from_sha1);
918 b = lookup_commit_reference(sha1);
921 "Invalid symmetric difference expression %s...%s" :
922 "Invalid revision range %s..%s",
926 if (!cant_be_filename) {
928 verify_non_filename(revs->prefix, arg);
932 exclude = get_merge_bases(a, b, 1);
933 add_pending_commit_list(revs, exclude,
935 free_commit_list(exclude);
936 a->object.flags |= flags | SYMMETRIC_LEFT;
938 a->object.flags |= flags_exclude;
939 b->object.flags |= flags;
940 add_pending_object(revs, &a->object, this);
941 add_pending_object(revs, &b->object, next);
946 dotdot = strstr(arg, "^@");
947 if (dotdot && !dotdot[2]) {
949 if (add_parents_only(revs, arg, flags))
953 dotdot = strstr(arg, "^!");
954 if (dotdot && !dotdot[2]) {
956 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
962 local_flags = UNINTERESTING;
965 if (get_sha1_with_mode(arg, sha1, &mode))
967 if (!cant_be_filename)
968 verify_non_filename(revs->prefix, arg);
969 object = get_reference(revs, arg, sha1, flags ^ local_flags);
970 add_pending_object_with_mode(revs, object, arg, mode);
974 static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb, const char ***prune_data)
976 const char **prune = *prune_data;
980 /* count existing ones */
984 for (prune_nr = 0; prune[prune_nr]; prune_nr++)
986 prune_alloc = prune_nr; /* not really, but we do not know */
988 while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
990 if (len && sb->buf[len - 1] == '\n')
991 sb->buf[--len] = '\0';
992 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
993 prune[prune_nr++] = xstrdup(sb->buf);
996 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
997 prune[prune_nr] = NULL;
1002 static void read_revisions_from_stdin(struct rev_info *revs, const char ***prune)
1005 int seen_dashdash = 0;
1007 strbuf_init(&sb, 1000);
1008 while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1010 if (len && sb.buf[len - 1] == '\n')
1011 sb.buf[--len] = '\0';
1014 if (sb.buf[0] == '-') {
1015 if (len == 2 && sb.buf[1] == '-') {
1019 die("options not supported in --stdin mode");
1021 if (handle_revision_arg(sb.buf, revs, 0, 1))
1022 die("bad revision '%s'", sb.buf);
1025 read_pathspec_from_stdin(revs, &sb, prune);
1026 strbuf_release(&sb);
1029 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1031 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1034 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1036 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1039 static void add_message_grep(struct rev_info *revs, const char *pattern)
1041 add_grep(revs, pattern, GREP_PATTERN_BODY);
1044 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1045 int *unkc, const char **unkv)
1047 const char *arg = argv[0];
1049 /* pseudo revision arguments */
1050 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1051 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1052 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1053 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1054 !strcmp(arg, "--bisect"))
1056 unkv[(*unkc)++] = arg;
1060 if (!prefixcmp(arg, "--max-count=")) {
1061 revs->max_count = atoi(arg + 12);
1062 } else if (!prefixcmp(arg, "--skip=")) {
1063 revs->skip_count = atoi(arg + 7);
1064 } else if ((*arg == '-') && isdigit(arg[1])) {
1065 /* accept -<digit>, like traditional "head" */
1066 revs->max_count = atoi(arg + 1);
1067 } else if (!strcmp(arg, "-n")) {
1069 return error("-n requires an argument");
1070 revs->max_count = atoi(argv[1]);
1072 } else if (!prefixcmp(arg, "-n")) {
1073 revs->max_count = atoi(arg + 2);
1074 } else if (!prefixcmp(arg, "--max-age=")) {
1075 revs->max_age = atoi(arg + 10);
1076 } else if (!prefixcmp(arg, "--since=")) {
1077 revs->max_age = approxidate(arg + 8);
1078 } else if (!prefixcmp(arg, "--after=")) {
1079 revs->max_age = approxidate(arg + 8);
1080 } else if (!prefixcmp(arg, "--min-age=")) {
1081 revs->min_age = atoi(arg + 10);
1082 } else if (!prefixcmp(arg, "--before=")) {
1083 revs->min_age = approxidate(arg + 9);
1084 } else if (!prefixcmp(arg, "--until=")) {
1085 revs->min_age = approxidate(arg + 8);
1086 } else if (!strcmp(arg, "--first-parent")) {
1087 revs->first_parent_only = 1;
1088 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1089 init_reflog_walk(&revs->reflog_info);
1090 } else if (!strcmp(arg, "--default")) {
1092 return error("bad --default argument");
1093 revs->def = argv[1];
1095 } else if (!strcmp(arg, "--merge")) {
1096 revs->show_merge = 1;
1097 } else if (!strcmp(arg, "--topo-order")) {
1099 revs->topo_order = 1;
1100 } else if (!strcmp(arg, "--simplify-merges")) {
1101 revs->simplify_merges = 1;
1102 revs->rewrite_parents = 1;
1103 revs->simplify_history = 0;
1105 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1106 revs->simplify_merges = 1;
1107 revs->rewrite_parents = 1;
1108 revs->simplify_history = 0;
1109 revs->simplify_by_decoration = 1;
1112 load_ref_decorations(DECORATE_SHORT_REFS);
1113 } else if (!strcmp(arg, "--date-order")) {
1115 revs->topo_order = 1;
1116 } else if (!prefixcmp(arg, "--early-output")) {
1120 count = atoi(arg+15);
1123 revs->topo_order = 1;
1124 revs->early_output = count;
1126 } else if (!strcmp(arg, "--parents")) {
1127 revs->rewrite_parents = 1;
1128 revs->print_parents = 1;
1129 } else if (!strcmp(arg, "--dense")) {
1131 } else if (!strcmp(arg, "--sparse")) {
1133 } else if (!strcmp(arg, "--show-all")) {
1135 } else if (!strcmp(arg, "--remove-empty")) {
1136 revs->remove_empty_trees = 1;
1137 } else if (!strcmp(arg, "--merges")) {
1138 revs->merges_only = 1;
1139 } else if (!strcmp(arg, "--no-merges")) {
1140 revs->no_merges = 1;
1141 } else if (!strcmp(arg, "--boundary")) {
1143 } else if (!strcmp(arg, "--left-right")) {
1144 revs->left_right = 1;
1145 } else if (!strcmp(arg, "--cherry-pick")) {
1146 revs->cherry_pick = 1;
1148 } else if (!strcmp(arg, "--objects")) {
1149 revs->tag_objects = 1;
1150 revs->tree_objects = 1;
1151 revs->blob_objects = 1;
1152 } else if (!strcmp(arg, "--objects-edge")) {
1153 revs->tag_objects = 1;
1154 revs->tree_objects = 1;
1155 revs->blob_objects = 1;
1156 revs->edge_hint = 1;
1157 } else if (!strcmp(arg, "--unpacked")) {
1159 } else if (!prefixcmp(arg, "--unpacked=")) {
1160 die("--unpacked=<packfile> no longer supported.");
1161 } else if (!strcmp(arg, "-r")) {
1163 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1164 } else if (!strcmp(arg, "-t")) {
1166 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1167 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1168 } else if (!strcmp(arg, "-m")) {
1169 revs->ignore_merges = 0;
1170 } else if (!strcmp(arg, "-c")) {
1172 revs->dense_combined_merges = 0;
1173 revs->combine_merges = 1;
1174 } else if (!strcmp(arg, "--cc")) {
1176 revs->dense_combined_merges = 1;
1177 revs->combine_merges = 1;
1178 } else if (!strcmp(arg, "-v")) {
1179 revs->verbose_header = 1;
1180 } else if (!strcmp(arg, "--pretty")) {
1181 revs->verbose_header = 1;
1182 revs->pretty_given = 1;
1183 get_commit_format(arg+8, revs);
1184 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1185 revs->verbose_header = 1;
1186 revs->pretty_given = 1;
1187 get_commit_format(arg+9, revs);
1188 } else if (!strcmp(arg, "--show-notes")) {
1189 revs->show_notes = 1;
1190 revs->show_notes_given = 1;
1191 } else if (!strcmp(arg, "--no-notes")) {
1192 revs->show_notes = 0;
1193 revs->show_notes_given = 1;
1194 } else if (!strcmp(arg, "--oneline")) {
1195 revs->verbose_header = 1;
1196 get_commit_format("oneline", revs);
1197 revs->pretty_given = 1;
1198 revs->abbrev_commit = 1;
1199 } else if (!strcmp(arg, "--graph")) {
1200 revs->topo_order = 1;
1201 revs->rewrite_parents = 1;
1202 revs->graph = graph_init(revs);
1203 } else if (!strcmp(arg, "--root")) {
1204 revs->show_root_diff = 1;
1205 } else if (!strcmp(arg, "--no-commit-id")) {
1206 revs->no_commit_id = 1;
1207 } else if (!strcmp(arg, "--always")) {
1208 revs->always_show_header = 1;
1209 } else if (!strcmp(arg, "--no-abbrev")) {
1211 } else if (!strcmp(arg, "--abbrev")) {
1212 revs->abbrev = DEFAULT_ABBREV;
1213 } else if (!prefixcmp(arg, "--abbrev=")) {
1214 revs->abbrev = strtoul(arg + 9, NULL, 10);
1215 if (revs->abbrev < MINIMUM_ABBREV)
1216 revs->abbrev = MINIMUM_ABBREV;
1217 else if (revs->abbrev > 40)
1219 } else if (!strcmp(arg, "--abbrev-commit")) {
1220 revs->abbrev_commit = 1;
1221 } else if (!strcmp(arg, "--full-diff")) {
1223 revs->full_diff = 1;
1224 } else if (!strcmp(arg, "--full-history")) {
1225 revs->simplify_history = 0;
1226 } else if (!strcmp(arg, "--relative-date")) {
1227 revs->date_mode = DATE_RELATIVE;
1228 revs->date_mode_explicit = 1;
1229 } else if (!strncmp(arg, "--date=", 7)) {
1230 revs->date_mode = parse_date_format(arg + 7);
1231 revs->date_mode_explicit = 1;
1232 } else if (!strcmp(arg, "--log-size")) {
1233 revs->show_log_size = 1;
1236 * Grepping the commit log
1238 else if (!prefixcmp(arg, "--author=")) {
1239 add_header_grep(revs, GREP_HEADER_AUTHOR, arg+9);
1240 } else if (!prefixcmp(arg, "--committer=")) {
1241 add_header_grep(revs, GREP_HEADER_COMMITTER, arg+12);
1242 } else if (!prefixcmp(arg, "--grep=")) {
1243 add_message_grep(revs, arg+7);
1244 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1245 revs->grep_filter.regflags |= REG_EXTENDED;
1246 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1247 revs->grep_filter.regflags |= REG_ICASE;
1248 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1249 revs->grep_filter.fixed = 1;
1250 } else if (!strcmp(arg, "--all-match")) {
1251 revs->grep_filter.all_match = 1;
1252 } else if (!prefixcmp(arg, "--encoding=")) {
1254 if (strcmp(arg, "none"))
1255 git_log_output_encoding = xstrdup(arg);
1257 git_log_output_encoding = "";
1258 } else if (!strcmp(arg, "--reverse")) {
1260 } else if (!strcmp(arg, "--children")) {
1261 revs->children.name = "children";
1264 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1266 unkv[(*unkc)++] = arg;
1273 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1274 const struct option *options,
1275 const char * const usagestr[])
1277 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1278 &ctx->cpidx, ctx->out);
1280 error("unknown option `%s'", ctx->argv[0]);
1281 usage_with_options(usagestr, options);
1287 static int for_each_bad_bisect_ref(each_ref_fn fn, void *cb_data)
1289 return for_each_ref_in("refs/bisect/bad", fn, cb_data);
1292 static int for_each_good_bisect_ref(each_ref_fn fn, void *cb_data)
1294 return for_each_ref_in("refs/bisect/good", fn, cb_data);
1297 static void append_prune_data(const char ***prune_data, const char **av)
1299 const char **prune = *prune_data;
1308 /* count existing ones */
1309 for (prune_nr = 0; prune[prune_nr]; prune_nr++)
1311 prune_alloc = prune_nr; /* not really, but we do not know */
1314 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1315 prune[prune_nr++] = *av;
1319 ALLOC_GROW(prune, prune_nr+1, prune_alloc);
1320 prune[prune_nr] = NULL;
1322 *prune_data = prune;
1326 * Parse revision information, filling in the "rev_info" structure,
1327 * and removing the used arguments from the argument list.
1329 * Returns the number of arguments left that weren't recognized
1330 * (which are also moved to the head of the argument list)
1332 int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
1334 int i, flags, left, seen_dashdash, read_from_stdin;
1335 const char **prune_data = NULL;
1337 /* First, search for "--" */
1339 for (i = 1; i < argc; i++) {
1340 const char *arg = argv[i];
1341 if (strcmp(arg, "--"))
1346 prune_data = argv + i + 1;
1351 /* Second, deal with arguments and options */
1353 read_from_stdin = 0;
1354 for (left = i = 1; i < argc; i++) {
1355 const char *arg = argv[i];
1359 if (!strcmp(arg, "--all")) {
1360 handle_refs(revs, flags, for_each_ref);
1361 handle_refs(revs, flags, head_ref);
1364 if (!strcmp(arg, "--branches")) {
1365 handle_refs(revs, flags, for_each_branch_ref);
1368 if (!strcmp(arg, "--bisect")) {
1369 handle_refs(revs, flags, for_each_bad_bisect_ref);
1370 handle_refs(revs, flags ^ UNINTERESTING, for_each_good_bisect_ref);
1374 if (!strcmp(arg, "--tags")) {
1375 handle_refs(revs, flags, for_each_tag_ref);
1378 if (!strcmp(arg, "--remotes")) {
1379 handle_refs(revs, flags, for_each_remote_ref);
1382 if (!prefixcmp(arg, "--glob=")) {
1383 struct all_refs_cb cb;
1384 init_all_refs_cb(&cb, revs, flags);
1385 for_each_glob_ref(handle_one_ref, arg + 7, &cb);
1388 if (!prefixcmp(arg, "--branches=")) {
1389 struct all_refs_cb cb;
1390 init_all_refs_cb(&cb, revs, flags);
1391 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1394 if (!prefixcmp(arg, "--tags=")) {
1395 struct all_refs_cb cb;
1396 init_all_refs_cb(&cb, revs, flags);
1397 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1400 if (!prefixcmp(arg, "--remotes=")) {
1401 struct all_refs_cb cb;
1402 init_all_refs_cb(&cb, revs, flags);
1403 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1406 if (!strcmp(arg, "--reflog")) {
1407 handle_reflog(revs, flags);
1410 if (!strcmp(arg, "--not")) {
1411 flags ^= UNINTERESTING;
1414 if (!strcmp(arg, "--no-walk")) {
1418 if (!strcmp(arg, "--do-walk")) {
1422 if (!strcmp(arg, "--stdin")) {
1423 if (revs->disable_stdin) {
1427 if (read_from_stdin++)
1428 die("--stdin given twice?");
1429 read_revisions_from_stdin(revs, &prune_data);
1433 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
1443 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1445 if (seen_dashdash || *arg == '^')
1446 die("bad revision '%s'", arg);
1448 /* If we didn't have a "--":
1449 * (1) all filenames must exist;
1450 * (2) all rev-args must not be interpretable
1451 * as a valid filename.
1452 * but the latter we have checked in the main loop.
1454 for (j = i; j < argc; j++)
1455 verify_filename(revs->prefix, argv[j]);
1457 append_prune_data(&prune_data, argv + i);
1463 revs->prune_data = get_pathspec(revs->prefix, prune_data);
1465 if (revs->def == NULL)
1467 if (revs->show_merge)
1468 prepare_show_merge(revs);
1469 if (revs->def && !revs->pending.nr) {
1470 unsigned char sha1[20];
1471 struct object *object;
1473 if (get_sha1_with_mode(revs->def, sha1, &mode))
1474 die("bad default revision '%s'", revs->def);
1475 object = get_reference(revs, revs->def, sha1, 0);
1476 add_pending_object_with_mode(revs, object, revs->def, mode);
1479 /* Did the user ask for any diff output? Run the diff! */
1480 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1483 /* Pickaxe, diff-filter and rename following need diffs */
1484 if (revs->diffopt.pickaxe ||
1485 revs->diffopt.filter ||
1486 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1489 if (revs->topo_order)
1492 if (revs->prune_data) {
1493 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1494 /* Can't prune commits with rename following: the paths change.. */
1495 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1497 if (!revs->full_diff)
1498 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1500 if (revs->combine_merges) {
1501 revs->ignore_merges = 0;
1502 if (revs->dense_combined_merges && !revs->diffopt.output_format)
1503 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1505 revs->diffopt.abbrev = revs->abbrev;
1506 if (diff_setup_done(&revs->diffopt) < 0)
1507 die("diff_setup_done failed");
1509 compile_grep_patterns(&revs->grep_filter);
1511 if (revs->reverse && revs->reflog_info)
1512 die("cannot combine --reverse with --walk-reflogs");
1513 if (revs->rewrite_parents && revs->children.name)
1514 die("cannot combine --parents and --children");
1517 * Limitations on the graph functionality
1519 if (revs->reverse && revs->graph)
1520 die("cannot combine --reverse with --graph");
1522 if (revs->reflog_info && revs->graph)
1523 die("cannot combine --walk-reflogs with --graph");
1528 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1530 struct commit_list *l = xcalloc(1, sizeof(*l));
1533 l->next = add_decoration(&revs->children, &parent->object, l);
1536 static int remove_duplicate_parents(struct commit *commit)
1538 struct commit_list **pp, *p;
1539 int surviving_parents;
1541 /* Examine existing parents while marking ones we have seen... */
1542 pp = &commit->parents;
1543 while ((p = *pp) != NULL) {
1544 struct commit *parent = p->item;
1545 if (parent->object.flags & TMP_MARK) {
1549 parent->object.flags |= TMP_MARK;
1552 /* count them while clearing the temporary mark */
1553 surviving_parents = 0;
1554 for (p = commit->parents; p; p = p->next) {
1555 p->item->object.flags &= ~TMP_MARK;
1556 surviving_parents++;
1558 return surviving_parents;
1561 struct merge_simplify_state {
1562 struct commit *simplified;
1565 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1567 struct merge_simplify_state *st;
1569 st = lookup_decoration(&revs->merge_simplification, &commit->object);
1571 st = xcalloc(1, sizeof(*st));
1572 add_decoration(&revs->merge_simplification, &commit->object, st);
1577 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
1579 struct commit_list *p;
1580 struct merge_simplify_state *st, *pst;
1583 st = locate_simplify_state(revs, commit);
1586 * Have we handled this one?
1592 * An UNINTERESTING commit simplifies to itself, so does a
1593 * root commit. We do not rewrite parents of such commit
1596 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
1597 st->simplified = commit;
1602 * Do we know what commit all of our parents should be rewritten to?
1603 * Otherwise we are not ready to rewrite this one yet.
1605 for (cnt = 0, p = commit->parents; p; p = p->next) {
1606 pst = locate_simplify_state(revs, p->item);
1607 if (!pst->simplified) {
1608 tail = &commit_list_insert(p->item, tail)->next;
1613 tail = &commit_list_insert(commit, tail)->next;
1618 * Rewrite our list of parents.
1620 for (p = commit->parents; p; p = p->next) {
1621 pst = locate_simplify_state(revs, p->item);
1622 p->item = pst->simplified;
1624 cnt = remove_duplicate_parents(commit);
1627 * It is possible that we are a merge and one side branch
1628 * does not have any commit that touches the given paths;
1629 * in such a case, the immediate parents will be rewritten
1630 * to different commits.
1632 * o----X X: the commit we are looking at;
1633 * / / o: a commit that touches the paths;
1636 * Further reduce the parents by removing redundant parents.
1639 struct commit_list *h = reduce_heads(commit->parents);
1640 cnt = commit_list_count(h);
1641 free_commit_list(commit->parents);
1642 commit->parents = h;
1646 * A commit simplifies to itself if it is a root, if it is
1647 * UNINTERESTING, if it touches the given paths, or if it is a
1648 * merge and its parents simplifies to more than one commits
1649 * (the first two cases are already handled at the beginning of
1652 * Otherwise, it simplifies to what its sole parent simplifies to.
1655 (commit->object.flags & UNINTERESTING) ||
1656 !(commit->object.flags & TREESAME) ||
1658 st->simplified = commit;
1660 pst = locate_simplify_state(revs, commit->parents->item);
1661 st->simplified = pst->simplified;
1666 static void simplify_merges(struct rev_info *revs)
1668 struct commit_list *list;
1669 struct commit_list *yet_to_do, **tail;
1671 if (!revs->topo_order)
1672 sort_in_topological_order(&revs->commits, revs->lifo);
1676 /* feed the list reversed */
1678 for (list = revs->commits; list; list = list->next)
1679 commit_list_insert(list->item, &yet_to_do);
1685 struct commit *commit = list->item;
1686 struct commit_list *next = list->next;
1689 tail = simplify_one(revs, commit, tail);
1693 /* clean up the result, removing the simplified ones */
1694 list = revs->commits;
1695 revs->commits = NULL;
1696 tail = &revs->commits;
1698 struct commit *commit = list->item;
1699 struct commit_list *next = list->next;
1700 struct merge_simplify_state *st;
1703 st = locate_simplify_state(revs, commit);
1704 if (st->simplified == commit)
1705 tail = &commit_list_insert(commit, tail)->next;
1709 static void set_children(struct rev_info *revs)
1711 struct commit_list *l;
1712 for (l = revs->commits; l; l = l->next) {
1713 struct commit *commit = l->item;
1714 struct commit_list *p;
1716 for (p = commit->parents; p; p = p->next)
1717 add_child(revs, p->item, commit);
1721 int prepare_revision_walk(struct rev_info *revs)
1723 int nr = revs->pending.nr;
1724 struct object_array_entry *e, *list;
1726 e = list = revs->pending.objects;
1727 revs->pending.nr = 0;
1728 revs->pending.alloc = 0;
1729 revs->pending.objects = NULL;
1731 struct commit *commit = handle_commit(revs, e->item, e->name);
1733 if (!(commit->object.flags & SEEN)) {
1734 commit->object.flags |= SEEN;
1735 insert_by_date(commit, &revs->commits);
1745 if (limit_list(revs) < 0)
1747 if (revs->topo_order)
1748 sort_in_topological_order(&revs->commits, revs->lifo);
1749 if (revs->simplify_merges)
1750 simplify_merges(revs);
1751 if (revs->children.name)
1756 enum rewrite_result {
1758 rewrite_one_noparents,
1762 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
1764 struct commit_list *cache = NULL;
1767 struct commit *p = *pp;
1769 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
1770 return rewrite_one_error;
1771 if (p->parents && p->parents->next)
1772 return rewrite_one_ok;
1773 if (p->object.flags & UNINTERESTING)
1774 return rewrite_one_ok;
1775 if (!(p->object.flags & TREESAME))
1776 return rewrite_one_ok;
1778 return rewrite_one_noparents;
1779 *pp = p->parents->item;
1783 static int rewrite_parents(struct rev_info *revs, struct commit *commit)
1785 struct commit_list **pp = &commit->parents;
1787 struct commit_list *parent = *pp;
1788 switch (rewrite_one(revs, &parent->item)) {
1789 case rewrite_one_ok:
1791 case rewrite_one_noparents:
1794 case rewrite_one_error:
1799 remove_duplicate_parents(commit);
1803 static int commit_match(struct commit *commit, struct rev_info *opt)
1805 if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
1807 return grep_buffer(&opt->grep_filter,
1808 NULL, /* we say nothing, not even filename */
1809 commit->buffer, strlen(commit->buffer));
1812 static inline int want_ancestry(struct rev_info *revs)
1814 return (revs->rewrite_parents || revs->children.name);
1817 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
1819 if (commit->object.flags & SHOWN)
1820 return commit_ignore;
1821 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
1822 return commit_ignore;
1825 if (commit->object.flags & UNINTERESTING)
1826 return commit_ignore;
1827 if (revs->min_age != -1 && (commit->date > revs->min_age))
1828 return commit_ignore;
1829 if (revs->no_merges && commit->parents && commit->parents->next)
1830 return commit_ignore;
1831 if (revs->merges_only && !(commit->parents && commit->parents->next))
1832 return commit_ignore;
1833 if (!commit_match(commit, revs))
1834 return commit_ignore;
1835 if (revs->prune && revs->dense) {
1836 /* Commit without changes? */
1837 if (commit->object.flags & TREESAME) {
1838 /* drop merges unless we want parenthood */
1839 if (!want_ancestry(revs))
1840 return commit_ignore;
1841 /* non-merge - always ignore it */
1842 if (!commit->parents || !commit->parents->next)
1843 return commit_ignore;
1849 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
1851 enum commit_action action = get_commit_action(revs, commit);
1853 if (action == commit_show &&
1855 revs->prune && revs->dense && want_ancestry(revs)) {
1856 if (rewrite_parents(revs, commit) < 0)
1857 return commit_error;
1862 static struct commit *get_revision_1(struct rev_info *revs)
1868 struct commit_list *entry = revs->commits;
1869 struct commit *commit = entry->item;
1871 revs->commits = entry->next;
1874 if (revs->reflog_info)
1875 fake_reflog_parent(revs->reflog_info, commit);
1878 * If we haven't done the list limiting, we need to look at
1879 * the parents here. We also need to do the date-based limiting
1880 * that we'd otherwise have done in limit_list().
1882 if (!revs->limited) {
1883 if (revs->max_age != -1 &&
1884 (commit->date < revs->max_age))
1886 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
1887 die("Failed to traverse parents of commit %s",
1888 sha1_to_hex(commit->object.sha1));
1891 switch (simplify_commit(revs, commit)) {
1895 die("Failed to simplify parents of commit %s",
1896 sha1_to_hex(commit->object.sha1));
1900 } while (revs->commits);
1904 static void gc_boundary(struct object_array *array)
1906 unsigned nr = array->nr;
1907 unsigned alloc = array->alloc;
1908 struct object_array_entry *objects = array->objects;
1912 for (i = j = 0; i < nr; i++) {
1913 if (objects[i].item->flags & SHOWN)
1916 objects[j] = objects[i];
1919 for (i = j; i < nr; i++)
1920 objects[i].item = NULL;
1925 static void create_boundary_commit_list(struct rev_info *revs)
1929 struct object_array *array = &revs->boundary_commits;
1930 struct object_array_entry *objects = array->objects;
1933 * If revs->commits is non-NULL at this point, an error occurred in
1934 * get_revision_1(). Ignore the error and continue printing the
1935 * boundary commits anyway. (This is what the code has always
1938 if (revs->commits) {
1939 free_commit_list(revs->commits);
1940 revs->commits = NULL;
1944 * Put all of the actual boundary commits from revs->boundary_commits
1945 * into revs->commits
1947 for (i = 0; i < array->nr; i++) {
1948 c = (struct commit *)(objects[i].item);
1951 if (!(c->object.flags & CHILD_SHOWN))
1953 if (c->object.flags & (SHOWN | BOUNDARY))
1955 c->object.flags |= BOUNDARY;
1956 commit_list_insert(c, &revs->commits);
1960 * If revs->topo_order is set, sort the boundary commits
1961 * in topological order
1963 sort_in_topological_order(&revs->commits, revs->lifo);
1966 static struct commit *get_revision_internal(struct rev_info *revs)
1968 struct commit *c = NULL;
1969 struct commit_list *l;
1971 if (revs->boundary == 2) {
1973 * All of the normal commits have already been returned,
1974 * and we are now returning boundary commits.
1975 * create_boundary_commit_list() has populated
1976 * revs->commits with the remaining commits to return.
1978 c = pop_commit(&revs->commits);
1980 c->object.flags |= SHOWN;
1985 * Now pick up what they want to give us
1987 c = get_revision_1(revs);
1989 while (0 < revs->skip_count) {
1991 c = get_revision_1(revs);
1998 * Check the max_count.
2000 switch (revs->max_count) {
2011 c->object.flags |= SHOWN;
2013 if (!revs->boundary) {
2019 * get_revision_1() runs out the commits, and
2020 * we are done computing the boundaries.
2021 * switch to boundary commits output mode.
2026 * Update revs->commits to contain the list of
2029 create_boundary_commit_list(revs);
2031 return get_revision_internal(revs);
2035 * boundary commits are the commits that are parents of the
2036 * ones we got from get_revision_1() but they themselves are
2037 * not returned from get_revision_1(). Before returning
2038 * 'c', we need to mark its parents that they could be boundaries.
2041 for (l = c->parents; l; l = l->next) {
2043 p = &(l->item->object);
2044 if (p->flags & (CHILD_SHOWN | SHOWN))
2046 p->flags |= CHILD_SHOWN;
2047 gc_boundary(&revs->boundary_commits);
2048 add_object_array(p, NULL, &revs->boundary_commits);
2054 struct commit *get_revision(struct rev_info *revs)
2057 struct commit_list *reversed;
2059 if (revs->reverse) {
2061 while ((c = get_revision_internal(revs))) {
2062 commit_list_insert(c, &reversed);
2064 revs->commits = reversed;
2066 revs->reverse_output_stage = 1;
2069 if (revs->reverse_output_stage)
2070 return pop_commit(&revs->commits);
2072 c = get_revision_internal(revs);
2073 if (c && revs->graph)
2074 graph_update(revs->graph, c);