diff mbox series

[4/6] revision: factor out add_parents()

Message ID 23a6914b-47e5-d3a9-ca95-01035b691bca@web.de (mailing list archive)
State New, archived
Headers show
Series revision: fix order of revs for ^! | expand

Commit Message

René Scharfe Sept. 15, 2022, 2:53 p.m. UTC
Add a function that registers all parents.  Use it in add_parents_only()
to handle the case of exclude_parent being unset early.

With that out of the way we can specialize the remaining loop can to
register only the specified parent.  And since we exit reporting success
once we got it we no longer need to check the total number of parents
beforehand.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
 revision.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

--
2.37.3
diff mbox series

Patch

diff --git a/revision.c b/revision.c
index 14cb73e508..284393a146 100644
--- a/revision.c
+++ b/revision.c
@@ -1861,6 +1861,13 @@  static void add_parent(struct rev_info *revs, struct object *parent,
 	add_pending_object(revs, parent, arg);
 }

+static void add_parents(struct rev_info *revs, struct commit_list *parents,
+			const char *arg, int flags)
+{
+	for (; parents; parents = parents->next)
+		add_parent(revs, &parents->item->object, arg, flags);
+}
+
 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
 			    int exclude_parent)
 {
@@ -1870,18 +1877,19 @@  static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,

 	if (!commit)
 		return 0;
-	if (exclude_parent &&
-	    exclude_parent > commit_list_count(commit->parents))
-		return 0;
+	if (!exclude_parent) {
+		add_parents(revs, commit->parents, arg_, flags);
+		return 1;
+	}
 	for (parents = commit->parents, parent_number = 1;
 	     parents;
 	     parents = parents->next, parent_number++) {
-		if (exclude_parent && parent_number != exclude_parent)
-			continue;
-
-		add_parent(revs, &parents->item->object, arg_, flags);
+		if (parent_number == exclude_parent) {
+			add_parent(revs, &parents->item->object, arg_, flags);
+			return 1;
+		}
 	}
-	return 1;
+	return 0;
 }

 void repo_init_revisions(struct repository *r,