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