Skip to content

Commit aa5877e

Browse files
committed
repository: return a list from listall
Make listall_references() and listall_branches() return a list instead of a tuple, as they're simply sequences of objects.
1 parent 4c47eba commit aa5877e

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

src/repository.c

+17-25
Original file line numberDiff line numberDiff line change
@@ -877,9 +877,9 @@ PyObject* Repository_create_branch(Repository *self, PyObject *args)
877877

878878

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

884884
PyObject *
885885
Repository_listall_references(Repository *self, PyObject *args)
@@ -895,18 +895,18 @@ Repository_listall_references(Repository *self, PyObject *args)
895895
return Error_set(err);
896896

897897
/* Create a new PyTuple */
898-
py_result = PyTuple_New(c_result.count);
898+
py_result = PyList_New(c_result.count);
899899
if (py_result == NULL)
900900
goto out;
901901

902902
/* Fill it */
903903
for (index=0; index < c_result.count; index++) {
904-
py_string = to_path((c_result.strings)[index]);
904+
py_string = to_path(c_result.strings[index]);
905905
if (py_string == NULL) {
906906
Py_CLEAR(py_result);
907907
goto out;
908908
}
909-
PyTuple_SET_ITEM(py_result, index, py_string);
909+
PyList_SET_ITEM(py_result, index, py_string);
910910
}
911911

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

917917

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

@@ -926,57 +926,49 @@ Repository_listall_branches(Repository *self, PyObject *args)
926926
git_branch_t list_flags = GIT_BRANCH_LOCAL;
927927
git_branch_iterator *iter;
928928
git_reference *ref = NULL;
929-
Py_ssize_t pos = 0;
930929
int err;
931930
git_branch_t type;
932-
PyObject *tuple;
931+
PyObject *list;
933932

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

938-
tuple = PyTuple_New(4);
939-
if (tuple == NULL)
937+
list = PyList_New(0);
938+
if (list == NULL)
940939
return NULL;
941940

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

945944
while ((err = git_branch_next(&ref, &type, iter)) == 0) {
946-
if (PyTuple_Size(tuple) <= pos) {
947-
if (_PyTuple_Resize(&tuple, pos * 2) < 0)
948-
goto on_error;
949-
}
950-
951945
PyObject *py_branch_name = to_path(git_reference_shorthand(ref));
952946
git_reference_free(ref);
953-
ref = NULL;
954947

955948
if (py_branch_name == NULL)
956949
goto on_error;
957950

958-
PyTuple_SET_ITEM(tuple, pos++, py_branch_name);
951+
err = PyList_Append(list, py_branch_name);
952+
Py_DECREF(py_branch_name);
953+
954+
if (err < 0)
955+
goto on_error;
959956
}
960957

961958
git_branch_iterator_free(iter);
962959
if (err == GIT_ITEROVER)
963960
err = 0;
964961

965962
if (err < 0) {
966-
Py_CLEAR(tuple);
963+
Py_CLEAR(list);
967964
return Error_set(err);
968965
}
969966

970-
/* Remove the elements we might have overallocated in the loop */
971-
if (_PyTuple_Resize(&tuple, pos) < 0)
972-
return Error_set(err);
973-
974-
return tuple;
967+
return list;
975968

976969
on_error:
977-
git_reference_free(ref);
978970
git_branch_iterator_free(iter);
979-
Py_CLEAR(tuple);
971+
Py_CLEAR(list);
980972
return NULL;
981973
}
982974

0 commit comments

Comments
 (0)