From fe39b53757666f0a59b8419088322996261ad43b Mon Sep 17 00:00:00 2001
From: Dave Borowitz <dborowitz@google.com>
Date: Mon, 1 Nov 2010 10:59:27 -0700
Subject: [PATCH] Factor out a helper function for initializing Objects.

Change-Id: Ibf1585bbd4fd5adfb835e34d02fc6186a97d1cd9
---
 pygit2.c | 33 +++++++++++++++++++++------------
 1 file changed, 21 insertions(+), 12 deletions(-)

diff --git a/pygit2.c b/pygit2.c
index fdfa895..ef3ee6b 100644
--- a/pygit2.c
+++ b/pygit2.c
@@ -376,22 +376,31 @@ static PyTypeObject ObjectType = {
 };
 
 static int
-Commit_init(Commit *py_commit, PyObject *args, PyObject **kwds) {
+object_init_check(const char *type_name, PyObject *args, PyObject *kwds,
+                  Repository **repo) {
+    if (kwds) {
+        PyErr_Format(PyExc_TypeError, "%s takes no keyword arugments",
+                     type_name);
+        return 0;
+    }
+
+    if (!PyArg_ParseTuple(args, "O", repo))
+        return 0;
+
+    if (!PyObject_TypeCheck(*repo, &RepositoryType)) {
+        PyErr_SetString(PyExc_TypeError, "Expected Repository for repo");
+        return 0;
+    }
+    return 1;
+}
+
+static int
+Commit_init(Commit *py_commit, PyObject *args, PyObject *kwds) {
     Repository *repo = NULL;
     git_commit *commit;
 
-    if (kwds) {
-        PyErr_SetString(PyExc_TypeError, "Commit takes no keyword arugments");
+    if (!object_init_check("Commit", args, kwds, &repo))
         return -1;
-    }
-
-    if (!PyArg_ParseTuple(args, "O", &repo))
-        return -1;
-
-    if (!PyObject_TypeCheck(repo, &RepositoryType)) {
-        PyErr_SetString(PyExc_TypeError, "Expected Repository for repo");
-        return -1;
-    }
 
     commit = git_commit_new(repo->repo);
     if (!commit) {