Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refspec getter/setters #319

Merged
merged 2 commits into from
Jan 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 100 additions & 50 deletions src/remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,55 @@ get_pylist_from_git_strarray(git_strarray *strarray)
return new_list;
}

int
get_strarraygit_from_pylist(git_strarray *array, PyObject *pylist)
{
Py_ssize_t index, n;
PyObject *item;
void *ptr;

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);
char *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);

PyDoc_STRVAR(Remote_get_fetch_refspecs__doc__, "Fetch refspecs");
return -1;
}

PyDoc_STRVAR(Remote_fetch_refspecs__doc__, "Fetch refspecs");

PyObject *
Remote_get_fetch_refspecs(Remote *self)
Remote_fetch_refspecs__get__(Remote *self)
{
int err;
git_strarray refspecs;
Expand All @@ -391,12 +434,30 @@ Remote_get_fetch_refspecs(Remote *self)
return new_list;
}

int
Remote_fetch_refspecs__set__(Remote *self, PyObject *py_list)
{
int err;
git_strarray fetch_refspecs;

PyDoc_STRVAR(Remote_get_push_refspecs__doc__, "Push refspecs");
if (get_strarraygit_from_pylist(&fetch_refspecs, py_list) < 0)
return -1;

err = git_remote_set_fetch_refspecs(self->remote, &fetch_refspecs);
git_strarray_free(&fetch_refspecs);

if (err < 0) {
Error_set(err);
return -1;
}

return 0;
}

PyDoc_STRVAR(Remote_push_refspecs__doc__, "Push refspecs");

PyObject *
Remote_get_push_refspecs(Remote *self)
Remote_push_refspecs__get__(Remote *self)
{
int err;
git_strarray refspecs;
Expand All @@ -412,89 +473,76 @@ Remote_get_push_refspecs(Remote *self)
return new_list;
}


int
get_strarraygit_from_pylist(git_strarray *array, PyObject *pylist)
Remote_push_refspecs__set__(Remote *self, PyObject *py_list)
{
long index, n;
PyObject *item;
void *ptr;
int err;
git_strarray push_refspecs;

n = PyObject_Length(pylist);
if (n < 0)
if (get_strarraygit_from_pylist(&push_refspecs, py_list) != 0)
return -1;

/* allocate new git_strarray */
ptr = calloc(n, sizeof(char *));
if (!ptr) {
PyErr_SetNone(PyExc_MemoryError);
err = git_remote_set_push_refspecs(self->remote, &push_refspecs);
git_strarray_free(&push_refspecs);

if (err < 0) {
Error_set(err);
return -1;
}

array->strings = ptr;
array->count = n;
return 0;
}

PyDoc_STRVAR(Remote_get_fetch_refspecs__doc__,
"Fetch refspecs.\n"
"This function is deprecated, please use the fetch_refspecs attribute"
"\n");

for (index = 0; index < n; index++) {
item = PyList_GetItem(pylist, index);
array->strings[index] = py_str_to_c_str(item, NULL);
}

return GIT_OK;
PyObject *
Remote_get_fetch_refspecs(Remote *self)
{
return Remote_fetch_refspecs__get__(self);
}


PyDoc_STRVAR(Remote_get_push_refspecs__doc__, "Push refspecs");


PyObject *
Remote_get_push_refspecs(Remote *self)
{
return Remote_push_refspecs__get__(self);
}

PyDoc_STRVAR(Remote_set_fetch_refspecs__doc__,
"set_fetch_refspecs([str])\n"
"This function is deprecated, please use the push_refspecs attribute"
"\n");


PyObject *
Remote_set_fetch_refspecs(Remote *self, PyObject *args)
{
int err;
PyObject *pyrefspecs;
git_strarray fetch_refspecs;

if (! PyArg_Parse(args, "O", &pyrefspecs))
if (Remote_fetch_refspecs__set__(self, args) < 0)
return NULL;

if (get_strarraygit_from_pylist(&fetch_refspecs, pyrefspecs) != GIT_OK)
return NULL;

err = git_remote_set_fetch_refspecs(self->remote, &fetch_refspecs);
git_strarray_free(&fetch_refspecs);

if (err != GIT_OK)
return Error_set(err);

Py_RETURN_NONE;
}


PyDoc_STRVAR(Remote_set_push_refspecs__doc__,
"set_push_refspecs([str])\n"
"This function is deprecated, please use the push_refspecs attribute"
"\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)
if (Remote_push_refspecs__set__(self, args) < 0)
return NULL;

err = git_remote_set_push_refspecs(self->remote, &push_refspecs);
git_strarray_free(&push_refspecs);

if (err != GIT_OK)
return Error_set(err);

Py_RETURN_NONE;
}

Expand Down Expand Up @@ -795,6 +843,8 @@ PyGetSetDef Remote_getseters[] = {
GETSET(Remote, url),
GETSET(Remote, push_url),
GETTER(Remote, refspec_count),
GETSET(Remote, fetch_refspecs),
GETSET(Remote, push_refspecs),
{NULL}
};

Expand Down
13 changes: 13 additions & 0 deletions test/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,19 @@ def test_refspec(self):
self.assertEqual(list, type(remote.get_push_refspecs()))
self.assertEqual(0, len(remote.get_push_refspecs()))

push_specs = remote.push_refspecs
self.assertEqual(list, type(push_specs))
self.assertEqual(0, len(push_specs))

remote.set_fetch_refspecs(['+refs/*:refs/remotes/*'])
self.assertEqual('+refs/*:refs/remotes/*',
remote.get_fetch_refspecs()[0])

fetch_specs = remote.fetch_refspecs
self.assertEqual(list, type(fetch_specs))
self.assertEqual(1, len(fetch_specs))
self.assertEqual('+refs/*:refs/remotes/*', fetch_specs[0])

remote.set_fetch_refspecs([
'+refs/*:refs/remotes/*',
'+refs/test/*:refs/test/remotes/*'
Expand All @@ -121,6 +130,10 @@ def test_refspec(self):
'+refs/test/*:refs/test/remotes/*'
])

self.assertRaises(TypeError, setattr, remote, 'push_refspecs', '+refs/*:refs/*')
self.assertRaises(TypeError, setattr, remote, 'fetch_refspecs', '+refs/*:refs/*')
self.assertRaises(TypeError, setattr, remote, 'fetch_refspecs', ['+refs/*:refs/*', 5])

self.assertEqual('+refs/*:refs/remotes/*',
remote.get_push_refspecs()[0])
self.assertEqual('+refs/test/*:refs/test/remotes/*',
Expand Down