Added methods to interact with the remote refspecs

This commit is contained in:
Jose Plana
2013-11-21 01:05:36 +01:00
parent aa5877e011
commit 336f042e22
2 changed files with 191 additions and 5 deletions

View File

@@ -33,6 +33,7 @@
#include "types.h" #include "types.h"
#include "remote.h" #include "remote.h"
extern PyObject *GitError; extern PyObject *GitError;
extern PyTypeObject RepositoryType; extern PyTypeObject RepositoryType;
@@ -95,8 +96,164 @@ Remote_name__set__(Remote *self, PyObject* py_name)
} }
PyObject * get_pylist_from_git_strarray(git_strarray *strarray)
{
int index;
PyObject *new_list;
new_list = PyList_New(strarray->count);
for (index = 0; index < strarray->count; (index)++ ) {
PyList_SET_ITEM(
new_list,
index,
PyString_FromString(strarray->strings[index]));
}
return new_list;
}
PyDoc_STRVAR(Remote_fetch_refspecs__doc__, "Fetch refspecs");
PyObject *
Remote_fetch_refspecs__get__(Remote *self)
{
int err;
git_strarray refspecs;
PyObject *new_list;
err = git_remote_get_fetch_refspecs(&refspecs, self->remote);
if (err != 0) {
Error_set(err);
return NULL;
}
new_list = get_pylist_from_git_strarray(&refspecs);
git_strarray_free(&refspecs);
return new_list;
}
PyDoc_STRVAR(Remote_push_refspecs__doc__, "Push refspecs");
PyObject *
Remote_push_refspecs__get__(Remote *self)
{
int err;
git_strarray refspecs;
PyObject *new_list;
err = git_remote_get_push_refspecs(&refspecs, self->remote);
if (err != 0) {
Error_set(err);
return NULL;
}
new_list = get_pylist_from_git_strarray(&refspecs);
git_strarray_free(&refspecs);
return new_list;
}
int
get_strarraygit_from_pylist(git_strarray *array, PyObject *pylist)
{
long index, n;
PyObject *item;
n = PyObject_Length(pylist);
if (n < 0)
return -1;
// allocate new git_strarray
void *ptr = calloc(n, sizeof(char *));
if (!ptr)
return -1;
array->strings = ptr;
array->count = n;
for (index = 0; index < n; index++) {
item = PyList_GetItem(pylist, index);
array->strings[index] = py_str_to_c_str(item, NULL);
}
return 0;
}
PyDoc_STRVAR(Remote_set_fetch_refspecs__doc__,
"set_fetch_refspecs([str])\n"
"\n");
PyObject *
Remote_set_fetch_refspecs(Remote *self, PyObject *args)
{
int err;
PyObject *pyrefspecs;
git_strarray fetch_refspecs;
if (! PyArg_Parse(args, "O", &pyrefspecs))
return NULL;
if (get_strarraygit_from_pylist(&fetch_refspecs , pyrefspecs) != 0) {
return NULL;
}
err = git_remote_set_fetch_refspecs(self->remote, &fetch_refspecs);
if (err != 0) {
Error_set(err);
return NULL;
}
git_strarray_free(&fetch_refspecs);
Py_RETURN_NONE;
}
PyDoc_STRVAR(Remote_set_push_refspecs__doc__,
"set_push_refspecs([str])\n"
"\n");
PyObject *
Remote_set_push_refspecs(Remote *self, PyObject *args)
{
int err;
PyObject *pyrefspecs;
git_strarray push_refspecs;
if (! PyArg_Parse(args, "O", &pyrefspecs))
return NULL;
if (get_strarraygit_from_pylist(&push_refspecs, pyrefspecs) != 0) {
return NULL;
}
err = git_remote_set_push_refspecs(self->remote, &push_refspecs);
if (err != 0) {
Error_set(err);
return NULL;
}
git_strarray_free(&push_refspecs);
Py_RETURN_NONE;
}
PyDoc_STRVAR(Remote_url__doc__, "Url of the remote"); PyDoc_STRVAR(Remote_url__doc__, "Url of the remote");
PyObject * PyObject *
Remote_url__get__(Remote *self) Remote_url__get__(Remote *self)
{ {
@@ -288,6 +445,8 @@ PyMethodDef Remote_methods[] = {
METHOD(Remote, save, METH_NOARGS), METHOD(Remote, save, METH_NOARGS),
METHOD(Remote, get_refspec, METH_O), METHOD(Remote, get_refspec, METH_O),
METHOD(Remote, push, METH_VARARGS), METHOD(Remote, push, METH_VARARGS),
METHOD(Remote, set_fetch_refspecs, METH_O),
METHOD(Remote, set_push_refspecs, METH_O),
{NULL} {NULL}
}; };
@@ -295,6 +454,8 @@ PyGetSetDef Remote_getseters[] = {
GETSET(Remote, name), GETSET(Remote, name),
GETSET(Remote, url), GETSET(Remote, url),
GETTER(Remote, refspec_count), GETTER(Remote, refspec_count),
GETTER(Remote, fetch_refspecs),
GETTER(Remote, push_refspecs),
{NULL} {NULL}
}; };

View File

@@ -83,11 +83,36 @@ class RepositoryTest(utils.RepoTestCase):
self.assertEqual(refspec[0], REMOTE_FETCHSPEC_SRC) self.assertEqual(refspec[0], REMOTE_FETCHSPEC_SRC)
self.assertEqual(refspec[1], REMOTE_FETCHSPEC_DST) self.assertEqual(refspec[1], REMOTE_FETCHSPEC_DST)
# new_fetchspec = ('refs/foo/*', 'refs/remotes/foo/*') self.assertEqual(list, type(remote.fetch_refspecs))
# remote.fetchspec = new_fetchspec self.assertEqual(1, len(remote.fetch_refspecs))
# refspec = remote.get_refspec(0) self.assertEqual('+refs/heads/*:refs/remotes/origin/*',
# self.assertEqual(new_fetchspec[0], refspec[0]) remote.fetch_refspecs[0])
# self.assertEqual(new_fetchspec[1], refspec[1])
self.assertEqual(list, type(remote.fetch_refspecs))
self.assertEqual(0, len(remote.push_refspecs))
remote.set_fetch_refspecs(['+refs/*:refs/remotes/*'])
self.assertEqual('+refs/*:refs/remotes/*',
remote.fetch_refspecs[0])
remote.set_fetch_refspecs([
'+refs/*:refs/remotes/*',
'+refs/test/*:refs/test/remotes/*'
])
self.assertEqual('+refs/*:refs/remotes/*',
remote.fetch_refspecs[0])
self.assertEqual('+refs/test/*:refs/test/remotes/*',
remote.fetch_refspecs[1])
remote.set_push_refspecs([
'+refs/*:refs/remotes/*',
'+refs/test/*:refs/test/remotes/*'
])
self.assertEqual('+refs/*:refs/remotes/*',
remote.push_refspecs[0])
self.assertEqual('+refs/test/*:refs/test/remotes/*',
remote.push_refspecs[1])
def test_remote_list(self): def test_remote_list(self):