Improve the Repository.walk method a little

Now it is possible to pass None as the commit hash to get a non
initialized walker.  A second and optional parameter allows to set
the sorting mode.

Signed-off-by: J. David Ibañez <jdavid@itaapy.com>
This commit is contained in:
J. David Ibañez
2011-02-14 15:42:50 +01:00
parent fc4f82cc68
commit 0416276d2f

View File

@@ -364,31 +364,47 @@ py_str_to_git_commit(Repository *py_repo, PyObject *py_hex) {
} }
static PyObject * static PyObject *
Repository_walk(Repository *self, PyObject *py_hex) Repository_walk(Repository *self, PyObject *args)
{ {
PyObject *value;
unsigned int sort;
int err; int err;
git_commit *commit; git_commit *commit;
git_revwalk *walk; git_revwalk *walk;
Walker *py_walker; Walker *py_walker;
commit = py_str_to_git_commit(self, py_hex); sort = GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE;
if (commit == NULL) if (!PyArg_ParseTuple(args, "O|I", &value, &sort))
return NULL; return NULL;
if (value != Py_None && !PyString_Check(value)) {
PyErr_SetObject(PyExc_TypeError, value);
return NULL;
}
err = git_revwalk_new(&walk, self->repo); err = git_revwalk_new(&walk, self->repo);
if (err < 0) if (err < 0)
return Error_set(err); return Error_set(err);
err = git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE); /* Sort */
err = git_revwalk_sorting(walk, sort);
if (err < 0) { if (err < 0) {
git_revwalk_free(walk); git_revwalk_free(walk);
return Error_set(err); return Error_set(err);
} }
err = git_revwalk_push(walk, commit); /* Push */
if (err < 0) { if (value != Py_None) {
git_revwalk_free(walk); commit = py_str_to_git_commit(self, value);
return Error_set(err); if (commit == NULL) {
git_revwalk_free(walk);
return NULL;
}
err = git_revwalk_push(walk, commit);
if (err < 0) {
git_revwalk_free(walk);
return Error_set(err);
}
} }
py_walker = PyObject_New(Walker, &WalkerType); py_walker = PyObject_New(Walker, &WalkerType);
@@ -404,7 +420,7 @@ Repository_walk(Repository *self, PyObject *py_hex)
} }
static PyMethodDef Repository_methods[] = { static PyMethodDef Repository_methods[] = {
{"walk", (PyCFunction)Repository_walk, METH_O, {"walk", (PyCFunction)Repository_walk, METH_VARARGS,
"Generator that traverses the history starting from the given commit."}, "Generator that traverses the history starting from the given commit."},
{"read", (PyCFunction)Repository_read, METH_O, {"read", (PyCFunction)Repository_read, METH_O,
"Read raw object data from the repository."}, "Read raw object data from the repository."},