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