Merge server capability refactoring from Dave.
[jelmer/dulwich-libgit2.git] / dulwich / _objects.c
1 /* 
2  * Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; version 2
7  * of the License or (at your option) a later version of the License.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17  * MA  02110-1301, USA.
18  */
19
20 #include <Python.h>
21
22 #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
23
24 static PyObject *sha_to_pyhex(const unsigned char *sha)
25 {
26         char hexsha[41];
27         int i;
28         for (i = 0; i < 20; i++) {
29                 hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
30                 hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
31         }
32         
33         return PyString_FromStringAndSize(hexsha, 40);
34 }
35
36 static PyObject *py_parse_tree(PyObject *self, PyObject *args)
37 {
38         char *text, *end;
39         int len, namelen;
40         PyObject *ret, *item, *name;
41
42         if (!PyArg_ParseTuple(args, "s#", &text, &len))
43                 return NULL;
44
45         ret = PyDict_New();
46         if (ret == NULL) {
47                 return NULL;
48         }
49
50         end = text + len;
51
52     while (text < end) {
53         long mode;
54                 mode = strtol(text, &text, 8);
55
56                 if (*text != ' ') {
57                         PyErr_SetString(PyExc_RuntimeError, "Expected space");
58                         Py_DECREF(ret);
59                         return NULL;
60                 }
61
62                 text++;
63
64         namelen = strlen(text);
65
66                 name = PyString_FromStringAndSize(text, namelen);
67                 if (name == NULL) {
68                         Py_DECREF(ret);
69                         return NULL;
70                 }
71
72         item = Py_BuildValue("(lN)", mode, sha_to_pyhex((unsigned char *)text+namelen+1));
73         if (item == NULL) {
74             Py_DECREF(ret);
75                         Py_DECREF(name);
76             return NULL;
77         }
78                 if (PyDict_SetItem(ret, name, item) == -1) {
79                         Py_DECREF(ret);
80                         Py_DECREF(item);
81                         return NULL;
82                 }
83                 Py_DECREF(name);
84                 Py_DECREF(item);
85
86                 text += namelen+21;
87     }
88
89     return ret;
90 }
91
92 static PyMethodDef py_objects_methods[] = {
93         { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL },
94         { NULL, NULL, 0, NULL }
95 };
96
97 void init_objects(void)
98 {
99         PyObject *m;
100
101         m = Py_InitModule3("_objects", py_objects_methods, NULL);
102         if (m == NULL)
103                 return;
104 }