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