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