Skip to content

Commit

Permalink
Remove "del index[xxx]" from the API
Browse files Browse the repository at this point in the history
Use "index.remove(xxx)" instead.
  • Loading branch information
jdavid committed May 2, 2013
1 parent e186c06 commit b12a596
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 63 deletions.
81 changes: 22 additions & 59 deletions src/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,43 +227,6 @@ Index_write(Index *self)
Py_RETURN_NONE;
}

/* This is an internal function, used by Index_getitem and Index_setitem */
size_t
Index_get_position(Index *self, PyObject *value)
{
char *path;
size_t idx;
int err;

/* Case 1: integer */
if (PyLong_Check(value)) {
err = (int)PyLong_AsLong(value);
if (err == -1 && PyErr_Occurred())
return -1;
if (err < 0) {
PyErr_SetObject(PyExc_ValueError, value);
return -1;
}
return err;
}

/* Case 2: byte or text string */
path = py_path_to_c_str(value);
if (!path)
return -1;

err = git_index_find(&idx, self->index, path);
if (err < 0) {
Error_set_str(err, path);
free(path);
return -1;
}

free(path);

return idx;
}

int
Index_contains(Index *self, PyObject *value)
{
Expand Down Expand Up @@ -322,19 +285,34 @@ wrap_index_entry(const git_index_entry *entry, Index *index)
PyObject *
Index_getitem(Index *self, PyObject *value)
{
size_t idx;
long idx;
char *path;
const git_index_entry *index_entry;

idx = Index_get_position(self, value);
if (idx == -1)
return NULL;
/* Case 1: integer */
if (PyLong_Check(value)) {
idx = PyLong_AsLong(value);
if (idx == -1 && PyErr_Occurred())
return NULL;
if (idx < 0) {
PyErr_SetObject(PyExc_ValueError, value);
return NULL;
}
index_entry = git_index_get_byindex(self->index, (size_t)idx);
/* Case 2: byte or text string */
} else {
path = py_path_to_c_str(value);
if (!path)
return NULL;

index_entry = git_index_get_bypath(self->index, path, 0);
free(path);
}

index_entry = git_index_get_byindex(self->index, idx);
if (!index_entry) {
PyErr_SetObject(PyExc_KeyError, value);
return NULL;
}

return wrap_index_entry(index_entry, self);
}

Expand Down Expand Up @@ -362,21 +340,6 @@ Index_remove(Index *self, PyObject *args)
Py_RETURN_NONE;
}

int
Index_setitem(Index *self, PyObject *key, PyObject *value)
{
if (value != NULL) {
PyErr_SetString(PyExc_NotImplementedError,
"set item on index not yet implemented");
return -1;
}

if (Index_remove(self, Py_BuildValue("(N)", key)) == NULL)
return -1;

return 0;
}


PyDoc_STRVAR(Index_read_tree__doc__,
"read_tree(tree)\n"
Expand Down Expand Up @@ -453,7 +416,7 @@ PySequenceMethods Index_as_sequence = {
PyMappingMethods Index_as_mapping = {
(lenfunc)Index_len, /* mp_length */
(binaryfunc)Index_getitem, /* mp_subscript */
(objobjargproc)Index_setitem, /* mp_ass_subscript */
NULL, /* mp_ass_subscript */
};

PyDoc_STRVAR(Index__doc__, "Index file.");
Expand Down
7 changes: 3 additions & 4 deletions test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,12 @@ def test_bare_index(self):

self.assertRaises(pygit2.GitError, lambda: index.add('bye.txt'))

def test_del(self):
index = self.repo.index
del index['hello.txt']

def test_remove(self):
index = self.repo.index
self.assertTrue('hello.txt' in index)
index.remove('hello.txt')
self.assertFalse('hello.txt' in index)


if __name__ == '__main__':
unittest.main()

0 comments on commit b12a596

Please sign in to comment.