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