From f3f3d28637b1a183f31cc920d93ec22227674770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sun, 26 Jan 2014 05:00:45 +0100 Subject: [PATCH] Index: allow writing the tree to a particular repository Take an optional repository in Index.write_tree() to serialize to a tree into a particular repository's odb. --- src/index.c | 20 +++++++++++++++----- src/index.h | 2 +- test/test_index.py | 9 +++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/index.c b/src/index.c index e0b0ed4..2149a70 100644 --- a/src/index.c +++ b/src/index.c @@ -40,6 +40,7 @@ extern PyTypeObject DiffType; extern PyTypeObject IndexIterType; extern PyTypeObject IndexEntryType; extern PyTypeObject OidType; +extern PyTypeObject RepositoryType; int Index_init(Index *self, PyObject *args, PyObject *kwds) @@ -428,17 +429,26 @@ Index_read_tree(Index *self, PyObject *value) PyDoc_STRVAR(Index_write_tree__doc__, - "write_tree() -> Oid\n" + "write_tree([repo]) -> Oid\n" "\n" - "Create a tree object from the index file, return its oid."); + "Create a tree object from the index file, return its oid.\n" + "If 'repo' is passed, write to that repository's odb."); PyObject * -Index_write_tree(Index *self) +Index_write_tree(Index *self, PyObject *args) { git_oid oid; + Repository *repo = NULL; int err; - err = git_index_write_tree(&oid, self->index); + if (!PyArg_ParseTuple(args, "|O!", &RepositoryType, &repo)) + return NULL; + + if (repo) + err = git_index_write_tree_to(&oid, self->index, repo->repo); + else + err = git_index_write_tree(&oid, self->index); + if (err < 0) return Error_set(err); @@ -455,7 +465,7 @@ PyMethodDef Index_methods[] = { METHOD(Index, read, METH_VARARGS), METHOD(Index, write, METH_NOARGS), METHOD(Index, read_tree, METH_O), - METHOD(Index, write_tree, METH_NOARGS), + METHOD(Index, write_tree, METH_VARARGS), {NULL} }; diff --git a/src/index.h b/src/index.h index f38cb4a..e59262c 100644 --- a/src/index.h +++ b/src/index.h @@ -40,7 +40,7 @@ PyObject* Index_write(Index *self); PyObject* Index_iter(Index *self); PyObject* Index_getitem(Index *self, PyObject *value); PyObject* Index_read_tree(Index *self, PyObject *value); -PyObject* Index_write_tree(Index *self); +PyObject* Index_write_tree(Index *self, PyObject *args); Py_ssize_t Index_len(Index *self); int Index_setitem(Index *self, PyObject *key, PyObject *value); diff --git a/test/test_index.py b/test/test_index.py index 6242efa..1afdf56 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -31,6 +31,7 @@ from __future__ import absolute_import from __future__ import unicode_literals import os import unittest +import tempfile import pygit2 from . import utils @@ -152,6 +153,14 @@ class IndexTest(utils.RepoTestCase): self.assertEqual(ign_entry.oid, entry.oid) self.assertEqual(pygit2.GIT_FILEMODE_BLOB_EXECUTABLE, entry.mode) + def test_write_tree_to(self): + path = tempfile.mkdtemp() + pygit2.init_repository(path) + nrepo = pygit2.Repository(path) + + id = self.repo.index.write_tree(nrepo) + self.assertNotEqual(None, nrepo[id]) + class IndexEntryTest(utils.RepoTestCase): def test_create_entry(self):