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