Use basic types for objects and grouped types into groups

This commit is contained in:
Nico von Geyso
2013-03-12 15:31:26 +01:00
parent f97f58ec56
commit 78cddb6c91
2 changed files with 88 additions and 62 deletions

View File

@@ -32,39 +32,47 @@
#include <Python.h>
#include <git2.h>
/* Python objects */
/*
* Python objects
*
**/
/* git_repository */
typedef struct {
PyObject_HEAD
git_repository *repo;
PyObject *index; /* It will be None for a bare repository */
PyObject *config;
PyObject *config; /* It will be None for a bare repository */
} Repository;
/* The structs for some of the object subtypes are identical except for
* the type of their object pointers. */
#define OBJECT_STRUCT(_name, _ptr_type, _ptr_name) \
#define SIMPLE_TYPE(_name, _ptr_type, _ptr_name) \
typedef struct {\
PyObject_HEAD\
Repository *repo;\
_ptr_type *_ptr_name;\
} _name;
OBJECT_STRUCT(Object, git_object, obj)
OBJECT_STRUCT(Commit, git_commit, commit)
OBJECT_STRUCT(Tree, git_tree, tree)
OBJECT_STRUCT(TreeBuilder, git_treebuilder, bld)
OBJECT_STRUCT(Blob, git_blob, blob)
OBJECT_STRUCT(Tag, git_tag, tag)
OBJECT_STRUCT(Index, git_index, index)
OBJECT_STRUCT(Walker, git_revwalk, walk)
OBJECT_STRUCT(Remote, git_remote, remote)
OBJECT_STRUCT(Diff, git_diff_list, list)
/* git object types
*
* The structs for some of the object subtypes are identical except for
* the type of their object pointers. */
SIMPLE_TYPE(Object, git_object, obj)
SIMPLE_TYPE(Commit, git_commit, commit)
SIMPLE_TYPE(Tree, git_tree, tree)
SIMPLE_TYPE(Blob, git_blob, blob)
SIMPLE_TYPE(Tag, git_tag, tag)
/* git_config */
typedef struct {
PyObject_HEAD
git_config* config;
} Config;
/* git_note */
typedef struct {
PyObject_HEAD
Repository *repo;
@@ -79,9 +87,12 @@ typedef struct {
char* ref;
} NoteIter;
/* git _diff */
SIMPLE_TYPE(Diff, git_diff_list, list)
typedef struct {
PyObject_HEAD
Diff* diff;
size_t i;
size_t n;
@@ -98,12 +109,6 @@ typedef struct {
unsigned similarity;
} Patch;
typedef struct {
PyObject_HEAD
PyObject *owner; /* Tree or TreeBuilder */
const git_tree_entry *entry;
} TreeEntry;
typedef struct {
PyObject_HEAD
PyObject* lines;
@@ -113,12 +118,26 @@ typedef struct {
int new_lines;
} Hunk;
/* git_tree_walk , git_treebuilder*/
SIMPLE_TYPE(TreeBuilder, git_treebuilder, bld)
typedef struct {
PyObject_HEAD
PyObject *owner; /* Tree or TreeBuilder */
const git_tree_entry *entry;
} TreeEntry;
typedef struct {
PyObject_HEAD
Tree *owner;
int i;
} TreeIter;
/* git_index */
SIMPLE_TYPE(Index, git_index, index)
typedef struct {
PyObject_HEAD
const git_index_entry *entry;
@@ -130,6 +149,10 @@ typedef struct {
int i;
} IndexIter;
/* git_reference, git_reflog */
SIMPLE_TYPE(Walker, git_revwalk, walk)
typedef struct {
PyObject_HEAD
git_reference *reference;
@@ -137,9 +160,9 @@ typedef struct {
typedef struct {
PyObject_HEAD
PyObject *oid_old;
PyObject *oid_new;
PyObject *committer;
git_signature *signature;
char *oid_old;
char *oid_new;
char *message;
} RefLogEntry;
@@ -151,6 +174,7 @@ typedef struct {
} RefLogIter;
/* git_signature */
typedef struct {
PyObject_HEAD
Object *obj;
@@ -159,6 +183,10 @@ typedef struct {
} Signature;
/* git_remote */
SIMPLE_TYPE(Remote, git_remote, remote)
PyObject*
lookup_object_prefix(Repository *repo, const git_oid *oid, size_t len,
git_otype type);

View File

@@ -47,40 +47,24 @@ void RefLogIter_dealloc(RefLogIter *self)
PyObject_Del(self);
}
PyObject* RefLogIter_iternext(PyObject *self)
PyObject* RefLogIter_iternext(RefLogIter *self)
{
RefLogIter *p = (RefLogIter *) self;
const git_reflog_entry *entry;
char oid_old[40], oid_new[40];
if (p->i < p->size) {
RefLogEntry *py_entry;
git_signature *signature;
entry = git_reflog_entry_byindex(p->reflog, p->i);
py_entry = (RefLogEntry*) PyType_GenericNew(&RefLogEntryType, NULL,
NULL);
git_oid_fmt(oid_old, git_reflog_entry_id_old(entry));
git_oid_fmt(oid_new, git_reflog_entry_id_new(entry));
py_entry->oid_new = PyUnicode_FromStringAndSize(oid_new, 40);
py_entry->oid_old = PyUnicode_FromStringAndSize(oid_old, 40);
if (self->i < self->size) {
entry = git_reflog_entry_byindex(self->reflog, self->i);
py_entry = PyObject_New(RefLogEntry, &RefLogEntryType);
py_entry->oid_old = git_oid_allocfmt(git_reflog_entry_id_old(entry));
py_entry->oid_new = git_oid_allocfmt(git_reflog_entry_id_new(entry));
py_entry->message = strdup(git_reflog_entry_message(entry));
py_entry->signature = git_signature_dup(
git_reflog_entry_committer(entry));
signature = git_signature_dup(
git_reflog_entry_committer(entry)
);
if (signature != NULL)
py_entry->committer = build_signature(
(Object*)py_entry, signature, "utf-8");
++(p->i);
++(self->i);
return (PyObject*) py_entry;
}
PyErr_SetNone(PyExc_StopIteration);
@@ -383,13 +367,23 @@ Reference_log(Reference *self)
return (PyObject*)iter;
}
PyDoc_STRVAR(RefLogEntry_committer__doc__, "Committer.");
PyObject *
RefLogEntry_committer__get__(RefLogEntry *self)
{
return build_signature((Object*) self, self->signature, "utf-8");
}
static int
RefLogEntry_init(RefLogEntry *self, PyObject *args, PyObject *kwds)
{
self->oid_old = Py_None;
self->oid_new = Py_None;
self->message = "";
self->committer = Py_None;
self->oid_old = NULL;
self->oid_new = NULL;
self->message = NULL;
self->signature = NULL;
return 0;
}
@@ -398,18 +392,22 @@ RefLogEntry_init(RefLogEntry *self, PyObject *args, PyObject *kwds)
static void
RefLogEntry_dealloc(RefLogEntry *self)
{
Py_CLEAR(self->oid_old);
Py_CLEAR(self->oid_new);
Py_CLEAR(self->committer);
free(self->oid_old);
free(self->oid_new);
free(self->message);
git_signature_free(self->signature);
PyObject_Del(self);
}
PyMemberDef RefLogEntry_members[] = {
MEMBER(RefLogEntry, oid_new, T_OBJECT, "New oid."),
MEMBER(RefLogEntry, oid_old, T_OBJECT, "Old oid."),
MEMBER(RefLogEntry, oid_new, T_STRING, "New oid."),
MEMBER(RefLogEntry, oid_old, T_STRING, "Old oid."),
MEMBER(RefLogEntry, message, T_STRING, "Message."),
MEMBER(RefLogEntry, committer, T_OBJECT, "Committer."),
{NULL}
};
PyGetSetDef RefLogEntry_getseters[] = {
GETTER(RefLogEntry, committer),
{NULL}
};