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