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