diff --git a/docs/working-copy.rst b/docs/working-copy.rst index ab8c6fe..76bcada 100644 --- a/docs/working-copy.rst +++ b/docs/working-copy.rst @@ -22,6 +22,9 @@ Index write:: >>> del index['path/to/file'] # git rm >>> index.write() # don't forget to save the changes +Custom entries:: + >>> entry = pygit2.IndexEntry('README.md', blob_id, blob_filemode) + >>> repo.index.add(entry) The Index type ==================== diff --git a/src/index.c b/src/index.c index ac08898..e0b0ed4 100644 --- a/src/index.c +++ b/src/index.c @@ -82,7 +82,7 @@ Index_traverse(Index *self, visitproc visit, void *arg) PyDoc_STRVAR(Index_add__doc__, - "add(path)\n" + "add([path|entry])\n" "\n" "Add or update an index entry from a file in disk."); @@ -91,6 +91,16 @@ Index_add(Index *self, PyObject *args) { int err; const char *path; + IndexEntry *py_entry; + + if (PyArg_ParseTuple(args, "O!", &IndexEntryType, &py_entry)) { + err = git_index_add(self->index, &py_entry->entry); + if (err < 0) + return Error_set(err); + + Py_RETURN_NONE; + } + PyErr_Clear(); if (!PyArg_ParseTuple(args, "s", &path)) return NULL; diff --git a/test/test_index.py b/test/test_index.py index 47b98b0..6242efa 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -152,6 +152,15 @@ class IndexTest(utils.RepoTestCase): self.assertEqual(ign_entry.oid, entry.oid) self.assertEqual(pygit2.GIT_FILEMODE_BLOB_EXECUTABLE, entry.mode) +class IndexEntryTest(utils.RepoTestCase): + + def test_create_entry(self): + index = self.repo.index + hello_entry = index['hello.txt'] + entry = pygit2.IndexEntry('README.md', hello_entry.oid, hello_entry.mode) + index.add(entry) + tree_id = index.write_tree() + self.assertEqual('60e769e57ae1d6a2ab75d8d253139e6260e1f912', str(tree_id)) if __name__ == '__main__': unittest.main()