Added git_add_all

Moved git strarray conversion to utils
This commit is contained in:
Leonardo Rhodes
2014-02-12 21:47:41 +01:00
parent 9e6d33855c
commit 1e6062932c
5 changed files with 98 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 -1;
err = git_index_add_all(self->index, &pathspec, 0, NULL, NULL);
git_strarray_free(&pathspec);
if (err < 0) {
Error_set(err);
return -1;
}
return 0;
}
PyDoc_STRVAR(Index_clear__doc__,
"clear()\n"
"\n"

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

@@ -243,70 +243,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)