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