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