2 #include "structmember.h"
4 #include <libgit-thin.h>
6 #define UNUSED(x) (x = x)
8 PyMODINIT_FUNC
initpygit(void);
16 repo_read_obj(PyObject
*args
,
17 int (*read_obj
)(unsigned char *sha1
, void **buf
, size_t *len
))
23 unsigned char sha1
[GIT_SHA1_SIZE
];
25 if (!PyArg_ParseTuple(args
, "s", &hex
))
28 err
= git_hex_to_sha1(hex
, sha1
);
30 return PyErr_SetFromErrno(PyExc_TypeError
);
32 err
= read_obj(sha1
, &buf
, NULL
);
34 return PyErr_SetFromErrno(PyExc_ValueError
);
36 ret
= PyString_FromString((char *) buf
);
43 pygit_repo_read_commit(GitRepoObject
*self
, PyObject
*args
)
46 return repo_read_obj(args
, git_repo_commit_read
);
50 pygit_repo_read_blob(GitRepoObject
*self
, PyObject
*args
)
53 return repo_read_obj(args
, git_repo_blob_read
);
56 static PyMethodDef git_repo_methods
[] = {
57 {"read_commit", (PyCFunction
) pygit_repo_read_commit
,
59 {"read_blob", (PyCFunction
) pygit_repo_read_blob
,
64 static PyMemberDef git_repo_members
[] = {
65 {"path", T_OBJECT_EX
, offsetof(GitRepoObject
, path
), 0,
71 git_repo_dealloc(GitRepoObject
*self
)
73 Py_XDECREF(self
->path
);
74 self
->ob_type
->tp_free((PyObject
*) self
);
77 static PyTypeObject Git_Repo_Type
= {
78 PyObject_HEAD_INIT(NULL
)
80 "pygit.repo", /* tp_name */
81 sizeof(GitRepoObject
), /* tp_basicsize */
83 (destructor
)git_repo_dealloc
, /* tp_dealloc */
90 0, /* tp_as_sequence */
91 0, /* tp_as_mapping */
98 Py_TPFLAGS_DEFAULT
, /* tp_flags */
102 0, /* tp_richcompare */
103 0, /* tp_weaklistoffset */
106 git_repo_methods
, /* tp_methods */
107 git_repo_members
, /* tp_members */
111 0, /* tp_descr_get */
112 0, /* tp_descr_set */
113 0, /* tp_dictoffset */
120 pygit_open(PyObject
*self
, PyObject
*args
)
128 if (!PyArg_ParseTuple(args
, "s", &dir
))
131 err
= git_repo_open(dir
);
133 PyErr_SetFromErrno(PyExc_IOError
);
137 m
= PyObject_New(GitRepoObject
, &Git_Repo_Type
);
141 err
= PyType_Ready(&Git_Repo_Type
);
147 m
->path
= PyString_FromString(dir
);
153 return (PyObject
*) m
;
156 static PyMethodDef pygit_methods
[] = {
157 { "open", pygit_open
, METH_VARARGS
, "Open a GIT repository" },
158 { NULL
, NULL
, 0, NULL
}
164 (void) Py_InitModule("pygit", pygit_methods
);