From 2acf49c6f77251dafb919ed96c7ab06b989c400b Mon Sep 17 00:00:00 2001 From: John Szakmeister <john@szakmeister.net> Date: Sun, 27 Feb 2011 07:01:33 -0500 Subject: [PATCH] Support adding parents to a commit. --- pygit2.c | 41 ++++++++++++++++++++++++++++++++++++++++- test/test_commit.py | 6 ++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/pygit2.c b/pygit2.c index 355848f..8eda9ce 100644 --- a/pygit2.c +++ b/pygit2.c @@ -668,6 +668,39 @@ Commit_get_parents(Commit *commit) return list; } +static PyObject * +Commit_add_parent(Commit *self, PyObject *parent) +{ + int err; + git_commit *parent_commit; + + if (PyString_Check(parent)) { + git_oid oid; + + err = py_str_to_git_oid(parent, &oid); + if (err < 0) + return Error_set_py_obj(err, parent); + + err = git_commit_lookup(&parent_commit, self->repo->repo, &oid); + if (err < 0) + return Error_set_py_obj(err, parent); + } else { + if (!PyObject_TypeCheck(parent, &CommitType)) { + PyErr_Format(PyExc_TypeError, "target must be %.200s, not %.200s", + CommitType.tp_name, parent->ob_type->tp_name); + return NULL; + } + + parent_commit = ((Commit *) parent)->commit; + } + + err = git_commit_add_parent(self->commit, parent_commit); + if (err < 0) + return Error_set(err); + + Py_RETURN_NONE; +} + static PyGetSetDef Commit_getseters[] = { {"message_short", (getter)Commit_get_message_short, NULL, "short message", NULL}, @@ -684,6 +717,12 @@ static PyGetSetDef Commit_getseters[] = { {NULL} }; +static PyMethodDef Commit_methods[] = { + {"add_parent", (PyCFunction)Commit_add_parent, METH_O, + "Add a new parent commit to an existing commit."}, + {NULL} +}; + static PyTypeObject CommitType = { PyObject_HEAD_INIT(NULL) 0, /*ob_size*/ @@ -713,7 +752,7 @@ static PyTypeObject CommitType = { 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - 0, /* tp_methods */ + Commit_methods, /* tp_methods */ 0, /* tp_members */ Commit_getseters, /* tp_getset */ 0, /* tp_base */ diff --git a/test/test_commit.py b/test/test_commit.py index c924b9e..192d414 100644 --- a/test/test_commit.py +++ b/test/test_commit.py @@ -67,6 +67,10 @@ class CommitTest(utils.BareRepoTestCase): commit.committer = committer commit.author = author + self.assertEqual(0, len(commit.parents)) + + commit.add_parent(COMMIT_SHA) + self.assertEqual(None, commit.sha) self.assertEqual(pygit2.GIT_OBJ_COMMIT, commit.type) self.assertEqual(message, commit.message) @@ -74,6 +78,8 @@ class CommitTest(utils.BareRepoTestCase): self.assertEqual(12346, commit.commit_time) self.assertEqual(committer, commit.committer) self.assertEqual(author, commit.author) + self.assertEqual(1, len(commit.parents)) + self.assertEqual(COMMIT_SHA, commit.parents[0].sha) def test_modify_commit(self): message = 'New commit.\n\nMessage.\n'