Merge remote-tracking branch 'rhodes/master'

This commit is contained in:
J. David Ibáñez
2014-02-16 12:26:24 +01:00
6 changed files with 135 additions and 65 deletions

View File

@@ -81,7 +81,6 @@ Index_traverse(Index *self, visitproc visit, void *arg)
return 0;
}
PyDoc_STRVAR(Index_add__doc__,
"add([path|entry])\n"
"\n"
@@ -114,6 +113,31 @@ Index_add(Index *self, PyObject *args)
}
PyDoc_STRVAR(Index_add_all__doc__,
"add_all([file names|glob pattern])\n"
"\n"
"Add or update index entries matching files in the working directory.");
PyObject *
Index_add_all(Index *self, PyObject *pylist)
{
int err;
git_strarray pathspec;
if (get_strarraygit_from_pylist(&pathspec, pylist) < 0)
return NULL;
err = git_index_add_all(self->index, &pathspec, 0, NULL, NULL);
git_strarray_free(&pathspec);
if (err < 0) {
Error_set(err);
return NULL;
}
Py_RETURN_NONE;
}
PyDoc_STRVAR(Index_clear__doc__,
"clear()\n"
"\n"
@@ -457,6 +481,7 @@ Index_write_tree(Index *self, PyObject *args)
PyMethodDef Index_methods[] = {
METHOD(Index, add, METH_VARARGS),
METHOD(Index, add_all, METH_O),
METHOD(Index, remove, METH_VARARGS),
METHOD(Index, clear, METH_NOARGS),
METHOD(Index, diff_to_workdir, METH_VARARGS),

View File

@@ -33,6 +33,7 @@
#include <git2.h>
PyObject* Index_add(Index *self, PyObject *args);
PyObject* Index_add_all(Index *self, PyObject *pylist);
PyObject* Index_clear(Index *self);
PyObject* Index_find(Index *self, PyObject *py_path);
PyObject* Index_read(Index *self, PyObject *args);

View File

@@ -252,70 +252,6 @@ Remote_name__set__(Remote *self, PyObject* py_name)
return -1;
}
PyObject *
get_pylist_from_git_strarray(git_strarray *strarray)
{
int index;
PyObject *new_list;
new_list = PyList_New(strarray->count);
if (new_list == NULL)
return NULL;
for (index = 0; index < strarray->count; index++)
PyList_SET_ITEM(new_list, index,
to_unicode(strarray->strings[index], NULL, NULL));
return new_list;
}
int
get_strarraygit_from_pylist(git_strarray *array, PyObject *pylist)
{
Py_ssize_t index, n;
PyObject *item;
void *ptr;
char *str;
if (!PyList_Check(pylist)) {
PyErr_SetString(PyExc_TypeError, "Value must be a list");
return -1;
}
n = PyList_Size(pylist);
/* allocate new git_strarray */
ptr = calloc(n, sizeof(char *));
if (!ptr) {
PyErr_SetNone(PyExc_MemoryError);
return -1;
}
array->strings = ptr;
array->count = n;
for (index = 0; index < n; index++) {
item = PyList_GetItem(pylist, index);
str = py_str_to_c_str(item, NULL);
if (!str)
goto on_error;
array->strings[index] = str;
}
return 0;
on_error:
n = index;
for (index = 0; index < n; index++) {
free(array->strings[index]);
}
free(array->strings);
return -1;
}
PyDoc_STRVAR(Remote_fetch_refspecs__doc__, "Fetch refspecs");
PyObject *

View File

@@ -83,3 +83,73 @@ py_str_borrow_c_str(PyObject **tvalue, PyObject *value, const char *encoding)
Py_TYPE(value)->tp_name);
return NULL;
}
/**
* Converts the (struct) git_strarray to a Python list
*/
PyObject *
get_pylist_from_git_strarray(git_strarray *strarray)
{
int index;
PyObject *new_list;
new_list = PyList_New(strarray->count);
if (new_list == NULL)
return NULL;
for (index = 0; index < strarray->count; index++)
PyList_SET_ITEM(new_list, index,
to_unicode(strarray->strings[index], NULL, NULL));
return new_list;
}
/**
* Converts the Python list to struct git_strarray
* returns -1 if conversion failed
*/
int
get_strarraygit_from_pylist(git_strarray *array, PyObject *pylist)
{
Py_ssize_t index, n;
PyObject *item;
void *ptr;
char *str;
if (!PyList_Check(pylist)) {
PyErr_SetString(PyExc_TypeError, "Value must be a list");
return -1;
}
n = PyList_Size(pylist);
/* allocate new git_strarray */
ptr = calloc(n, sizeof(char *));
if (!ptr) {
PyErr_SetNone(PyExc_MemoryError);
return -1;
}
array->strings = ptr;
array->count = n;
for (index = 0; index < n; index++) {
item = PyList_GetItem(pylist, index);
str = py_str_to_c_str(item, NULL);
if (!str)
goto on_error;
array->strings[index] = str;
}
return 0;
on_error:
n = index;
for (index = 0; index < n; index++) {
free(array->strings[index]);
}
free(array->strings);
return -1;
}

View File

@@ -114,6 +114,8 @@ to_bytes(const char * value)
char * py_str_to_c_str(PyObject *value, const char *encoding);
const char *py_str_borrow_c_str(PyObject **tvaue, PyObject *value, const char *encoding);
PyObject * get_pylist_from_git_strarray(git_strarray *strarray);
int get_strarraygit_from_pylist(git_strarray *array, PyObject *pylist);
#define py_path_to_c_str(py_path) \
py_str_to_c_str(py_path, Py_FileSystemDefaultEncoding)

View File

@@ -74,6 +74,42 @@ class IndexTest(utils.RepoTestCase):
self.assertEqual(len(index), 3)
self.assertEqual(index['bye.txt'].hex, sha)
def test_add_all(self):
self.test_clear()
index = self.repo.index
sha_bye = '0907563af06c7464d62a70cdd135a6ba7d2b41d8'
sha_hello = 'a520c24d85fbfc815d385957eed41406ca5a860b'
index.add_all(['*.txt'])
self.assertTrue('bye.txt' in index)
self.assertTrue('hello.txt' in index)
self.assertEqual(index['bye.txt'].hex, sha_bye)
self.assertEqual(index['hello.txt'].hex, sha_hello)
self.test_clear()
index.add_all(['bye.t??', 'hello.*'])
self.assertTrue('bye.txt' in index)
self.assertTrue('hello.txt' in index)
self.assertEqual(index['bye.txt'].hex, sha_bye)
self.assertEqual(index['hello.txt'].hex, sha_hello)
self.test_clear()
index.add_all(['[byehlo]*.txt'])
self.assertTrue('bye.txt' in index)
self.assertTrue('hello.txt' in index)
self.assertEqual(index['bye.txt'].hex, sha_bye)
self.assertEqual(index['hello.txt'].hex, sha_hello)
def test_clear(self):
index = self.repo.index
self.assertEqual(len(index), 2)