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