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