index: implement Index.read_tree
This commit is contained in:
27
pygit2.c
27
pygit2.c
@@ -1985,6 +1985,31 @@ Index_setitem(Index *self, PyObject *key, PyObject *value)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
Index_read_tree(Index *self, PyObject *value)
|
||||
{
|
||||
git_oid oid;
|
||||
git_tree *tree;
|
||||
size_t len;
|
||||
int err;
|
||||
|
||||
len = py_str_to_git_oid(value, &oid);
|
||||
TODO_SUPPORT_SHORT_HEXS(len)
|
||||
if (len == 0)
|
||||
return NULL;
|
||||
|
||||
err = git_tree_lookup_prefix(&tree, self->repo->repo, &oid,
|
||||
(unsigned int)len);
|
||||
if (err < 0)
|
||||
return Error_set(err);
|
||||
|
||||
err = git_index_read_tree(self->index, tree);
|
||||
if (err < 0)
|
||||
return Error_set(err);
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
Index_write_tree(Index *self)
|
||||
{
|
||||
@@ -2012,6 +2037,8 @@ static PyMethodDef Index_methods[] = {
|
||||
{"write", (PyCFunction)Index_write, METH_NOARGS,
|
||||
"Write an existing index object from memory back to disk using an"
|
||||
" atomic file lock."},
|
||||
{"read_tree", (PyCFunction)Index_read_tree, METH_O,
|
||||
"Update the index file from the given tree object."},
|
||||
{"write_tree", (PyCFunction)Index_write_tree, METH_NOARGS,
|
||||
"Create a tree object from the index file, return its oid."},
|
||||
{NULL}
|
||||
|
@@ -92,6 +92,23 @@ class IndexTest(utils.RepoTestCase):
|
||||
index.read()
|
||||
self.assertTrue('bye.txt' in index)
|
||||
|
||||
|
||||
def test_read_tree(self):
|
||||
tree_oid = '68aba62e560c0ebc3396e8ae9335232cd93a3f60'
|
||||
# Test reading first tree
|
||||
index = self.repo.index
|
||||
self.assertEqual(len(index), 2)
|
||||
index.read_tree(tree_oid)
|
||||
self.assertEqual(len(index), 1)
|
||||
# Test read-write returns the same oid
|
||||
oid = index.write_tree()
|
||||
oid = b2a_hex(oid).decode('ascii')
|
||||
self.assertEqual(oid, tree_oid)
|
||||
# Test the index is only modified in memory
|
||||
index.read()
|
||||
self.assertEqual(len(index), 2)
|
||||
|
||||
|
||||
def test_write_tree(self):
|
||||
oid = self.repo.index.write_tree()
|
||||
sha = b2a_hex(oid).decode('ascii')
|
||||
|
Reference in New Issue
Block a user