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