11 #include "reflog-walk.h"
12 #include "patch-ids.h"
15 #include "rev-cache.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 void add_object(struct object *obj,
44 struct object_array *p,
45 struct name_path *path,
48 add_object_array(obj, path_name(path, name), p);
51 static void mark_blob_uninteresting(struct blob *blob)
55 if (blob->object.flags & UNINTERESTING)
57 blob->object.flags |= UNINTERESTING;
60 void mark_tree_uninteresting(struct tree *tree)
62 struct tree_desc desc;
63 struct name_entry entry;
64 struct object *obj = &tree->object;
68 if (obj->flags & UNINTERESTING)
70 obj->flags |= UNINTERESTING;
71 if (!has_sha1_file(obj->sha1))
73 if (parse_tree(tree) < 0)
74 die("bad tree %s", sha1_to_hex(obj->sha1));
76 init_tree_desc(&desc, tree->buffer, tree->size);
77 while (tree_entry(&desc, &entry)) {
78 switch (object_type(entry.mode)) {
80 mark_tree_uninteresting(lookup_tree(entry.sha1));
83 mark_blob_uninteresting(lookup_blob(entry.sha1));
86 /* Subproject commit - not in this repository */
92 * We don't care about the tree any more
93 * after it has been marked uninteresting.
99 void mark_parents_uninteresting(struct commit *commit)
101 struct commit_list *parents = commit->parents;
104 struct commit *commit = parents->item;
105 if (!(commit->object.flags & UNINTERESTING)) {
106 commit->object.flags |= UNINTERESTING;
109 * Normally we haven't parsed the parent
110 * yet, so we won't have a parent of a parent
111 * here. However, it may turn out that we've
112 * reached this commit some other way (where it
113 * wasn't uninteresting), in which case we need
114 * to mark its parents recursively too..
117 mark_parents_uninteresting(commit);
121 * A missing commit is ok iff its parent is marked
124 * We just mark such a thing parsed, so that when
125 * it is popped next time around, we won't be trying
126 * to parse it and get an error.
128 if (!has_sha1_file(commit->object.sha1))
129 commit->object.parsed = 1;
130 parents = parents->next;
134 static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
136 if (revs->no_walk && (obj->flags & UNINTERESTING))
138 if (revs->reflog_info && obj->type == OBJ_COMMIT &&
139 add_reflog_for_walk(revs->reflog_info,
140 (struct commit *)obj, name))
142 add_object_array_with_mode(obj, name, &revs->pending, mode);
145 void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
147 add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
150 void add_head_to_pending(struct rev_info *revs)
152 unsigned char sha1[20];
154 if (get_sha1("HEAD", sha1))
156 obj = parse_object(sha1);
159 add_pending_object(revs, obj, "HEAD");
162 static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
164 struct object *object;
166 object = parse_object(sha1);
168 die("bad object %s", name);
169 object->flags |= flags;
173 static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
175 unsigned long flags = object->flags;
178 * Tag object? Look what it points to..
180 while (object->type == OBJ_TAG) {
181 struct tag *tag = (struct tag *) object;
182 if (revs->tag_objects && !(flags & UNINTERESTING))
183 add_pending_object(revs, object, tag->tag);
186 object = parse_object(tag->tagged->sha1);
188 if (flags & UNINTERESTING)
190 die("bad object %s", sha1_to_hex(tag->tagged->sha1));
195 * Commit object? Just return it, we'll do all the complex
198 if (object->type == OBJ_COMMIT) {
199 struct commit *commit = (struct commit *)object;
200 if (parse_commit(commit) < 0)
201 die("unable to parse commit %s", name);
202 if (flags & UNINTERESTING) {
203 commit->object.flags |= UNINTERESTING;
204 mark_parents_uninteresting(commit);
207 if (revs->show_source && !commit->util)
208 commit->util = (void *) name;
213 * Tree object? Either mark it uninteresting, or add it
214 * to the list of objects to look at later..
216 if (object->type == OBJ_TREE) {
217 struct tree *tree = (struct tree *)object;
218 if (!revs->tree_objects)
220 if (flags & UNINTERESTING) {
221 mark_tree_uninteresting(tree);
224 add_pending_object(revs, object, "");
229 * Blob object? You know the drill by now..
231 if (object->type == OBJ_BLOB) {
232 struct blob *blob = (struct blob *)object;
233 if (!revs->blob_objects)
235 if (flags & UNINTERESTING) {
236 mark_blob_uninteresting(blob);
239 add_pending_object(revs, object, "");
242 die("%s is unknown object", name);
245 static int everybody_uninteresting(struct commit_list *orig)
247 struct commit_list *list = orig;
249 struct commit *commit = list->item;
251 if (commit->object.flags & UNINTERESTING)
259 * The goal is to get REV_TREE_NEW as the result only if the
260 * diff consists of all '+' (and no other changes), REV_TREE_OLD
261 * if the whole diff is removal of old data, and otherwise
262 * REV_TREE_DIFFERENT (of course if the trees are the same we
263 * want REV_TREE_SAME).
264 * That means that once we get to REV_TREE_DIFFERENT, we do not
265 * have to look any further.
267 static int tree_difference = REV_TREE_SAME;
269 static void file_add_remove(struct diff_options *options,
270 int addremove, unsigned mode,
271 const unsigned char *sha1,
272 const char *fullpath)
274 int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
276 tree_difference |= diff;
277 if (tree_difference == REV_TREE_DIFFERENT)
278 DIFF_OPT_SET(options, HAS_CHANGES);
281 static void file_change(struct diff_options *options,
282 unsigned old_mode, unsigned new_mode,
283 const unsigned char *old_sha1,
284 const unsigned char *new_sha1,
285 const char *fullpath)
287 tree_difference = REV_TREE_DIFFERENT;
288 DIFF_OPT_SET(options, HAS_CHANGES);
291 static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
293 struct tree *t1 = parent->tree;
294 struct tree *t2 = commit->tree;
301 if (revs->simplify_by_decoration) {
303 * If we are simplifying by decoration, then the commit
304 * is worth showing if it has a tag pointing at it.
306 if (lookup_decoration(&name_decoration, &commit->object))
307 return REV_TREE_DIFFERENT;
309 * A commit that is not pointed by a tag is uninteresting
310 * if we are not limited by path. This means that you will
311 * see the usual "commits that touch the paths" plus any
312 * tagged commit by specifying both --simplify-by-decoration
315 if (!revs->prune_data)
316 return REV_TREE_SAME;
319 tree_difference = REV_TREE_SAME;
320 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
321 if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
323 return REV_TREE_DIFFERENT;
324 return tree_difference;
327 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
332 struct tree_desc empty, real;
333 struct tree *t1 = commit->tree;
338 tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
341 init_tree_desc(&real, tree, size);
342 init_tree_desc(&empty, "", 0);
344 tree_difference = REV_TREE_SAME;
345 DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
346 retval = diff_tree(&empty, &real, "", &revs->pruning);
349 return retval >= 0 && (tree_difference == REV_TREE_SAME);
352 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
354 struct commit_list **pp, *parent;
355 int tree_changed = 0, tree_same = 0;
358 * If we don't do pruning, everything is interesting
366 if (!commit->parents) {
367 if (rev_same_tree_as_empty(revs, commit))
368 commit->object.flags |= TREESAME;
373 * Normal non-merge commit? If we don't want to make the
374 * history dense, we consider it always to be a change..
376 if (!revs->dense && !commit->parents->next)
379 pp = &commit->parents;
380 while ((parent = *pp) != NULL) {
381 struct commit *p = parent->item;
383 if (parse_commit(p) < 0)
384 die("cannot simplify commit %s (because of %s)",
385 sha1_to_hex(commit->object.sha1),
386 sha1_to_hex(p->object.sha1));
387 switch (rev_compare_tree(revs, p, commit)) {
390 if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
391 /* Even if a merge with an uninteresting
392 * side branch brought the entire change
393 * we are interested in, we do not want
394 * to lose the other branches of this
395 * merge, so we just keep going.
401 commit->parents = parent;
402 commit->object.flags |= TREESAME;
406 if (revs->remove_empty_trees &&
407 rev_same_tree_as_empty(revs, p)) {
408 /* We are adding all the specified
409 * paths from this parent, so the
410 * history beyond this parent is not
411 * interesting. Remove its parents
412 * (they are grandparents for us).
413 * IOW, we pretend this parent is a
416 if (parse_commit(p) < 0)
417 die("cannot simplify commit %s (invalid %s)",
418 sha1_to_hex(commit->object.sha1),
419 sha1_to_hex(p->object.sha1));
424 case REV_TREE_DIFFERENT:
429 die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
431 if (tree_changed && !tree_same)
433 commit->object.flags |= TREESAME;
436 void insert_by_date_cached(struct commit *p, struct commit_list **head,
437 struct commit_list *cached_base, struct commit_list **cache)
439 struct commit_list *new_entry;
441 if (cached_base && p->date < cached_base->item->date)
442 new_entry = insert_by_date(p, &cached_base->next);
444 new_entry = insert_by_date(p, head);
446 if (cache && (!*cache || p->date < (*cache)->item->date))
450 static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
451 struct commit_list **list, struct commit_list **cache_ptr)
453 struct commit_list *parent = commit->parents;
455 struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
457 if (commit->object.flags & ADDED)
459 commit->object.flags |= ADDED;
462 * If the commit is uninteresting, don't try to
463 * prune parents - we want the maximal uninteresting
466 * Normally we haven't parsed the parent
467 * yet, so we won't have a parent of a parent
468 * here. However, it may turn out that we've
469 * reached this commit some other way (where it
470 * wasn't uninteresting), in which case we need
471 * to mark its parents recursively too..
473 if (commit->object.flags & UNINTERESTING) {
475 struct commit *p = parent->item;
476 parent = parent->next;
478 p->object.flags |= UNINTERESTING;
479 if (parse_commit(p) < 0)
482 mark_parents_uninteresting(p);
483 if (p->object.flags & SEEN)
485 p->object.flags |= SEEN;
486 insert_by_date_cached(p, list, cached_base, cache_ptr);
492 * Ok, the commit wasn't uninteresting. Try to
493 * simplify the commit history and find the parent
494 * that has no differences in the path set if one exists.
496 try_to_simplify_commit(revs, commit);
501 left_flag = (commit->object.flags & SYMMETRIC_LEFT);
503 for (parent = commit->parents; parent; parent = parent->next) {
504 struct commit *p = parent->item;
506 if (parse_commit(p) < 0)
508 if (revs->show_source && !p->util)
509 p->util = commit->util;
510 p->object.flags |= left_flag;
511 if (!(p->object.flags & SEEN)) {
512 p->object.flags |= SEEN;
513 insert_by_date_cached(p, list, cached_base, cache_ptr);
515 if (revs->first_parent_only)
521 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
523 struct commit_list *p;
524 int left_count = 0, right_count = 0;
526 struct patch_ids ids;
528 /* First count the commits on the left and on the right */
529 for (p = list; p; p = p->next) {
530 struct commit *commit = p->item;
531 unsigned flags = commit->object.flags;
532 if (flags & BOUNDARY)
534 else if (flags & SYMMETRIC_LEFT)
540 left_first = left_count < right_count;
541 init_patch_ids(&ids);
542 if (revs->diffopt.nr_paths) {
543 ids.diffopts.nr_paths = revs->diffopt.nr_paths;
544 ids.diffopts.paths = revs->diffopt.paths;
545 ids.diffopts.pathlens = revs->diffopt.pathlens;
548 /* Compute patch-ids for one side */
549 for (p = list; p; p = p->next) {
550 struct commit *commit = p->item;
551 unsigned flags = commit->object.flags;
553 if (flags & BOUNDARY)
556 * If we have fewer left, left_first is set and we omit
557 * commits on the right branch in this loop. If we have
558 * fewer right, we skip the left ones.
560 if (left_first != !!(flags & SYMMETRIC_LEFT))
562 commit->util = add_commit_patch_id(commit, &ids);
565 /* Check the other side */
566 for (p = list; p; p = p->next) {
567 struct commit *commit = p->item;
569 unsigned flags = commit->object.flags;
571 if (flags & BOUNDARY)
574 * If we have fewer left, left_first is set and we omit
575 * commits on the left branch in this loop.
577 if (left_first == !!(flags & SYMMETRIC_LEFT))
581 * Have we seen the same patch id?
583 id = has_commit_patch_id(commit, &ids);
587 commit->object.flags |= SHOWN;
590 /* Now check the original side for seen ones */
591 for (p = list; p; p = p->next) {
592 struct commit *commit = p->item;
593 struct patch_id *ent;
599 commit->object.flags |= SHOWN;
603 free_patch_ids(&ids);
606 /* How many extra uninteresting commits we want to see.. */
609 static int still_interesting(struct commit_list *src, unsigned long date, int slop)
612 * No source list at all? We're definitely done..
618 * Does the destination list contain entries with a date
619 * before the source list? Definitely _not_ done.
621 if (date < src->item->date)
625 * Does the source list still have interesting commits in
626 * it? Definitely not done..
628 if (!everybody_uninteresting(src))
631 /* Ok, we're closing in.. */
635 static int limit_list(struct rev_info *revs)
638 unsigned long date = ~0ul;
639 struct commit_list *list = revs->commits;
640 struct commit_list *newlist = NULL;
641 struct commit_list **p = &newlist;
642 unsigned char *cache_sha1;
646 struct commit_list *entry = list;
647 struct commit *commit = list->item;
648 struct object *obj = &commit->object;
649 show_early_output_fn_t show;
654 if (revs->max_age != -1 && (commit->date < revs->max_age))
655 obj->flags |= UNINTERESTING;
657 /* rev-cache to the rescue!!! */
659 if (!revs->dont_cache_me && !(obj->flags & ADDED)) {
660 cache_sha1 = get_cache_slice(commit);
662 if (traverse_cache_slice(revs, cache_sha1, commit, &date, &slop, &p, &list) < 0)
670 if (add_parents_to_list(revs, commit, &list, NULL) < 0)
672 if (obj->flags & UNINTERESTING) {
673 mark_parents_uninteresting(commit); /* ME: why? */
675 p = &commit_list_insert(commit, p)->next;
676 slop = still_interesting(list, date, slop);
679 /* If showing all, add the whole pending list to the end */
684 if (revs->min_age != -1 && (commit->date > revs->min_age))
687 p = &commit_list_insert(commit, p)->next;
690 show = show_early_output;
695 show_early_output = NULL;
697 if (revs->cherry_pick)
698 cherry_pick_list(newlist, revs);
700 revs->commits = newlist;
706 int warned_bad_reflog;
707 struct rev_info *all_revs;
708 const char *name_for_errormsg;
711 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
713 struct all_refs_cb *cb = cb_data;
714 struct object *object = get_reference(cb->all_revs, path, sha1,
716 add_pending_object(cb->all_revs, object, path);
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;
725 cb.all_flags = flags;
726 for_each(handle_one_ref, &cb);
729 static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
731 struct all_refs_cb *cb = cb_data;
732 if (!is_null_sha1(sha1)) {
733 struct object *o = parse_object(sha1);
735 o->flags |= cb->all_flags;
736 add_pending_object(cb->all_revs, o, "");
738 else if (!cb->warned_bad_reflog) {
739 warning("reflog of '%s' references pruned commits",
740 cb->name_for_errormsg);
741 cb->warned_bad_reflog = 1;
746 static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
747 const char *email, unsigned long timestamp, int tz,
748 const char *message, void *cb_data)
750 handle_one_reflog_commit(osha1, cb_data);
751 handle_one_reflog_commit(nsha1, cb_data);
755 static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
757 struct all_refs_cb *cb = cb_data;
758 cb->warned_bad_reflog = 0;
759 cb->name_for_errormsg = path;
760 for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
764 static void handle_reflog(struct rev_info *revs, unsigned flags)
766 struct all_refs_cb cb;
768 cb.all_flags = flags;
769 for_each_reflog(handle_one_reflog, &cb);
772 static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
774 unsigned char sha1[20];
776 struct commit *commit;
777 struct commit_list *parents;
780 flags ^= UNINTERESTING;
783 if (get_sha1(arg, sha1))
786 it = get_reference(revs, arg, sha1, 0);
787 if (it->type != OBJ_TAG)
789 if (!((struct tag*)it)->tagged)
791 hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
793 if (it->type != OBJ_COMMIT)
795 commit = (struct commit *)it;
796 for (parents = commit->parents; parents; parents = parents->next) {
797 it = &parents->item->object;
799 add_pending_object(revs, it, arg);
804 void init_revisions(struct rev_info *revs, const char *prefix)
806 memset(revs, 0, sizeof(*revs));
808 revs->abbrev = DEFAULT_ABBREV;
809 revs->ignore_merges = 1;
810 revs->simplify_history = 1;
811 DIFF_OPT_SET(&revs->pruning, RECURSIVE);
812 DIFF_OPT_SET(&revs->pruning, QUICK);
813 revs->pruning.add_remove = file_add_remove;
814 revs->pruning.change = file_change;
817 revs->prefix = prefix;
820 revs->skip_count = -1;
821 revs->max_count = -1;
823 revs->commit_format = CMIT_FMT_DEFAULT;
825 revs->grep_filter.status_only = 1;
826 revs->grep_filter.pattern_tail = &(revs->grep_filter.pattern_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);
835 init_rev_cache_info(&revs->rev_cache_info);
838 static void add_pending_commit_list(struct rev_info *revs,
839 struct commit_list *commit_list,
842 while (commit_list) {
843 struct object *object = &commit_list->item->object;
844 object->flags |= flags;
845 add_pending_object(revs, object, sha1_to_hex(object->sha1));
846 commit_list = commit_list->next;
850 static void prepare_show_merge(struct rev_info *revs)
852 struct commit_list *bases;
853 struct commit *head, *other;
854 unsigned char sha1[20];
855 const char **prune = NULL;
856 int i, prune_num = 1; /* counting terminating NULL */
858 if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
859 die("--merge without HEAD?");
860 if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
861 die("--merge without MERGE_HEAD?");
862 add_pending_object(revs, &head->object, "HEAD");
863 add_pending_object(revs, &other->object, "MERGE_HEAD");
864 bases = get_merge_bases(head, other, 1);
865 add_pending_commit_list(revs, bases, UNINTERESTING);
866 free_commit_list(bases);
867 head->object.flags |= SYMMETRIC_LEFT;
871 for (i = 0; i < active_nr; i++) {
872 struct cache_entry *ce = active_cache[i];
875 if (ce_path_match(ce, revs->prune_data)) {
877 prune = xrealloc(prune, sizeof(*prune) * prune_num);
878 prune[prune_num-2] = ce->name;
879 prune[prune_num-1] = NULL;
881 while ((i+1 < active_nr) &&
882 ce_same_name(ce, active_cache[i+1]))
885 revs->prune_data = prune;
889 int handle_revision_arg(const char *arg, struct rev_info *revs,
891 int cant_be_filename)
895 struct object *object;
896 unsigned char sha1[20];
899 dotdot = strstr(arg, "..");
901 unsigned char from_sha1[20];
902 const char *next = dotdot + 2;
903 const char *this = arg;
904 int symmetric = *next == '.';
905 unsigned int flags_exclude = flags ^ UNINTERESTING;
914 if (!get_sha1(this, from_sha1) &&
915 !get_sha1(next, sha1)) {
916 struct commit *a, *b;
917 struct commit_list *exclude;
919 a = lookup_commit_reference(from_sha1);
920 b = lookup_commit_reference(sha1);
923 "Invalid symmetric difference expression %s...%s" :
924 "Invalid revision range %s..%s",
928 if (!cant_be_filename) {
930 verify_non_filename(revs->prefix, arg);
934 exclude = get_merge_bases(a, b, 1);
935 add_pending_commit_list(revs, exclude,
937 free_commit_list(exclude);
938 a->object.flags |= flags | SYMMETRIC_LEFT;
940 a->object.flags |= flags_exclude;
941 b->object.flags |= flags;
942 add_pending_object(revs, &a->object, this);
943 add_pending_object(revs, &b->object, next);
948 dotdot = strstr(arg, "^@");
949 if (dotdot && !dotdot[2]) {
951 if (add_parents_only(revs, arg, flags))
955 dotdot = strstr(arg, "^!");
956 if (dotdot && !dotdot[2]) {
958 if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
964 local_flags = UNINTERESTING;
967 if (get_sha1_with_mode(arg, sha1, &mode))
969 if (!cant_be_filename)
970 verify_non_filename(revs->prefix, arg);
971 object = get_reference(revs, arg, sha1, flags ^ local_flags);
972 add_pending_object_with_mode(revs, object, arg, mode);
976 void read_revisions_from_stdin(struct rev_info *revs)
980 while (fgets(line, sizeof(line), stdin) != NULL) {
981 int len = strlen(line);
982 if (len && line[len - 1] == '\n')
987 die("options not supported in --stdin mode");
988 if (handle_revision_arg(line, revs, 0, 1))
989 die("bad revision '%s'", line);
993 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
995 append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
998 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1000 append_header_grep_pattern(&revs->grep_filter, field, pattern);
1003 static void add_message_grep(struct rev_info *revs, const char *pattern)
1005 add_grep(revs, pattern, GREP_PATTERN_BODY);
1008 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1009 int *unkc, const char **unkv)
1011 const char *arg = argv[0];
1013 /* pseudo revision arguments */
1014 if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1015 !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1016 !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1017 !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk"))
1019 unkv[(*unkc)++] = arg;
1023 if (!prefixcmp(arg, "--max-count=")) {
1024 revs->max_count = atoi(arg + 12);
1025 } else if (!prefixcmp(arg, "--skip=")) {
1026 revs->skip_count = atoi(arg + 7);
1027 } else if ((*arg == '-') && isdigit(arg[1])) {
1028 /* accept -<digit>, like traditional "head" */
1029 revs->max_count = atoi(arg + 1);
1030 } else if (!strcmp(arg, "-n")) {
1032 return error("-n requires an argument");
1033 revs->max_count = atoi(argv[1]);
1035 } else if (!prefixcmp(arg, "-n")) {
1036 revs->max_count = atoi(arg + 2);
1037 } else if (!prefixcmp(arg, "--max-age=")) {
1038 revs->max_age = atoi(arg + 10);
1039 } else if (!prefixcmp(arg, "--since=")) {
1040 revs->max_age = approxidate(arg + 8);
1041 } else if (!prefixcmp(arg, "--after=")) {
1042 revs->max_age = approxidate(arg + 8);
1043 } else if (!prefixcmp(arg, "--min-age=")) {
1044 revs->min_age = atoi(arg + 10);
1045 } else if (!prefixcmp(arg, "--before=")) {
1046 revs->min_age = approxidate(arg + 9);
1047 } else if (!prefixcmp(arg, "--until=")) {
1048 revs->min_age = approxidate(arg + 8);
1049 } else if (!strcmp(arg, "--first-parent")) {
1050 revs->first_parent_only = 1;
1051 } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1052 init_reflog_walk(&revs->reflog_info);
1053 } else if (!strcmp(arg, "--default")) {
1055 return error("bad --default argument");
1056 revs->def = argv[1];
1058 } else if (!strcmp(arg, "--merge")) {
1059 revs->show_merge = 1;
1060 } else if (!strcmp(arg, "--topo-order")) {
1062 revs->topo_order = 1;
1063 } else if (!strcmp(arg, "--simplify-merges")) {
1064 revs->simplify_merges = 1;
1065 revs->rewrite_parents = 1;
1066 revs->simplify_history = 0;
1068 } else if (!strcmp(arg, "--simplify-by-decoration")) {
1069 revs->simplify_merges = 1;
1070 revs->rewrite_parents = 1;
1071 revs->simplify_history = 0;
1072 revs->simplify_by_decoration = 1;
1075 load_ref_decorations(DECORATE_SHORT_REFS);
1076 } else if (!strcmp(arg, "--date-order")) {
1078 revs->topo_order = 1;
1079 } else if (!prefixcmp(arg, "--early-output")) {
1083 count = atoi(arg+15);
1086 revs->topo_order = 1;
1087 revs->early_output = count;
1089 } else if (!strcmp(arg, "--parents")) {
1090 revs->rewrite_parents = 1;
1091 revs->print_parents = 1;
1092 } else if (!strcmp(arg, "--dense")) {
1094 } else if (!strcmp(arg, "--sparse")) {
1096 } else if (!strcmp(arg, "--show-all")) {
1098 } else if (!strcmp(arg, "--remove-empty")) {
1099 revs->remove_empty_trees = 1;
1100 } else if (!strcmp(arg, "--merges")) {
1101 revs->merges_only = 1;
1102 } else if (!strcmp(arg, "--no-merges")) {
1103 revs->no_merges = 1;
1104 } else if (!strcmp(arg, "--boundary")) {
1106 } else if (!strcmp(arg, "--left-right")) {
1107 revs->left_right = 1;
1108 } else if (!strcmp(arg, "--cherry-pick")) {
1109 revs->cherry_pick = 1;
1111 } else if (!strcmp(arg, "--objects")) {
1112 revs->tag_objects = 1;
1113 revs->tree_objects = 1;
1114 revs->blob_objects = 1;
1115 } else if (!strcmp(arg, "--objects-edge")) {
1116 revs->tag_objects = 1;
1117 revs->tree_objects = 1;
1118 revs->blob_objects = 1;
1119 revs->edge_hint = 1;
1120 } else if (!strcmp(arg, "--unpacked")) {
1122 } else if (!prefixcmp(arg, "--unpacked=")) {
1123 die("--unpacked=<packfile> no longer supported.");
1124 } else if (!strcmp(arg, "-r")) {
1126 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1127 } else if (!strcmp(arg, "-t")) {
1129 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1130 DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1131 } else if (!strcmp(arg, "-m")) {
1132 revs->ignore_merges = 0;
1133 } else if (!strcmp(arg, "-c")) {
1135 revs->dense_combined_merges = 0;
1136 revs->combine_merges = 1;
1137 } else if (!strcmp(arg, "--cc")) {
1139 revs->dense_combined_merges = 1;
1140 revs->combine_merges = 1;
1141 } else if (!strcmp(arg, "-v")) {
1142 revs->verbose_header = 1;
1143 } else if (!strcmp(arg, "--pretty")) {
1144 revs->verbose_header = 1;
1145 get_commit_format(arg+8, revs);
1146 } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1147 revs->verbose_header = 1;
1148 get_commit_format(arg+9, revs);
1149 } else if (!strcmp(arg, "--oneline")) {
1150 revs->verbose_header = 1;
1151 get_commit_format("oneline", revs);
1152 revs->abbrev_commit = 1;
1153 } else if (!strcmp(arg, "--graph")) {
1154 revs->topo_order = 1;
1155 revs->rewrite_parents = 1;
1156 revs->graph = graph_init(revs);
1157 } else if (!strcmp(arg, "--root")) {
1158 revs->show_root_diff = 1;
1159 } else if (!strcmp(arg, "--no-commit-id")) {
1160 revs->no_commit_id = 1;
1161 } else if (!strcmp(arg, "--always")) {
1162 revs->always_show_header = 1;
1163 } else if (!strcmp(arg, "--no-abbrev")) {
1165 } else if (!strcmp(arg, "--abbrev")) {
1166 revs->abbrev = DEFAULT_ABBREV;
1167 } else if (!prefixcmp(arg, "--abbrev=")) {
1168 revs->abbrev = strtoul(arg + 9, NULL, 10);
1169 if (revs->abbrev < MINIMUM_ABBREV)
1170 revs->abbrev = MINIMUM_ABBREV;
1171 else if (revs->abbrev > 40)
1173 } else if (!strcmp(arg, "--abbrev-commit")) {
1174 revs->abbrev_commit = 1;
1175 } else if (!strcmp(arg, "--full-diff")) {
1177 revs->full_diff = 1;
1178 } else if (!strcmp(arg, "--full-history")) {
1179 revs->simplify_history = 0;
1180 } else if (!strcmp(arg, "--relative-date")) {
1181 revs->date_mode = DATE_RELATIVE;
1182 revs->date_mode_explicit = 1;
1183 } else if (!strncmp(arg, "--date=", 7)) {
1184 revs->date_mode = parse_date_format(arg + 7);
1185 revs->date_mode_explicit = 1;
1186 } else if (!strcmp(arg, "--log-size")) {
1187 revs->show_log_size = 1;
1190 * Grepping the commit log
1192 else if (!prefixcmp(arg, "--author=")) {
1193 add_header_grep(revs, GREP_HEADER_AUTHOR, arg+9);
1194 } else if (!prefixcmp(arg, "--committer=")) {
1195 add_header_grep(revs, GREP_HEADER_COMMITTER, arg+12);
1196 } else if (!prefixcmp(arg, "--grep=")) {
1197 add_message_grep(revs, arg+7);
1198 } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1199 revs->grep_filter.regflags |= REG_EXTENDED;
1200 } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1201 revs->grep_filter.regflags |= REG_ICASE;
1202 } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1203 revs->grep_filter.fixed = 1;
1204 } else if (!strcmp(arg, "--all-match")) {
1205 revs->grep_filter.all_match = 1;
1206 } else if (!prefixcmp(arg, "--encoding=")) {
1208 if (strcmp(arg, "none"))
1209 git_log_output_encoding = xstrdup(arg);
1211 git_log_output_encoding = "";
1212 } else if (!strcmp(arg, "--reverse")) {
1214 } else if (!strcmp(arg, "--children")) {
1215 revs->children.name = "children";
1218 int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1220 unkv[(*unkc)++] = arg;
1227 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1228 const struct option *options,
1229 const char * const usagestr[])
1231 int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1232 &ctx->cpidx, ctx->out);
1234 error("unknown option `%s'", ctx->argv[0]);
1235 usage_with_options(usagestr, options);
1242 * Parse revision information, filling in the "rev_info" structure,
1243 * and removing the used arguments from the argument list.
1245 * Returns the number of arguments left that weren't recognized
1246 * (which are also moved to the head of the argument list)
1248 int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
1250 int i, flags, left, seen_dashdash;
1252 /* First, search for "--" */
1254 for (i = 1; i < argc; i++) {
1255 const char *arg = argv[i];
1256 if (strcmp(arg, "--"))
1261 revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
1266 /* Second, deal with arguments and options */
1268 for (left = i = 1; i < argc; i++) {
1269 const char *arg = argv[i];
1273 if (!strcmp(arg, "--all")) {
1274 handle_refs(revs, flags, for_each_ref);
1275 handle_refs(revs, flags, head_ref);
1278 if (!strcmp(arg, "--branches")) {
1279 handle_refs(revs, flags, for_each_branch_ref);
1282 if (!strcmp(arg, "--tags")) {
1283 handle_refs(revs, flags, for_each_tag_ref);
1286 if (!strcmp(arg, "--remotes")) {
1287 handle_refs(revs, flags, for_each_remote_ref);
1290 if (!strcmp(arg, "--reflog")) {
1291 handle_reflog(revs, flags);
1294 if (!strcmp(arg, "--not")) {
1295 flags ^= UNINTERESTING;
1298 if (!strcmp(arg, "--no-walk")) {
1302 if (!strcmp(arg, "--do-walk")) {
1307 opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
1317 if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1319 if (seen_dashdash || *arg == '^')
1320 die("bad revision '%s'", arg);
1322 /* If we didn't have a "--":
1323 * (1) all filenames must exist;
1324 * (2) all rev-args must not be interpretable
1325 * as a valid filename.
1326 * but the latter we have checked in the main loop.
1328 for (j = i; j < argc; j++)
1329 verify_filename(revs->prefix, argv[j]);
1331 revs->prune_data = get_pathspec(revs->prefix,
1337 if (revs->def == NULL)
1339 if (revs->show_merge)
1340 prepare_show_merge(revs);
1341 if (revs->def && !revs->pending.nr) {
1342 unsigned char sha1[20];
1343 struct object *object;
1345 if (get_sha1_with_mode(revs->def, sha1, &mode))
1346 die("bad default revision '%s'", revs->def);
1347 object = get_reference(revs, revs->def, sha1, 0);
1348 add_pending_object_with_mode(revs, object, revs->def, mode);
1351 /* Did the user ask for any diff output? Run the diff! */
1352 if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1355 /* Pickaxe, diff-filter and rename following need diffs */
1356 if (revs->diffopt.pickaxe ||
1357 revs->diffopt.filter ||
1358 DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1361 if (revs->topo_order)
1364 if (revs->prune_data) {
1365 diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1366 /* Can't prune commits with rename following: the paths change.. */
1367 if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1369 if (!revs->full_diff)
1370 diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1372 if (revs->combine_merges) {
1373 revs->ignore_merges = 0;
1374 if (revs->dense_combined_merges && !revs->diffopt.output_format)
1375 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1377 revs->diffopt.abbrev = revs->abbrev;
1378 if (diff_setup_done(&revs->diffopt) < 0)
1379 die("diff_setup_done failed");
1381 compile_grep_patterns(&revs->grep_filter);
1383 if (revs->reverse && revs->reflog_info)
1384 die("cannot combine --reverse with --walk-reflogs");
1385 if (revs->rewrite_parents && revs->children.name)
1386 die("cannot combine --parents and --children");
1389 * Limitations on the graph functionality
1391 if (revs->reverse && revs->graph)
1392 die("cannot combine --reverse with --graph");
1394 if (revs->reflog_info && revs->graph)
1395 die("cannot combine --walk-reflogs with --graph");
1397 /* limits on caching
1398 * todo: implement this functionality */
1399 if (revs->prune || revs->diff)
1400 revs->dont_cache_me = 1;
1405 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1407 struct commit_list *l = xcalloc(1, sizeof(*l));
1410 l->next = add_decoration(&revs->children, &parent->object, l);
1413 static int remove_duplicate_parents(struct commit *commit)
1415 struct commit_list **pp, *p;
1416 int surviving_parents;
1418 /* Examine existing parents while marking ones we have seen... */
1419 pp = &commit->parents;
1420 while ((p = *pp) != NULL) {
1421 struct commit *parent = p->item;
1422 if (parent->object.flags & TMP_MARK) {
1426 parent->object.flags |= TMP_MARK;
1429 /* count them while clearing the temporary mark */
1430 surviving_parents = 0;
1431 for (p = commit->parents; p; p = p->next) {
1432 p->item->object.flags &= ~TMP_MARK;
1433 surviving_parents++;
1435 return surviving_parents;
1438 struct merge_simplify_state {
1439 struct commit *simplified;
1442 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1444 struct merge_simplify_state *st;
1446 st = lookup_decoration(&revs->merge_simplification, &commit->object);
1448 st = xcalloc(1, sizeof(*st));
1449 add_decoration(&revs->merge_simplification, &commit->object, st);
1454 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
1456 struct commit_list *p;
1457 struct merge_simplify_state *st, *pst;
1460 st = locate_simplify_state(revs, commit);
1463 * Have we handled this one?
1469 * An UNINTERESTING commit simplifies to itself, so does a
1470 * root commit. We do not rewrite parents of such commit
1473 if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
1474 st->simplified = commit;
1479 * Do we know what commit all of our parents should be rewritten to?
1480 * Otherwise we are not ready to rewrite this one yet.
1482 for (cnt = 0, p = commit->parents; p; p = p->next) {
1483 pst = locate_simplify_state(revs, p->item);
1484 if (!pst->simplified) {
1485 tail = &commit_list_insert(p->item, tail)->next;
1490 tail = &commit_list_insert(commit, tail)->next;
1495 * Rewrite our list of parents.
1497 for (p = commit->parents; p; p = p->next) {
1498 pst = locate_simplify_state(revs, p->item);
1499 p->item = pst->simplified;
1501 cnt = remove_duplicate_parents(commit);
1504 * It is possible that we are a merge and one side branch
1505 * does not have any commit that touches the given paths;
1506 * in such a case, the immediate parents will be rewritten
1507 * to different commits.
1509 * o----X X: the commit we are looking at;
1510 * / / o: a commit that touches the paths;
1513 * Further reduce the parents by removing redundant parents.
1516 struct commit_list *h = reduce_heads(commit->parents);
1517 cnt = commit_list_count(h);
1518 free_commit_list(commit->parents);
1519 commit->parents = h;
1523 * A commit simplifies to itself if it is a root, if it is
1524 * UNINTERESTING, if it touches the given paths, or if it is a
1525 * merge and its parents simplifies to more than one commits
1526 * (the first two cases are already handled at the beginning of
1529 * Otherwise, it simplifies to what its sole parent simplifies to.
1532 (commit->object.flags & UNINTERESTING) ||
1533 !(commit->object.flags & TREESAME) ||
1535 st->simplified = commit;
1537 pst = locate_simplify_state(revs, commit->parents->item);
1538 st->simplified = pst->simplified;
1543 static void simplify_merges(struct rev_info *revs)
1545 struct commit_list *list;
1546 struct commit_list *yet_to_do, **tail;
1548 if (!revs->topo_order)
1549 sort_in_topological_order(&revs->commits, revs->lifo);
1553 /* feed the list reversed */
1555 for (list = revs->commits; list; list = list->next)
1556 commit_list_insert(list->item, &yet_to_do);
1562 struct commit *commit = list->item;
1563 struct commit_list *next = list->next;
1566 tail = simplify_one(revs, commit, tail);
1570 /* clean up the result, removing the simplified ones */
1571 list = revs->commits;
1572 revs->commits = NULL;
1573 tail = &revs->commits;
1575 struct commit *commit = list->item;
1576 struct commit_list *next = list->next;
1577 struct merge_simplify_state *st;
1580 st = locate_simplify_state(revs, commit);
1581 if (st->simplified == commit)
1582 tail = &commit_list_insert(commit, tail)->next;
1586 static void set_children(struct rev_info *revs)
1588 struct commit_list *l;
1589 for (l = revs->commits; l; l = l->next) {
1590 struct commit *commit = l->item;
1591 struct commit_list *p;
1593 for (p = commit->parents; p; p = p->next)
1594 add_child(revs, p->item, commit);
1598 int prepare_revision_walk(struct rev_info *revs)
1600 int nr = revs->pending.nr;
1601 struct object_array_entry *e, *list;
1603 e = list = revs->pending.objects;
1604 revs->pending.nr = 0;
1605 revs->pending.alloc = 0;
1606 revs->pending.objects = NULL;
1608 struct commit *commit = handle_commit(revs, e->item, e->name);
1610 if (!(commit->object.flags & SEEN)) {
1611 commit->object.flags |= SEEN;
1612 insert_by_date(commit, &revs->commits);
1622 if (limit_list(revs) < 0)
1624 if (revs->topo_order)
1625 sort_in_topological_order(&revs->commits, revs->lifo);
1626 if (revs->simplify_merges)
1627 simplify_merges(revs);
1628 if (revs->children.name)
1633 enum rewrite_result {
1635 rewrite_one_noparents,
1639 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
1641 struct commit_list *cache = NULL;
1644 struct commit *p = *pp;
1646 if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
1647 return rewrite_one_error;
1648 if (p->parents && p->parents->next)
1649 return rewrite_one_ok;
1650 if (p->object.flags & UNINTERESTING)
1651 return rewrite_one_ok;
1652 if (!(p->object.flags & TREESAME))
1653 return rewrite_one_ok;
1655 return rewrite_one_noparents;
1656 *pp = p->parents->item;
1660 static int rewrite_parents(struct rev_info *revs, struct commit *commit)
1662 struct commit_list **pp = &commit->parents;
1664 struct commit_list *parent = *pp;
1665 switch (rewrite_one(revs, &parent->item)) {
1666 case rewrite_one_ok:
1668 case rewrite_one_noparents:
1671 case rewrite_one_error:
1676 remove_duplicate_parents(commit);
1680 static int commit_match(struct commit *commit, struct rev_info *opt)
1682 if (!opt->grep_filter.pattern_list)
1684 if (!commit->object.parsed)
1685 parse_commit(commit);
1686 return grep_buffer(&opt->grep_filter,
1687 NULL, /* we say nothing, not even filename */
1688 commit->buffer, strlen(commit->buffer));
1691 static inline int want_ancestry(struct rev_info *revs)
1693 return (revs->rewrite_parents || revs->children.name);
1696 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
1698 if (commit->object.flags & SHOWN)
1699 return commit_ignore;
1700 if (revs->unpacked && has_sha1_pack(commit->object.sha1))
1701 return commit_ignore;
1704 if (commit->object.flags & UNINTERESTING)
1705 return commit_ignore;
1706 if (revs->min_age != -1 && (commit->date > revs->min_age))
1707 return commit_ignore;
1708 if (revs->no_merges && commit->parents && commit->parents->next)
1709 return commit_ignore;
1710 if (revs->merges_only && !(commit->parents && commit->parents->next))
1711 return commit_ignore;
1712 if (!commit_match(commit, revs))
1713 return commit_ignore;
1714 if (revs->prune && revs->dense) {
1715 /* Commit without changes? */
1716 if (commit->object.flags & TREESAME) {
1717 /* drop merges unless we want parenthood */
1718 if (!want_ancestry(revs))
1719 return commit_ignore;
1720 /* non-merge - always ignore it */
1721 if (!commit->parents || !commit->parents->next)
1722 return commit_ignore;
1728 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
1730 enum commit_action action = get_commit_action(revs, commit);
1732 if (action == commit_show &&
1734 revs->prune && revs->dense && want_ancestry(revs)) {
1735 if (rewrite_parents(revs, commit) < 0)
1736 return commit_error;
1741 static struct commit *get_revision_1(struct rev_info *revs)
1747 struct commit_list *entry = revs->commits;
1748 struct commit *commit = entry->item;
1749 struct object *obj = &commit->object;
1751 revs->commits = entry->next;
1754 if (revs->reflog_info)
1755 fake_reflog_parent(revs->reflog_info, commit);
1758 * If we haven't done the list limiting, we need to look at
1759 * the parents here. We also need to do the date-based limiting
1760 * that we'd otherwise have done in limit_list().
1762 if (!revs->limited) {
1763 if (revs->max_age != -1 &&
1764 (commit->date < revs->max_age))
1767 if (!revs->dont_cache_me) {
1768 struct commit_list *queue = 0, **queuep = &queue;
1769 unsigned char *cache_sha1;
1771 if (obj->flags & ADDED)
1772 goto skip_parenting;
1774 cache_sha1 = get_cache_slice(commit);
1776 if (!traverse_cache_slice(revs, cache_sha1, commit, 0, 0, &queuep, &revs->commits)) {
1777 struct commit_list *work = revs->commits;
1779 /* attach queue to end of ->commits */
1780 while (work && work->next)
1786 revs->commits = queue;
1788 goto skip_parenting;
1793 if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
1794 die("Failed to traverse parents of commit %s",
1795 sha1_to_hex(commit->object.sha1));
1799 switch (simplify_commit(revs, commit)) {
1803 die("Failed to simplify parents of commit %s",
1804 sha1_to_hex(commit->object.sha1));
1808 } while (revs->commits);
1812 static void gc_boundary(struct object_array *array)
1814 unsigned nr = array->nr;
1815 unsigned alloc = array->alloc;
1816 struct object_array_entry *objects = array->objects;
1820 for (i = j = 0; i < nr; i++) {
1821 if (objects[i].item->flags & SHOWN)
1824 objects[j] = objects[i];
1827 for (i = j; i < nr; i++)
1828 objects[i].item = NULL;
1833 static void create_boundary_commit_list(struct rev_info *revs)
1837 struct object_array *array = &revs->boundary_commits;
1838 struct object_array_entry *objects = array->objects;
1841 * If revs->commits is non-NULL at this point, an error occurred in
1842 * get_revision_1(). Ignore the error and continue printing the
1843 * boundary commits anyway. (This is what the code has always
1846 if (revs->commits) {
1847 free_commit_list(revs->commits);
1848 revs->commits = NULL;
1852 * Put all of the actual boundary commits from revs->boundary_commits
1853 * into revs->commits
1855 for (i = 0; i < array->nr; i++) {
1856 c = (struct commit *)(objects[i].item);
1859 if (!(c->object.flags & CHILD_SHOWN))
1861 if (c->object.flags & (SHOWN | BOUNDARY))
1863 c->object.flags |= BOUNDARY;
1864 commit_list_insert(c, &revs->commits);
1868 * If revs->topo_order is set, sort the boundary commits
1869 * in topological order
1871 sort_in_topological_order(&revs->commits, revs->lifo);
1874 static struct commit *get_revision_internal(struct rev_info *revs)
1876 struct commit *c = NULL;
1877 struct commit_list *l;
1879 if (revs->boundary == 2) {
1881 * All of the normal commits have already been returned,
1882 * and we are now returning boundary commits.
1883 * create_boundary_commit_list() has populated
1884 * revs->commits with the remaining commits to return.
1886 c = pop_commit(&revs->commits);
1888 c->object.flags |= SHOWN;
1893 * Now pick up what they want to give us
1895 c = get_revision_1(revs);
1897 while (0 < revs->skip_count) {
1899 c = get_revision_1(revs);
1906 * Check the max_count.
1908 switch (revs->max_count) {
1919 c->object.flags |= SHOWN;
1921 if (!revs->boundary) {
1927 * get_revision_1() runs out the commits, and
1928 * we are done computing the boundaries.
1929 * switch to boundary commits output mode.
1934 * Update revs->commits to contain the list of
1937 create_boundary_commit_list(revs);
1939 return get_revision_internal(revs);
1943 * boundary commits are the commits that are parents of the
1944 * ones we got from get_revision_1() but they themselves are
1945 * not returned from get_revision_1(). Before returning
1946 * 'c', we need to mark its parents that they could be boundaries.
1949 for (l = c->parents; l; l = l->next) {
1951 p = &(l->item->object);
1952 if (p->flags & (CHILD_SHOWN | SHOWN))
1954 p->flags |= CHILD_SHOWN;
1955 gc_boundary(&revs->boundary_commits);
1956 add_object_array(p, NULL, &revs->boundary_commits);
1962 struct commit *get_revision(struct rev_info *revs)
1965 struct commit_list *reversed;
1967 if (revs->reverse) {
1969 while ((c = get_revision_internal(revs))) {
1970 commit_list_insert(c, &reversed);
1972 revs->commits = reversed;
1974 revs->reverse_output_stage = 1;
1977 if (revs->reverse_output_stage)
1978 return pop_commit(&revs->commits);
1980 c = get_revision_internal(revs);
1981 if (c && revs->graph)
1982 graph_update(revs->graph, c);