Replace internal function wrap_object by lookup_object
The new function does a little more, saving some lines from the callers. This change will allow to implement a cache, now all object lookups are centralized in a single function.
This commit is contained in:
73
pygit2.c
73
pygit2.c
@@ -164,9 +164,20 @@ Error_set_py_obj(int err, PyObject *py_obj) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Object *
|
static PyObject *
|
||||||
wrap_object(git_object *obj, Repository *repo) {
|
lookup_object(Repository *repo, const git_oid *oid, git_otype type) {
|
||||||
|
int err;
|
||||||
|
char hex[GIT_OID_HEXSZ + 1];
|
||||||
|
git_object *obj;
|
||||||
Object *py_obj = NULL;
|
Object *py_obj = NULL;
|
||||||
|
|
||||||
|
err = git_object_lookup(&obj, repo->repo, oid, type);
|
||||||
|
if (err < 0) {
|
||||||
|
git_oid_fmt(hex, oid);
|
||||||
|
hex[GIT_OID_HEXSZ] = '\0';
|
||||||
|
return Error_set_str(err, hex);
|
||||||
|
}
|
||||||
|
|
||||||
switch (git_object_type(obj)) {
|
switch (git_object_type(obj)) {
|
||||||
case GIT_OBJ_COMMIT:
|
case GIT_OBJ_COMMIT:
|
||||||
py_obj = (Object*)CommitType.tp_alloc(&CommitType, 0);
|
py_obj = (Object*)CommitType.tp_alloc(&CommitType, 0);
|
||||||
@@ -184,12 +195,12 @@ wrap_object(git_object *obj, Repository *repo) {
|
|||||||
assert(0);
|
assert(0);
|
||||||
}
|
}
|
||||||
if (!py_obj)
|
if (!py_obj)
|
||||||
return (Object*)PyErr_NoMemory();
|
return PyErr_NoMemory();
|
||||||
|
|
||||||
py_obj->obj = obj;
|
py_obj->obj = obj;
|
||||||
py_obj->repo = repo;
|
py_obj->repo = repo;
|
||||||
Py_INCREF(repo);
|
Py_INCREF(repo);
|
||||||
return py_obj;
|
return (PyObject*)py_obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
@@ -268,21 +279,12 @@ Repository_contains(Repository *self, PyObject *value) {
|
|||||||
static PyObject *
|
static PyObject *
|
||||||
Repository_getitem(Repository *self, PyObject *value) {
|
Repository_getitem(Repository *self, PyObject *value) {
|
||||||
git_oid oid;
|
git_oid oid;
|
||||||
int err;
|
|
||||||
git_object *obj;
|
git_object *obj;
|
||||||
Object *py_obj;
|
|
||||||
|
|
||||||
if (!py_str_to_git_oid(value, &oid))
|
if (!py_str_to_git_oid(value, &oid))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
err = git_object_lookup(&obj, self->repo, &oid, GIT_OBJ_ANY);
|
return lookup_object(self, &oid, GIT_OBJ_ANY);
|
||||||
if (err < 0)
|
|
||||||
return Error_set_py_obj(err, value);
|
|
||||||
|
|
||||||
py_obj = wrap_object(obj, self);
|
|
||||||
if (!py_obj)
|
|
||||||
return NULL;
|
|
||||||
return (PyObject*)py_obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@@ -890,8 +892,7 @@ static PyObject *
|
|||||||
Commit_get_parents(Commit *commit)
|
Commit_get_parents(Commit *commit)
|
||||||
{
|
{
|
||||||
unsigned int i, parent_count;
|
unsigned int i, parent_count;
|
||||||
int err;
|
const git_oid *parent_oid;
|
||||||
git_commit *parent;
|
|
||||||
PyObject *obj;
|
PyObject *obj;
|
||||||
PyObject *list;
|
PyObject *list;
|
||||||
|
|
||||||
@@ -901,13 +902,13 @@ Commit_get_parents(Commit *commit)
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
for (i=0; i < parent_count; i++) {
|
for (i=0; i < parent_count; i++) {
|
||||||
err = git_commit_parent(&parent, commit->commit, i);
|
parent_oid = git_commit_parent_oid(commit->commit, i);
|
||||||
if (err < 0) {
|
if (parent_oid == NULL) {
|
||||||
Py_DECREF(list);
|
Py_DECREF(list);
|
||||||
Error_set(err);
|
Error_set(GIT_ENOTFOUND);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
obj = (PyObject*)wrap_object((git_object *)parent, commit->repo);
|
obj = lookup_object(commit->repo, parent_oid, GIT_OBJ_COMMIT);
|
||||||
if (obj == NULL) {
|
if (obj == NULL) {
|
||||||
Py_DECREF(list);
|
Py_DECREF(list);
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -1000,17 +1001,10 @@ TreeEntry_get_sha(TreeEntry *self) {
|
|||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
TreeEntry_to_object(TreeEntry *self) {
|
TreeEntry_to_object(TreeEntry *self) {
|
||||||
git_object *obj;
|
const git_oid *entry_oid;
|
||||||
int err;
|
|
||||||
char hex[GIT_OID_HEXSZ + 1];
|
|
||||||
|
|
||||||
err = git_tree_entry_2object(&obj, self->tree->repo->repo, self->entry);
|
entry_oid = git_tree_entry_id(self->entry);
|
||||||
if (err < 0) {
|
return lookup_object(self->tree->repo, entry_oid, GIT_OBJ_ANY);
|
||||||
git_oid_fmt(hex, git_tree_entry_id(self->entry));
|
|
||||||
hex[GIT_OID_HEXSZ] = '\0';
|
|
||||||
return Error_set_str(err, hex);
|
|
||||||
}
|
|
||||||
return (PyObject*)wrap_object(obj, self->tree->repo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyGetSetDef TreeEntry_getseters[] = {
|
static PyGetSetDef TreeEntry_getseters[] = {
|
||||||
@@ -1284,20 +1278,15 @@ Tag_dealloc(Tag *self) {
|
|||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
Tag_get_target(Tag *self) {
|
Tag_get_target(Tag *self) {
|
||||||
git_object *target;
|
const git_oid *target_oid;
|
||||||
int err;
|
git_otype target_type;
|
||||||
|
|
||||||
if (self->target == NULL) {
|
if (self->target == NULL) {
|
||||||
err = git_tag_target(&target, self->tag);
|
target_oid = git_tag_target_oid(self->tag);
|
||||||
if (err == GIT_ENOTFOUND) {
|
target_type = git_tag_type(self->tag);
|
||||||
/* This can only happen if we have a new tag with no target set
|
self->target = lookup_object(self->repo, target_oid, target_type);
|
||||||
* yet. */
|
if (self->target == NULL)
|
||||||
Py_INCREF(Py_None);
|
return NULL;
|
||||||
self->target = Py_None;
|
|
||||||
} else if (err < 0)
|
|
||||||
return Error_set(err);
|
|
||||||
else
|
|
||||||
self->target = (PyObject*)wrap_object(target, self->repo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_INCREF(self->target);
|
Py_INCREF(self->target);
|
||||||
|
Reference in New Issue
Block a user