Skip to content

Commit

Permalink
OdbBackend: initial groundwork
Browse files Browse the repository at this point in the history
  • Loading branch information
ddevault committed Sep 20, 2019
1 parent 24d176c commit 6e132a7
Show file tree
Hide file tree
Showing 6 changed files with 512 additions and 10 deletions.
92 changes: 83 additions & 9 deletions src/odb.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
#include <Python.h>
#include "error.h"
#include "object.h"
#include "odb_backend.h"
#include "oid.h"
#include "types.h"
#include "utils.h"
#include <git2/odb.h>

PyTypeObject OdbBackendType;

static git_otype
int_to_loose_object_type(int type_id)
{
Expand Down Expand Up @@ -234,6 +237,85 @@ Odb_write(Odb *self, PyObject *args)
}


PyDoc_STRVAR(Odb_add_backend__doc__,
"add_backend(backend, priority)\n"
"\n"
"Adds an OdbBackend to the list of backends for this object database.\n");

PyObject *
Odb_add_backend(Odb *self, PyObject *args)
{
int err, priority;
OdbBackend *backend;

if (!PyArg_ParseTuple(args, "OI", &backend, &priority))
return NULL;

if (!PyObject_IsInstance((PyObject *)backend, (PyObject *)&OdbBackendType)) {
PyErr_SetString(PyExc_TypeError,
"add_backend expects an object of type "
"pygit2.OdbBackend");
return NULL;
}

err = git_odb_add_backend(self->odb, backend->odb_backend, priority);
if (err > 0)
return Error_set(err);

Py_RETURN_NONE;
}


PyMethodDef Odb_methods[] = {
METHOD(Odb, add_disk_alternate, METH_O),
METHOD(Odb, read, METH_O),
METHOD(Odb, write, METH_VARARGS),
METHOD(Odb, add_backend, METH_VARARGS),
{NULL}
};


PyDoc_STRVAR(Odb_backends__doc__,
"Return an iterable of backends for this object database.");

PyObject *
Odb_backends__get__(Odb *self)
{
int err;
size_t i, nbackends;
git_odb_backend *backend;
PyObject *accum = PyList_New(0);
PyObject *ret;

if (accum == NULL)
return NULL;

nbackends = git_odb_num_backends(self->odb);
for (i = 0; i < nbackends; ++i) {
err = git_odb_get_backend(&backend, self->odb, i);
if (err != 0) {
Py_DECREF(accum);
return Error_set(err);
}

err = PyList_Append(accum, wrap_odb_backend(backend));
if (err != 0)
return NULL;
}

ret = PyObject_GetIter(accum);
Py_DECREF(accum);

return ret;
}


PyGetSetDef Odb_getseters[] = {
GETTER(Odb, backends),
{NULL}
};


int
Odb_contains(Odb *self, PyObject *py_name)
{
Expand All @@ -249,14 +331,6 @@ Odb_contains(Odb *self, PyObject *py_name)
return git_odb_exists(self->odb, &oid);
}


PyMethodDef Odb_methods[] = {
METHOD(Odb, add_disk_alternate, METH_O),
METHOD(Odb, read, METH_O),
METHOD(Odb, write, METH_VARARGS),
{NULL}
};

PySequenceMethods Odb_as_sequence = {
0, /* sq_length */
0, /* sq_concat */
Expand Down Expand Up @@ -300,7 +374,7 @@ PyTypeObject OdbType = {
0, /* tp_iternext */
Odb_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
Odb_getseters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
Expand Down
Loading

0 comments on commit 6e132a7

Please sign in to comment.