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