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