diff --git a/src/index.c b/src/index.c index 90fae76..6e6f1e1 100644 --- a/src/index.c +++ b/src/index.c @@ -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" diff --git a/src/index.h b/src/index.h index cd570aa..44c150d 100644 --- a/src/index.h +++ b/src/index.h @@ -33,6 +33,7 @@ #include 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); diff --git a/src/remote.c b/src/remote.c index 39a593d..ead4b14 100644 --- a/src/remote.c +++ b/src/remote.c @@ -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 * diff --git a/src/utils.c b/src/utils.c index e63ea9d..44acf5c 100644 --- a/src/utils.c +++ b/src/utils.c @@ -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; +} diff --git a/src/utils.h b/src/utils.h index 60575b1..81744f6 100644 --- a/src/utils.h +++ b/src/utils.h @@ -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)