##// END OF EJS Templates
parsers: fix typing issue when constructing Python integer object...
Henrik Stuart -
r22400:888bc106 default
parent child Browse files
Show More
@@ -1,2173 +1,2173 b''
1 /*
1 /*
2 parsers.c - efficient content parsing
2 parsers.c - efficient content parsing
3
3
4 Copyright 2008 Matt Mackall <mpm@selenic.com> and others
4 Copyright 2008 Matt Mackall <mpm@selenic.com> and others
5
5
6 This software may be used and distributed according to the terms of
6 This software may be used and distributed according to the terms of
7 the GNU General Public License, incorporated herein by reference.
7 the GNU General Public License, incorporated herein by reference.
8 */
8 */
9
9
10 #include <Python.h>
10 #include <Python.h>
11 #include <ctype.h>
11 #include <ctype.h>
12 #include <stddef.h>
12 #include <stddef.h>
13 #include <string.h>
13 #include <string.h>
14
14
15 #include "util.h"
15 #include "util.h"
16
16
17 static char *versionerrortext = "Python minor version mismatch";
17 static char *versionerrortext = "Python minor version mismatch";
18
18
19 static int8_t hextable[256] = {
19 static int8_t hextable[256] = {
20 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
20 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
21 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
21 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
22 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
22 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
23 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0-9 */
23 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0-9 */
24 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* A-F */
24 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* A-F */
25 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
25 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
26 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a-f */
26 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a-f */
27 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
27 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
28 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
28 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
29 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
29 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
30 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
30 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
31 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
31 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
32 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
32 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
35 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
36 };
36 };
37
37
38 static inline int hexdigit(const char *p, Py_ssize_t off)
38 static inline int hexdigit(const char *p, Py_ssize_t off)
39 {
39 {
40 int8_t val = hextable[(unsigned char)p[off]];
40 int8_t val = hextable[(unsigned char)p[off]];
41
41
42 if (val >= 0) {
42 if (val >= 0) {
43 return val;
43 return val;
44 }
44 }
45
45
46 PyErr_SetString(PyExc_ValueError, "input contains non-hex character");
46 PyErr_SetString(PyExc_ValueError, "input contains non-hex character");
47 return 0;
47 return 0;
48 }
48 }
49
49
50 /*
50 /*
51 * Turn a hex-encoded string into binary.
51 * Turn a hex-encoded string into binary.
52 */
52 */
53 static PyObject *unhexlify(const char *str, int len)
53 static PyObject *unhexlify(const char *str, int len)
54 {
54 {
55 PyObject *ret;
55 PyObject *ret;
56 char *d;
56 char *d;
57 int i;
57 int i;
58
58
59 ret = PyBytes_FromStringAndSize(NULL, len / 2);
59 ret = PyBytes_FromStringAndSize(NULL, len / 2);
60
60
61 if (!ret)
61 if (!ret)
62 return NULL;
62 return NULL;
63
63
64 d = PyBytes_AsString(ret);
64 d = PyBytes_AsString(ret);
65
65
66 for (i = 0; i < len;) {
66 for (i = 0; i < len;) {
67 int hi = hexdigit(str, i++);
67 int hi = hexdigit(str, i++);
68 int lo = hexdigit(str, i++);
68 int lo = hexdigit(str, i++);
69 *d++ = (hi << 4) | lo;
69 *d++ = (hi << 4) | lo;
70 }
70 }
71
71
72 return ret;
72 return ret;
73 }
73 }
74
74
75 /*
75 /*
76 * This code assumes that a manifest is stitched together with newline
76 * This code assumes that a manifest is stitched together with newline
77 * ('\n') characters.
77 * ('\n') characters.
78 */
78 */
79 static PyObject *parse_manifest(PyObject *self, PyObject *args)
79 static PyObject *parse_manifest(PyObject *self, PyObject *args)
80 {
80 {
81 PyObject *mfdict, *fdict;
81 PyObject *mfdict, *fdict;
82 char *str, *start, *end;
82 char *str, *start, *end;
83 int len;
83 int len;
84
84
85 if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest",
85 if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest",
86 &PyDict_Type, &mfdict,
86 &PyDict_Type, &mfdict,
87 &PyDict_Type, &fdict,
87 &PyDict_Type, &fdict,
88 &str, &len))
88 &str, &len))
89 goto quit;
89 goto quit;
90
90
91 start = str;
91 start = str;
92 end = str + len;
92 end = str + len;
93 while (start < end) {
93 while (start < end) {
94 PyObject *file = NULL, *node = NULL;
94 PyObject *file = NULL, *node = NULL;
95 PyObject *flags = NULL;
95 PyObject *flags = NULL;
96 char *zero = NULL, *newline = NULL;
96 char *zero = NULL, *newline = NULL;
97 ptrdiff_t nlen;
97 ptrdiff_t nlen;
98
98
99 zero = memchr(start, '\0', end - start);
99 zero = memchr(start, '\0', end - start);
100 if (!zero) {
100 if (!zero) {
101 PyErr_SetString(PyExc_ValueError,
101 PyErr_SetString(PyExc_ValueError,
102 "manifest entry has no separator");
102 "manifest entry has no separator");
103 goto quit;
103 goto quit;
104 }
104 }
105
105
106 newline = memchr(zero + 1, '\n', end - (zero + 1));
106 newline = memchr(zero + 1, '\n', end - (zero + 1));
107 if (!newline) {
107 if (!newline) {
108 PyErr_SetString(PyExc_ValueError,
108 PyErr_SetString(PyExc_ValueError,
109 "manifest contains trailing garbage");
109 "manifest contains trailing garbage");
110 goto quit;
110 goto quit;
111 }
111 }
112
112
113 file = PyBytes_FromStringAndSize(start, zero - start);
113 file = PyBytes_FromStringAndSize(start, zero - start);
114
114
115 if (!file)
115 if (!file)
116 goto bail;
116 goto bail;
117
117
118 nlen = newline - zero - 1;
118 nlen = newline - zero - 1;
119
119
120 node = unhexlify(zero + 1, nlen > 40 ? 40 : (int)nlen);
120 node = unhexlify(zero + 1, nlen > 40 ? 40 : (int)nlen);
121 if (!node)
121 if (!node)
122 goto bail;
122 goto bail;
123
123
124 if (nlen > 40) {
124 if (nlen > 40) {
125 flags = PyBytes_FromStringAndSize(zero + 41,
125 flags = PyBytes_FromStringAndSize(zero + 41,
126 nlen - 40);
126 nlen - 40);
127 if (!flags)
127 if (!flags)
128 goto bail;
128 goto bail;
129
129
130 if (PyDict_SetItem(fdict, file, flags) == -1)
130 if (PyDict_SetItem(fdict, file, flags) == -1)
131 goto bail;
131 goto bail;
132 }
132 }
133
133
134 if (PyDict_SetItem(mfdict, file, node) == -1)
134 if (PyDict_SetItem(mfdict, file, node) == -1)
135 goto bail;
135 goto bail;
136
136
137 start = newline + 1;
137 start = newline + 1;
138
138
139 Py_XDECREF(flags);
139 Py_XDECREF(flags);
140 Py_XDECREF(node);
140 Py_XDECREF(node);
141 Py_XDECREF(file);
141 Py_XDECREF(file);
142 continue;
142 continue;
143 bail:
143 bail:
144 Py_XDECREF(flags);
144 Py_XDECREF(flags);
145 Py_XDECREF(node);
145 Py_XDECREF(node);
146 Py_XDECREF(file);
146 Py_XDECREF(file);
147 goto quit;
147 goto quit;
148 }
148 }
149
149
150 Py_INCREF(Py_None);
150 Py_INCREF(Py_None);
151 return Py_None;
151 return Py_None;
152 quit:
152 quit:
153 return NULL;
153 return NULL;
154 }
154 }
155
155
156 static inline dirstateTupleObject *make_dirstate_tuple(char state, int mode,
156 static inline dirstateTupleObject *make_dirstate_tuple(char state, int mode,
157 int size, int mtime)
157 int size, int mtime)
158 {
158 {
159 dirstateTupleObject *t = PyObject_New(dirstateTupleObject,
159 dirstateTupleObject *t = PyObject_New(dirstateTupleObject,
160 &dirstateTupleType);
160 &dirstateTupleType);
161 if (!t)
161 if (!t)
162 return NULL;
162 return NULL;
163 t->state = state;
163 t->state = state;
164 t->mode = mode;
164 t->mode = mode;
165 t->size = size;
165 t->size = size;
166 t->mtime = mtime;
166 t->mtime = mtime;
167 return t;
167 return t;
168 }
168 }
169
169
170 static PyObject *dirstate_tuple_new(PyTypeObject *subtype, PyObject *args,
170 static PyObject *dirstate_tuple_new(PyTypeObject *subtype, PyObject *args,
171 PyObject *kwds)
171 PyObject *kwds)
172 {
172 {
173 /* We do all the initialization here and not a tp_init function because
173 /* We do all the initialization here and not a tp_init function because
174 * dirstate_tuple is immutable. */
174 * dirstate_tuple is immutable. */
175 dirstateTupleObject *t;
175 dirstateTupleObject *t;
176 char state;
176 char state;
177 int size, mode, mtime;
177 int size, mode, mtime;
178 if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime))
178 if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime))
179 return NULL;
179 return NULL;
180
180
181 t = (dirstateTupleObject *)subtype->tp_alloc(subtype, 1);
181 t = (dirstateTupleObject *)subtype->tp_alloc(subtype, 1);
182 if (!t)
182 if (!t)
183 return NULL;
183 return NULL;
184 t->state = state;
184 t->state = state;
185 t->mode = mode;
185 t->mode = mode;
186 t->size = size;
186 t->size = size;
187 t->mtime = mtime;
187 t->mtime = mtime;
188
188
189 return (PyObject *)t;
189 return (PyObject *)t;
190 }
190 }
191
191
192 static void dirstate_tuple_dealloc(PyObject *o)
192 static void dirstate_tuple_dealloc(PyObject *o)
193 {
193 {
194 PyObject_Del(o);
194 PyObject_Del(o);
195 }
195 }
196
196
197 static Py_ssize_t dirstate_tuple_length(PyObject *o)
197 static Py_ssize_t dirstate_tuple_length(PyObject *o)
198 {
198 {
199 return 4;
199 return 4;
200 }
200 }
201
201
202 static PyObject *dirstate_tuple_item(PyObject *o, Py_ssize_t i)
202 static PyObject *dirstate_tuple_item(PyObject *o, Py_ssize_t i)
203 {
203 {
204 dirstateTupleObject *t = (dirstateTupleObject *)o;
204 dirstateTupleObject *t = (dirstateTupleObject *)o;
205 switch (i) {
205 switch (i) {
206 case 0:
206 case 0:
207 return PyBytes_FromStringAndSize(&t->state, 1);
207 return PyBytes_FromStringAndSize(&t->state, 1);
208 case 1:
208 case 1:
209 return PyInt_FromLong(t->mode);
209 return PyInt_FromLong(t->mode);
210 case 2:
210 case 2:
211 return PyInt_FromLong(t->size);
211 return PyInt_FromLong(t->size);
212 case 3:
212 case 3:
213 return PyInt_FromLong(t->mtime);
213 return PyInt_FromLong(t->mtime);
214 default:
214 default:
215 PyErr_SetString(PyExc_IndexError, "index out of range");
215 PyErr_SetString(PyExc_IndexError, "index out of range");
216 return NULL;
216 return NULL;
217 }
217 }
218 }
218 }
219
219
220 static PySequenceMethods dirstate_tuple_sq = {
220 static PySequenceMethods dirstate_tuple_sq = {
221 dirstate_tuple_length, /* sq_length */
221 dirstate_tuple_length, /* sq_length */
222 0, /* sq_concat */
222 0, /* sq_concat */
223 0, /* sq_repeat */
223 0, /* sq_repeat */
224 dirstate_tuple_item, /* sq_item */
224 dirstate_tuple_item, /* sq_item */
225 0, /* sq_ass_item */
225 0, /* sq_ass_item */
226 0, /* sq_contains */
226 0, /* sq_contains */
227 0, /* sq_inplace_concat */
227 0, /* sq_inplace_concat */
228 0 /* sq_inplace_repeat */
228 0 /* sq_inplace_repeat */
229 };
229 };
230
230
231 PyTypeObject dirstateTupleType = {
231 PyTypeObject dirstateTupleType = {
232 PyVarObject_HEAD_INIT(NULL, 0)
232 PyVarObject_HEAD_INIT(NULL, 0)
233 "dirstate_tuple", /* tp_name */
233 "dirstate_tuple", /* tp_name */
234 sizeof(dirstateTupleObject),/* tp_basicsize */
234 sizeof(dirstateTupleObject),/* tp_basicsize */
235 0, /* tp_itemsize */
235 0, /* tp_itemsize */
236 (destructor)dirstate_tuple_dealloc, /* tp_dealloc */
236 (destructor)dirstate_tuple_dealloc, /* tp_dealloc */
237 0, /* tp_print */
237 0, /* tp_print */
238 0, /* tp_getattr */
238 0, /* tp_getattr */
239 0, /* tp_setattr */
239 0, /* tp_setattr */
240 0, /* tp_compare */
240 0, /* tp_compare */
241 0, /* tp_repr */
241 0, /* tp_repr */
242 0, /* tp_as_number */
242 0, /* tp_as_number */
243 &dirstate_tuple_sq, /* tp_as_sequence */
243 &dirstate_tuple_sq, /* tp_as_sequence */
244 0, /* tp_as_mapping */
244 0, /* tp_as_mapping */
245 0, /* tp_hash */
245 0, /* tp_hash */
246 0, /* tp_call */
246 0, /* tp_call */
247 0, /* tp_str */
247 0, /* tp_str */
248 0, /* tp_getattro */
248 0, /* tp_getattro */
249 0, /* tp_setattro */
249 0, /* tp_setattro */
250 0, /* tp_as_buffer */
250 0, /* tp_as_buffer */
251 Py_TPFLAGS_DEFAULT, /* tp_flags */
251 Py_TPFLAGS_DEFAULT, /* tp_flags */
252 "dirstate tuple", /* tp_doc */
252 "dirstate tuple", /* tp_doc */
253 0, /* tp_traverse */
253 0, /* tp_traverse */
254 0, /* tp_clear */
254 0, /* tp_clear */
255 0, /* tp_richcompare */
255 0, /* tp_richcompare */
256 0, /* tp_weaklistoffset */
256 0, /* tp_weaklistoffset */
257 0, /* tp_iter */
257 0, /* tp_iter */
258 0, /* tp_iternext */
258 0, /* tp_iternext */
259 0, /* tp_methods */
259 0, /* tp_methods */
260 0, /* tp_members */
260 0, /* tp_members */
261 0, /* tp_getset */
261 0, /* tp_getset */
262 0, /* tp_base */
262 0, /* tp_base */
263 0, /* tp_dict */
263 0, /* tp_dict */
264 0, /* tp_descr_get */
264 0, /* tp_descr_get */
265 0, /* tp_descr_set */
265 0, /* tp_descr_set */
266 0, /* tp_dictoffset */
266 0, /* tp_dictoffset */
267 0, /* tp_init */
267 0, /* tp_init */
268 0, /* tp_alloc */
268 0, /* tp_alloc */
269 dirstate_tuple_new, /* tp_new */
269 dirstate_tuple_new, /* tp_new */
270 };
270 };
271
271
272 static PyObject *parse_dirstate(PyObject *self, PyObject *args)
272 static PyObject *parse_dirstate(PyObject *self, PyObject *args)
273 {
273 {
274 PyObject *dmap, *cmap, *parents = NULL, *ret = NULL;
274 PyObject *dmap, *cmap, *parents = NULL, *ret = NULL;
275 PyObject *fname = NULL, *cname = NULL, *entry = NULL;
275 PyObject *fname = NULL, *cname = NULL, *entry = NULL;
276 char state, *cur, *str, *cpos;
276 char state, *cur, *str, *cpos;
277 int mode, size, mtime;
277 int mode, size, mtime;
278 unsigned int flen;
278 unsigned int flen;
279 int len, pos = 40;
279 int len, pos = 40;
280
280
281 if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate",
281 if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate",
282 &PyDict_Type, &dmap,
282 &PyDict_Type, &dmap,
283 &PyDict_Type, &cmap,
283 &PyDict_Type, &cmap,
284 &str, &len))
284 &str, &len))
285 goto quit;
285 goto quit;
286
286
287 /* read parents */
287 /* read parents */
288 if (len < 40)
288 if (len < 40)
289 goto quit;
289 goto quit;
290
290
291 parents = Py_BuildValue("s#s#", str, 20, str + 20, 20);
291 parents = Py_BuildValue("s#s#", str, 20, str + 20, 20);
292 if (!parents)
292 if (!parents)
293 goto quit;
293 goto quit;
294
294
295 /* read filenames */
295 /* read filenames */
296 while (pos >= 40 && pos < len) {
296 while (pos >= 40 && pos < len) {
297 cur = str + pos;
297 cur = str + pos;
298 /* unpack header */
298 /* unpack header */
299 state = *cur;
299 state = *cur;
300 mode = getbe32(cur + 1);
300 mode = getbe32(cur + 1);
301 size = getbe32(cur + 5);
301 size = getbe32(cur + 5);
302 mtime = getbe32(cur + 9);
302 mtime = getbe32(cur + 9);
303 flen = getbe32(cur + 13);
303 flen = getbe32(cur + 13);
304 pos += 17;
304 pos += 17;
305 cur += 17;
305 cur += 17;
306 if (flen > len - pos) {
306 if (flen > len - pos) {
307 PyErr_SetString(PyExc_ValueError, "overflow in dirstate");
307 PyErr_SetString(PyExc_ValueError, "overflow in dirstate");
308 goto quit;
308 goto quit;
309 }
309 }
310
310
311 entry = (PyObject *)make_dirstate_tuple(state, mode, size,
311 entry = (PyObject *)make_dirstate_tuple(state, mode, size,
312 mtime);
312 mtime);
313 cpos = memchr(cur, 0, flen);
313 cpos = memchr(cur, 0, flen);
314 if (cpos) {
314 if (cpos) {
315 fname = PyBytes_FromStringAndSize(cur, cpos - cur);
315 fname = PyBytes_FromStringAndSize(cur, cpos - cur);
316 cname = PyBytes_FromStringAndSize(cpos + 1,
316 cname = PyBytes_FromStringAndSize(cpos + 1,
317 flen - (cpos - cur) - 1);
317 flen - (cpos - cur) - 1);
318 if (!fname || !cname ||
318 if (!fname || !cname ||
319 PyDict_SetItem(cmap, fname, cname) == -1 ||
319 PyDict_SetItem(cmap, fname, cname) == -1 ||
320 PyDict_SetItem(dmap, fname, entry) == -1)
320 PyDict_SetItem(dmap, fname, entry) == -1)
321 goto quit;
321 goto quit;
322 Py_DECREF(cname);
322 Py_DECREF(cname);
323 } else {
323 } else {
324 fname = PyBytes_FromStringAndSize(cur, flen);
324 fname = PyBytes_FromStringAndSize(cur, flen);
325 if (!fname ||
325 if (!fname ||
326 PyDict_SetItem(dmap, fname, entry) == -1)
326 PyDict_SetItem(dmap, fname, entry) == -1)
327 goto quit;
327 goto quit;
328 }
328 }
329 Py_DECREF(fname);
329 Py_DECREF(fname);
330 Py_DECREF(entry);
330 Py_DECREF(entry);
331 fname = cname = entry = NULL;
331 fname = cname = entry = NULL;
332 pos += flen;
332 pos += flen;
333 }
333 }
334
334
335 ret = parents;
335 ret = parents;
336 Py_INCREF(ret);
336 Py_INCREF(ret);
337 quit:
337 quit:
338 Py_XDECREF(fname);
338 Py_XDECREF(fname);
339 Py_XDECREF(cname);
339 Py_XDECREF(cname);
340 Py_XDECREF(entry);
340 Py_XDECREF(entry);
341 Py_XDECREF(parents);
341 Py_XDECREF(parents);
342 return ret;
342 return ret;
343 }
343 }
344
344
345 /*
345 /*
346 * Efficiently pack a dirstate object into its on-disk format.
346 * Efficiently pack a dirstate object into its on-disk format.
347 */
347 */
348 static PyObject *pack_dirstate(PyObject *self, PyObject *args)
348 static PyObject *pack_dirstate(PyObject *self, PyObject *args)
349 {
349 {
350 PyObject *packobj = NULL;
350 PyObject *packobj = NULL;
351 PyObject *map, *copymap, *pl, *mtime_unset = NULL;
351 PyObject *map, *copymap, *pl, *mtime_unset = NULL;
352 Py_ssize_t nbytes, pos, l;
352 Py_ssize_t nbytes, pos, l;
353 PyObject *k, *v, *pn;
353 PyObject *k, *v, *pn;
354 char *p, *s;
354 char *p, *s;
355 double now;
355 double now;
356
356
357 if (!PyArg_ParseTuple(args, "O!O!Od:pack_dirstate",
357 if (!PyArg_ParseTuple(args, "O!O!Od:pack_dirstate",
358 &PyDict_Type, &map, &PyDict_Type, &copymap,
358 &PyDict_Type, &map, &PyDict_Type, &copymap,
359 &pl, &now))
359 &pl, &now))
360 return NULL;
360 return NULL;
361
361
362 if (!PySequence_Check(pl) || PySequence_Size(pl) != 2) {
362 if (!PySequence_Check(pl) || PySequence_Size(pl) != 2) {
363 PyErr_SetString(PyExc_TypeError, "expected 2-element sequence");
363 PyErr_SetString(PyExc_TypeError, "expected 2-element sequence");
364 return NULL;
364 return NULL;
365 }
365 }
366
366
367 /* Figure out how much we need to allocate. */
367 /* Figure out how much we need to allocate. */
368 for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) {
368 for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) {
369 PyObject *c;
369 PyObject *c;
370 if (!PyString_Check(k)) {
370 if (!PyString_Check(k)) {
371 PyErr_SetString(PyExc_TypeError, "expected string key");
371 PyErr_SetString(PyExc_TypeError, "expected string key");
372 goto bail;
372 goto bail;
373 }
373 }
374 nbytes += PyString_GET_SIZE(k) + 17;
374 nbytes += PyString_GET_SIZE(k) + 17;
375 c = PyDict_GetItem(copymap, k);
375 c = PyDict_GetItem(copymap, k);
376 if (c) {
376 if (c) {
377 if (!PyString_Check(c)) {
377 if (!PyString_Check(c)) {
378 PyErr_SetString(PyExc_TypeError,
378 PyErr_SetString(PyExc_TypeError,
379 "expected string key");
379 "expected string key");
380 goto bail;
380 goto bail;
381 }
381 }
382 nbytes += PyString_GET_SIZE(c) + 1;
382 nbytes += PyString_GET_SIZE(c) + 1;
383 }
383 }
384 }
384 }
385
385
386 packobj = PyString_FromStringAndSize(NULL, nbytes);
386 packobj = PyString_FromStringAndSize(NULL, nbytes);
387 if (packobj == NULL)
387 if (packobj == NULL)
388 goto bail;
388 goto bail;
389
389
390 p = PyString_AS_STRING(packobj);
390 p = PyString_AS_STRING(packobj);
391
391
392 pn = PySequence_ITEM(pl, 0);
392 pn = PySequence_ITEM(pl, 0);
393 if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
393 if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
394 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
394 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
395 goto bail;
395 goto bail;
396 }
396 }
397 memcpy(p, s, l);
397 memcpy(p, s, l);
398 p += 20;
398 p += 20;
399 pn = PySequence_ITEM(pl, 1);
399 pn = PySequence_ITEM(pl, 1);
400 if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
400 if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
401 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
401 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
402 goto bail;
402 goto bail;
403 }
403 }
404 memcpy(p, s, l);
404 memcpy(p, s, l);
405 p += 20;
405 p += 20;
406
406
407 for (pos = 0; PyDict_Next(map, &pos, &k, &v); ) {
407 for (pos = 0; PyDict_Next(map, &pos, &k, &v); ) {
408 dirstateTupleObject *tuple;
408 dirstateTupleObject *tuple;
409 char state;
409 char state;
410 uint32_t mode, size, mtime;
410 uint32_t mode, size, mtime;
411 Py_ssize_t len, l;
411 Py_ssize_t len, l;
412 PyObject *o;
412 PyObject *o;
413 char *t;
413 char *t;
414
414
415 if (!dirstate_tuple_check(v)) {
415 if (!dirstate_tuple_check(v)) {
416 PyErr_SetString(PyExc_TypeError,
416 PyErr_SetString(PyExc_TypeError,
417 "expected a dirstate tuple");
417 "expected a dirstate tuple");
418 goto bail;
418 goto bail;
419 }
419 }
420 tuple = (dirstateTupleObject *)v;
420 tuple = (dirstateTupleObject *)v;
421
421
422 state = tuple->state;
422 state = tuple->state;
423 mode = tuple->mode;
423 mode = tuple->mode;
424 size = tuple->size;
424 size = tuple->size;
425 mtime = tuple->mtime;
425 mtime = tuple->mtime;
426 if (state == 'n' && mtime == (uint32_t)now) {
426 if (state == 'n' && mtime == (uint32_t)now) {
427 /* See pure/parsers.py:pack_dirstate for why we do
427 /* See pure/parsers.py:pack_dirstate for why we do
428 * this. */
428 * this. */
429 mtime = -1;
429 mtime = -1;
430 mtime_unset = (PyObject *)make_dirstate_tuple(
430 mtime_unset = (PyObject *)make_dirstate_tuple(
431 state, mode, size, mtime);
431 state, mode, size, mtime);
432 if (!mtime_unset)
432 if (!mtime_unset)
433 goto bail;
433 goto bail;
434 if (PyDict_SetItem(map, k, mtime_unset) == -1)
434 if (PyDict_SetItem(map, k, mtime_unset) == -1)
435 goto bail;
435 goto bail;
436 Py_DECREF(mtime_unset);
436 Py_DECREF(mtime_unset);
437 mtime_unset = NULL;
437 mtime_unset = NULL;
438 }
438 }
439 *p++ = state;
439 *p++ = state;
440 putbe32(mode, p);
440 putbe32(mode, p);
441 putbe32(size, p + 4);
441 putbe32(size, p + 4);
442 putbe32(mtime, p + 8);
442 putbe32(mtime, p + 8);
443 t = p + 12;
443 t = p + 12;
444 p += 16;
444 p += 16;
445 len = PyString_GET_SIZE(k);
445 len = PyString_GET_SIZE(k);
446 memcpy(p, PyString_AS_STRING(k), len);
446 memcpy(p, PyString_AS_STRING(k), len);
447 p += len;
447 p += len;
448 o = PyDict_GetItem(copymap, k);
448 o = PyDict_GetItem(copymap, k);
449 if (o) {
449 if (o) {
450 *p++ = '\0';
450 *p++ = '\0';
451 l = PyString_GET_SIZE(o);
451 l = PyString_GET_SIZE(o);
452 memcpy(p, PyString_AS_STRING(o), l);
452 memcpy(p, PyString_AS_STRING(o), l);
453 p += l;
453 p += l;
454 len += l + 1;
454 len += l + 1;
455 }
455 }
456 putbe32((uint32_t)len, t);
456 putbe32((uint32_t)len, t);
457 }
457 }
458
458
459 pos = p - PyString_AS_STRING(packobj);
459 pos = p - PyString_AS_STRING(packobj);
460 if (pos != nbytes) {
460 if (pos != nbytes) {
461 PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld",
461 PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld",
462 (long)pos, (long)nbytes);
462 (long)pos, (long)nbytes);
463 goto bail;
463 goto bail;
464 }
464 }
465
465
466 return packobj;
466 return packobj;
467 bail:
467 bail:
468 Py_XDECREF(mtime_unset);
468 Py_XDECREF(mtime_unset);
469 Py_XDECREF(packobj);
469 Py_XDECREF(packobj);
470 return NULL;
470 return NULL;
471 }
471 }
472
472
473 /*
473 /*
474 * A base-16 trie for fast node->rev mapping.
474 * A base-16 trie for fast node->rev mapping.
475 *
475 *
476 * Positive value is index of the next node in the trie
476 * Positive value is index of the next node in the trie
477 * Negative value is a leaf: -(rev + 1)
477 * Negative value is a leaf: -(rev + 1)
478 * Zero is empty
478 * Zero is empty
479 */
479 */
480 typedef struct {
480 typedef struct {
481 int children[16];
481 int children[16];
482 } nodetree;
482 } nodetree;
483
483
484 /*
484 /*
485 * This class has two behaviours.
485 * This class has two behaviours.
486 *
486 *
487 * When used in a list-like way (with integer keys), we decode an
487 * When used in a list-like way (with integer keys), we decode an
488 * entry in a RevlogNG index file on demand. Our last entry is a
488 * entry in a RevlogNG index file on demand. Our last entry is a
489 * sentinel, always a nullid. We have limited support for
489 * sentinel, always a nullid. We have limited support for
490 * integer-keyed insert and delete, only at elements right before the
490 * integer-keyed insert and delete, only at elements right before the
491 * sentinel.
491 * sentinel.
492 *
492 *
493 * With string keys, we lazily perform a reverse mapping from node to
493 * With string keys, we lazily perform a reverse mapping from node to
494 * rev, using a base-16 trie.
494 * rev, using a base-16 trie.
495 */
495 */
496 typedef struct {
496 typedef struct {
497 PyObject_HEAD
497 PyObject_HEAD
498 /* Type-specific fields go here. */
498 /* Type-specific fields go here. */
499 PyObject *data; /* raw bytes of index */
499 PyObject *data; /* raw bytes of index */
500 PyObject **cache; /* cached tuples */
500 PyObject **cache; /* cached tuples */
501 const char **offsets; /* populated on demand */
501 const char **offsets; /* populated on demand */
502 Py_ssize_t raw_length; /* original number of elements */
502 Py_ssize_t raw_length; /* original number of elements */
503 Py_ssize_t length; /* current number of elements */
503 Py_ssize_t length; /* current number of elements */
504 PyObject *added; /* populated on demand */
504 PyObject *added; /* populated on demand */
505 PyObject *headrevs; /* cache, invalidated on changes */
505 PyObject *headrevs; /* cache, invalidated on changes */
506 nodetree *nt; /* base-16 trie */
506 nodetree *nt; /* base-16 trie */
507 int ntlength; /* # nodes in use */
507 int ntlength; /* # nodes in use */
508 int ntcapacity; /* # nodes allocated */
508 int ntcapacity; /* # nodes allocated */
509 int ntdepth; /* maximum depth of tree */
509 int ntdepth; /* maximum depth of tree */
510 int ntsplits; /* # splits performed */
510 int ntsplits; /* # splits performed */
511 int ntrev; /* last rev scanned */
511 int ntrev; /* last rev scanned */
512 int ntlookups; /* # lookups */
512 int ntlookups; /* # lookups */
513 int ntmisses; /* # lookups that miss the cache */
513 int ntmisses; /* # lookups that miss the cache */
514 int inlined;
514 int inlined;
515 } indexObject;
515 } indexObject;
516
516
517 static Py_ssize_t index_length(const indexObject *self)
517 static Py_ssize_t index_length(const indexObject *self)
518 {
518 {
519 if (self->added == NULL)
519 if (self->added == NULL)
520 return self->length;
520 return self->length;
521 return self->length + PyList_GET_SIZE(self->added);
521 return self->length + PyList_GET_SIZE(self->added);
522 }
522 }
523
523
524 static PyObject *nullentry;
524 static PyObject *nullentry;
525 static const char nullid[20];
525 static const char nullid[20];
526
526
527 static long inline_scan(indexObject *self, const char **offsets);
527 static long inline_scan(indexObject *self, const char **offsets);
528
528
529 #if LONG_MAX == 0x7fffffffL
529 #if LONG_MAX == 0x7fffffffL
530 static char *tuple_format = "Kiiiiiis#";
530 static char *tuple_format = "Kiiiiiis#";
531 #else
531 #else
532 static char *tuple_format = "kiiiiiis#";
532 static char *tuple_format = "kiiiiiis#";
533 #endif
533 #endif
534
534
535 /* A RevlogNG v1 index entry is 64 bytes long. */
535 /* A RevlogNG v1 index entry is 64 bytes long. */
536 static const long v1_hdrsize = 64;
536 static const long v1_hdrsize = 64;
537
537
538 /*
538 /*
539 * Return a pointer to the beginning of a RevlogNG record.
539 * Return a pointer to the beginning of a RevlogNG record.
540 */
540 */
541 static const char *index_deref(indexObject *self, Py_ssize_t pos)
541 static const char *index_deref(indexObject *self, Py_ssize_t pos)
542 {
542 {
543 if (self->inlined && pos > 0) {
543 if (self->inlined && pos > 0) {
544 if (self->offsets == NULL) {
544 if (self->offsets == NULL) {
545 self->offsets = malloc(self->raw_length *
545 self->offsets = malloc(self->raw_length *
546 sizeof(*self->offsets));
546 sizeof(*self->offsets));
547 if (self->offsets == NULL)
547 if (self->offsets == NULL)
548 return (const char *)PyErr_NoMemory();
548 return (const char *)PyErr_NoMemory();
549 inline_scan(self, self->offsets);
549 inline_scan(self, self->offsets);
550 }
550 }
551 return self->offsets[pos];
551 return self->offsets[pos];
552 }
552 }
553
553
554 return PyString_AS_STRING(self->data) + pos * v1_hdrsize;
554 return PyString_AS_STRING(self->data) + pos * v1_hdrsize;
555 }
555 }
556
556
557 /*
557 /*
558 * RevlogNG format (all in big endian, data may be inlined):
558 * RevlogNG format (all in big endian, data may be inlined):
559 * 6 bytes: offset
559 * 6 bytes: offset
560 * 2 bytes: flags
560 * 2 bytes: flags
561 * 4 bytes: compressed length
561 * 4 bytes: compressed length
562 * 4 bytes: uncompressed length
562 * 4 bytes: uncompressed length
563 * 4 bytes: base revision
563 * 4 bytes: base revision
564 * 4 bytes: link revision
564 * 4 bytes: link revision
565 * 4 bytes: parent 1 revision
565 * 4 bytes: parent 1 revision
566 * 4 bytes: parent 2 revision
566 * 4 bytes: parent 2 revision
567 * 32 bytes: nodeid (only 20 bytes used)
567 * 32 bytes: nodeid (only 20 bytes used)
568 */
568 */
569 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
569 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
570 {
570 {
571 uint64_t offset_flags;
571 uint64_t offset_flags;
572 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
572 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
573 const char *c_node_id;
573 const char *c_node_id;
574 const char *data;
574 const char *data;
575 Py_ssize_t length = index_length(self);
575 Py_ssize_t length = index_length(self);
576 PyObject *entry;
576 PyObject *entry;
577
577
578 if (pos < 0)
578 if (pos < 0)
579 pos += length;
579 pos += length;
580
580
581 if (pos < 0 || pos >= length) {
581 if (pos < 0 || pos >= length) {
582 PyErr_SetString(PyExc_IndexError, "revlog index out of range");
582 PyErr_SetString(PyExc_IndexError, "revlog index out of range");
583 return NULL;
583 return NULL;
584 }
584 }
585
585
586 if (pos == length - 1) {
586 if (pos == length - 1) {
587 Py_INCREF(nullentry);
587 Py_INCREF(nullentry);
588 return nullentry;
588 return nullentry;
589 }
589 }
590
590
591 if (pos >= self->length - 1) {
591 if (pos >= self->length - 1) {
592 PyObject *obj;
592 PyObject *obj;
593 obj = PyList_GET_ITEM(self->added, pos - self->length + 1);
593 obj = PyList_GET_ITEM(self->added, pos - self->length + 1);
594 Py_INCREF(obj);
594 Py_INCREF(obj);
595 return obj;
595 return obj;
596 }
596 }
597
597
598 if (self->cache) {
598 if (self->cache) {
599 if (self->cache[pos]) {
599 if (self->cache[pos]) {
600 Py_INCREF(self->cache[pos]);
600 Py_INCREF(self->cache[pos]);
601 return self->cache[pos];
601 return self->cache[pos];
602 }
602 }
603 } else {
603 } else {
604 self->cache = calloc(self->raw_length, sizeof(PyObject *));
604 self->cache = calloc(self->raw_length, sizeof(PyObject *));
605 if (self->cache == NULL)
605 if (self->cache == NULL)
606 return PyErr_NoMemory();
606 return PyErr_NoMemory();
607 }
607 }
608
608
609 data = index_deref(self, pos);
609 data = index_deref(self, pos);
610 if (data == NULL)
610 if (data == NULL)
611 return NULL;
611 return NULL;
612
612
613 offset_flags = getbe32(data + 4);
613 offset_flags = getbe32(data + 4);
614 if (pos == 0) /* mask out version number for the first entry */
614 if (pos == 0) /* mask out version number for the first entry */
615 offset_flags &= 0xFFFF;
615 offset_flags &= 0xFFFF;
616 else {
616 else {
617 uint32_t offset_high = getbe32(data);
617 uint32_t offset_high = getbe32(data);
618 offset_flags |= ((uint64_t)offset_high) << 32;
618 offset_flags |= ((uint64_t)offset_high) << 32;
619 }
619 }
620
620
621 comp_len = getbe32(data + 8);
621 comp_len = getbe32(data + 8);
622 uncomp_len = getbe32(data + 12);
622 uncomp_len = getbe32(data + 12);
623 base_rev = getbe32(data + 16);
623 base_rev = getbe32(data + 16);
624 link_rev = getbe32(data + 20);
624 link_rev = getbe32(data + 20);
625 parent_1 = getbe32(data + 24);
625 parent_1 = getbe32(data + 24);
626 parent_2 = getbe32(data + 28);
626 parent_2 = getbe32(data + 28);
627 c_node_id = data + 32;
627 c_node_id = data + 32;
628
628
629 entry = Py_BuildValue(tuple_format, offset_flags, comp_len,
629 entry = Py_BuildValue(tuple_format, offset_flags, comp_len,
630 uncomp_len, base_rev, link_rev,
630 uncomp_len, base_rev, link_rev,
631 parent_1, parent_2, c_node_id, 20);
631 parent_1, parent_2, c_node_id, 20);
632
632
633 if (entry) {
633 if (entry) {
634 PyObject_GC_UnTrack(entry);
634 PyObject_GC_UnTrack(entry);
635 Py_INCREF(entry);
635 Py_INCREF(entry);
636 }
636 }
637
637
638 self->cache[pos] = entry;
638 self->cache[pos] = entry;
639
639
640 return entry;
640 return entry;
641 }
641 }
642
642
643 /*
643 /*
644 * Return the 20-byte SHA of the node corresponding to the given rev.
644 * Return the 20-byte SHA of the node corresponding to the given rev.
645 */
645 */
646 static const char *index_node(indexObject *self, Py_ssize_t pos)
646 static const char *index_node(indexObject *self, Py_ssize_t pos)
647 {
647 {
648 Py_ssize_t length = index_length(self);
648 Py_ssize_t length = index_length(self);
649 const char *data;
649 const char *data;
650
650
651 if (pos == length - 1 || pos == INT_MAX)
651 if (pos == length - 1 || pos == INT_MAX)
652 return nullid;
652 return nullid;
653
653
654 if (pos >= length)
654 if (pos >= length)
655 return NULL;
655 return NULL;
656
656
657 if (pos >= self->length - 1) {
657 if (pos >= self->length - 1) {
658 PyObject *tuple, *str;
658 PyObject *tuple, *str;
659 tuple = PyList_GET_ITEM(self->added, pos - self->length + 1);
659 tuple = PyList_GET_ITEM(self->added, pos - self->length + 1);
660 str = PyTuple_GetItem(tuple, 7);
660 str = PyTuple_GetItem(tuple, 7);
661 return str ? PyString_AS_STRING(str) : NULL;
661 return str ? PyString_AS_STRING(str) : NULL;
662 }
662 }
663
663
664 data = index_deref(self, pos);
664 data = index_deref(self, pos);
665 return data ? data + 32 : NULL;
665 return data ? data + 32 : NULL;
666 }
666 }
667
667
668 static int nt_insert(indexObject *self, const char *node, int rev);
668 static int nt_insert(indexObject *self, const char *node, int rev);
669
669
670 static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen)
670 static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen)
671 {
671 {
672 if (PyString_AsStringAndSize(obj, node, nodelen) == -1)
672 if (PyString_AsStringAndSize(obj, node, nodelen) == -1)
673 return -1;
673 return -1;
674 if (*nodelen == 20)
674 if (*nodelen == 20)
675 return 0;
675 return 0;
676 PyErr_SetString(PyExc_ValueError, "20-byte hash required");
676 PyErr_SetString(PyExc_ValueError, "20-byte hash required");
677 return -1;
677 return -1;
678 }
678 }
679
679
680 static PyObject *index_insert(indexObject *self, PyObject *args)
680 static PyObject *index_insert(indexObject *self, PyObject *args)
681 {
681 {
682 PyObject *obj;
682 PyObject *obj;
683 char *node;
683 char *node;
684 long offset;
684 long offset;
685 Py_ssize_t len, nodelen;
685 Py_ssize_t len, nodelen;
686
686
687 if (!PyArg_ParseTuple(args, "lO", &offset, &obj))
687 if (!PyArg_ParseTuple(args, "lO", &offset, &obj))
688 return NULL;
688 return NULL;
689
689
690 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
690 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
691 PyErr_SetString(PyExc_TypeError, "8-tuple required");
691 PyErr_SetString(PyExc_TypeError, "8-tuple required");
692 return NULL;
692 return NULL;
693 }
693 }
694
694
695 if (node_check(PyTuple_GET_ITEM(obj, 7), &node, &nodelen) == -1)
695 if (node_check(PyTuple_GET_ITEM(obj, 7), &node, &nodelen) == -1)
696 return NULL;
696 return NULL;
697
697
698 len = index_length(self);
698 len = index_length(self);
699
699
700 if (offset < 0)
700 if (offset < 0)
701 offset += len;
701 offset += len;
702
702
703 if (offset != len - 1) {
703 if (offset != len - 1) {
704 PyErr_SetString(PyExc_IndexError,
704 PyErr_SetString(PyExc_IndexError,
705 "insert only supported at index -1");
705 "insert only supported at index -1");
706 return NULL;
706 return NULL;
707 }
707 }
708
708
709 if (offset > INT_MAX) {
709 if (offset > INT_MAX) {
710 PyErr_SetString(PyExc_ValueError,
710 PyErr_SetString(PyExc_ValueError,
711 "currently only 2**31 revs supported");
711 "currently only 2**31 revs supported");
712 return NULL;
712 return NULL;
713 }
713 }
714
714
715 if (self->added == NULL) {
715 if (self->added == NULL) {
716 self->added = PyList_New(0);
716 self->added = PyList_New(0);
717 if (self->added == NULL)
717 if (self->added == NULL)
718 return NULL;
718 return NULL;
719 }
719 }
720
720
721 if (PyList_Append(self->added, obj) == -1)
721 if (PyList_Append(self->added, obj) == -1)
722 return NULL;
722 return NULL;
723
723
724 if (self->nt)
724 if (self->nt)
725 nt_insert(self, node, (int)offset);
725 nt_insert(self, node, (int)offset);
726
726
727 Py_CLEAR(self->headrevs);
727 Py_CLEAR(self->headrevs);
728 Py_RETURN_NONE;
728 Py_RETURN_NONE;
729 }
729 }
730
730
731 static void _index_clearcaches(indexObject *self)
731 static void _index_clearcaches(indexObject *self)
732 {
732 {
733 if (self->cache) {
733 if (self->cache) {
734 Py_ssize_t i;
734 Py_ssize_t i;
735
735
736 for (i = 0; i < self->raw_length; i++)
736 for (i = 0; i < self->raw_length; i++)
737 Py_CLEAR(self->cache[i]);
737 Py_CLEAR(self->cache[i]);
738 free(self->cache);
738 free(self->cache);
739 self->cache = NULL;
739 self->cache = NULL;
740 }
740 }
741 if (self->offsets) {
741 if (self->offsets) {
742 free(self->offsets);
742 free(self->offsets);
743 self->offsets = NULL;
743 self->offsets = NULL;
744 }
744 }
745 if (self->nt) {
745 if (self->nt) {
746 free(self->nt);
746 free(self->nt);
747 self->nt = NULL;
747 self->nt = NULL;
748 }
748 }
749 Py_CLEAR(self->headrevs);
749 Py_CLEAR(self->headrevs);
750 }
750 }
751
751
752 static PyObject *index_clearcaches(indexObject *self)
752 static PyObject *index_clearcaches(indexObject *self)
753 {
753 {
754 _index_clearcaches(self);
754 _index_clearcaches(self);
755 self->ntlength = self->ntcapacity = 0;
755 self->ntlength = self->ntcapacity = 0;
756 self->ntdepth = self->ntsplits = 0;
756 self->ntdepth = self->ntsplits = 0;
757 self->ntrev = -1;
757 self->ntrev = -1;
758 self->ntlookups = self->ntmisses = 0;
758 self->ntlookups = self->ntmisses = 0;
759 Py_RETURN_NONE;
759 Py_RETURN_NONE;
760 }
760 }
761
761
762 static PyObject *index_stats(indexObject *self)
762 static PyObject *index_stats(indexObject *self)
763 {
763 {
764 PyObject *obj = PyDict_New();
764 PyObject *obj = PyDict_New();
765
765
766 if (obj == NULL)
766 if (obj == NULL)
767 return NULL;
767 return NULL;
768
768
769 #define istat(__n, __d) \
769 #define istat(__n, __d) \
770 if (PyDict_SetItemString(obj, __d, PyInt_FromSsize_t(self->__n)) == -1) \
770 if (PyDict_SetItemString(obj, __d, PyInt_FromSsize_t(self->__n)) == -1) \
771 goto bail;
771 goto bail;
772
772
773 if (self->added) {
773 if (self->added) {
774 Py_ssize_t len = PyList_GET_SIZE(self->added);
774 Py_ssize_t len = PyList_GET_SIZE(self->added);
775 if (PyDict_SetItemString(obj, "index entries added",
775 if (PyDict_SetItemString(obj, "index entries added",
776 PyInt_FromSsize_t(len)) == -1)
776 PyInt_FromSsize_t(len)) == -1)
777 goto bail;
777 goto bail;
778 }
778 }
779
779
780 if (self->raw_length != self->length - 1)
780 if (self->raw_length != self->length - 1)
781 istat(raw_length, "revs on disk");
781 istat(raw_length, "revs on disk");
782 istat(length, "revs in memory");
782 istat(length, "revs in memory");
783 istat(ntcapacity, "node trie capacity");
783 istat(ntcapacity, "node trie capacity");
784 istat(ntdepth, "node trie depth");
784 istat(ntdepth, "node trie depth");
785 istat(ntlength, "node trie count");
785 istat(ntlength, "node trie count");
786 istat(ntlookups, "node trie lookups");
786 istat(ntlookups, "node trie lookups");
787 istat(ntmisses, "node trie misses");
787 istat(ntmisses, "node trie misses");
788 istat(ntrev, "node trie last rev scanned");
788 istat(ntrev, "node trie last rev scanned");
789 istat(ntsplits, "node trie splits");
789 istat(ntsplits, "node trie splits");
790
790
791 #undef istat
791 #undef istat
792
792
793 return obj;
793 return obj;
794
794
795 bail:
795 bail:
796 Py_XDECREF(obj);
796 Py_XDECREF(obj);
797 return NULL;
797 return NULL;
798 }
798 }
799
799
800 /*
800 /*
801 * When we cache a list, we want to be sure the caller can't mutate
801 * When we cache a list, we want to be sure the caller can't mutate
802 * the cached copy.
802 * the cached copy.
803 */
803 */
804 static PyObject *list_copy(PyObject *list)
804 static PyObject *list_copy(PyObject *list)
805 {
805 {
806 Py_ssize_t len = PyList_GET_SIZE(list);
806 Py_ssize_t len = PyList_GET_SIZE(list);
807 PyObject *newlist = PyList_New(len);
807 PyObject *newlist = PyList_New(len);
808 Py_ssize_t i;
808 Py_ssize_t i;
809
809
810 if (newlist == NULL)
810 if (newlist == NULL)
811 return NULL;
811 return NULL;
812
812
813 for (i = 0; i < len; i++) {
813 for (i = 0; i < len; i++) {
814 PyObject *obj = PyList_GET_ITEM(list, i);
814 PyObject *obj = PyList_GET_ITEM(list, i);
815 Py_INCREF(obj);
815 Py_INCREF(obj);
816 PyList_SET_ITEM(newlist, i, obj);
816 PyList_SET_ITEM(newlist, i, obj);
817 }
817 }
818
818
819 return newlist;
819 return newlist;
820 }
820 }
821
821
822 static PyObject *index_headrevs(indexObject *self)
822 static PyObject *index_headrevs(indexObject *self)
823 {
823 {
824 Py_ssize_t i, len, addlen;
824 Py_ssize_t i, len, addlen;
825 char *nothead = NULL;
825 char *nothead = NULL;
826 PyObject *heads;
826 PyObject *heads;
827
827
828 if (self->headrevs)
828 if (self->headrevs)
829 return list_copy(self->headrevs);
829 return list_copy(self->headrevs);
830
830
831 len = index_length(self) - 1;
831 len = index_length(self) - 1;
832 heads = PyList_New(0);
832 heads = PyList_New(0);
833 if (heads == NULL)
833 if (heads == NULL)
834 goto bail;
834 goto bail;
835 if (len == 0) {
835 if (len == 0) {
836 PyObject *nullid = PyInt_FromLong(-1);
836 PyObject *nullid = PyInt_FromLong(-1);
837 if (nullid == NULL || PyList_Append(heads, nullid) == -1) {
837 if (nullid == NULL || PyList_Append(heads, nullid) == -1) {
838 Py_XDECREF(nullid);
838 Py_XDECREF(nullid);
839 goto bail;
839 goto bail;
840 }
840 }
841 goto done;
841 goto done;
842 }
842 }
843
843
844 nothead = calloc(len, 1);
844 nothead = calloc(len, 1);
845 if (nothead == NULL)
845 if (nothead == NULL)
846 goto bail;
846 goto bail;
847
847
848 for (i = 0; i < self->raw_length; i++) {
848 for (i = 0; i < self->raw_length; i++) {
849 const char *data = index_deref(self, i);
849 const char *data = index_deref(self, i);
850 int parent_1 = getbe32(data + 24);
850 int parent_1 = getbe32(data + 24);
851 int parent_2 = getbe32(data + 28);
851 int parent_2 = getbe32(data + 28);
852 if (parent_1 >= 0)
852 if (parent_1 >= 0)
853 nothead[parent_1] = 1;
853 nothead[parent_1] = 1;
854 if (parent_2 >= 0)
854 if (parent_2 >= 0)
855 nothead[parent_2] = 1;
855 nothead[parent_2] = 1;
856 }
856 }
857
857
858 addlen = self->added ? PyList_GET_SIZE(self->added) : 0;
858 addlen = self->added ? PyList_GET_SIZE(self->added) : 0;
859
859
860 for (i = 0; i < addlen; i++) {
860 for (i = 0; i < addlen; i++) {
861 PyObject *rev = PyList_GET_ITEM(self->added, i);
861 PyObject *rev = PyList_GET_ITEM(self->added, i);
862 PyObject *p1 = PyTuple_GET_ITEM(rev, 5);
862 PyObject *p1 = PyTuple_GET_ITEM(rev, 5);
863 PyObject *p2 = PyTuple_GET_ITEM(rev, 6);
863 PyObject *p2 = PyTuple_GET_ITEM(rev, 6);
864 long parent_1, parent_2;
864 long parent_1, parent_2;
865
865
866 if (!PyInt_Check(p1) || !PyInt_Check(p2)) {
866 if (!PyInt_Check(p1) || !PyInt_Check(p2)) {
867 PyErr_SetString(PyExc_TypeError,
867 PyErr_SetString(PyExc_TypeError,
868 "revlog parents are invalid");
868 "revlog parents are invalid");
869 goto bail;
869 goto bail;
870 }
870 }
871 parent_1 = PyInt_AS_LONG(p1);
871 parent_1 = PyInt_AS_LONG(p1);
872 parent_2 = PyInt_AS_LONG(p2);
872 parent_2 = PyInt_AS_LONG(p2);
873 if (parent_1 >= 0)
873 if (parent_1 >= 0)
874 nothead[parent_1] = 1;
874 nothead[parent_1] = 1;
875 if (parent_2 >= 0)
875 if (parent_2 >= 0)
876 nothead[parent_2] = 1;
876 nothead[parent_2] = 1;
877 }
877 }
878
878
879 for (i = 0; i < len; i++) {
879 for (i = 0; i < len; i++) {
880 PyObject *head;
880 PyObject *head;
881
881
882 if (nothead[i])
882 if (nothead[i])
883 continue;
883 continue;
884 head = PyInt_FromLong(i);
884 head = PyInt_FromSsize_t(i);
885 if (head == NULL || PyList_Append(heads, head) == -1) {
885 if (head == NULL || PyList_Append(heads, head) == -1) {
886 Py_XDECREF(head);
886 Py_XDECREF(head);
887 goto bail;
887 goto bail;
888 }
888 }
889 }
889 }
890
890
891 done:
891 done:
892 self->headrevs = heads;
892 self->headrevs = heads;
893 free(nothead);
893 free(nothead);
894 return list_copy(self->headrevs);
894 return list_copy(self->headrevs);
895 bail:
895 bail:
896 Py_XDECREF(heads);
896 Py_XDECREF(heads);
897 free(nothead);
897 free(nothead);
898 return NULL;
898 return NULL;
899 }
899 }
900
900
901 static inline int nt_level(const char *node, Py_ssize_t level)
901 static inline int nt_level(const char *node, Py_ssize_t level)
902 {
902 {
903 int v = node[level>>1];
903 int v = node[level>>1];
904 if (!(level & 1))
904 if (!(level & 1))
905 v >>= 4;
905 v >>= 4;
906 return v & 0xf;
906 return v & 0xf;
907 }
907 }
908
908
909 /*
909 /*
910 * Return values:
910 * Return values:
911 *
911 *
912 * -4: match is ambiguous (multiple candidates)
912 * -4: match is ambiguous (multiple candidates)
913 * -2: not found
913 * -2: not found
914 * rest: valid rev
914 * rest: valid rev
915 */
915 */
916 static int nt_find(indexObject *self, const char *node, Py_ssize_t nodelen,
916 static int nt_find(indexObject *self, const char *node, Py_ssize_t nodelen,
917 int hex)
917 int hex)
918 {
918 {
919 int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level;
919 int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level;
920 int level, maxlevel, off;
920 int level, maxlevel, off;
921
921
922 if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0)
922 if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0)
923 return -1;
923 return -1;
924
924
925 if (self->nt == NULL)
925 if (self->nt == NULL)
926 return -2;
926 return -2;
927
927
928 if (hex)
928 if (hex)
929 maxlevel = nodelen > 40 ? 40 : (int)nodelen;
929 maxlevel = nodelen > 40 ? 40 : (int)nodelen;
930 else
930 else
931 maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2);
931 maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2);
932
932
933 for (level = off = 0; level < maxlevel; level++) {
933 for (level = off = 0; level < maxlevel; level++) {
934 int k = getnybble(node, level);
934 int k = getnybble(node, level);
935 nodetree *n = &self->nt[off];
935 nodetree *n = &self->nt[off];
936 int v = n->children[k];
936 int v = n->children[k];
937
937
938 if (v < 0) {
938 if (v < 0) {
939 const char *n;
939 const char *n;
940 Py_ssize_t i;
940 Py_ssize_t i;
941
941
942 v = -v - 1;
942 v = -v - 1;
943 n = index_node(self, v);
943 n = index_node(self, v);
944 if (n == NULL)
944 if (n == NULL)
945 return -2;
945 return -2;
946 for (i = level; i < maxlevel; i++)
946 for (i = level; i < maxlevel; i++)
947 if (getnybble(node, i) != nt_level(n, i))
947 if (getnybble(node, i) != nt_level(n, i))
948 return -2;
948 return -2;
949 return v;
949 return v;
950 }
950 }
951 if (v == 0)
951 if (v == 0)
952 return -2;
952 return -2;
953 off = v;
953 off = v;
954 }
954 }
955 /* multiple matches against an ambiguous prefix */
955 /* multiple matches against an ambiguous prefix */
956 return -4;
956 return -4;
957 }
957 }
958
958
959 static int nt_new(indexObject *self)
959 static int nt_new(indexObject *self)
960 {
960 {
961 if (self->ntlength == self->ntcapacity) {
961 if (self->ntlength == self->ntcapacity) {
962 self->ntcapacity *= 2;
962 self->ntcapacity *= 2;
963 self->nt = realloc(self->nt,
963 self->nt = realloc(self->nt,
964 self->ntcapacity * sizeof(nodetree));
964 self->ntcapacity * sizeof(nodetree));
965 if (self->nt == NULL) {
965 if (self->nt == NULL) {
966 PyErr_SetString(PyExc_MemoryError, "out of memory");
966 PyErr_SetString(PyExc_MemoryError, "out of memory");
967 return -1;
967 return -1;
968 }
968 }
969 memset(&self->nt[self->ntlength], 0,
969 memset(&self->nt[self->ntlength], 0,
970 sizeof(nodetree) * (self->ntcapacity - self->ntlength));
970 sizeof(nodetree) * (self->ntcapacity - self->ntlength));
971 }
971 }
972 return self->ntlength++;
972 return self->ntlength++;
973 }
973 }
974
974
975 static int nt_insert(indexObject *self, const char *node, int rev)
975 static int nt_insert(indexObject *self, const char *node, int rev)
976 {
976 {
977 int level = 0;
977 int level = 0;
978 int off = 0;
978 int off = 0;
979
979
980 while (level < 40) {
980 while (level < 40) {
981 int k = nt_level(node, level);
981 int k = nt_level(node, level);
982 nodetree *n;
982 nodetree *n;
983 int v;
983 int v;
984
984
985 n = &self->nt[off];
985 n = &self->nt[off];
986 v = n->children[k];
986 v = n->children[k];
987
987
988 if (v == 0) {
988 if (v == 0) {
989 n->children[k] = -rev - 1;
989 n->children[k] = -rev - 1;
990 return 0;
990 return 0;
991 }
991 }
992 if (v < 0) {
992 if (v < 0) {
993 const char *oldnode = index_node(self, -v - 1);
993 const char *oldnode = index_node(self, -v - 1);
994 int noff;
994 int noff;
995
995
996 if (!oldnode || !memcmp(oldnode, node, 20)) {
996 if (!oldnode || !memcmp(oldnode, node, 20)) {
997 n->children[k] = -rev - 1;
997 n->children[k] = -rev - 1;
998 return 0;
998 return 0;
999 }
999 }
1000 noff = nt_new(self);
1000 noff = nt_new(self);
1001 if (noff == -1)
1001 if (noff == -1)
1002 return -1;
1002 return -1;
1003 /* self->nt may have been changed by realloc */
1003 /* self->nt may have been changed by realloc */
1004 self->nt[off].children[k] = noff;
1004 self->nt[off].children[k] = noff;
1005 off = noff;
1005 off = noff;
1006 n = &self->nt[off];
1006 n = &self->nt[off];
1007 n->children[nt_level(oldnode, ++level)] = v;
1007 n->children[nt_level(oldnode, ++level)] = v;
1008 if (level > self->ntdepth)
1008 if (level > self->ntdepth)
1009 self->ntdepth = level;
1009 self->ntdepth = level;
1010 self->ntsplits += 1;
1010 self->ntsplits += 1;
1011 } else {
1011 } else {
1012 level += 1;
1012 level += 1;
1013 off = v;
1013 off = v;
1014 }
1014 }
1015 }
1015 }
1016
1016
1017 return -1;
1017 return -1;
1018 }
1018 }
1019
1019
1020 static int nt_init(indexObject *self)
1020 static int nt_init(indexObject *self)
1021 {
1021 {
1022 if (self->nt == NULL) {
1022 if (self->nt == NULL) {
1023 if (self->raw_length > INT_MAX) {
1023 if (self->raw_length > INT_MAX) {
1024 PyErr_SetString(PyExc_ValueError, "overflow in nt_init");
1024 PyErr_SetString(PyExc_ValueError, "overflow in nt_init");
1025 return -1;
1025 return -1;
1026 }
1026 }
1027 self->ntcapacity = self->raw_length < 4
1027 self->ntcapacity = self->raw_length < 4
1028 ? 4 : (int)self->raw_length / 2;
1028 ? 4 : (int)self->raw_length / 2;
1029
1029
1030 self->nt = calloc(self->ntcapacity, sizeof(nodetree));
1030 self->nt = calloc(self->ntcapacity, sizeof(nodetree));
1031 if (self->nt == NULL) {
1031 if (self->nt == NULL) {
1032 PyErr_NoMemory();
1032 PyErr_NoMemory();
1033 return -1;
1033 return -1;
1034 }
1034 }
1035 self->ntlength = 1;
1035 self->ntlength = 1;
1036 self->ntrev = (int)index_length(self) - 1;
1036 self->ntrev = (int)index_length(self) - 1;
1037 self->ntlookups = 1;
1037 self->ntlookups = 1;
1038 self->ntmisses = 0;
1038 self->ntmisses = 0;
1039 if (nt_insert(self, nullid, INT_MAX) == -1)
1039 if (nt_insert(self, nullid, INT_MAX) == -1)
1040 return -1;
1040 return -1;
1041 }
1041 }
1042 return 0;
1042 return 0;
1043 }
1043 }
1044
1044
1045 /*
1045 /*
1046 * Return values:
1046 * Return values:
1047 *
1047 *
1048 * -3: error (exception set)
1048 * -3: error (exception set)
1049 * -2: not found (no exception set)
1049 * -2: not found (no exception set)
1050 * rest: valid rev
1050 * rest: valid rev
1051 */
1051 */
1052 static int index_find_node(indexObject *self,
1052 static int index_find_node(indexObject *self,
1053 const char *node, Py_ssize_t nodelen)
1053 const char *node, Py_ssize_t nodelen)
1054 {
1054 {
1055 int rev;
1055 int rev;
1056
1056
1057 self->ntlookups++;
1057 self->ntlookups++;
1058 rev = nt_find(self, node, nodelen, 0);
1058 rev = nt_find(self, node, nodelen, 0);
1059 if (rev >= -1)
1059 if (rev >= -1)
1060 return rev;
1060 return rev;
1061
1061
1062 if (nt_init(self) == -1)
1062 if (nt_init(self) == -1)
1063 return -3;
1063 return -3;
1064
1064
1065 /*
1065 /*
1066 * For the first handful of lookups, we scan the entire index,
1066 * For the first handful of lookups, we scan the entire index,
1067 * and cache only the matching nodes. This optimizes for cases
1067 * and cache only the matching nodes. This optimizes for cases
1068 * like "hg tip", where only a few nodes are accessed.
1068 * like "hg tip", where only a few nodes are accessed.
1069 *
1069 *
1070 * After that, we cache every node we visit, using a single
1070 * After that, we cache every node we visit, using a single
1071 * scan amortized over multiple lookups. This gives the best
1071 * scan amortized over multiple lookups. This gives the best
1072 * bulk performance, e.g. for "hg log".
1072 * bulk performance, e.g. for "hg log".
1073 */
1073 */
1074 if (self->ntmisses++ < 4) {
1074 if (self->ntmisses++ < 4) {
1075 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1075 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1076 const char *n = index_node(self, rev);
1076 const char *n = index_node(self, rev);
1077 if (n == NULL)
1077 if (n == NULL)
1078 return -2;
1078 return -2;
1079 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1079 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1080 if (nt_insert(self, n, rev) == -1)
1080 if (nt_insert(self, n, rev) == -1)
1081 return -3;
1081 return -3;
1082 break;
1082 break;
1083 }
1083 }
1084 }
1084 }
1085 } else {
1085 } else {
1086 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1086 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1087 const char *n = index_node(self, rev);
1087 const char *n = index_node(self, rev);
1088 if (n == NULL) {
1088 if (n == NULL) {
1089 self->ntrev = rev + 1;
1089 self->ntrev = rev + 1;
1090 return -2;
1090 return -2;
1091 }
1091 }
1092 if (nt_insert(self, n, rev) == -1) {
1092 if (nt_insert(self, n, rev) == -1) {
1093 self->ntrev = rev + 1;
1093 self->ntrev = rev + 1;
1094 return -3;
1094 return -3;
1095 }
1095 }
1096 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1096 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1097 break;
1097 break;
1098 }
1098 }
1099 }
1099 }
1100 self->ntrev = rev;
1100 self->ntrev = rev;
1101 }
1101 }
1102
1102
1103 if (rev >= 0)
1103 if (rev >= 0)
1104 return rev;
1104 return rev;
1105 return -2;
1105 return -2;
1106 }
1106 }
1107
1107
1108 static PyObject *raise_revlog_error(void)
1108 static PyObject *raise_revlog_error(void)
1109 {
1109 {
1110 static PyObject *errclass;
1110 static PyObject *errclass;
1111 PyObject *mod = NULL, *errobj;
1111 PyObject *mod = NULL, *errobj;
1112
1112
1113 if (errclass == NULL) {
1113 if (errclass == NULL) {
1114 PyObject *dict;
1114 PyObject *dict;
1115
1115
1116 mod = PyImport_ImportModule("mercurial.error");
1116 mod = PyImport_ImportModule("mercurial.error");
1117 if (mod == NULL)
1117 if (mod == NULL)
1118 goto classfail;
1118 goto classfail;
1119
1119
1120 dict = PyModule_GetDict(mod);
1120 dict = PyModule_GetDict(mod);
1121 if (dict == NULL)
1121 if (dict == NULL)
1122 goto classfail;
1122 goto classfail;
1123
1123
1124 errclass = PyDict_GetItemString(dict, "RevlogError");
1124 errclass = PyDict_GetItemString(dict, "RevlogError");
1125 if (errclass == NULL) {
1125 if (errclass == NULL) {
1126 PyErr_SetString(PyExc_SystemError,
1126 PyErr_SetString(PyExc_SystemError,
1127 "could not find RevlogError");
1127 "could not find RevlogError");
1128 goto classfail;
1128 goto classfail;
1129 }
1129 }
1130 Py_INCREF(errclass);
1130 Py_INCREF(errclass);
1131 }
1131 }
1132
1132
1133 errobj = PyObject_CallFunction(errclass, NULL);
1133 errobj = PyObject_CallFunction(errclass, NULL);
1134 if (errobj == NULL)
1134 if (errobj == NULL)
1135 return NULL;
1135 return NULL;
1136 PyErr_SetObject(errclass, errobj);
1136 PyErr_SetObject(errclass, errobj);
1137 return errobj;
1137 return errobj;
1138
1138
1139 classfail:
1139 classfail:
1140 Py_XDECREF(mod);
1140 Py_XDECREF(mod);
1141 return NULL;
1141 return NULL;
1142 }
1142 }
1143
1143
1144 static PyObject *index_getitem(indexObject *self, PyObject *value)
1144 static PyObject *index_getitem(indexObject *self, PyObject *value)
1145 {
1145 {
1146 char *node;
1146 char *node;
1147 Py_ssize_t nodelen;
1147 Py_ssize_t nodelen;
1148 int rev;
1148 int rev;
1149
1149
1150 if (PyInt_Check(value))
1150 if (PyInt_Check(value))
1151 return index_get(self, PyInt_AS_LONG(value));
1151 return index_get(self, PyInt_AS_LONG(value));
1152
1152
1153 if (node_check(value, &node, &nodelen) == -1)
1153 if (node_check(value, &node, &nodelen) == -1)
1154 return NULL;
1154 return NULL;
1155 rev = index_find_node(self, node, nodelen);
1155 rev = index_find_node(self, node, nodelen);
1156 if (rev >= -1)
1156 if (rev >= -1)
1157 return PyInt_FromLong(rev);
1157 return PyInt_FromLong(rev);
1158 if (rev == -2)
1158 if (rev == -2)
1159 raise_revlog_error();
1159 raise_revlog_error();
1160 return NULL;
1160 return NULL;
1161 }
1161 }
1162
1162
1163 static int nt_partialmatch(indexObject *self, const char *node,
1163 static int nt_partialmatch(indexObject *self, const char *node,
1164 Py_ssize_t nodelen)
1164 Py_ssize_t nodelen)
1165 {
1165 {
1166 int rev;
1166 int rev;
1167
1167
1168 if (nt_init(self) == -1)
1168 if (nt_init(self) == -1)
1169 return -3;
1169 return -3;
1170
1170
1171 if (self->ntrev > 0) {
1171 if (self->ntrev > 0) {
1172 /* ensure that the radix tree is fully populated */
1172 /* ensure that the radix tree is fully populated */
1173 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1173 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1174 const char *n = index_node(self, rev);
1174 const char *n = index_node(self, rev);
1175 if (n == NULL)
1175 if (n == NULL)
1176 return -2;
1176 return -2;
1177 if (nt_insert(self, n, rev) == -1)
1177 if (nt_insert(self, n, rev) == -1)
1178 return -3;
1178 return -3;
1179 }
1179 }
1180 self->ntrev = rev;
1180 self->ntrev = rev;
1181 }
1181 }
1182
1182
1183 return nt_find(self, node, nodelen, 1);
1183 return nt_find(self, node, nodelen, 1);
1184 }
1184 }
1185
1185
1186 static PyObject *index_partialmatch(indexObject *self, PyObject *args)
1186 static PyObject *index_partialmatch(indexObject *self, PyObject *args)
1187 {
1187 {
1188 const char *fullnode;
1188 const char *fullnode;
1189 int nodelen;
1189 int nodelen;
1190 char *node;
1190 char *node;
1191 int rev, i;
1191 int rev, i;
1192
1192
1193 if (!PyArg_ParseTuple(args, "s#", &node, &nodelen))
1193 if (!PyArg_ParseTuple(args, "s#", &node, &nodelen))
1194 return NULL;
1194 return NULL;
1195
1195
1196 if (nodelen < 4) {
1196 if (nodelen < 4) {
1197 PyErr_SetString(PyExc_ValueError, "key too short");
1197 PyErr_SetString(PyExc_ValueError, "key too short");
1198 return NULL;
1198 return NULL;
1199 }
1199 }
1200
1200
1201 if (nodelen > 40) {
1201 if (nodelen > 40) {
1202 PyErr_SetString(PyExc_ValueError, "key too long");
1202 PyErr_SetString(PyExc_ValueError, "key too long");
1203 return NULL;
1203 return NULL;
1204 }
1204 }
1205
1205
1206 for (i = 0; i < nodelen; i++)
1206 for (i = 0; i < nodelen; i++)
1207 hexdigit(node, i);
1207 hexdigit(node, i);
1208 if (PyErr_Occurred()) {
1208 if (PyErr_Occurred()) {
1209 /* input contains non-hex characters */
1209 /* input contains non-hex characters */
1210 PyErr_Clear();
1210 PyErr_Clear();
1211 Py_RETURN_NONE;
1211 Py_RETURN_NONE;
1212 }
1212 }
1213
1213
1214 rev = nt_partialmatch(self, node, nodelen);
1214 rev = nt_partialmatch(self, node, nodelen);
1215
1215
1216 switch (rev) {
1216 switch (rev) {
1217 case -4:
1217 case -4:
1218 raise_revlog_error();
1218 raise_revlog_error();
1219 case -3:
1219 case -3:
1220 return NULL;
1220 return NULL;
1221 case -2:
1221 case -2:
1222 Py_RETURN_NONE;
1222 Py_RETURN_NONE;
1223 case -1:
1223 case -1:
1224 return PyString_FromStringAndSize(nullid, 20);
1224 return PyString_FromStringAndSize(nullid, 20);
1225 }
1225 }
1226
1226
1227 fullnode = index_node(self, rev);
1227 fullnode = index_node(self, rev);
1228 if (fullnode == NULL) {
1228 if (fullnode == NULL) {
1229 PyErr_Format(PyExc_IndexError,
1229 PyErr_Format(PyExc_IndexError,
1230 "could not access rev %d", rev);
1230 "could not access rev %d", rev);
1231 return NULL;
1231 return NULL;
1232 }
1232 }
1233 return PyString_FromStringAndSize(fullnode, 20);
1233 return PyString_FromStringAndSize(fullnode, 20);
1234 }
1234 }
1235
1235
1236 static PyObject *index_m_get(indexObject *self, PyObject *args)
1236 static PyObject *index_m_get(indexObject *self, PyObject *args)
1237 {
1237 {
1238 Py_ssize_t nodelen;
1238 Py_ssize_t nodelen;
1239 PyObject *val;
1239 PyObject *val;
1240 char *node;
1240 char *node;
1241 int rev;
1241 int rev;
1242
1242
1243 if (!PyArg_ParseTuple(args, "O", &val))
1243 if (!PyArg_ParseTuple(args, "O", &val))
1244 return NULL;
1244 return NULL;
1245 if (node_check(val, &node, &nodelen) == -1)
1245 if (node_check(val, &node, &nodelen) == -1)
1246 return NULL;
1246 return NULL;
1247 rev = index_find_node(self, node, nodelen);
1247 rev = index_find_node(self, node, nodelen);
1248 if (rev == -3)
1248 if (rev == -3)
1249 return NULL;
1249 return NULL;
1250 if (rev == -2)
1250 if (rev == -2)
1251 Py_RETURN_NONE;
1251 Py_RETURN_NONE;
1252 return PyInt_FromLong(rev);
1252 return PyInt_FromLong(rev);
1253 }
1253 }
1254
1254
1255 static int index_contains(indexObject *self, PyObject *value)
1255 static int index_contains(indexObject *self, PyObject *value)
1256 {
1256 {
1257 char *node;
1257 char *node;
1258 Py_ssize_t nodelen;
1258 Py_ssize_t nodelen;
1259
1259
1260 if (PyInt_Check(value)) {
1260 if (PyInt_Check(value)) {
1261 long rev = PyInt_AS_LONG(value);
1261 long rev = PyInt_AS_LONG(value);
1262 return rev >= -1 && rev < index_length(self);
1262 return rev >= -1 && rev < index_length(self);
1263 }
1263 }
1264
1264
1265 if (node_check(value, &node, &nodelen) == -1)
1265 if (node_check(value, &node, &nodelen) == -1)
1266 return -1;
1266 return -1;
1267
1267
1268 switch (index_find_node(self, node, nodelen)) {
1268 switch (index_find_node(self, node, nodelen)) {
1269 case -3:
1269 case -3:
1270 return -1;
1270 return -1;
1271 case -2:
1271 case -2:
1272 return 0;
1272 return 0;
1273 default:
1273 default:
1274 return 1;
1274 return 1;
1275 }
1275 }
1276 }
1276 }
1277
1277
1278 static inline void index_get_parents(indexObject *self, int rev, int *ps)
1278 static inline void index_get_parents(indexObject *self, int rev, int *ps)
1279 {
1279 {
1280 if (rev >= self->length - 1) {
1280 if (rev >= self->length - 1) {
1281 PyObject *tuple = PyList_GET_ITEM(self->added,
1281 PyObject *tuple = PyList_GET_ITEM(self->added,
1282 rev - self->length + 1);
1282 rev - self->length + 1);
1283 ps[0] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 5));
1283 ps[0] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 5));
1284 ps[1] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 6));
1284 ps[1] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 6));
1285 } else {
1285 } else {
1286 const char *data = index_deref(self, rev);
1286 const char *data = index_deref(self, rev);
1287 ps[0] = getbe32(data + 24);
1287 ps[0] = getbe32(data + 24);
1288 ps[1] = getbe32(data + 28);
1288 ps[1] = getbe32(data + 28);
1289 }
1289 }
1290 }
1290 }
1291
1291
1292 typedef uint64_t bitmask;
1292 typedef uint64_t bitmask;
1293
1293
1294 /*
1294 /*
1295 * Given a disjoint set of revs, return all candidates for the
1295 * Given a disjoint set of revs, return all candidates for the
1296 * greatest common ancestor. In revset notation, this is the set
1296 * greatest common ancestor. In revset notation, this is the set
1297 * "heads(::a and ::b and ...)"
1297 * "heads(::a and ::b and ...)"
1298 */
1298 */
1299 static PyObject *find_gca_candidates(indexObject *self, const int *revs,
1299 static PyObject *find_gca_candidates(indexObject *self, const int *revs,
1300 int revcount)
1300 int revcount)
1301 {
1301 {
1302 const bitmask allseen = (1ull << revcount) - 1;
1302 const bitmask allseen = (1ull << revcount) - 1;
1303 const bitmask poison = 1ull << revcount;
1303 const bitmask poison = 1ull << revcount;
1304 PyObject *gca = PyList_New(0);
1304 PyObject *gca = PyList_New(0);
1305 int i, v, interesting;
1305 int i, v, interesting;
1306 int maxrev = -1;
1306 int maxrev = -1;
1307 bitmask sp;
1307 bitmask sp;
1308 bitmask *seen;
1308 bitmask *seen;
1309
1309
1310 if (gca == NULL)
1310 if (gca == NULL)
1311 return PyErr_NoMemory();
1311 return PyErr_NoMemory();
1312
1312
1313 for (i = 0; i < revcount; i++) {
1313 for (i = 0; i < revcount; i++) {
1314 if (revs[i] > maxrev)
1314 if (revs[i] > maxrev)
1315 maxrev = revs[i];
1315 maxrev = revs[i];
1316 }
1316 }
1317
1317
1318 seen = calloc(sizeof(*seen), maxrev + 1);
1318 seen = calloc(sizeof(*seen), maxrev + 1);
1319 if (seen == NULL) {
1319 if (seen == NULL) {
1320 Py_DECREF(gca);
1320 Py_DECREF(gca);
1321 return PyErr_NoMemory();
1321 return PyErr_NoMemory();
1322 }
1322 }
1323
1323
1324 for (i = 0; i < revcount; i++)
1324 for (i = 0; i < revcount; i++)
1325 seen[revs[i]] = 1ull << i;
1325 seen[revs[i]] = 1ull << i;
1326
1326
1327 interesting = revcount;
1327 interesting = revcount;
1328
1328
1329 for (v = maxrev; v >= 0 && interesting; v--) {
1329 for (v = maxrev; v >= 0 && interesting; v--) {
1330 bitmask sv = seen[v];
1330 bitmask sv = seen[v];
1331 int parents[2];
1331 int parents[2];
1332
1332
1333 if (!sv)
1333 if (!sv)
1334 continue;
1334 continue;
1335
1335
1336 if (sv < poison) {
1336 if (sv < poison) {
1337 interesting -= 1;
1337 interesting -= 1;
1338 if (sv == allseen) {
1338 if (sv == allseen) {
1339 PyObject *obj = PyInt_FromLong(v);
1339 PyObject *obj = PyInt_FromLong(v);
1340 if (obj == NULL)
1340 if (obj == NULL)
1341 goto bail;
1341 goto bail;
1342 if (PyList_Append(gca, obj) == -1) {
1342 if (PyList_Append(gca, obj) == -1) {
1343 Py_DECREF(obj);
1343 Py_DECREF(obj);
1344 goto bail;
1344 goto bail;
1345 }
1345 }
1346 sv |= poison;
1346 sv |= poison;
1347 for (i = 0; i < revcount; i++) {
1347 for (i = 0; i < revcount; i++) {
1348 if (revs[i] == v)
1348 if (revs[i] == v)
1349 goto done;
1349 goto done;
1350 }
1350 }
1351 }
1351 }
1352 }
1352 }
1353 index_get_parents(self, v, parents);
1353 index_get_parents(self, v, parents);
1354
1354
1355 for (i = 0; i < 2; i++) {
1355 for (i = 0; i < 2; i++) {
1356 int p = parents[i];
1356 int p = parents[i];
1357 if (p == -1)
1357 if (p == -1)
1358 continue;
1358 continue;
1359 sp = seen[p];
1359 sp = seen[p];
1360 if (sv < poison) {
1360 if (sv < poison) {
1361 if (sp == 0) {
1361 if (sp == 0) {
1362 seen[p] = sv;
1362 seen[p] = sv;
1363 interesting++;
1363 interesting++;
1364 }
1364 }
1365 else if (sp != sv)
1365 else if (sp != sv)
1366 seen[p] |= sv;
1366 seen[p] |= sv;
1367 } else {
1367 } else {
1368 if (sp && sp < poison)
1368 if (sp && sp < poison)
1369 interesting--;
1369 interesting--;
1370 seen[p] = sv;
1370 seen[p] = sv;
1371 }
1371 }
1372 }
1372 }
1373 }
1373 }
1374
1374
1375 done:
1375 done:
1376 free(seen);
1376 free(seen);
1377 return gca;
1377 return gca;
1378 bail:
1378 bail:
1379 free(seen);
1379 free(seen);
1380 Py_XDECREF(gca);
1380 Py_XDECREF(gca);
1381 return NULL;
1381 return NULL;
1382 }
1382 }
1383
1383
1384 /*
1384 /*
1385 * Given a disjoint set of revs, return the subset with the longest
1385 * Given a disjoint set of revs, return the subset with the longest
1386 * path to the root.
1386 * path to the root.
1387 */
1387 */
1388 static PyObject *find_deepest(indexObject *self, PyObject *revs)
1388 static PyObject *find_deepest(indexObject *self, PyObject *revs)
1389 {
1389 {
1390 const Py_ssize_t revcount = PyList_GET_SIZE(revs);
1390 const Py_ssize_t revcount = PyList_GET_SIZE(revs);
1391 static const Py_ssize_t capacity = 24;
1391 static const Py_ssize_t capacity = 24;
1392 int *depth, *interesting = NULL;
1392 int *depth, *interesting = NULL;
1393 int i, j, v, ninteresting;
1393 int i, j, v, ninteresting;
1394 PyObject *dict = NULL, *keys = NULL;
1394 PyObject *dict = NULL, *keys = NULL;
1395 long *seen = NULL;
1395 long *seen = NULL;
1396 int maxrev = -1;
1396 int maxrev = -1;
1397 long final;
1397 long final;
1398
1398
1399 if (revcount > capacity) {
1399 if (revcount > capacity) {
1400 PyErr_Format(PyExc_OverflowError,
1400 PyErr_Format(PyExc_OverflowError,
1401 "bitset size (%ld) > capacity (%ld)",
1401 "bitset size (%ld) > capacity (%ld)",
1402 (long)revcount, (long)capacity);
1402 (long)revcount, (long)capacity);
1403 return NULL;
1403 return NULL;
1404 }
1404 }
1405
1405
1406 for (i = 0; i < revcount; i++) {
1406 for (i = 0; i < revcount; i++) {
1407 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
1407 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
1408 if (n > maxrev)
1408 if (n > maxrev)
1409 maxrev = n;
1409 maxrev = n;
1410 }
1410 }
1411
1411
1412 depth = calloc(sizeof(*depth), maxrev + 1);
1412 depth = calloc(sizeof(*depth), maxrev + 1);
1413 if (depth == NULL)
1413 if (depth == NULL)
1414 return PyErr_NoMemory();
1414 return PyErr_NoMemory();
1415
1415
1416 seen = calloc(sizeof(*seen), maxrev + 1);
1416 seen = calloc(sizeof(*seen), maxrev + 1);
1417 if (seen == NULL) {
1417 if (seen == NULL) {
1418 PyErr_NoMemory();
1418 PyErr_NoMemory();
1419 goto bail;
1419 goto bail;
1420 }
1420 }
1421
1421
1422 interesting = calloc(sizeof(*interesting), 2 << revcount);
1422 interesting = calloc(sizeof(*interesting), 2 << revcount);
1423 if (interesting == NULL) {
1423 if (interesting == NULL) {
1424 PyErr_NoMemory();
1424 PyErr_NoMemory();
1425 goto bail;
1425 goto bail;
1426 }
1426 }
1427
1427
1428 if (PyList_Sort(revs) == -1)
1428 if (PyList_Sort(revs) == -1)
1429 goto bail;
1429 goto bail;
1430
1430
1431 for (i = 0; i < revcount; i++) {
1431 for (i = 0; i < revcount; i++) {
1432 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
1432 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
1433 long b = 1l << i;
1433 long b = 1l << i;
1434 depth[n] = 1;
1434 depth[n] = 1;
1435 seen[n] = b;
1435 seen[n] = b;
1436 interesting[b] = 1;
1436 interesting[b] = 1;
1437 }
1437 }
1438
1438
1439 ninteresting = (int)revcount;
1439 ninteresting = (int)revcount;
1440
1440
1441 for (v = maxrev; v >= 0 && ninteresting > 1; v--) {
1441 for (v = maxrev; v >= 0 && ninteresting > 1; v--) {
1442 int dv = depth[v];
1442 int dv = depth[v];
1443 int parents[2];
1443 int parents[2];
1444 long sv;
1444 long sv;
1445
1445
1446 if (dv == 0)
1446 if (dv == 0)
1447 continue;
1447 continue;
1448
1448
1449 sv = seen[v];
1449 sv = seen[v];
1450 index_get_parents(self, v, parents);
1450 index_get_parents(self, v, parents);
1451
1451
1452 for (i = 0; i < 2; i++) {
1452 for (i = 0; i < 2; i++) {
1453 int p = parents[i];
1453 int p = parents[i];
1454 long nsp, sp;
1454 long nsp, sp;
1455 int dp;
1455 int dp;
1456
1456
1457 if (p == -1)
1457 if (p == -1)
1458 continue;
1458 continue;
1459
1459
1460 dp = depth[p];
1460 dp = depth[p];
1461 nsp = sp = seen[p];
1461 nsp = sp = seen[p];
1462 if (dp <= dv) {
1462 if (dp <= dv) {
1463 depth[p] = dv + 1;
1463 depth[p] = dv + 1;
1464 if (sp != sv) {
1464 if (sp != sv) {
1465 interesting[sv] += 1;
1465 interesting[sv] += 1;
1466 nsp = seen[p] = sv;
1466 nsp = seen[p] = sv;
1467 if (sp) {
1467 if (sp) {
1468 interesting[sp] -= 1;
1468 interesting[sp] -= 1;
1469 if (interesting[sp] == 0)
1469 if (interesting[sp] == 0)
1470 ninteresting -= 1;
1470 ninteresting -= 1;
1471 }
1471 }
1472 }
1472 }
1473 }
1473 }
1474 else if (dv == dp - 1) {
1474 else if (dv == dp - 1) {
1475 nsp = sp | sv;
1475 nsp = sp | sv;
1476 if (nsp == sp)
1476 if (nsp == sp)
1477 continue;
1477 continue;
1478 seen[p] = nsp;
1478 seen[p] = nsp;
1479 interesting[sp] -= 1;
1479 interesting[sp] -= 1;
1480 if (interesting[sp] == 0 && interesting[nsp] > 0)
1480 if (interesting[sp] == 0 && interesting[nsp] > 0)
1481 ninteresting -= 1;
1481 ninteresting -= 1;
1482 interesting[nsp] += 1;
1482 interesting[nsp] += 1;
1483 }
1483 }
1484 }
1484 }
1485 interesting[sv] -= 1;
1485 interesting[sv] -= 1;
1486 if (interesting[sv] == 0)
1486 if (interesting[sv] == 0)
1487 ninteresting -= 1;
1487 ninteresting -= 1;
1488 }
1488 }
1489
1489
1490 final = 0;
1490 final = 0;
1491 j = ninteresting;
1491 j = ninteresting;
1492 for (i = 0; i < (int)(2 << revcount) && j > 0; i++) {
1492 for (i = 0; i < (int)(2 << revcount) && j > 0; i++) {
1493 if (interesting[i] == 0)
1493 if (interesting[i] == 0)
1494 continue;
1494 continue;
1495 final |= i;
1495 final |= i;
1496 j -= 1;
1496 j -= 1;
1497 }
1497 }
1498 if (final == 0) {
1498 if (final == 0) {
1499 keys = PyList_New(0);
1499 keys = PyList_New(0);
1500 goto bail;
1500 goto bail;
1501 }
1501 }
1502
1502
1503 dict = PyDict_New();
1503 dict = PyDict_New();
1504 if (dict == NULL)
1504 if (dict == NULL)
1505 goto bail;
1505 goto bail;
1506
1506
1507 for (i = 0; i < revcount; i++) {
1507 for (i = 0; i < revcount; i++) {
1508 PyObject *key;
1508 PyObject *key;
1509
1509
1510 if ((final & (1 << i)) == 0)
1510 if ((final & (1 << i)) == 0)
1511 continue;
1511 continue;
1512
1512
1513 key = PyList_GET_ITEM(revs, i);
1513 key = PyList_GET_ITEM(revs, i);
1514 Py_INCREF(key);
1514 Py_INCREF(key);
1515 Py_INCREF(Py_None);
1515 Py_INCREF(Py_None);
1516 if (PyDict_SetItem(dict, key, Py_None) == -1) {
1516 if (PyDict_SetItem(dict, key, Py_None) == -1) {
1517 Py_DECREF(key);
1517 Py_DECREF(key);
1518 Py_DECREF(Py_None);
1518 Py_DECREF(Py_None);
1519 goto bail;
1519 goto bail;
1520 }
1520 }
1521 }
1521 }
1522
1522
1523 keys = PyDict_Keys(dict);
1523 keys = PyDict_Keys(dict);
1524
1524
1525 bail:
1525 bail:
1526 free(depth);
1526 free(depth);
1527 free(seen);
1527 free(seen);
1528 free(interesting);
1528 free(interesting);
1529 Py_XDECREF(dict);
1529 Py_XDECREF(dict);
1530
1530
1531 return keys;
1531 return keys;
1532 }
1532 }
1533
1533
1534 /*
1534 /*
1535 * Given a (possibly overlapping) set of revs, return the greatest
1535 * Given a (possibly overlapping) set of revs, return the greatest
1536 * common ancestors: those with the longest path to the root.
1536 * common ancestors: those with the longest path to the root.
1537 */
1537 */
1538 static PyObject *index_ancestors(indexObject *self, PyObject *args)
1538 static PyObject *index_ancestors(indexObject *self, PyObject *args)
1539 {
1539 {
1540 PyObject *ret = NULL, *gca = NULL;
1540 PyObject *ret = NULL, *gca = NULL;
1541 Py_ssize_t argcount, i, len;
1541 Py_ssize_t argcount, i, len;
1542 bitmask repeat = 0;
1542 bitmask repeat = 0;
1543 int revcount = 0;
1543 int revcount = 0;
1544 int *revs;
1544 int *revs;
1545
1545
1546 argcount = PySequence_Length(args);
1546 argcount = PySequence_Length(args);
1547 revs = malloc(argcount * sizeof(*revs));
1547 revs = malloc(argcount * sizeof(*revs));
1548 if (argcount > 0 && revs == NULL)
1548 if (argcount > 0 && revs == NULL)
1549 return PyErr_NoMemory();
1549 return PyErr_NoMemory();
1550 len = index_length(self) - 1;
1550 len = index_length(self) - 1;
1551
1551
1552 for (i = 0; i < argcount; i++) {
1552 for (i = 0; i < argcount; i++) {
1553 static const int capacity = 24;
1553 static const int capacity = 24;
1554 PyObject *obj = PySequence_GetItem(args, i);
1554 PyObject *obj = PySequence_GetItem(args, i);
1555 bitmask x;
1555 bitmask x;
1556 long val;
1556 long val;
1557
1557
1558 if (!PyInt_Check(obj)) {
1558 if (!PyInt_Check(obj)) {
1559 PyErr_SetString(PyExc_TypeError,
1559 PyErr_SetString(PyExc_TypeError,
1560 "arguments must all be ints");
1560 "arguments must all be ints");
1561 goto bail;
1561 goto bail;
1562 }
1562 }
1563 val = PyInt_AsLong(obj);
1563 val = PyInt_AsLong(obj);
1564 if (val == -1) {
1564 if (val == -1) {
1565 ret = PyList_New(0);
1565 ret = PyList_New(0);
1566 goto done;
1566 goto done;
1567 }
1567 }
1568 if (val < 0 || val >= len) {
1568 if (val < 0 || val >= len) {
1569 PyErr_SetString(PyExc_IndexError,
1569 PyErr_SetString(PyExc_IndexError,
1570 "index out of range");
1570 "index out of range");
1571 goto bail;
1571 goto bail;
1572 }
1572 }
1573 /* this cheesy bloom filter lets us avoid some more
1573 /* this cheesy bloom filter lets us avoid some more
1574 * expensive duplicate checks in the common set-is-disjoint
1574 * expensive duplicate checks in the common set-is-disjoint
1575 * case */
1575 * case */
1576 x = 1ull << (val & 0x3f);
1576 x = 1ull << (val & 0x3f);
1577 if (repeat & x) {
1577 if (repeat & x) {
1578 int k;
1578 int k;
1579 for (k = 0; k < revcount; k++) {
1579 for (k = 0; k < revcount; k++) {
1580 if (val == revs[k])
1580 if (val == revs[k])
1581 goto duplicate;
1581 goto duplicate;
1582 }
1582 }
1583 }
1583 }
1584 else repeat |= x;
1584 else repeat |= x;
1585 if (revcount >= capacity) {
1585 if (revcount >= capacity) {
1586 PyErr_Format(PyExc_OverflowError,
1586 PyErr_Format(PyExc_OverflowError,
1587 "bitset size (%d) > capacity (%d)",
1587 "bitset size (%d) > capacity (%d)",
1588 revcount, capacity);
1588 revcount, capacity);
1589 goto bail;
1589 goto bail;
1590 }
1590 }
1591 revs[revcount++] = (int)val;
1591 revs[revcount++] = (int)val;
1592 duplicate:;
1592 duplicate:;
1593 }
1593 }
1594
1594
1595 if (revcount == 0) {
1595 if (revcount == 0) {
1596 ret = PyList_New(0);
1596 ret = PyList_New(0);
1597 goto done;
1597 goto done;
1598 }
1598 }
1599 if (revcount == 1) {
1599 if (revcount == 1) {
1600 PyObject *obj;
1600 PyObject *obj;
1601 ret = PyList_New(1);
1601 ret = PyList_New(1);
1602 if (ret == NULL)
1602 if (ret == NULL)
1603 goto bail;
1603 goto bail;
1604 obj = PyInt_FromLong(revs[0]);
1604 obj = PyInt_FromLong(revs[0]);
1605 if (obj == NULL)
1605 if (obj == NULL)
1606 goto bail;
1606 goto bail;
1607 PyList_SET_ITEM(ret, 0, obj);
1607 PyList_SET_ITEM(ret, 0, obj);
1608 goto done;
1608 goto done;
1609 }
1609 }
1610
1610
1611 gca = find_gca_candidates(self, revs, revcount);
1611 gca = find_gca_candidates(self, revs, revcount);
1612 if (gca == NULL)
1612 if (gca == NULL)
1613 goto bail;
1613 goto bail;
1614
1614
1615 if (PyList_GET_SIZE(gca) <= 1) {
1615 if (PyList_GET_SIZE(gca) <= 1) {
1616 ret = gca;
1616 ret = gca;
1617 Py_INCREF(gca);
1617 Py_INCREF(gca);
1618 }
1618 }
1619 else ret = find_deepest(self, gca);
1619 else ret = find_deepest(self, gca);
1620
1620
1621 done:
1621 done:
1622 free(revs);
1622 free(revs);
1623 Py_XDECREF(gca);
1623 Py_XDECREF(gca);
1624
1624
1625 return ret;
1625 return ret;
1626
1626
1627 bail:
1627 bail:
1628 free(revs);
1628 free(revs);
1629 Py_XDECREF(gca);
1629 Py_XDECREF(gca);
1630 Py_XDECREF(ret);
1630 Py_XDECREF(ret);
1631 return NULL;
1631 return NULL;
1632 }
1632 }
1633
1633
1634 /*
1634 /*
1635 * Given a (possibly overlapping) set of revs, return all the
1635 * Given a (possibly overlapping) set of revs, return all the
1636 * common ancestors heads: heads(::args[0] and ::a[1] and ...)
1636 * common ancestors heads: heads(::args[0] and ::a[1] and ...)
1637 */
1637 */
1638 static PyObject *index_commonancestorsheads(indexObject *self, PyObject *args)
1638 static PyObject *index_commonancestorsheads(indexObject *self, PyObject *args)
1639 {
1639 {
1640 PyObject *ret = NULL;
1640 PyObject *ret = NULL;
1641 Py_ssize_t argcount, i, len;
1641 Py_ssize_t argcount, i, len;
1642 bitmask repeat = 0;
1642 bitmask repeat = 0;
1643 int revcount = 0;
1643 int revcount = 0;
1644 int *revs;
1644 int *revs;
1645
1645
1646 argcount = PySequence_Length(args);
1646 argcount = PySequence_Length(args);
1647 revs = malloc(argcount * sizeof(*revs));
1647 revs = malloc(argcount * sizeof(*revs));
1648 if (argcount > 0 && revs == NULL)
1648 if (argcount > 0 && revs == NULL)
1649 return PyErr_NoMemory();
1649 return PyErr_NoMemory();
1650 len = index_length(self) - 1;
1650 len = index_length(self) - 1;
1651
1651
1652 for (i = 0; i < argcount; i++) {
1652 for (i = 0; i < argcount; i++) {
1653 static const int capacity = 24;
1653 static const int capacity = 24;
1654 PyObject *obj = PySequence_GetItem(args, i);
1654 PyObject *obj = PySequence_GetItem(args, i);
1655 bitmask x;
1655 bitmask x;
1656 long val;
1656 long val;
1657
1657
1658 if (!PyInt_Check(obj)) {
1658 if (!PyInt_Check(obj)) {
1659 PyErr_SetString(PyExc_TypeError,
1659 PyErr_SetString(PyExc_TypeError,
1660 "arguments must all be ints");
1660 "arguments must all be ints");
1661 goto bail;
1661 goto bail;
1662 }
1662 }
1663 val = PyInt_AsLong(obj);
1663 val = PyInt_AsLong(obj);
1664 if (val == -1) {
1664 if (val == -1) {
1665 ret = PyList_New(0);
1665 ret = PyList_New(0);
1666 goto done;
1666 goto done;
1667 }
1667 }
1668 if (val < 0 || val >= len) {
1668 if (val < 0 || val >= len) {
1669 PyErr_SetString(PyExc_IndexError,
1669 PyErr_SetString(PyExc_IndexError,
1670 "index out of range");
1670 "index out of range");
1671 goto bail;
1671 goto bail;
1672 }
1672 }
1673 /* this cheesy bloom filter lets us avoid some more
1673 /* this cheesy bloom filter lets us avoid some more
1674 * expensive duplicate checks in the common set-is-disjoint
1674 * expensive duplicate checks in the common set-is-disjoint
1675 * case */
1675 * case */
1676 x = 1ull << (val & 0x3f);
1676 x = 1ull << (val & 0x3f);
1677 if (repeat & x) {
1677 if (repeat & x) {
1678 int k;
1678 int k;
1679 for (k = 0; k < revcount; k++) {
1679 for (k = 0; k < revcount; k++) {
1680 if (val == revs[k])
1680 if (val == revs[k])
1681 goto duplicate;
1681 goto duplicate;
1682 }
1682 }
1683 }
1683 }
1684 else repeat |= x;
1684 else repeat |= x;
1685 if (revcount >= capacity) {
1685 if (revcount >= capacity) {
1686 PyErr_Format(PyExc_OverflowError,
1686 PyErr_Format(PyExc_OverflowError,
1687 "bitset size (%d) > capacity (%d)",
1687 "bitset size (%d) > capacity (%d)",
1688 revcount, capacity);
1688 revcount, capacity);
1689 goto bail;
1689 goto bail;
1690 }
1690 }
1691 revs[revcount++] = (int)val;
1691 revs[revcount++] = (int)val;
1692 duplicate:;
1692 duplicate:;
1693 }
1693 }
1694
1694
1695 if (revcount == 0) {
1695 if (revcount == 0) {
1696 ret = PyList_New(0);
1696 ret = PyList_New(0);
1697 goto done;
1697 goto done;
1698 }
1698 }
1699 if (revcount == 1) {
1699 if (revcount == 1) {
1700 PyObject *obj;
1700 PyObject *obj;
1701 ret = PyList_New(1);
1701 ret = PyList_New(1);
1702 if (ret == NULL)
1702 if (ret == NULL)
1703 goto bail;
1703 goto bail;
1704 obj = PyInt_FromLong(revs[0]);
1704 obj = PyInt_FromLong(revs[0]);
1705 if (obj == NULL)
1705 if (obj == NULL)
1706 goto bail;
1706 goto bail;
1707 PyList_SET_ITEM(ret, 0, obj);
1707 PyList_SET_ITEM(ret, 0, obj);
1708 goto done;
1708 goto done;
1709 }
1709 }
1710
1710
1711 ret = find_gca_candidates(self, revs, revcount);
1711 ret = find_gca_candidates(self, revs, revcount);
1712 if (ret == NULL)
1712 if (ret == NULL)
1713 goto bail;
1713 goto bail;
1714
1714
1715 done:
1715 done:
1716 free(revs);
1716 free(revs);
1717 return ret;
1717 return ret;
1718
1718
1719 bail:
1719 bail:
1720 free(revs);
1720 free(revs);
1721 Py_XDECREF(ret);
1721 Py_XDECREF(ret);
1722 return NULL;
1722 return NULL;
1723 }
1723 }
1724
1724
1725 /*
1725 /*
1726 * Invalidate any trie entries introduced by added revs.
1726 * Invalidate any trie entries introduced by added revs.
1727 */
1727 */
1728 static void nt_invalidate_added(indexObject *self, Py_ssize_t start)
1728 static void nt_invalidate_added(indexObject *self, Py_ssize_t start)
1729 {
1729 {
1730 Py_ssize_t i, len = PyList_GET_SIZE(self->added);
1730 Py_ssize_t i, len = PyList_GET_SIZE(self->added);
1731
1731
1732 for (i = start; i < len; i++) {
1732 for (i = start; i < len; i++) {
1733 PyObject *tuple = PyList_GET_ITEM(self->added, i);
1733 PyObject *tuple = PyList_GET_ITEM(self->added, i);
1734 PyObject *node = PyTuple_GET_ITEM(tuple, 7);
1734 PyObject *node = PyTuple_GET_ITEM(tuple, 7);
1735
1735
1736 nt_insert(self, PyString_AS_STRING(node), -1);
1736 nt_insert(self, PyString_AS_STRING(node), -1);
1737 }
1737 }
1738
1738
1739 if (start == 0)
1739 if (start == 0)
1740 Py_CLEAR(self->added);
1740 Py_CLEAR(self->added);
1741 }
1741 }
1742
1742
1743 /*
1743 /*
1744 * Delete a numeric range of revs, which must be at the end of the
1744 * Delete a numeric range of revs, which must be at the end of the
1745 * range, but exclude the sentinel nullid entry.
1745 * range, but exclude the sentinel nullid entry.
1746 */
1746 */
1747 static int index_slice_del(indexObject *self, PyObject *item)
1747 static int index_slice_del(indexObject *self, PyObject *item)
1748 {
1748 {
1749 Py_ssize_t start, stop, step, slicelength;
1749 Py_ssize_t start, stop, step, slicelength;
1750 Py_ssize_t length = index_length(self);
1750 Py_ssize_t length = index_length(self);
1751 int ret = 0;
1751 int ret = 0;
1752
1752
1753 if (PySlice_GetIndicesEx((PySliceObject*)item, length,
1753 if (PySlice_GetIndicesEx((PySliceObject*)item, length,
1754 &start, &stop, &step, &slicelength) < 0)
1754 &start, &stop, &step, &slicelength) < 0)
1755 return -1;
1755 return -1;
1756
1756
1757 if (slicelength <= 0)
1757 if (slicelength <= 0)
1758 return 0;
1758 return 0;
1759
1759
1760 if ((step < 0 && start < stop) || (step > 0 && start > stop))
1760 if ((step < 0 && start < stop) || (step > 0 && start > stop))
1761 stop = start;
1761 stop = start;
1762
1762
1763 if (step < 0) {
1763 if (step < 0) {
1764 stop = start + 1;
1764 stop = start + 1;
1765 start = stop + step*(slicelength - 1) - 1;
1765 start = stop + step*(slicelength - 1) - 1;
1766 step = -step;
1766 step = -step;
1767 }
1767 }
1768
1768
1769 if (step != 1) {
1769 if (step != 1) {
1770 PyErr_SetString(PyExc_ValueError,
1770 PyErr_SetString(PyExc_ValueError,
1771 "revlog index delete requires step size of 1");
1771 "revlog index delete requires step size of 1");
1772 return -1;
1772 return -1;
1773 }
1773 }
1774
1774
1775 if (stop != length - 1) {
1775 if (stop != length - 1) {
1776 PyErr_SetString(PyExc_IndexError,
1776 PyErr_SetString(PyExc_IndexError,
1777 "revlog index deletion indices are invalid");
1777 "revlog index deletion indices are invalid");
1778 return -1;
1778 return -1;
1779 }
1779 }
1780
1780
1781 if (start < self->length - 1) {
1781 if (start < self->length - 1) {
1782 if (self->nt) {
1782 if (self->nt) {
1783 Py_ssize_t i;
1783 Py_ssize_t i;
1784
1784
1785 for (i = start + 1; i < self->length - 1; i++) {
1785 for (i = start + 1; i < self->length - 1; i++) {
1786 const char *node = index_node(self, i);
1786 const char *node = index_node(self, i);
1787
1787
1788 if (node)
1788 if (node)
1789 nt_insert(self, node, -1);
1789 nt_insert(self, node, -1);
1790 }
1790 }
1791 if (self->added)
1791 if (self->added)
1792 nt_invalidate_added(self, 0);
1792 nt_invalidate_added(self, 0);
1793 if (self->ntrev > start)
1793 if (self->ntrev > start)
1794 self->ntrev = (int)start;
1794 self->ntrev = (int)start;
1795 }
1795 }
1796 self->length = start + 1;
1796 self->length = start + 1;
1797 if (start < self->raw_length) {
1797 if (start < self->raw_length) {
1798 if (self->cache) {
1798 if (self->cache) {
1799 Py_ssize_t i;
1799 Py_ssize_t i;
1800 for (i = start; i < self->raw_length; i++)
1800 for (i = start; i < self->raw_length; i++)
1801 Py_CLEAR(self->cache[i]);
1801 Py_CLEAR(self->cache[i]);
1802 }
1802 }
1803 self->raw_length = start;
1803 self->raw_length = start;
1804 }
1804 }
1805 goto done;
1805 goto done;
1806 }
1806 }
1807
1807
1808 if (self->nt) {
1808 if (self->nt) {
1809 nt_invalidate_added(self, start - self->length + 1);
1809 nt_invalidate_added(self, start - self->length + 1);
1810 if (self->ntrev > start)
1810 if (self->ntrev > start)
1811 self->ntrev = (int)start;
1811 self->ntrev = (int)start;
1812 }
1812 }
1813 if (self->added)
1813 if (self->added)
1814 ret = PyList_SetSlice(self->added, start - self->length + 1,
1814 ret = PyList_SetSlice(self->added, start - self->length + 1,
1815 PyList_GET_SIZE(self->added), NULL);
1815 PyList_GET_SIZE(self->added), NULL);
1816 done:
1816 done:
1817 Py_CLEAR(self->headrevs);
1817 Py_CLEAR(self->headrevs);
1818 return ret;
1818 return ret;
1819 }
1819 }
1820
1820
1821 /*
1821 /*
1822 * Supported ops:
1822 * Supported ops:
1823 *
1823 *
1824 * slice deletion
1824 * slice deletion
1825 * string assignment (extend node->rev mapping)
1825 * string assignment (extend node->rev mapping)
1826 * string deletion (shrink node->rev mapping)
1826 * string deletion (shrink node->rev mapping)
1827 */
1827 */
1828 static int index_assign_subscript(indexObject *self, PyObject *item,
1828 static int index_assign_subscript(indexObject *self, PyObject *item,
1829 PyObject *value)
1829 PyObject *value)
1830 {
1830 {
1831 char *node;
1831 char *node;
1832 Py_ssize_t nodelen;
1832 Py_ssize_t nodelen;
1833 long rev;
1833 long rev;
1834
1834
1835 if (PySlice_Check(item) && value == NULL)
1835 if (PySlice_Check(item) && value == NULL)
1836 return index_slice_del(self, item);
1836 return index_slice_del(self, item);
1837
1837
1838 if (node_check(item, &node, &nodelen) == -1)
1838 if (node_check(item, &node, &nodelen) == -1)
1839 return -1;
1839 return -1;
1840
1840
1841 if (value == NULL)
1841 if (value == NULL)
1842 return self->nt ? nt_insert(self, node, -1) : 0;
1842 return self->nt ? nt_insert(self, node, -1) : 0;
1843 rev = PyInt_AsLong(value);
1843 rev = PyInt_AsLong(value);
1844 if (rev > INT_MAX || rev < 0) {
1844 if (rev > INT_MAX || rev < 0) {
1845 if (!PyErr_Occurred())
1845 if (!PyErr_Occurred())
1846 PyErr_SetString(PyExc_ValueError, "rev out of range");
1846 PyErr_SetString(PyExc_ValueError, "rev out of range");
1847 return -1;
1847 return -1;
1848 }
1848 }
1849 return nt_insert(self, node, (int)rev);
1849 return nt_insert(self, node, (int)rev);
1850 }
1850 }
1851
1851
1852 /*
1852 /*
1853 * Find all RevlogNG entries in an index that has inline data. Update
1853 * Find all RevlogNG entries in an index that has inline data. Update
1854 * the optional "offsets" table with those entries.
1854 * the optional "offsets" table with those entries.
1855 */
1855 */
1856 static long inline_scan(indexObject *self, const char **offsets)
1856 static long inline_scan(indexObject *self, const char **offsets)
1857 {
1857 {
1858 const char *data = PyString_AS_STRING(self->data);
1858 const char *data = PyString_AS_STRING(self->data);
1859 Py_ssize_t pos = 0;
1859 Py_ssize_t pos = 0;
1860 Py_ssize_t end = PyString_GET_SIZE(self->data);
1860 Py_ssize_t end = PyString_GET_SIZE(self->data);
1861 long incr = v1_hdrsize;
1861 long incr = v1_hdrsize;
1862 Py_ssize_t len = 0;
1862 Py_ssize_t len = 0;
1863
1863
1864 while (pos + v1_hdrsize <= end && pos >= 0) {
1864 while (pos + v1_hdrsize <= end && pos >= 0) {
1865 uint32_t comp_len;
1865 uint32_t comp_len;
1866 /* 3rd element of header is length of compressed inline data */
1866 /* 3rd element of header is length of compressed inline data */
1867 comp_len = getbe32(data + pos + 8);
1867 comp_len = getbe32(data + pos + 8);
1868 incr = v1_hdrsize + comp_len;
1868 incr = v1_hdrsize + comp_len;
1869 if (offsets)
1869 if (offsets)
1870 offsets[len] = data + pos;
1870 offsets[len] = data + pos;
1871 len++;
1871 len++;
1872 pos += incr;
1872 pos += incr;
1873 }
1873 }
1874
1874
1875 if (pos != end) {
1875 if (pos != end) {
1876 if (!PyErr_Occurred())
1876 if (!PyErr_Occurred())
1877 PyErr_SetString(PyExc_ValueError, "corrupt index file");
1877 PyErr_SetString(PyExc_ValueError, "corrupt index file");
1878 return -1;
1878 return -1;
1879 }
1879 }
1880
1880
1881 return len;
1881 return len;
1882 }
1882 }
1883
1883
1884 static int index_init(indexObject *self, PyObject *args)
1884 static int index_init(indexObject *self, PyObject *args)
1885 {
1885 {
1886 PyObject *data_obj, *inlined_obj;
1886 PyObject *data_obj, *inlined_obj;
1887 Py_ssize_t size;
1887 Py_ssize_t size;
1888
1888
1889 /* Initialize before argument-checking to avoid index_dealloc() crash. */
1889 /* Initialize before argument-checking to avoid index_dealloc() crash. */
1890 self->raw_length = 0;
1890 self->raw_length = 0;
1891 self->added = NULL;
1891 self->added = NULL;
1892 self->cache = NULL;
1892 self->cache = NULL;
1893 self->data = NULL;
1893 self->data = NULL;
1894 self->headrevs = NULL;
1894 self->headrevs = NULL;
1895 self->nt = NULL;
1895 self->nt = NULL;
1896 self->offsets = NULL;
1896 self->offsets = NULL;
1897
1897
1898 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
1898 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
1899 return -1;
1899 return -1;
1900 if (!PyString_Check(data_obj)) {
1900 if (!PyString_Check(data_obj)) {
1901 PyErr_SetString(PyExc_TypeError, "data is not a string");
1901 PyErr_SetString(PyExc_TypeError, "data is not a string");
1902 return -1;
1902 return -1;
1903 }
1903 }
1904 size = PyString_GET_SIZE(data_obj);
1904 size = PyString_GET_SIZE(data_obj);
1905
1905
1906 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
1906 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
1907 self->data = data_obj;
1907 self->data = data_obj;
1908
1908
1909 self->ntlength = self->ntcapacity = 0;
1909 self->ntlength = self->ntcapacity = 0;
1910 self->ntdepth = self->ntsplits = 0;
1910 self->ntdepth = self->ntsplits = 0;
1911 self->ntlookups = self->ntmisses = 0;
1911 self->ntlookups = self->ntmisses = 0;
1912 self->ntrev = -1;
1912 self->ntrev = -1;
1913 Py_INCREF(self->data);
1913 Py_INCREF(self->data);
1914
1914
1915 if (self->inlined) {
1915 if (self->inlined) {
1916 long len = inline_scan(self, NULL);
1916 long len = inline_scan(self, NULL);
1917 if (len == -1)
1917 if (len == -1)
1918 goto bail;
1918 goto bail;
1919 self->raw_length = len;
1919 self->raw_length = len;
1920 self->length = len + 1;
1920 self->length = len + 1;
1921 } else {
1921 } else {
1922 if (size % v1_hdrsize) {
1922 if (size % v1_hdrsize) {
1923 PyErr_SetString(PyExc_ValueError, "corrupt index file");
1923 PyErr_SetString(PyExc_ValueError, "corrupt index file");
1924 goto bail;
1924 goto bail;
1925 }
1925 }
1926 self->raw_length = size / v1_hdrsize;
1926 self->raw_length = size / v1_hdrsize;
1927 self->length = self->raw_length + 1;
1927 self->length = self->raw_length + 1;
1928 }
1928 }
1929
1929
1930 return 0;
1930 return 0;
1931 bail:
1931 bail:
1932 return -1;
1932 return -1;
1933 }
1933 }
1934
1934
1935 static PyObject *index_nodemap(indexObject *self)
1935 static PyObject *index_nodemap(indexObject *self)
1936 {
1936 {
1937 Py_INCREF(self);
1937 Py_INCREF(self);
1938 return (PyObject *)self;
1938 return (PyObject *)self;
1939 }
1939 }
1940
1940
1941 static void index_dealloc(indexObject *self)
1941 static void index_dealloc(indexObject *self)
1942 {
1942 {
1943 _index_clearcaches(self);
1943 _index_clearcaches(self);
1944 Py_XDECREF(self->data);
1944 Py_XDECREF(self->data);
1945 Py_XDECREF(self->added);
1945 Py_XDECREF(self->added);
1946 PyObject_Del(self);
1946 PyObject_Del(self);
1947 }
1947 }
1948
1948
1949 static PySequenceMethods index_sequence_methods = {
1949 static PySequenceMethods index_sequence_methods = {
1950 (lenfunc)index_length, /* sq_length */
1950 (lenfunc)index_length, /* sq_length */
1951 0, /* sq_concat */
1951 0, /* sq_concat */
1952 0, /* sq_repeat */
1952 0, /* sq_repeat */
1953 (ssizeargfunc)index_get, /* sq_item */
1953 (ssizeargfunc)index_get, /* sq_item */
1954 0, /* sq_slice */
1954 0, /* sq_slice */
1955 0, /* sq_ass_item */
1955 0, /* sq_ass_item */
1956 0, /* sq_ass_slice */
1956 0, /* sq_ass_slice */
1957 (objobjproc)index_contains, /* sq_contains */
1957 (objobjproc)index_contains, /* sq_contains */
1958 };
1958 };
1959
1959
1960 static PyMappingMethods index_mapping_methods = {
1960 static PyMappingMethods index_mapping_methods = {
1961 (lenfunc)index_length, /* mp_length */
1961 (lenfunc)index_length, /* mp_length */
1962 (binaryfunc)index_getitem, /* mp_subscript */
1962 (binaryfunc)index_getitem, /* mp_subscript */
1963 (objobjargproc)index_assign_subscript, /* mp_ass_subscript */
1963 (objobjargproc)index_assign_subscript, /* mp_ass_subscript */
1964 };
1964 };
1965
1965
1966 static PyMethodDef index_methods[] = {
1966 static PyMethodDef index_methods[] = {
1967 {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS,
1967 {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS,
1968 "return the gca set of the given revs"},
1968 "return the gca set of the given revs"},
1969 {"commonancestorsheads", (PyCFunction)index_commonancestorsheads,
1969 {"commonancestorsheads", (PyCFunction)index_commonancestorsheads,
1970 METH_VARARGS,
1970 METH_VARARGS,
1971 "return the heads of the common ancestors of the given revs"},
1971 "return the heads of the common ancestors of the given revs"},
1972 {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS,
1972 {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS,
1973 "clear the index caches"},
1973 "clear the index caches"},
1974 {"get", (PyCFunction)index_m_get, METH_VARARGS,
1974 {"get", (PyCFunction)index_m_get, METH_VARARGS,
1975 "get an index entry"},
1975 "get an index entry"},
1976 {"headrevs", (PyCFunction)index_headrevs, METH_NOARGS,
1976 {"headrevs", (PyCFunction)index_headrevs, METH_NOARGS,
1977 "get head revisions"},
1977 "get head revisions"},
1978 {"insert", (PyCFunction)index_insert, METH_VARARGS,
1978 {"insert", (PyCFunction)index_insert, METH_VARARGS,
1979 "insert an index entry"},
1979 "insert an index entry"},
1980 {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS,
1980 {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS,
1981 "match a potentially ambiguous node ID"},
1981 "match a potentially ambiguous node ID"},
1982 {"stats", (PyCFunction)index_stats, METH_NOARGS,
1982 {"stats", (PyCFunction)index_stats, METH_NOARGS,
1983 "stats for the index"},
1983 "stats for the index"},
1984 {NULL} /* Sentinel */
1984 {NULL} /* Sentinel */
1985 };
1985 };
1986
1986
1987 static PyGetSetDef index_getset[] = {
1987 static PyGetSetDef index_getset[] = {
1988 {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL},
1988 {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL},
1989 {NULL} /* Sentinel */
1989 {NULL} /* Sentinel */
1990 };
1990 };
1991
1991
1992 static PyTypeObject indexType = {
1992 static PyTypeObject indexType = {
1993 PyObject_HEAD_INIT(NULL)
1993 PyObject_HEAD_INIT(NULL)
1994 0, /* ob_size */
1994 0, /* ob_size */
1995 "parsers.index", /* tp_name */
1995 "parsers.index", /* tp_name */
1996 sizeof(indexObject), /* tp_basicsize */
1996 sizeof(indexObject), /* tp_basicsize */
1997 0, /* tp_itemsize */
1997 0, /* tp_itemsize */
1998 (destructor)index_dealloc, /* tp_dealloc */
1998 (destructor)index_dealloc, /* tp_dealloc */
1999 0, /* tp_print */
1999 0, /* tp_print */
2000 0, /* tp_getattr */
2000 0, /* tp_getattr */
2001 0, /* tp_setattr */
2001 0, /* tp_setattr */
2002 0, /* tp_compare */
2002 0, /* tp_compare */
2003 0, /* tp_repr */
2003 0, /* tp_repr */
2004 0, /* tp_as_number */
2004 0, /* tp_as_number */
2005 &index_sequence_methods, /* tp_as_sequence */
2005 &index_sequence_methods, /* tp_as_sequence */
2006 &index_mapping_methods, /* tp_as_mapping */
2006 &index_mapping_methods, /* tp_as_mapping */
2007 0, /* tp_hash */
2007 0, /* tp_hash */
2008 0, /* tp_call */
2008 0, /* tp_call */
2009 0, /* tp_str */
2009 0, /* tp_str */
2010 0, /* tp_getattro */
2010 0, /* tp_getattro */
2011 0, /* tp_setattro */
2011 0, /* tp_setattro */
2012 0, /* tp_as_buffer */
2012 0, /* tp_as_buffer */
2013 Py_TPFLAGS_DEFAULT, /* tp_flags */
2013 Py_TPFLAGS_DEFAULT, /* tp_flags */
2014 "revlog index", /* tp_doc */
2014 "revlog index", /* tp_doc */
2015 0, /* tp_traverse */
2015 0, /* tp_traverse */
2016 0, /* tp_clear */
2016 0, /* tp_clear */
2017 0, /* tp_richcompare */
2017 0, /* tp_richcompare */
2018 0, /* tp_weaklistoffset */
2018 0, /* tp_weaklistoffset */
2019 0, /* tp_iter */
2019 0, /* tp_iter */
2020 0, /* tp_iternext */
2020 0, /* tp_iternext */
2021 index_methods, /* tp_methods */
2021 index_methods, /* tp_methods */
2022 0, /* tp_members */
2022 0, /* tp_members */
2023 index_getset, /* tp_getset */
2023 index_getset, /* tp_getset */
2024 0, /* tp_base */
2024 0, /* tp_base */
2025 0, /* tp_dict */
2025 0, /* tp_dict */
2026 0, /* tp_descr_get */
2026 0, /* tp_descr_get */
2027 0, /* tp_descr_set */
2027 0, /* tp_descr_set */
2028 0, /* tp_dictoffset */
2028 0, /* tp_dictoffset */
2029 (initproc)index_init, /* tp_init */
2029 (initproc)index_init, /* tp_init */
2030 0, /* tp_alloc */
2030 0, /* tp_alloc */
2031 };
2031 };
2032
2032
2033 /*
2033 /*
2034 * returns a tuple of the form (index, index, cache) with elements as
2034 * returns a tuple of the form (index, index, cache) with elements as
2035 * follows:
2035 * follows:
2036 *
2036 *
2037 * index: an index object that lazily parses RevlogNG records
2037 * index: an index object that lazily parses RevlogNG records
2038 * cache: if data is inlined, a tuple (index_file_content, 0), else None
2038 * cache: if data is inlined, a tuple (index_file_content, 0), else None
2039 *
2039 *
2040 * added complications are for backwards compatibility
2040 * added complications are for backwards compatibility
2041 */
2041 */
2042 static PyObject *parse_index2(PyObject *self, PyObject *args)
2042 static PyObject *parse_index2(PyObject *self, PyObject *args)
2043 {
2043 {
2044 PyObject *tuple = NULL, *cache = NULL;
2044 PyObject *tuple = NULL, *cache = NULL;
2045 indexObject *idx;
2045 indexObject *idx;
2046 int ret;
2046 int ret;
2047
2047
2048 idx = PyObject_New(indexObject, &indexType);
2048 idx = PyObject_New(indexObject, &indexType);
2049 if (idx == NULL)
2049 if (idx == NULL)
2050 goto bail;
2050 goto bail;
2051
2051
2052 ret = index_init(idx, args);
2052 ret = index_init(idx, args);
2053 if (ret == -1)
2053 if (ret == -1)
2054 goto bail;
2054 goto bail;
2055
2055
2056 if (idx->inlined) {
2056 if (idx->inlined) {
2057 cache = Py_BuildValue("iO", 0, idx->data);
2057 cache = Py_BuildValue("iO", 0, idx->data);
2058 if (cache == NULL)
2058 if (cache == NULL)
2059 goto bail;
2059 goto bail;
2060 } else {
2060 } else {
2061 cache = Py_None;
2061 cache = Py_None;
2062 Py_INCREF(cache);
2062 Py_INCREF(cache);
2063 }
2063 }
2064
2064
2065 tuple = Py_BuildValue("NN", idx, cache);
2065 tuple = Py_BuildValue("NN", idx, cache);
2066 if (!tuple)
2066 if (!tuple)
2067 goto bail;
2067 goto bail;
2068 return tuple;
2068 return tuple;
2069
2069
2070 bail:
2070 bail:
2071 Py_XDECREF(idx);
2071 Py_XDECREF(idx);
2072 Py_XDECREF(cache);
2072 Py_XDECREF(cache);
2073 Py_XDECREF(tuple);
2073 Py_XDECREF(tuple);
2074 return NULL;
2074 return NULL;
2075 }
2075 }
2076
2076
2077 static char parsers_doc[] = "Efficient content parsing.";
2077 static char parsers_doc[] = "Efficient content parsing.";
2078
2078
2079 PyObject *encodedir(PyObject *self, PyObject *args);
2079 PyObject *encodedir(PyObject *self, PyObject *args);
2080 PyObject *pathencode(PyObject *self, PyObject *args);
2080 PyObject *pathencode(PyObject *self, PyObject *args);
2081 PyObject *lowerencode(PyObject *self, PyObject *args);
2081 PyObject *lowerencode(PyObject *self, PyObject *args);
2082
2082
2083 static PyMethodDef methods[] = {
2083 static PyMethodDef methods[] = {
2084 {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"},
2084 {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"},
2085 {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"},
2085 {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"},
2086 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
2086 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
2087 {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"},
2087 {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"},
2088 {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"},
2088 {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"},
2089 {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"},
2089 {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"},
2090 {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"},
2090 {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"},
2091 {NULL, NULL}
2091 {NULL, NULL}
2092 };
2092 };
2093
2093
2094 void dirs_module_init(PyObject *mod);
2094 void dirs_module_init(PyObject *mod);
2095
2095
2096 static void module_init(PyObject *mod)
2096 static void module_init(PyObject *mod)
2097 {
2097 {
2098 /* This module constant has two purposes. First, it lets us unit test
2098 /* This module constant has two purposes. First, it lets us unit test
2099 * the ImportError raised without hard-coding any error text. This
2099 * the ImportError raised without hard-coding any error text. This
2100 * means we can change the text in the future without breaking tests,
2100 * means we can change the text in the future without breaking tests,
2101 * even across changesets without a recompile. Second, its presence
2101 * even across changesets without a recompile. Second, its presence
2102 * can be used to determine whether the version-checking logic is
2102 * can be used to determine whether the version-checking logic is
2103 * present, which also helps in testing across changesets without a
2103 * present, which also helps in testing across changesets without a
2104 * recompile. Note that this means the pure-Python version of parsers
2104 * recompile. Note that this means the pure-Python version of parsers
2105 * should not have this module constant. */
2105 * should not have this module constant. */
2106 PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext);
2106 PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext);
2107
2107
2108 dirs_module_init(mod);
2108 dirs_module_init(mod);
2109
2109
2110 indexType.tp_new = PyType_GenericNew;
2110 indexType.tp_new = PyType_GenericNew;
2111 if (PyType_Ready(&indexType) < 0 ||
2111 if (PyType_Ready(&indexType) < 0 ||
2112 PyType_Ready(&dirstateTupleType) < 0)
2112 PyType_Ready(&dirstateTupleType) < 0)
2113 return;
2113 return;
2114 Py_INCREF(&indexType);
2114 Py_INCREF(&indexType);
2115 PyModule_AddObject(mod, "index", (PyObject *)&indexType);
2115 PyModule_AddObject(mod, "index", (PyObject *)&indexType);
2116 Py_INCREF(&dirstateTupleType);
2116 Py_INCREF(&dirstateTupleType);
2117 PyModule_AddObject(mod, "dirstatetuple",
2117 PyModule_AddObject(mod, "dirstatetuple",
2118 (PyObject *)&dirstateTupleType);
2118 (PyObject *)&dirstateTupleType);
2119
2119
2120 nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0,
2120 nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0,
2121 -1, -1, -1, -1, nullid, 20);
2121 -1, -1, -1, -1, nullid, 20);
2122 if (nullentry)
2122 if (nullentry)
2123 PyObject_GC_UnTrack(nullentry);
2123 PyObject_GC_UnTrack(nullentry);
2124 }
2124 }
2125
2125
2126 static int check_python_version(void)
2126 static int check_python_version(void)
2127 {
2127 {
2128 PyObject *sys = PyImport_ImportModule("sys");
2128 PyObject *sys = PyImport_ImportModule("sys");
2129 long hexversion = PyInt_AsLong(PyObject_GetAttrString(sys, "hexversion"));
2129 long hexversion = PyInt_AsLong(PyObject_GetAttrString(sys, "hexversion"));
2130 /* sys.hexversion is a 32-bit number by default, so the -1 case
2130 /* sys.hexversion is a 32-bit number by default, so the -1 case
2131 * should only occur in unusual circumstances (e.g. if sys.hexversion
2131 * should only occur in unusual circumstances (e.g. if sys.hexversion
2132 * is manually set to an invalid value). */
2132 * is manually set to an invalid value). */
2133 if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) {
2133 if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) {
2134 PyErr_Format(PyExc_ImportError, "%s: The Mercurial extension "
2134 PyErr_Format(PyExc_ImportError, "%s: The Mercurial extension "
2135 "modules were compiled with Python " PY_VERSION ", but "
2135 "modules were compiled with Python " PY_VERSION ", but "
2136 "Mercurial is currently using Python with sys.hexversion=%ld: "
2136 "Mercurial is currently using Python with sys.hexversion=%ld: "
2137 "Python %s\n at: %s", versionerrortext, hexversion,
2137 "Python %s\n at: %s", versionerrortext, hexversion,
2138 Py_GetVersion(), Py_GetProgramFullPath());
2138 Py_GetVersion(), Py_GetProgramFullPath());
2139 return -1;
2139 return -1;
2140 }
2140 }
2141 return 0;
2141 return 0;
2142 }
2142 }
2143
2143
2144 #ifdef IS_PY3K
2144 #ifdef IS_PY3K
2145 static struct PyModuleDef parsers_module = {
2145 static struct PyModuleDef parsers_module = {
2146 PyModuleDef_HEAD_INIT,
2146 PyModuleDef_HEAD_INIT,
2147 "parsers",
2147 "parsers",
2148 parsers_doc,
2148 parsers_doc,
2149 -1,
2149 -1,
2150 methods
2150 methods
2151 };
2151 };
2152
2152
2153 PyMODINIT_FUNC PyInit_parsers(void)
2153 PyMODINIT_FUNC PyInit_parsers(void)
2154 {
2154 {
2155 PyObject *mod;
2155 PyObject *mod;
2156
2156
2157 if (check_python_version() == -1)
2157 if (check_python_version() == -1)
2158 return;
2158 return;
2159 mod = PyModule_Create(&parsers_module);
2159 mod = PyModule_Create(&parsers_module);
2160 module_init(mod);
2160 module_init(mod);
2161 return mod;
2161 return mod;
2162 }
2162 }
2163 #else
2163 #else
2164 PyMODINIT_FUNC initparsers(void)
2164 PyMODINIT_FUNC initparsers(void)
2165 {
2165 {
2166 PyObject *mod;
2166 PyObject *mod;
2167
2167
2168 if (check_python_version() == -1)
2168 if (check_python_version() == -1)
2169 return;
2169 return;
2170 mod = Py_InitModule3("parsers", methods, parsers_doc);
2170 mod = Py_InitModule3("parsers", methods, parsers_doc);
2171 module_init(mod);
2171 module_init(mod);
2172 }
2172 }
2173 #endif
2173 #endif
General Comments 0
You need to be logged in to leave comments. Login now