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