3f939c19e5d9b36242d6697f2a3786ac2ea954be
[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 #include <stdlib.h>
22 #include <sys/stat.h>
23
24 #define bytehex(x) (((x)<0xa)?('0'+(x)):('a'-0xa+(x)))
25
26 static PyObject *sha_to_pyhex(const unsigned char *sha)
27 {
28         char hexsha[41];
29         int i;
30         for (i = 0; i < 20; i++) {
31                 hexsha[i*2] = bytehex((sha[i] & 0xF0) >> 4);
32                 hexsha[i*2+1] = bytehex(sha[i] & 0x0F);
33         }
34         
35         return PyString_FromStringAndSize(hexsha, 40);
36 }
37
38 static PyObject *py_parse_tree(PyObject *self, PyObject *args)
39 {
40         char *text, *end;
41         int len, namelen;
42         PyObject *ret, *item, *name;
43
44         if (!PyArg_ParseTuple(args, "s#", &text, &len))
45                 return NULL;
46
47         /* TODO: currently this returns a list; if memory usage is a concern,
48         * consider rewriting as a custom iterator object */
49         ret = PyList_New(0);
50
51         if (ret == NULL) {
52                 return NULL;
53         }
54
55         end = text + len;
56
57         while (text < end) {
58                 long mode;
59                 mode = strtol(text, &text, 8);
60
61                 if (*text != ' ') {
62                         PyErr_SetString(PyExc_RuntimeError, "Expected space");
63                         Py_DECREF(ret);
64                         return NULL;
65                 }
66
67                 text++;
68
69                 namelen = strlen(text);
70
71                 name = PyString_FromStringAndSize(text, namelen);
72                 if (name == NULL) {
73                         Py_DECREF(ret);
74                         return NULL;
75                 }
76
77                 item = Py_BuildValue("(NlN)", name, mode,
78                                                          sha_to_pyhex((unsigned char *)text+namelen+1));
79                 if (item == NULL) {
80                         Py_DECREF(ret);
81                         Py_DECREF(name);
82                         return NULL;
83                 }
84                 if (PyList_Append(ret, item) == -1) {
85                         Py_DECREF(ret);
86                         Py_DECREF(item);
87                         return NULL;
88                 }
89                 Py_DECREF(item);
90
91                 text += namelen+21;
92         }
93
94         return ret;
95 }
96
97 struct tree_item {
98         const char *name;
99         int mode;
100         PyObject *tuple;
101 };
102
103 int cmp_tree_item(const void *_a, const void *_b)
104 {
105         const struct tree_item *a = _a, *b = _b;
106         const char *remain_a, *remain_b;
107         int ret, common;
108         if (strlen(a->name) > strlen(b->name)) {
109                 common = strlen(b->name);
110                 remain_a = a->name + common;
111                 remain_b = (S_ISDIR(b->mode)?"/":"");
112         } else if (strlen(b->name) > strlen(a->name)) { 
113                 common = strlen(a->name);
114                 remain_a = (S_ISDIR(a->mode)?"/":"");
115                 remain_b = b->name + common;
116         } else { /* strlen(a->name) == strlen(b->name) */
117                 common = 0;
118                 remain_a = a->name;
119                 remain_b = b->name;
120         }
121         ret = strncmp(a->name, b->name, common);
122         if (ret != 0)
123                 return ret;
124         return strcmp(remain_a, remain_b);
125 }
126
127 static PyObject *py_sorted_tree_items(PyObject *self, PyObject *entries)
128 {
129         struct tree_item *qsort_entries;
130         int num, i;
131         PyObject *ret;
132         Py_ssize_t pos = 0; 
133         PyObject *key, *value;
134
135         if (!PyDict_Check(entries)) {
136                 PyErr_SetString(PyExc_TypeError, "Argument not a dictionary");
137                 return NULL;
138         }
139
140         num = PyDict_Size(entries);
141         qsort_entries = malloc(num * sizeof(struct tree_item));
142         if (qsort_entries == NULL) {
143                 PyErr_NoMemory();
144                 return NULL;
145         }
146
147         i = 0;
148         while (PyDict_Next(entries, &pos, &key, &value)) {
149                 PyObject *py_mode, *py_sha;
150                 
151                 if (PyTuple_Size(value) != 2) {
152                         PyErr_SetString(PyExc_ValueError, "Tuple has invalid size");
153                         free(qsort_entries);
154                         return NULL;
155                 }
156
157                 py_mode = PyTuple_GET_ITEM(value, 0);
158                 py_sha = PyTuple_GET_ITEM(value, 1);
159                 qsort_entries[i].tuple = Py_BuildValue("(OOO)", key, py_mode, py_sha);
160                 if (!PyString_CheckExact(key)) {
161                         PyErr_SetString(PyExc_TypeError, "Name is not a string");
162                         free(qsort_entries);
163                         return NULL;
164                 }
165                 qsort_entries[i].name = PyString_AS_STRING(key);
166                 if (!PyInt_CheckExact(py_mode)) {
167                         PyErr_SetString(PyExc_TypeError, "Mode is not an int");
168                         free(qsort_entries);
169                         return NULL;
170                 }
171                 qsort_entries[i].mode = PyInt_AS_LONG(py_mode);
172                 i++;
173         }
174
175         qsort(qsort_entries, num, sizeof(struct tree_item), cmp_tree_item);
176
177         ret = PyList_New(num);
178         if (ret == NULL) {
179                 free(qsort_entries);
180                 PyErr_NoMemory();
181                 return NULL;
182         }
183
184         for (i = 0; i < num; i++) {
185                 PyList_SET_ITEM(ret, i, qsort_entries[i].tuple);
186         }
187
188         free(qsort_entries);
189
190         return ret;
191 }
192
193 static PyMethodDef py_objects_methods[] = {
194         { "parse_tree", (PyCFunction)py_parse_tree, METH_VARARGS, NULL },
195         { "sorted_tree_items", (PyCFunction)py_sorted_tree_items, METH_O, NULL },
196         { NULL, NULL, 0, NULL }
197 };
198
199 void init_objects(void)
200 {
201         PyObject *m;
202
203         m = Py_InitModule3("_objects", py_objects_methods, NULL);
204         if (m == NULL)
205                 return;
206 }