Skip to content

Commit

Permalink
repository: return a list from listall
Browse files Browse the repository at this point in the history
Make listall_references() and listall_branches() return a list instead
of a tuple, as they're simply sequences of objects.
  • Loading branch information
carlosmn committed Nov 20, 2013
1 parent 4c47eba commit aa5877e
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions src/repository.c
Original file line number Diff line number Diff line change
Expand Up @@ -877,9 +877,9 @@ PyObject* Repository_create_branch(Repository *self, PyObject *args)


PyDoc_STRVAR(Repository_listall_references__doc__,
"listall_references() -> (str, ...)\n"
"listall_references() -> [str, ...]\n"
"\n"
"Return a tuple with all the references in the repository.");
"Return a list with all the references in the repository.");

PyObject *
Repository_listall_references(Repository *self, PyObject *args)
Expand All @@ -895,18 +895,18 @@ Repository_listall_references(Repository *self, PyObject *args)
return Error_set(err);

/* Create a new PyTuple */
py_result = PyTuple_New(c_result.count);
py_result = PyList_New(c_result.count);
if (py_result == NULL)
goto out;

/* Fill it */
for (index=0; index < c_result.count; index++) {
py_string = to_path((c_result.strings)[index]);
py_string = to_path(c_result.strings[index]);
if (py_string == NULL) {
Py_CLEAR(py_result);
goto out;
}
PyTuple_SET_ITEM(py_result, index, py_string);
PyList_SET_ITEM(py_result, index, py_string);
}

out:
Expand All @@ -916,7 +916,7 @@ Repository_listall_references(Repository *self, PyObject *args)


PyDoc_STRVAR(Repository_listall_branches__doc__,
"listall_branches([flags]) -> (str, ...)\n"
"listall_branches([flags]) -> [str, ...]\n"
"\n"
"Return a tuple with all the branches in the repository.");

Expand All @@ -926,57 +926,49 @@ Repository_listall_branches(Repository *self, PyObject *args)
git_branch_t list_flags = GIT_BRANCH_LOCAL;
git_branch_iterator *iter;
git_reference *ref = NULL;
Py_ssize_t pos = 0;
int err;
git_branch_t type;
PyObject *tuple;
PyObject *list;

/* 1- Get list_flags */
if (!PyArg_ParseTuple(args, "|I", &list_flags))
return NULL;

tuple = PyTuple_New(4);
if (tuple == NULL)
list = PyList_New(0);
if (list == NULL)
return NULL;

if ((err = git_branch_iterator_new(&iter, self->repo, list_flags)) < 0)
return Error_set(err);

while ((err = git_branch_next(&ref, &type, iter)) == 0) {
if (PyTuple_Size(tuple) <= pos) {
if (_PyTuple_Resize(&tuple, pos * 2) < 0)
goto on_error;
}

PyObject *py_branch_name = to_path(git_reference_shorthand(ref));
git_reference_free(ref);
ref = NULL;

if (py_branch_name == NULL)
goto on_error;

PyTuple_SET_ITEM(tuple, pos++, py_branch_name);
err = PyList_Append(list, py_branch_name);
Py_DECREF(py_branch_name);

if (err < 0)
goto on_error;
}

git_branch_iterator_free(iter);
if (err == GIT_ITEROVER)
err = 0;

if (err < 0) {
Py_CLEAR(tuple);
Py_CLEAR(list);
return Error_set(err);
}

/* Remove the elements we might have overallocated in the loop */
if (_PyTuple_Resize(&tuple, pos) < 0)
return Error_set(err);

return tuple;
return list;

on_error:
git_reference_free(ref);
git_branch_iterator_free(iter);
Py_CLEAR(tuple);
Py_CLEAR(list);
return NULL;
}

Expand Down

0 comments on commit aa5877e

Please sign in to comment.