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