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