From f6389ee2c30ea0807a86742736e6adfc39f6f7b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Tue, 21 Jan 2014 04:06:31 +0100 Subject: [PATCH] IndexEntry: make the attributes writable When updating entries in an index, it is necessary to modify the attributes of tree entries. make it possible to do so. --- src/index.c | 47 +++++++++++++++++++++++++++++++++++++++++++--- test/test_index.py | 13 +++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/index.c b/src/index.c index 9add4a9..ac08898 100644 --- a/src/index.c +++ b/src/index.c @@ -605,6 +605,19 @@ IndexEntry_mode__get__(IndexEntry *self) return PyLong_FromLong(self->entry.mode); } +int +IndexEntry_mode__set__(IndexEntry *self, PyObject *py_mode) +{ + long c_val; + + c_val = PyLong_AsLong(py_mode); + if (c_val == -1 && PyErr_Occurred()) + return -1; + + self->entry.mode = (unsigned int) c_val; + + return 0; +} PyDoc_STRVAR(IndexEntry_path__doc__, "Path."); @@ -614,6 +627,26 @@ IndexEntry_path__get__(IndexEntry *self) return to_path(self->entry.path); } +int +IndexEntry_path__set__(IndexEntry *self, PyObject *py_path) +{ + char *c_inner, *c_path; + + c_inner = py_str_to_c_str(py_path, NULL); + if (!c_inner) + return -1; + + c_path = strdup(c_inner); + if (!c_path) { + PyErr_NoMemory(); + return -1; + } + + free(self->entry.path); + self->entry.path = c_path; + + return 0; +} PyDoc_STRVAR(IndexEntry_oid__doc__, "Object id."); @@ -623,6 +656,14 @@ IndexEntry_oid__get__(IndexEntry *self) return git_oid_to_python(&self->entry.oid); } +int +IndexEntry_oid__set__(IndexEntry *self, PyObject *py_id) +{ + if (!py_oid_to_git_oid(py_id, &self->entry.oid)) + return -1; + + return 0; +} PyDoc_STRVAR(IndexEntry_hex__doc__, "Hex id."); @@ -633,9 +674,9 @@ IndexEntry_hex__get__(IndexEntry *self) } PyGetSetDef IndexEntry_getseters[] = { - GETTER(IndexEntry, mode), - GETTER(IndexEntry, path), - GETTER(IndexEntry, oid), + GETSET(IndexEntry, mode), + GETSET(IndexEntry, path), + GETSET(IndexEntry, oid), GETTER(IndexEntry, hex), {NULL}, }; diff --git a/test/test_index.py b/test/test_index.py index 8613020..47b98b0 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -139,6 +139,19 @@ class IndexTest(utils.RepoTestCase): index.remove('hello.txt') self.assertFalse('hello.txt' in index) + def test_change_attributes(self): + index = self.repo.index + entry = index['hello.txt'] + ign_entry = index['.gitignore'] + self.assertNotEqual(ign_entry.oid, entry.oid) + self.assertNotEqual(entry.mode, pygit2.GIT_FILEMODE_BLOB_EXECUTABLE) + entry.path = 'foo.txt' + entry.oid = ign_entry.oid + entry.mode = pygit2.GIT_FILEMODE_BLOB_EXECUTABLE + self.assertEqual('foo.txt', entry.path) + self.assertEqual(ign_entry.oid, entry.oid) + self.assertEqual(pygit2.GIT_FILEMODE_BLOB_EXECUTABLE, entry.mode) + if __name__ == '__main__': unittest.main()