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