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