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:
		
							
								
								
									
										20
									
								
								src/index.c
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								src/index.c
									
									
									
									
									
								
							| @@ -40,6 +40,7 @@ extern PyTypeObject DiffType; | |||||||
| extern PyTypeObject IndexIterType; | extern PyTypeObject IndexIterType; | ||||||
| extern PyTypeObject IndexEntryType; | extern PyTypeObject IndexEntryType; | ||||||
| extern PyTypeObject OidType; | extern PyTypeObject OidType; | ||||||
|  | extern PyTypeObject RepositoryType; | ||||||
|  |  | ||||||
| int | int | ||||||
| Index_init(Index *self, PyObject *args, PyObject *kwds) | 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__, | PyDoc_STRVAR(Index_write_tree__doc__, | ||||||
|   "write_tree() -> Oid\n" |   "write_tree([repo]) -> Oid\n" | ||||||
|   "\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 * | PyObject * | ||||||
| Index_write_tree(Index *self) | Index_write_tree(Index *self, PyObject *args) | ||||||
| { | { | ||||||
|     git_oid oid; |     git_oid oid; | ||||||
|  |     Repository *repo = NULL; | ||||||
|     int err; |     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) |     if (err < 0) | ||||||
|         return Error_set(err); |         return Error_set(err); | ||||||
|  |  | ||||||
| @@ -455,7 +465,7 @@ PyMethodDef Index_methods[] = { | |||||||
|     METHOD(Index, read, METH_VARARGS), |     METHOD(Index, read, METH_VARARGS), | ||||||
|     METHOD(Index, write, METH_NOARGS), |     METHOD(Index, write, METH_NOARGS), | ||||||
|     METHOD(Index, read_tree, METH_O), |     METHOD(Index, read_tree, METH_O), | ||||||
|     METHOD(Index, write_tree, METH_NOARGS), |     METHOD(Index, write_tree, METH_VARARGS), | ||||||
|     {NULL} |     {NULL} | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -40,7 +40,7 @@ PyObject* Index_write(Index *self); | |||||||
| PyObject* Index_iter(Index *self); | PyObject* Index_iter(Index *self); | ||||||
| PyObject* Index_getitem(Index *self, PyObject *value); | PyObject* Index_getitem(Index *self, PyObject *value); | ||||||
| PyObject* Index_read_tree(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); | Py_ssize_t Index_len(Index *self); | ||||||
| int Index_setitem(Index *self, PyObject *key, PyObject *value); | int Index_setitem(Index *self, PyObject *key, PyObject *value); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -31,6 +31,7 @@ from __future__ import absolute_import | |||||||
| from __future__ import unicode_literals | from __future__ import unicode_literals | ||||||
| import os | import os | ||||||
| import unittest | import unittest | ||||||
|  | import tempfile | ||||||
|  |  | ||||||
| import pygit2 | import pygit2 | ||||||
| from . import utils | from . import utils | ||||||
| @@ -152,6 +153,14 @@ class IndexTest(utils.RepoTestCase): | |||||||
|         self.assertEqual(ign_entry.oid, entry.oid) |         self.assertEqual(ign_entry.oid, entry.oid) | ||||||
|         self.assertEqual(pygit2.GIT_FILEMODE_BLOB_EXECUTABLE, entry.mode) |         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): | class IndexEntryTest(utils.RepoTestCase): | ||||||
|  |  | ||||||
|     def test_create_entry(self): |     def test_create_entry(self): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Carlos Martín Nieto
					Carlos Martín Nieto