[PATCH v2 02/12] pick: refactor checking parent in a check_parent function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Christian Couder <chriscool@xxxxxxxxxxxxx>
---
 pick.c |   69 ++++++++++++++++++++++++++++++++++++---------------------------
 1 files changed, 39 insertions(+), 30 deletions(-)

diff --git a/pick.c b/pick.c
index 1e1628a..40673ca 100644
--- a/pick.c
+++ b/pick.c
@@ -59,6 +59,40 @@ static struct tree *empty_tree(void)
 	return tree;
 }
 
+static int check_parent(struct commit *commit, int mainline, int flags,
+			struct commit **parent)
+{
+	if (!commit->parents) {
+		if (flags & PICK_REVERSE)
+			return error("Cannot revert a root commit");
+	}
+	else if (commit->parents->next) {
+		/* Reverting or cherry-picking a merge commit */
+		int cnt;
+		struct commit_list *p;
+
+		if (!mainline)
+			return error("Commit %s is a merge but no mainline was given.",
+				     sha1_to_hex(commit->object.sha1));
+
+		for (cnt = 1, p = commit->parents;
+		     cnt != mainline && p;
+		     cnt++)
+			p = p->next;
+		if (cnt != mainline || !p)
+			return error("Commit %s does not have parent %d",
+				     sha1_to_hex(commit->object.sha1),
+				     mainline);
+		*parent = p->item;
+	} else if (0 < mainline)
+		return error("Mainline was specified but commit %s is not a merge.",
+			     sha1_to_hex(commit->object.sha1));
+	else
+		*parent = commit->parents->item;
+
+	return 0;
+}
+
 /*
  * Pick changes introduced by "commit" argument into current working
  * tree and index.
@@ -80,8 +114,8 @@ int pick_commit(struct commit *pick_commit, int mainline, int flags,
 		struct strbuf *msg)
 {
 	unsigned char head[20];
-	struct commit *base, *next, *parent;
-	int i, index_fd, clean;
+	struct commit *base, *next, *parent = NULL;
+	int i, index_fd, clean, err;
 	int ret = 0;
 	char *oneline;
 	const char *message;
@@ -105,34 +139,9 @@ int pick_commit(struct commit *pick_commit, int mainline, int flags,
 		return error("Unable to create locked index: %s",
 			     strerror(errno));
 
-	if (!commit->parents) {
-		if (flags & PICK_REVERSE)
-			return error("Cannot revert a root commit");
-		parent = NULL;
-	}
-	else if (commit->parents->next) {
-		/* Reverting or cherry-picking a merge commit */
-		int cnt;
-		struct commit_list *p;
-
-		if (!mainline)
-			return error("Commit %s is a merge but no mainline was given.",
-				     sha1_to_hex(commit->object.sha1));
-
-		for (cnt = 1, p = commit->parents;
-		     cnt != mainline && p;
-		     cnt++)
-			p = p->next;
-		if (cnt != mainline || !p)
-			return error("Commit %s does not have parent %d",
-				     sha1_to_hex(commit->object.sha1),
-				     mainline);
-		parent = p->item;
-	} else if (0 < mainline)
-		return error("Mainline was specified but commit %s is not a merge.",
-			     sha1_to_hex(commit->object.sha1));
-	else
-		parent = commit->parents->item;
+	err = check_parent(commit, mainline, flags, &parent);
+	if (err)
+		return err;
 
 	if (!(message = commit->buffer))
 		return error("Cannot get commit message for %s",
-- 
1.7.0.321.g2d270


--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]