Index: accept adding either a path or an IndexEntry

A path is only useful if we have the file on the worktree. Passing an
IndexEntry allows us to add an entry with arbitrary attributes.
This commit is contained in:
Carlos Martín Nieto 2014-01-21 04:47:57 +01:00
parent f6389ee2c3
commit c43c320c3e
3 changed files with 23 additions and 1 deletions

View File

@ -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
====================

View File

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

View File

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