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