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.
This commit is contained in:
Carlos Martín Nieto
2014-01-26 05:00:45 +01:00
parent c43c320c3e
commit f3f3d28637
3 changed files with 25 additions and 6 deletions

View File

@@ -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}
};

View File

@@ -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);

View File

@@ -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):