+
+void prepare_revision_walk(struct rev_info *revs)
+{
+ struct object_list *list;
+
+ list = revs->pending_objects;
+ revs->pending_objects = NULL;
+ while (list) {
+ struct commit *commit = handle_commit(revs, list->item, list->name);
+ if (commit) {
+ if (!(commit->object.flags & SEEN)) {
+ commit->object.flags |= SEEN;
+ insert_by_date(commit, &revs->commits);
+ }
+ }
+ list = list->next;
+ }
+
+ if (revs->no_walk)
+ return;
+ if (revs->limited)
+ limit_list(revs);
+ if (revs->topo_order)
+ sort_in_topological_order_fn(&revs->commits, revs->lifo,
+ revs->topo_setter,
+ revs->topo_getter);
+}
+
+static int rewrite_one(struct rev_info *revs, struct commit **pp)
+{
+ for (;;) {
+ struct commit *p = *pp;
+ if (!revs->limited)
+ add_parents_to_list(revs, p, &revs->commits);
+ if (p->object.flags & (TREECHANGE | UNINTERESTING))
+ return 0;
+ if (!p->parents)
+ return -1;
+ *pp = p->parents->item;
+ }
+}
+
+static void rewrite_parents(struct rev_info *revs, struct commit *commit)
+{
+ struct commit_list **pp = &commit->parents;
+ while (*pp) {
+ struct commit_list *parent = *pp;
+ if (rewrite_one(revs, &parent->item) < 0) {
+ *pp = parent->next;
+ continue;
+ }
+ pp = &parent->next;
+ }
+}
+
+static void mark_boundary_to_show(struct commit *commit)
+{
+ struct commit_list *p = commit->parents;
+ while (p) {
+ commit = p->item;
+ p = p->next;
+ if (commit->object.flags & BOUNDARY)
+ commit->object.flags |= BOUNDARY_SHOW;
+ }
+}
+
+struct commit *get_revision(struct rev_info *revs)
+{
+ struct commit_list *list = revs->commits;
+
+ if (!list)
+ return NULL;
+
+ /* Check the max_count ... */
+ switch (revs->max_count) {
+ case -1:
+ break;
+ case 0:
+ return NULL;
+ default:
+ revs->max_count--;
+ }
+
+ do {
+ struct commit *commit = revs->commits->item;
+
+ revs->commits = revs->commits->next;
+
+ /*
+ * If we haven't done the list limiting, we need to look at
+ * the parents here. We also need to do the date-based limiting
+ * that we'd otherwise have done in limit_list().
+ */
+ if (!revs->limited) {
+ if ((revs->unpacked &&
+ has_sha1_pack(commit->object.sha1)) ||
+ (revs->max_age != -1 &&
+ (commit->date < revs->max_age)))
+ continue;
+ add_parents_to_list(revs, commit, &revs->commits);
+ }
+ if (commit->object.flags & SHOWN)
+ continue;
+
+ /* We want to show boundary commits only when their
+ * children are shown. When path-limiter is in effect,
+ * rewrite_parents() drops some commits from getting shown,
+ * and there is no point showing boundary parents that
+ * are not shown. After rewrite_parents() rewrites the
+ * parents of a commit that is shown, we mark the boundary
+ * parents with BOUNDARY_SHOW.
+ */
+ if (commit->object.flags & BOUNDARY_SHOW) {
+ commit->object.flags |= SHOWN;
+ return commit;
+ }
+ if (commit->object.flags & UNINTERESTING)
+ continue;
+ if (revs->min_age != -1 && (commit->date > revs->min_age))
+ continue;
+ if (revs->no_merges &&
+ commit->parents && commit->parents->next)
+ continue;
+ if (revs->prune_fn && revs->dense) {
+ if (!(commit->object.flags & TREECHANGE))
+ continue;
+ if (revs->parents)
+ rewrite_parents(revs, commit);
+ }
+ if (revs->boundary)
+ mark_boundary_to_show(commit);
+ commit->object.flags |= SHOWN;
+ return commit;
+ } while (revs->commits);
+ return NULL;
+}