From dd6fc972dd3b47668e3352af202cb33b76544a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Sun, 2 Feb 2014 19:40:48 +0100 Subject: [PATCH] Adjust to the git_buf changes We no longer need the max-path define, so we can get rid of that and the pypy checking. --- src/branch.c | 47 +++++++++-------------------------------------- src/config.c | 20 ++++++++++++++------ src/diff.c | 20 ++++++-------------- src/pygit2.c | 22 ++++++++-------------- src/refspec.c | 42 ++++++++++-------------------------------- 5 files changed, 47 insertions(+), 104 deletions(-) diff --git a/src/branch.c b/src/branch.c index 32879f3..39ddd67 100644 --- a/src/branch.c +++ b/src/branch.c @@ -135,34 +135,19 @@ PyObject * Branch_remote_name__get__(Branch *self) { int err; + git_buf name = {NULL}; const char *branch_name; - char *c_name = NULL; PyObject *py_name; CHECK_REFERENCE(self); branch_name = git_reference_name(self->reference); - /* Get the length of the remote name */ - err = git_branch_remote_name(NULL, 0, self->repo->repo, branch_name); + err = git_branch_remote_name(&name, self->repo->repo, branch_name); if (err < GIT_OK) return Error_set(err); - /* Get the actual remote name */ - c_name = calloc(err, sizeof(char)); - if (c_name == NULL) - return PyErr_NoMemory(); - - err = git_branch_remote_name(c_name, - err * sizeof(char), - self->repo->repo, - branch_name); - if (err < GIT_OK) { - free(c_name); - return Error_set(err); - } - - py_name = to_unicode_n(c_name, err - 1, NULL, NULL); - free(c_name); + py_name = to_unicode(name.ptr, NULL, NULL); + git_buf_free(&name); return py_name; } @@ -227,34 +212,20 @@ PyObject * Branch_upstream_name__get__(Branch *self) { int err; + git_buf name = {NULL}; const char *branch_name; - char *c_name = NULL; PyObject *py_name; CHECK_REFERENCE(self); branch_name = git_reference_name(self->reference); - /* Get the length of the upstream name */ - err = git_branch_upstream_name(NULL, 0, self->repo->repo, branch_name); + + err = git_branch_upstream_name(&name, self->repo->repo, branch_name); if (err < GIT_OK) return Error_set(err); - /* Get the actual upstream name */ - c_name = calloc(err, sizeof(char)); - if (c_name == NULL) - return PyErr_NoMemory(); - - err = git_branch_upstream_name(c_name, - err * sizeof(char), - self->repo->repo, - branch_name); - if (err < GIT_OK) { - free(c_name); - return Error_set(err); - } - - py_name = to_unicode_n(c_name, err - 1, NULL, NULL); - free(c_name); + py_name = to_unicode(name.ptr, NULL, NULL); + git_buf_free(&name); return py_name; } diff --git a/src/config.c b/src/config.c index 9b2625d..393fded 100644 --- a/src/config.c +++ b/src/config.c @@ -103,10 +103,11 @@ PyDoc_STRVAR(Config_get_global_config__doc__, PyObject * Config_get_global_config(void) { - char path[GIT_PATH_MAX]; + git_buf path = {NULL}; + PyObject *py_config; int err; - err = git_config_find_global(path, GIT_PATH_MAX); + err = git_config_find_global(&path); if (err < 0) { if (err == GIT_ENOTFOUND) { PyErr_SetString(PyExc_IOError, "Global config file not found."); @@ -116,7 +117,10 @@ Config_get_global_config(void) return Error_set(err); } - return wrap_config(path); + py_config = wrap_config(path.ptr); + + git_buf_free(&path); + return py_config; } @@ -128,10 +132,11 @@ PyDoc_STRVAR(Config_get_system_config__doc__, PyObject * Config_get_system_config(void) { - char path[GIT_PATH_MAX]; + git_buf path = {NULL}; + PyObject *py_config; int err; - err = git_config_find_system(path, GIT_PATH_MAX); + err = git_config_find_system(&path); if (err < 0) { if (err == GIT_ENOTFOUND) { PyErr_SetString(PyExc_IOError, "System config file not found."); @@ -140,7 +145,10 @@ Config_get_system_config(void) return Error_set(err); } - return wrap_config(path); + py_config = wrap_config(path.ptr); + + git_buf_free(&path); + return py_config; } diff --git a/src/diff.c b/src/diff.c index adea3cd..da08874 100644 --- a/src/diff.c +++ b/src/diff.c @@ -292,8 +292,7 @@ PyObject * Diff_patch__get__(Diff *self) { git_patch* patch; - char **strings = NULL; - char *buffer = NULL; + git_buf buf = {NULL}; int err = GIT_ERROR; size_t i, len, num; PyObject *py_patch = NULL; @@ -301,32 +300,25 @@ Diff_patch__get__(Diff *self) num = git_diff_num_deltas(self->list); if (num == 0) Py_RETURN_NONE; - MALLOC(strings, num * sizeof(char*), cleanup); for (i = 0, len = 1; i < num ; ++i) { err = git_patch_from_diff(&patch, self->list, i); if (err < 0) goto cleanup; - err = git_patch_to_str(&(strings[i]), patch); + /* This appends to the current buf, so we can simply keep passing it */ + err = git_patch_to_buf(&buf, patch); if (err < 0) goto cleanup; - len += strlen(strings[i]); git_patch_free(patch); } - CALLOC(buffer, (len + 1), sizeof(char), cleanup); - for (i = 0; i < num; ++i) { - strcat(buffer, strings[i]); - free(strings[i]); - } - free(strings); - - py_patch = to_unicode(buffer, NULL, NULL); - free(buffer); + py_patch = to_unicode(buf.ptr, NULL, NULL); + git_buf_free(&buf); cleanup: + git_buf_free(&buf); return (err < 0) ? Error_set(err) : py_patch; } diff --git a/src/pygit2.c b/src/pygit2.c index bda9545..2e80cc5 100644 --- a/src/pygit2.c +++ b/src/pygit2.c @@ -28,11 +28,6 @@ #define PY_SSIZE_T_CLEAN #include -/* Pypy does not provide this header */ -#ifndef PYPY_VERSION -# include -#endif - #include #include "error.h" #include "types.h" @@ -41,11 +36,6 @@ #include "oid.h" #include "options.h" -/* FIXME: This is for pypy */ -#ifndef MAXPATHLEN -# define MAXPATHLEN 1024 -#endif - extern PyObject *GitError; extern PyTypeObject RepositoryType; @@ -187,21 +177,25 @@ PyDoc_STRVAR(discover_repository__doc__, PyObject * discover_repository(PyObject *self, PyObject *args) { + git_buf repo_path = {NULL}; const char *path; + PyObject *py_repo_path; int across_fs = 0; const char *ceiling_dirs = NULL; - char repo_path[MAXPATHLEN]; int err; if (!PyArg_ParseTuple(args, "s|Is", &path, &across_fs, &ceiling_dirs)) return NULL; - err = git_repository_discover(repo_path, sizeof(repo_path), - path, across_fs, ceiling_dirs); + memset(&repo_path, 0, sizeof(git_buf)); + err = git_repository_discover(&repo_path, path, across_fs, ceiling_dirs); if (err < 0) return Error_set_str(err, path); - return to_path(repo_path); + py_repo_path = to_path(repo_path.ptr); + git_buf_free(&repo_path); + + return py_repo_path; }; PyDoc_STRVAR(hashfile__doc__, diff --git a/src/refspec.c b/src/refspec.c index 3053fe6..008aa66 100644 --- a/src/refspec.c +++ b/src/refspec.c @@ -155,35 +155,24 @@ PyDoc_STRVAR(Refspec_transform__doc__, PyObject * Refspec_transform(Refspec *self, PyObject *py_str) { + git_buf trans = {NULL}; const char *str; - char *trans; - int err, len, alen; + int err; PyObject *py_trans, *tstr; str = py_str_borrow_c_str(&tstr, py_str, NULL); - alen = len = strlen(str); - do { - alen *= alen; - trans = malloc(alen); - if (!trans) { - Py_DECREF(tstr); - return PyErr_NoMemory(); - } - - err = git_refspec_transform(trans, alen, self->refspec, str); - } while(err == GIT_EBUFS); + err = git_refspec_transform(&trans, self->refspec, str); Py_DECREF(tstr); if (err < 0) { - free(trans); Error_set(err); return NULL; } - py_trans = to_unicode(trans, NULL, NULL); + py_trans = to_unicode(trans.ptr, NULL, NULL); - free(trans); + git_buf_free(&trans); return py_trans; } @@ -196,35 +185,24 @@ PyDoc_STRVAR(Refspec_rtransform__doc__, PyObject * Refspec_rtransform(Refspec *self, PyObject *py_str) { + git_buf trans = {NULL}; const char *str; - char *trans; - int err, len, alen; + int err; PyObject *py_trans, *tstr; str = py_str_borrow_c_str(&tstr, py_str, NULL); - alen = len = strlen(str); - do { - alen *= alen; - trans = malloc(alen); - if (!trans) { - Py_DECREF(tstr); - return PyErr_NoMemory(); - } - - err = git_refspec_rtransform(trans, alen, self->refspec, str); - } while(err == GIT_EBUFS); + err = git_refspec_rtransform(&trans, self->refspec, str); Py_DECREF(tstr); if (err < 0) { - free(trans); Error_set(err); return NULL; } - py_trans = to_unicode(trans, NULL, NULL); + py_trans = to_unicode(trans.ptr, NULL, NULL); - free(trans); + git_buf_free(&trans); return py_trans; }