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