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