##// END OF EJS Templates
phases: fix error return with no exception from computephases()...
Yuya Nishihara -
r45735:21106364 default
parent child Browse files
Show More
@@ -1,2952 +1,2955 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 #define PY_SSIZE_T_CLEAN
11 11 #include <Python.h>
12 12 #include <assert.h>
13 13 #include <ctype.h>
14 14 #include <limits.h>
15 15 #include <stddef.h>
16 16 #include <stdlib.h>
17 17 #include <string.h>
18 18
19 19 #include "bitmanipulation.h"
20 20 #include "charencode.h"
21 21 #include "revlog.h"
22 22 #include "util.h"
23 23
24 24 #ifdef IS_PY3K
25 25 /* The mapping of Python types is meant to be temporary to get Python
26 26 * 3 to compile. We should remove this once Python 3 support is fully
27 27 * supported and proper types are used in the extensions themselves. */
28 28 #define PyInt_Check PyLong_Check
29 29 #define PyInt_FromLong PyLong_FromLong
30 30 #define PyInt_FromSsize_t PyLong_FromSsize_t
31 31 #define PyInt_AsLong PyLong_AsLong
32 32 #endif
33 33
34 34 typedef struct indexObjectStruct indexObject;
35 35
36 36 typedef struct {
37 37 int children[16];
38 38 } nodetreenode;
39 39
40 40 typedef struct {
41 41 int abi_version;
42 42 Py_ssize_t (*index_length)(const indexObject *);
43 43 const char *(*index_node)(indexObject *, Py_ssize_t);
44 44 int (*index_parents)(PyObject *, int, int *);
45 45 } Revlog_CAPI;
46 46
47 47 /*
48 48 * A base-16 trie for fast node->rev mapping.
49 49 *
50 50 * Positive value is index of the next node in the trie
51 51 * Negative value is a leaf: -(rev + 2)
52 52 * Zero is empty
53 53 */
54 54 typedef struct {
55 55 indexObject *index;
56 56 nodetreenode *nodes;
57 57 unsigned length; /* # nodes in use */
58 58 unsigned capacity; /* # nodes allocated */
59 59 int depth; /* maximum depth of tree */
60 60 int splits; /* # splits performed */
61 61 } nodetree;
62 62
63 63 typedef struct {
64 64 PyObject_HEAD /* ; */
65 65 nodetree nt;
66 66 } nodetreeObject;
67 67
68 68 /*
69 69 * This class has two behaviors.
70 70 *
71 71 * When used in a list-like way (with integer keys), we decode an
72 72 * entry in a RevlogNG index file on demand. We have limited support for
73 73 * integer-keyed insert and delete, only at elements right before the
74 74 * end.
75 75 *
76 76 * With string keys, we lazily perform a reverse mapping from node to
77 77 * rev, using a base-16 trie.
78 78 */
79 79 struct indexObjectStruct {
80 80 PyObject_HEAD
81 81 /* Type-specific fields go here. */
82 82 PyObject *data; /* raw bytes of index */
83 83 Py_buffer buf; /* buffer of data */
84 84 PyObject **cache; /* cached tuples */
85 85 const char **offsets; /* populated on demand */
86 86 Py_ssize_t raw_length; /* original number of elements */
87 87 Py_ssize_t length; /* current number of elements */
88 88 PyObject *added; /* populated on demand */
89 89 PyObject *headrevs; /* cache, invalidated on changes */
90 90 PyObject *filteredrevs; /* filtered revs set */
91 91 nodetree nt; /* base-16 trie */
92 92 int ntinitialized; /* 0 or 1 */
93 93 int ntrev; /* last rev scanned */
94 94 int ntlookups; /* # lookups */
95 95 int ntmisses; /* # lookups that miss the cache */
96 96 int inlined;
97 97 };
98 98
99 99 static Py_ssize_t index_length(const indexObject *self)
100 100 {
101 101 if (self->added == NULL)
102 102 return self->length;
103 103 return self->length + PyList_GET_SIZE(self->added);
104 104 }
105 105
106 106 static PyObject *nullentry = NULL;
107 107 static const char nullid[20] = {0};
108 108 static const Py_ssize_t nullrev = -1;
109 109
110 110 static Py_ssize_t inline_scan(indexObject *self, const char **offsets);
111 111
112 112 static int index_find_node(indexObject *self, const char *node,
113 113 Py_ssize_t nodelen);
114 114
115 115 #if LONG_MAX == 0x7fffffffL
116 116 static const char *const tuple_format = PY23("Kiiiiiis#", "Kiiiiiiy#");
117 117 #else
118 118 static const char *const tuple_format = PY23("kiiiiiis#", "kiiiiiiy#");
119 119 #endif
120 120
121 121 /* A RevlogNG v1 index entry is 64 bytes long. */
122 122 static const long v1_hdrsize = 64;
123 123
124 124 static void raise_revlog_error(void)
125 125 {
126 126 PyObject *mod = NULL, *dict = NULL, *errclass = NULL;
127 127
128 128 mod = PyImport_ImportModule("mercurial.error");
129 129 if (mod == NULL) {
130 130 goto cleanup;
131 131 }
132 132
133 133 dict = PyModule_GetDict(mod);
134 134 if (dict == NULL) {
135 135 goto cleanup;
136 136 }
137 137 Py_INCREF(dict);
138 138
139 139 errclass = PyDict_GetItemString(dict, "RevlogError");
140 140 if (errclass == NULL) {
141 141 PyErr_SetString(PyExc_SystemError,
142 142 "could not find RevlogError");
143 143 goto cleanup;
144 144 }
145 145
146 146 /* value of exception is ignored by callers */
147 147 PyErr_SetString(errclass, "RevlogError");
148 148
149 149 cleanup:
150 150 Py_XDECREF(dict);
151 151 Py_XDECREF(mod);
152 152 }
153 153
154 154 /*
155 155 * Return a pointer to the beginning of a RevlogNG record.
156 156 */
157 157 static const char *index_deref(indexObject *self, Py_ssize_t pos)
158 158 {
159 159 if (self->inlined && pos > 0) {
160 160 if (self->offsets == NULL) {
161 161 Py_ssize_t ret;
162 162 self->offsets = PyMem_Malloc(self->raw_length *
163 163 sizeof(*self->offsets));
164 164 if (self->offsets == NULL)
165 165 return (const char *)PyErr_NoMemory();
166 166 ret = inline_scan(self, self->offsets);
167 167 if (ret == -1) {
168 168 return NULL;
169 169 };
170 170 }
171 171 return self->offsets[pos];
172 172 }
173 173
174 174 return (const char *)(self->buf.buf) + pos * v1_hdrsize;
175 175 }
176 176
177 177 /*
178 178 * Get parents of the given rev.
179 179 *
180 180 * The specified rev must be valid and must not be nullrev. A returned
181 181 * parent revision may be nullrev, but is guaranteed to be in valid range.
182 182 */
183 183 static inline int index_get_parents(indexObject *self, Py_ssize_t rev, int *ps,
184 184 int maxrev)
185 185 {
186 186 if (rev >= self->length) {
187 187 long tmp;
188 188 PyObject *tuple =
189 189 PyList_GET_ITEM(self->added, rev - self->length);
190 190 if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 5), &tmp)) {
191 191 return -1;
192 192 }
193 193 ps[0] = (int)tmp;
194 194 if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 6), &tmp)) {
195 195 return -1;
196 196 }
197 197 ps[1] = (int)tmp;
198 198 } else {
199 199 const char *data = index_deref(self, rev);
200 200 ps[0] = getbe32(data + 24);
201 201 ps[1] = getbe32(data + 28);
202 202 }
203 203 /* If index file is corrupted, ps[] may point to invalid revisions. So
204 204 * there is a risk of buffer overflow to trust them unconditionally. */
205 205 if (ps[0] < -1 || ps[0] > maxrev || ps[1] < -1 || ps[1] > maxrev) {
206 206 PyErr_SetString(PyExc_ValueError, "parent out of range");
207 207 return -1;
208 208 }
209 209 return 0;
210 210 }
211 211
212 212 /*
213 213 * Get parents of the given rev.
214 214 *
215 215 * If the specified rev is out of range, IndexError will be raised. If the
216 216 * revlog entry is corrupted, ValueError may be raised.
217 217 *
218 218 * Returns 0 on success or -1 on failure.
219 219 */
220 220 static int HgRevlogIndex_GetParents(PyObject *op, int rev, int *ps)
221 221 {
222 222 int tiprev;
223 223 if (!op || !HgRevlogIndex_Check(op) || !ps) {
224 224 PyErr_BadInternalCall();
225 225 return -1;
226 226 }
227 227 tiprev = (int)index_length((indexObject *)op) - 1;
228 228 if (rev < -1 || rev > tiprev) {
229 229 PyErr_Format(PyExc_IndexError, "rev out of range: %d", rev);
230 230 return -1;
231 231 } else if (rev == -1) {
232 232 ps[0] = ps[1] = -1;
233 233 return 0;
234 234 } else {
235 235 return index_get_parents((indexObject *)op, rev, ps, tiprev);
236 236 }
237 237 }
238 238
239 239 static inline int64_t index_get_start(indexObject *self, Py_ssize_t rev)
240 240 {
241 241 uint64_t offset;
242 242 if (rev == nullrev) {
243 243 return 0;
244 244 }
245 245 if (rev >= self->length) {
246 246 PyObject *tuple;
247 247 PyObject *pylong;
248 248 PY_LONG_LONG tmp;
249 249 tuple = PyList_GET_ITEM(self->added, rev - self->length);
250 250 pylong = PyTuple_GET_ITEM(tuple, 0);
251 251 tmp = PyLong_AsLongLong(pylong);
252 252 if (tmp == -1 && PyErr_Occurred()) {
253 253 return -1;
254 254 }
255 255 if (tmp < 0) {
256 256 PyErr_Format(PyExc_OverflowError,
257 257 "revlog entry size out of bound (%lld)",
258 258 (long long)tmp);
259 259 return -1;
260 260 }
261 261 offset = (uint64_t)tmp;
262 262 } else {
263 263 const char *data = index_deref(self, rev);
264 264 offset = getbe32(data + 4);
265 265 if (rev == 0) {
266 266 /* mask out version number for the first entry */
267 267 offset &= 0xFFFF;
268 268 } else {
269 269 uint32_t offset_high = getbe32(data);
270 270 offset |= ((uint64_t)offset_high) << 32;
271 271 }
272 272 }
273 273 return (int64_t)(offset >> 16);
274 274 }
275 275
276 276 static inline int index_get_length(indexObject *self, Py_ssize_t rev)
277 277 {
278 278 if (rev == nullrev) {
279 279 return 0;
280 280 }
281 281 if (rev >= self->length) {
282 282 PyObject *tuple;
283 283 PyObject *pylong;
284 284 long ret;
285 285 tuple = PyList_GET_ITEM(self->added, rev - self->length);
286 286 pylong = PyTuple_GET_ITEM(tuple, 1);
287 287 ret = PyInt_AsLong(pylong);
288 288 if (ret == -1 && PyErr_Occurred()) {
289 289 return -1;
290 290 }
291 291 if (ret < 0 || ret > (long)INT_MAX) {
292 292 PyErr_Format(PyExc_OverflowError,
293 293 "revlog entry size out of bound (%ld)",
294 294 ret);
295 295 return -1;
296 296 }
297 297 return (int)ret;
298 298 } else {
299 299 const char *data = index_deref(self, rev);
300 300 int tmp = (int)getbe32(data + 8);
301 301 if (tmp < 0) {
302 302 PyErr_Format(PyExc_OverflowError,
303 303 "revlog entry size out of bound (%d)",
304 304 tmp);
305 305 return -1;
306 306 }
307 307 return tmp;
308 308 }
309 309 }
310 310
311 311 /*
312 312 * RevlogNG format (all in big endian, data may be inlined):
313 313 * 6 bytes: offset
314 314 * 2 bytes: flags
315 315 * 4 bytes: compressed length
316 316 * 4 bytes: uncompressed length
317 317 * 4 bytes: base revision
318 318 * 4 bytes: link revision
319 319 * 4 bytes: parent 1 revision
320 320 * 4 bytes: parent 2 revision
321 321 * 32 bytes: nodeid (only 20 bytes used)
322 322 */
323 323 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
324 324 {
325 325 uint64_t offset_flags;
326 326 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
327 327 const char *c_node_id;
328 328 const char *data;
329 329 Py_ssize_t length = index_length(self);
330 330 PyObject *entry;
331 331
332 332 if (pos == nullrev) {
333 333 Py_INCREF(nullentry);
334 334 return nullentry;
335 335 }
336 336
337 337 if (pos < 0 || pos >= length) {
338 338 PyErr_SetString(PyExc_IndexError, "revlog index out of range");
339 339 return NULL;
340 340 }
341 341
342 342 if (pos >= self->length) {
343 343 PyObject *obj;
344 344 obj = PyList_GET_ITEM(self->added, pos - self->length);
345 345 Py_INCREF(obj);
346 346 return obj;
347 347 }
348 348
349 349 if (self->cache) {
350 350 if (self->cache[pos]) {
351 351 Py_INCREF(self->cache[pos]);
352 352 return self->cache[pos];
353 353 }
354 354 } else {
355 355 self->cache = calloc(self->raw_length, sizeof(PyObject *));
356 356 if (self->cache == NULL)
357 357 return PyErr_NoMemory();
358 358 }
359 359
360 360 data = index_deref(self, pos);
361 361 if (data == NULL)
362 362 return NULL;
363 363
364 364 offset_flags = getbe32(data + 4);
365 365 if (pos == 0) /* mask out version number for the first entry */
366 366 offset_flags &= 0xFFFF;
367 367 else {
368 368 uint32_t offset_high = getbe32(data);
369 369 offset_flags |= ((uint64_t)offset_high) << 32;
370 370 }
371 371
372 372 comp_len = getbe32(data + 8);
373 373 uncomp_len = getbe32(data + 12);
374 374 base_rev = getbe32(data + 16);
375 375 link_rev = getbe32(data + 20);
376 376 parent_1 = getbe32(data + 24);
377 377 parent_2 = getbe32(data + 28);
378 378 c_node_id = data + 32;
379 379
380 380 entry = Py_BuildValue(tuple_format, offset_flags, comp_len, uncomp_len,
381 381 base_rev, link_rev, parent_1, parent_2, c_node_id,
382 382 (Py_ssize_t)20);
383 383
384 384 if (entry) {
385 385 PyObject_GC_UnTrack(entry);
386 386 Py_INCREF(entry);
387 387 }
388 388
389 389 self->cache[pos] = entry;
390 390
391 391 return entry;
392 392 }
393 393
394 394 /*
395 395 * Return the 20-byte SHA of the node corresponding to the given rev.
396 396 */
397 397 static const char *index_node(indexObject *self, Py_ssize_t pos)
398 398 {
399 399 Py_ssize_t length = index_length(self);
400 400 const char *data;
401 401
402 402 if (pos == nullrev)
403 403 return nullid;
404 404
405 405 if (pos >= length)
406 406 return NULL;
407 407
408 408 if (pos >= self->length) {
409 409 PyObject *tuple, *str;
410 410 tuple = PyList_GET_ITEM(self->added, pos - self->length);
411 411 str = PyTuple_GetItem(tuple, 7);
412 412 return str ? PyBytes_AS_STRING(str) : NULL;
413 413 }
414 414
415 415 data = index_deref(self, pos);
416 416 return data ? data + 32 : NULL;
417 417 }
418 418
419 419 /*
420 420 * Return the 20-byte SHA of the node corresponding to the given rev. The
421 421 * rev is assumed to be existing. If not, an exception is set.
422 422 */
423 423 static const char *index_node_existing(indexObject *self, Py_ssize_t pos)
424 424 {
425 425 const char *node = index_node(self, pos);
426 426 if (node == NULL) {
427 427 PyErr_Format(PyExc_IndexError, "could not access rev %d",
428 428 (int)pos);
429 429 }
430 430 return node;
431 431 }
432 432
433 433 static int nt_insert(nodetree *self, const char *node, int rev);
434 434
435 435 static int node_check(PyObject *obj, char **node)
436 436 {
437 437 Py_ssize_t nodelen;
438 438 if (PyBytes_AsStringAndSize(obj, node, &nodelen) == -1)
439 439 return -1;
440 440 if (nodelen == 20)
441 441 return 0;
442 442 PyErr_SetString(PyExc_ValueError, "20-byte hash required");
443 443 return -1;
444 444 }
445 445
446 446 static PyObject *index_append(indexObject *self, PyObject *obj)
447 447 {
448 448 char *node;
449 449 Py_ssize_t len;
450 450
451 451 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
452 452 PyErr_SetString(PyExc_TypeError, "8-tuple required");
453 453 return NULL;
454 454 }
455 455
456 456 if (node_check(PyTuple_GET_ITEM(obj, 7), &node) == -1)
457 457 return NULL;
458 458
459 459 len = index_length(self);
460 460
461 461 if (self->added == NULL) {
462 462 self->added = PyList_New(0);
463 463 if (self->added == NULL)
464 464 return NULL;
465 465 }
466 466
467 467 if (PyList_Append(self->added, obj) == -1)
468 468 return NULL;
469 469
470 470 if (self->ntinitialized)
471 471 nt_insert(&self->nt, node, (int)len);
472 472
473 473 Py_CLEAR(self->headrevs);
474 474 Py_RETURN_NONE;
475 475 }
476 476
477 477 static PyObject *index_stats(indexObject *self)
478 478 {
479 479 PyObject *obj = PyDict_New();
480 480 PyObject *s = NULL;
481 481 PyObject *t = NULL;
482 482
483 483 if (obj == NULL)
484 484 return NULL;
485 485
486 486 #define istat(__n, __d) \
487 487 do { \
488 488 s = PyBytes_FromString(__d); \
489 489 t = PyInt_FromSsize_t(self->__n); \
490 490 if (!s || !t) \
491 491 goto bail; \
492 492 if (PyDict_SetItem(obj, s, t) == -1) \
493 493 goto bail; \
494 494 Py_CLEAR(s); \
495 495 Py_CLEAR(t); \
496 496 } while (0)
497 497
498 498 if (self->added) {
499 499 Py_ssize_t len = PyList_GET_SIZE(self->added);
500 500 s = PyBytes_FromString("index entries added");
501 501 t = PyInt_FromSsize_t(len);
502 502 if (!s || !t)
503 503 goto bail;
504 504 if (PyDict_SetItem(obj, s, t) == -1)
505 505 goto bail;
506 506 Py_CLEAR(s);
507 507 Py_CLEAR(t);
508 508 }
509 509
510 510 if (self->raw_length != self->length)
511 511 istat(raw_length, "revs on disk");
512 512 istat(length, "revs in memory");
513 513 istat(ntlookups, "node trie lookups");
514 514 istat(ntmisses, "node trie misses");
515 515 istat(ntrev, "node trie last rev scanned");
516 516 if (self->ntinitialized) {
517 517 istat(nt.capacity, "node trie capacity");
518 518 istat(nt.depth, "node trie depth");
519 519 istat(nt.length, "node trie count");
520 520 istat(nt.splits, "node trie splits");
521 521 }
522 522
523 523 #undef istat
524 524
525 525 return obj;
526 526
527 527 bail:
528 528 Py_XDECREF(obj);
529 529 Py_XDECREF(s);
530 530 Py_XDECREF(t);
531 531 return NULL;
532 532 }
533 533
534 534 /*
535 535 * When we cache a list, we want to be sure the caller can't mutate
536 536 * the cached copy.
537 537 */
538 538 static PyObject *list_copy(PyObject *list)
539 539 {
540 540 Py_ssize_t len = PyList_GET_SIZE(list);
541 541 PyObject *newlist = PyList_New(len);
542 542 Py_ssize_t i;
543 543
544 544 if (newlist == NULL)
545 545 return NULL;
546 546
547 547 for (i = 0; i < len; i++) {
548 548 PyObject *obj = PyList_GET_ITEM(list, i);
549 549 Py_INCREF(obj);
550 550 PyList_SET_ITEM(newlist, i, obj);
551 551 }
552 552
553 553 return newlist;
554 554 }
555 555
556 556 static int check_filter(PyObject *filter, Py_ssize_t arg)
557 557 {
558 558 if (filter) {
559 559 PyObject *arglist, *result;
560 560 int isfiltered;
561 561
562 562 arglist = Py_BuildValue("(n)", arg);
563 563 if (!arglist) {
564 564 return -1;
565 565 }
566 566
567 567 result = PyEval_CallObject(filter, arglist);
568 568 Py_DECREF(arglist);
569 569 if (!result) {
570 570 return -1;
571 571 }
572 572
573 573 /* PyObject_IsTrue returns 1 if true, 0 if false, -1 if error,
574 574 * same as this function, so we can just return it directly.*/
575 575 isfiltered = PyObject_IsTrue(result);
576 576 Py_DECREF(result);
577 577 return isfiltered;
578 578 } else {
579 579 return 0;
580 580 }
581 581 }
582 582
583 583 static inline void set_phase_from_parents(char *phases, int parent_1,
584 584 int parent_2, Py_ssize_t i)
585 585 {
586 586 if (parent_1 >= 0 && phases[parent_1] > phases[i])
587 587 phases[i] = phases[parent_1];
588 588 if (parent_2 >= 0 && phases[parent_2] > phases[i])
589 589 phases[i] = phases[parent_2];
590 590 }
591 591
592 592 static PyObject *reachableroots2(indexObject *self, PyObject *args)
593 593 {
594 594
595 595 /* Input */
596 596 long minroot;
597 597 PyObject *includepatharg = NULL;
598 598 int includepath = 0;
599 599 /* heads and roots are lists */
600 600 PyObject *heads = NULL;
601 601 PyObject *roots = NULL;
602 602 PyObject *reachable = NULL;
603 603
604 604 PyObject *val;
605 605 Py_ssize_t len = index_length(self);
606 606 long revnum;
607 607 Py_ssize_t k;
608 608 Py_ssize_t i;
609 609 Py_ssize_t l;
610 610 int r;
611 611 int parents[2];
612 612
613 613 /* Internal data structure:
614 614 * tovisit: array of length len+1 (all revs + nullrev), filled upto
615 615 * lentovisit
616 616 *
617 617 * revstates: array of length len+1 (all revs + nullrev) */
618 618 int *tovisit = NULL;
619 619 long lentovisit = 0;
620 620 enum { RS_SEEN = 1, RS_ROOT = 2, RS_REACHABLE = 4 };
621 621 char *revstates = NULL;
622 622
623 623 /* Get arguments */
624 624 if (!PyArg_ParseTuple(args, "lO!O!O!", &minroot, &PyList_Type, &heads,
625 625 &PyList_Type, &roots, &PyBool_Type,
626 626 &includepatharg))
627 627 goto bail;
628 628
629 629 if (includepatharg == Py_True)
630 630 includepath = 1;
631 631
632 632 /* Initialize return set */
633 633 reachable = PyList_New(0);
634 634 if (reachable == NULL)
635 635 goto bail;
636 636
637 637 /* Initialize internal datastructures */
638 638 tovisit = (int *)malloc((len + 1) * sizeof(int));
639 639 if (tovisit == NULL) {
640 640 PyErr_NoMemory();
641 641 goto bail;
642 642 }
643 643
644 644 revstates = (char *)calloc(len + 1, 1);
645 645 if (revstates == NULL) {
646 646 PyErr_NoMemory();
647 647 goto bail;
648 648 }
649 649
650 650 l = PyList_GET_SIZE(roots);
651 651 for (i = 0; i < l; i++) {
652 652 revnum = PyInt_AsLong(PyList_GET_ITEM(roots, i));
653 653 if (revnum == -1 && PyErr_Occurred())
654 654 goto bail;
655 655 /* If root is out of range, e.g. wdir(), it must be unreachable
656 656 * from heads. So we can just ignore it. */
657 657 if (revnum + 1 < 0 || revnum + 1 >= len + 1)
658 658 continue;
659 659 revstates[revnum + 1] |= RS_ROOT;
660 660 }
661 661
662 662 /* Populate tovisit with all the heads */
663 663 l = PyList_GET_SIZE(heads);
664 664 for (i = 0; i < l; i++) {
665 665 revnum = PyInt_AsLong(PyList_GET_ITEM(heads, i));
666 666 if (revnum == -1 && PyErr_Occurred())
667 667 goto bail;
668 668 if (revnum + 1 < 0 || revnum + 1 >= len + 1) {
669 669 PyErr_SetString(PyExc_IndexError, "head out of range");
670 670 goto bail;
671 671 }
672 672 if (!(revstates[revnum + 1] & RS_SEEN)) {
673 673 tovisit[lentovisit++] = (int)revnum;
674 674 revstates[revnum + 1] |= RS_SEEN;
675 675 }
676 676 }
677 677
678 678 /* Visit the tovisit list and find the reachable roots */
679 679 k = 0;
680 680 while (k < lentovisit) {
681 681 /* Add the node to reachable if it is a root*/
682 682 revnum = tovisit[k++];
683 683 if (revstates[revnum + 1] & RS_ROOT) {
684 684 revstates[revnum + 1] |= RS_REACHABLE;
685 685 val = PyInt_FromLong(revnum);
686 686 if (val == NULL)
687 687 goto bail;
688 688 r = PyList_Append(reachable, val);
689 689 Py_DECREF(val);
690 690 if (r < 0)
691 691 goto bail;
692 692 if (includepath == 0)
693 693 continue;
694 694 }
695 695
696 696 /* Add its parents to the list of nodes to visit */
697 697 if (revnum == nullrev)
698 698 continue;
699 699 r = index_get_parents(self, revnum, parents, (int)len - 1);
700 700 if (r < 0)
701 701 goto bail;
702 702 for (i = 0; i < 2; i++) {
703 703 if (!(revstates[parents[i] + 1] & RS_SEEN) &&
704 704 parents[i] >= minroot) {
705 705 tovisit[lentovisit++] = parents[i];
706 706 revstates[parents[i] + 1] |= RS_SEEN;
707 707 }
708 708 }
709 709 }
710 710
711 711 /* Find all the nodes in between the roots we found and the heads
712 712 * and add them to the reachable set */
713 713 if (includepath == 1) {
714 714 long minidx = minroot;
715 715 if (minidx < 0)
716 716 minidx = 0;
717 717 for (i = minidx; i < len; i++) {
718 718 if (!(revstates[i + 1] & RS_SEEN))
719 719 continue;
720 720 r = index_get_parents(self, i, parents, (int)len - 1);
721 721 /* Corrupted index file, error is set from
722 722 * index_get_parents */
723 723 if (r < 0)
724 724 goto bail;
725 725 if (((revstates[parents[0] + 1] |
726 726 revstates[parents[1] + 1]) &
727 727 RS_REACHABLE) &&
728 728 !(revstates[i + 1] & RS_REACHABLE)) {
729 729 revstates[i + 1] |= RS_REACHABLE;
730 730 val = PyInt_FromSsize_t(i);
731 731 if (val == NULL)
732 732 goto bail;
733 733 r = PyList_Append(reachable, val);
734 734 Py_DECREF(val);
735 735 if (r < 0)
736 736 goto bail;
737 737 }
738 738 }
739 739 }
740 740
741 741 free(revstates);
742 742 free(tovisit);
743 743 return reachable;
744 744 bail:
745 745 Py_XDECREF(reachable);
746 746 free(revstates);
747 747 free(tovisit);
748 748 return NULL;
749 749 }
750 750
751 751 static int add_roots_get_min(indexObject *self, PyObject *roots, char *phases,
752 752 char phase)
753 753 {
754 754 Py_ssize_t len = index_length(self);
755 755 PyObject *item;
756 756 PyObject *iterator;
757 757 int rev, minrev = -1;
758 758 char *node;
759 759
760 if (!PySet_Check(roots))
760 if (!PySet_Check(roots)) {
761 PyErr_SetString(PyExc_TypeError,
762 "roots must be a set of nodes");
761 763 return -2;
764 }
762 765 iterator = PyObject_GetIter(roots);
763 766 if (iterator == NULL)
764 767 return -2;
765 768 while ((item = PyIter_Next(iterator))) {
766 769 if (node_check(item, &node) == -1)
767 770 goto failed;
768 771 rev = index_find_node(self, node, 20);
769 772 /* null is implicitly public, so negative is invalid */
770 773 if (rev < 0 || rev >= len)
771 774 goto failed;
772 775 phases[rev] = phase;
773 776 if (minrev == -1 || minrev > rev)
774 777 minrev = rev;
775 778 Py_DECREF(item);
776 779 }
777 780 Py_DECREF(iterator);
778 781 return minrev;
779 782 failed:
780 783 Py_DECREF(iterator);
781 784 Py_DECREF(item);
782 785 return -2;
783 786 }
784 787
785 788 static PyObject *compute_phases_map_sets(indexObject *self, PyObject *args)
786 789 {
787 790 /* 0: public (untracked), 1: draft, 2: secret, 32: archive,
788 791 96: internal */
789 792 static const char trackedphases[] = {1, 2, 32, 96};
790 793 PyObject *ret = NULL;
791 794 PyObject *roots = Py_None;
792 795 PyObject *pyphase = NULL;
793 796 PyObject *pyrev = NULL;
794 797 PyObject *phaseroots = NULL;
795 798 PyObject *phasessize = NULL;
796 799 PyObject *phasesets[4] = {NULL, NULL, NULL, NULL};
797 800 Py_ssize_t len = index_length(self);
798 801 char *phases = NULL;
799 802 int minphaserev = -1, rev, i;
800 803 const int numphases = (int)(sizeof(phasesets) / sizeof(phasesets[0]));
801 804
802 805 if (!PyArg_ParseTuple(args, "O", &roots))
803 806 return NULL;
804 807 if (roots == NULL || !PyDict_Check(roots)) {
805 808 PyErr_SetString(PyExc_TypeError, "roots must be a dictionary");
806 809 return NULL;
807 810 }
808 811
809 812 phases = calloc(len, 1);
810 813 if (phases == NULL) {
811 814 PyErr_NoMemory();
812 815 return NULL;
813 816 }
814 817
815 818 for (i = 0; i < numphases; ++i) {
816 819 pyphase = PyInt_FromLong(trackedphases[i]);
817 820 if (pyphase == NULL)
818 821 goto release;
819 822 phaseroots = PyDict_GetItem(roots, pyphase);
820 823 Py_DECREF(pyphase);
821 824 if (phaseroots == NULL)
822 825 continue;
823 826 rev = add_roots_get_min(self, phaseroots, phases, trackedphases[i]);
824 827 phaseroots = NULL;
825 828 if (rev == -2)
826 829 goto release;
827 830 if (rev != -1 && (minphaserev == -1 || rev < minphaserev))
828 831 minphaserev = rev;
829 832 }
830 833
831 834 for (i = 0; i < numphases; ++i) {
832 835 phasesets[i] = PySet_New(NULL);
833 836 if (phasesets[i] == NULL)
834 837 goto release;
835 838 }
836 839
837 840 if (minphaserev == -1)
838 841 minphaserev = len;
839 842 for (rev = minphaserev; rev < len; ++rev) {
840 843 int parents[2];
841 844 /*
842 845 * The parent lookup could be skipped for phaseroots, but
843 846 * phase --force would historically not recompute them
844 847 * correctly, leaving descendents with a lower phase around.
845 848 * As such, unconditionally recompute the phase.
846 849 */
847 850 if (index_get_parents(self, rev, parents, (int)len - 1) < 0)
848 851 goto release;
849 852 set_phase_from_parents(phases, parents[0], parents[1], rev);
850 853 switch (phases[rev]) {
851 854 case 0:
852 855 continue;
853 856 case 1:
854 857 pyphase = phasesets[0];
855 858 break;
856 859 case 2:
857 860 pyphase = phasesets[1];
858 861 break;
859 862 case 32:
860 863 pyphase = phasesets[2];
861 864 break;
862 865 case 96:
863 866 pyphase = phasesets[3];
864 867 break;
865 868 default:
866 869 goto release;
867 870 }
868 871 pyrev = PyInt_FromLong(rev);
869 872 if (pyrev == NULL)
870 873 goto release;
871 874 if (PySet_Add(pyphase, pyrev) == -1) {
872 875 Py_DECREF(pyrev);
873 876 goto release;
874 877 }
875 878 Py_DECREF(pyrev);
876 879 }
877 880 phaseroots = _dict_new_presized(numphases);
878 881 if (phaseroots == NULL)
879 882 goto release;
880 883 for (i = 0; i < numphases; ++i) {
881 884 pyphase = PyInt_FromLong(trackedphases[i]);
882 885 if (pyphase == NULL)
883 886 goto release;
884 887 if (PyDict_SetItem(phaseroots, pyphase, phasesets[i]) == -1) {
885 888 Py_DECREF(pyphase);
886 889 goto release;
887 890 }
888 891 Py_DECREF(phasesets[i]);
889 892 phasesets[i] = NULL;
890 893 }
891 894 phasessize = PyInt_FromSsize_t(len);
892 895 if (phasessize == NULL)
893 896 goto release;
894 897
895 898 ret = PyTuple_Pack(2, phasessize, phaseroots);
896 899 Py_DECREF(phasessize);
897 900 Py_DECREF(phaseroots);
898 901 return ret;
899 902
900 903 release:
901 904 for (i = 0; i < numphases; ++i)
902 905 Py_XDECREF(phasesets[i]);
903 906 Py_XDECREF(phaseroots);
904 907
905 908 free(phases);
906 909 return NULL;
907 910 }
908 911
909 912 static PyObject *index_headrevs(indexObject *self, PyObject *args)
910 913 {
911 914 Py_ssize_t i, j, len;
912 915 char *nothead = NULL;
913 916 PyObject *heads = NULL;
914 917 PyObject *filter = NULL;
915 918 PyObject *filteredrevs = Py_None;
916 919
917 920 if (!PyArg_ParseTuple(args, "|O", &filteredrevs)) {
918 921 return NULL;
919 922 }
920 923
921 924 if (self->headrevs && filteredrevs == self->filteredrevs)
922 925 return list_copy(self->headrevs);
923 926
924 927 Py_DECREF(self->filteredrevs);
925 928 self->filteredrevs = filteredrevs;
926 929 Py_INCREF(filteredrevs);
927 930
928 931 if (filteredrevs != Py_None) {
929 932 filter = PyObject_GetAttrString(filteredrevs, "__contains__");
930 933 if (!filter) {
931 934 PyErr_SetString(
932 935 PyExc_TypeError,
933 936 "filteredrevs has no attribute __contains__");
934 937 goto bail;
935 938 }
936 939 }
937 940
938 941 len = index_length(self);
939 942 heads = PyList_New(0);
940 943 if (heads == NULL)
941 944 goto bail;
942 945 if (len == 0) {
943 946 PyObject *nullid = PyInt_FromLong(-1);
944 947 if (nullid == NULL || PyList_Append(heads, nullid) == -1) {
945 948 Py_XDECREF(nullid);
946 949 goto bail;
947 950 }
948 951 goto done;
949 952 }
950 953
951 954 nothead = calloc(len, 1);
952 955 if (nothead == NULL) {
953 956 PyErr_NoMemory();
954 957 goto bail;
955 958 }
956 959
957 960 for (i = len - 1; i >= 0; i--) {
958 961 int isfiltered;
959 962 int parents[2];
960 963
961 964 /* If nothead[i] == 1, it means we've seen an unfiltered child
962 965 * of this node already, and therefore this node is not
963 966 * filtered. So we can skip the expensive check_filter step.
964 967 */
965 968 if (nothead[i] != 1) {
966 969 isfiltered = check_filter(filter, i);
967 970 if (isfiltered == -1) {
968 971 PyErr_SetString(PyExc_TypeError,
969 972 "unable to check filter");
970 973 goto bail;
971 974 }
972 975
973 976 if (isfiltered) {
974 977 nothead[i] = 1;
975 978 continue;
976 979 }
977 980 }
978 981
979 982 if (index_get_parents(self, i, parents, (int)len - 1) < 0)
980 983 goto bail;
981 984 for (j = 0; j < 2; j++) {
982 985 if (parents[j] >= 0)
983 986 nothead[parents[j]] = 1;
984 987 }
985 988 }
986 989
987 990 for (i = 0; i < len; i++) {
988 991 PyObject *head;
989 992
990 993 if (nothead[i])
991 994 continue;
992 995 head = PyInt_FromSsize_t(i);
993 996 if (head == NULL || PyList_Append(heads, head) == -1) {
994 997 Py_XDECREF(head);
995 998 goto bail;
996 999 }
997 1000 }
998 1001
999 1002 done:
1000 1003 self->headrevs = heads;
1001 1004 Py_XDECREF(filter);
1002 1005 free(nothead);
1003 1006 return list_copy(self->headrevs);
1004 1007 bail:
1005 1008 Py_XDECREF(filter);
1006 1009 Py_XDECREF(heads);
1007 1010 free(nothead);
1008 1011 return NULL;
1009 1012 }
1010 1013
1011 1014 /**
1012 1015 * Obtain the base revision index entry.
1013 1016 *
1014 1017 * Callers must ensure that rev >= 0 or illegal memory access may occur.
1015 1018 */
1016 1019 static inline int index_baserev(indexObject *self, int rev)
1017 1020 {
1018 1021 const char *data;
1019 1022 int result;
1020 1023
1021 1024 if (rev >= self->length) {
1022 1025 PyObject *tuple =
1023 1026 PyList_GET_ITEM(self->added, rev - self->length);
1024 1027 long ret;
1025 1028 if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 3), &ret)) {
1026 1029 return -2;
1027 1030 }
1028 1031 result = (int)ret;
1029 1032 } else {
1030 1033 data = index_deref(self, rev);
1031 1034 if (data == NULL) {
1032 1035 return -2;
1033 1036 }
1034 1037
1035 1038 result = getbe32(data + 16);
1036 1039 }
1037 1040 if (result > rev) {
1038 1041 PyErr_Format(
1039 1042 PyExc_ValueError,
1040 1043 "corrupted revlog, revision base above revision: %d, %d",
1041 1044 rev, result);
1042 1045 return -2;
1043 1046 }
1044 1047 if (result < -1) {
1045 1048 PyErr_Format(
1046 1049 PyExc_ValueError,
1047 1050 "corrupted revlog, revision base out of range: %d, %d", rev,
1048 1051 result);
1049 1052 return -2;
1050 1053 }
1051 1054 return result;
1052 1055 }
1053 1056
1054 1057 /**
1055 1058 * Find if a revision is a snapshot or not
1056 1059 *
1057 1060 * Only relevant for sparse-revlog case.
1058 1061 * Callers must ensure that rev is in a valid range.
1059 1062 */
1060 1063 static int index_issnapshotrev(indexObject *self, Py_ssize_t rev)
1061 1064 {
1062 1065 int ps[2];
1063 1066 Py_ssize_t base;
1064 1067 while (rev >= 0) {
1065 1068 base = (Py_ssize_t)index_baserev(self, rev);
1066 1069 if (base == rev) {
1067 1070 base = -1;
1068 1071 }
1069 1072 if (base == -2) {
1070 1073 assert(PyErr_Occurred());
1071 1074 return -1;
1072 1075 }
1073 1076 if (base == -1) {
1074 1077 return 1;
1075 1078 }
1076 1079 if (index_get_parents(self, rev, ps, (int)rev) < 0) {
1077 1080 assert(PyErr_Occurred());
1078 1081 return -1;
1079 1082 };
1080 1083 if (base == ps[0] || base == ps[1]) {
1081 1084 return 0;
1082 1085 }
1083 1086 rev = base;
1084 1087 }
1085 1088 return rev == -1;
1086 1089 }
1087 1090
1088 1091 static PyObject *index_issnapshot(indexObject *self, PyObject *value)
1089 1092 {
1090 1093 long rev;
1091 1094 int issnap;
1092 1095 Py_ssize_t length = index_length(self);
1093 1096
1094 1097 if (!pylong_to_long(value, &rev)) {
1095 1098 return NULL;
1096 1099 }
1097 1100 if (rev < -1 || rev >= length) {
1098 1101 PyErr_Format(PyExc_ValueError, "revlog index out of range: %ld",
1099 1102 rev);
1100 1103 return NULL;
1101 1104 };
1102 1105 issnap = index_issnapshotrev(self, (Py_ssize_t)rev);
1103 1106 if (issnap < 0) {
1104 1107 return NULL;
1105 1108 };
1106 1109 return PyBool_FromLong((long)issnap);
1107 1110 }
1108 1111
1109 1112 static PyObject *index_findsnapshots(indexObject *self, PyObject *args)
1110 1113 {
1111 1114 Py_ssize_t start_rev;
1112 1115 PyObject *cache;
1113 1116 Py_ssize_t base;
1114 1117 Py_ssize_t rev;
1115 1118 PyObject *key = NULL;
1116 1119 PyObject *value = NULL;
1117 1120 const Py_ssize_t length = index_length(self);
1118 1121 if (!PyArg_ParseTuple(args, "O!n", &PyDict_Type, &cache, &start_rev)) {
1119 1122 return NULL;
1120 1123 }
1121 1124 for (rev = start_rev; rev < length; rev++) {
1122 1125 int issnap;
1123 1126 PyObject *allvalues = NULL;
1124 1127 issnap = index_issnapshotrev(self, rev);
1125 1128 if (issnap < 0) {
1126 1129 goto bail;
1127 1130 }
1128 1131 if (issnap == 0) {
1129 1132 continue;
1130 1133 }
1131 1134 base = (Py_ssize_t)index_baserev(self, rev);
1132 1135 if (base == rev) {
1133 1136 base = -1;
1134 1137 }
1135 1138 if (base == -2) {
1136 1139 assert(PyErr_Occurred());
1137 1140 goto bail;
1138 1141 }
1139 1142 key = PyInt_FromSsize_t(base);
1140 1143 allvalues = PyDict_GetItem(cache, key);
1141 1144 if (allvalues == NULL && PyErr_Occurred()) {
1142 1145 goto bail;
1143 1146 }
1144 1147 if (allvalues == NULL) {
1145 1148 int r;
1146 1149 allvalues = PyList_New(0);
1147 1150 if (!allvalues) {
1148 1151 goto bail;
1149 1152 }
1150 1153 r = PyDict_SetItem(cache, key, allvalues);
1151 1154 Py_DECREF(allvalues);
1152 1155 if (r < 0) {
1153 1156 goto bail;
1154 1157 }
1155 1158 }
1156 1159 value = PyInt_FromSsize_t(rev);
1157 1160 if (PyList_Append(allvalues, value)) {
1158 1161 goto bail;
1159 1162 }
1160 1163 Py_CLEAR(key);
1161 1164 Py_CLEAR(value);
1162 1165 }
1163 1166 Py_RETURN_NONE;
1164 1167 bail:
1165 1168 Py_XDECREF(key);
1166 1169 Py_XDECREF(value);
1167 1170 return NULL;
1168 1171 }
1169 1172
1170 1173 static PyObject *index_deltachain(indexObject *self, PyObject *args)
1171 1174 {
1172 1175 int rev, generaldelta;
1173 1176 PyObject *stoparg;
1174 1177 int stoprev, iterrev, baserev = -1;
1175 1178 int stopped;
1176 1179 PyObject *chain = NULL, *result = NULL;
1177 1180 const Py_ssize_t length = index_length(self);
1178 1181
1179 1182 if (!PyArg_ParseTuple(args, "iOi", &rev, &stoparg, &generaldelta)) {
1180 1183 return NULL;
1181 1184 }
1182 1185
1183 1186 if (PyInt_Check(stoparg)) {
1184 1187 stoprev = (int)PyInt_AsLong(stoparg);
1185 1188 if (stoprev == -1 && PyErr_Occurred()) {
1186 1189 return NULL;
1187 1190 }
1188 1191 } else if (stoparg == Py_None) {
1189 1192 stoprev = -2;
1190 1193 } else {
1191 1194 PyErr_SetString(PyExc_ValueError,
1192 1195 "stoprev must be integer or None");
1193 1196 return NULL;
1194 1197 }
1195 1198
1196 1199 if (rev < 0 || rev >= length) {
1197 1200 PyErr_SetString(PyExc_ValueError, "revlog index out of range");
1198 1201 return NULL;
1199 1202 }
1200 1203
1201 1204 chain = PyList_New(0);
1202 1205 if (chain == NULL) {
1203 1206 return NULL;
1204 1207 }
1205 1208
1206 1209 baserev = index_baserev(self, rev);
1207 1210
1208 1211 /* This should never happen. */
1209 1212 if (baserev <= -2) {
1210 1213 /* Error should be set by index_deref() */
1211 1214 assert(PyErr_Occurred());
1212 1215 goto bail;
1213 1216 }
1214 1217
1215 1218 iterrev = rev;
1216 1219
1217 1220 while (iterrev != baserev && iterrev != stoprev) {
1218 1221 PyObject *value = PyInt_FromLong(iterrev);
1219 1222 if (value == NULL) {
1220 1223 goto bail;
1221 1224 }
1222 1225 if (PyList_Append(chain, value)) {
1223 1226 Py_DECREF(value);
1224 1227 goto bail;
1225 1228 }
1226 1229 Py_DECREF(value);
1227 1230
1228 1231 if (generaldelta) {
1229 1232 iterrev = baserev;
1230 1233 } else {
1231 1234 iterrev--;
1232 1235 }
1233 1236
1234 1237 if (iterrev < 0) {
1235 1238 break;
1236 1239 }
1237 1240
1238 1241 if (iterrev >= length) {
1239 1242 PyErr_SetString(PyExc_IndexError,
1240 1243 "revision outside index");
1241 1244 return NULL;
1242 1245 }
1243 1246
1244 1247 baserev = index_baserev(self, iterrev);
1245 1248
1246 1249 /* This should never happen. */
1247 1250 if (baserev <= -2) {
1248 1251 /* Error should be set by index_deref() */
1249 1252 assert(PyErr_Occurred());
1250 1253 goto bail;
1251 1254 }
1252 1255 }
1253 1256
1254 1257 if (iterrev == stoprev) {
1255 1258 stopped = 1;
1256 1259 } else {
1257 1260 PyObject *value = PyInt_FromLong(iterrev);
1258 1261 if (value == NULL) {
1259 1262 goto bail;
1260 1263 }
1261 1264 if (PyList_Append(chain, value)) {
1262 1265 Py_DECREF(value);
1263 1266 goto bail;
1264 1267 }
1265 1268 Py_DECREF(value);
1266 1269
1267 1270 stopped = 0;
1268 1271 }
1269 1272
1270 1273 if (PyList_Reverse(chain)) {
1271 1274 goto bail;
1272 1275 }
1273 1276
1274 1277 result = Py_BuildValue("OO", chain, stopped ? Py_True : Py_False);
1275 1278 Py_DECREF(chain);
1276 1279 return result;
1277 1280
1278 1281 bail:
1279 1282 Py_DECREF(chain);
1280 1283 return NULL;
1281 1284 }
1282 1285
1283 1286 static inline int64_t
1284 1287 index_segment_span(indexObject *self, Py_ssize_t start_rev, Py_ssize_t end_rev)
1285 1288 {
1286 1289 int64_t start_offset;
1287 1290 int64_t end_offset;
1288 1291 int end_size;
1289 1292 start_offset = index_get_start(self, start_rev);
1290 1293 if (start_offset < 0) {
1291 1294 return -1;
1292 1295 }
1293 1296 end_offset = index_get_start(self, end_rev);
1294 1297 if (end_offset < 0) {
1295 1298 return -1;
1296 1299 }
1297 1300 end_size = index_get_length(self, end_rev);
1298 1301 if (end_size < 0) {
1299 1302 return -1;
1300 1303 }
1301 1304 if (end_offset < start_offset) {
1302 1305 PyErr_Format(PyExc_ValueError,
1303 1306 "corrupted revlog index: inconsistent offset "
1304 1307 "between revisions (%zd) and (%zd)",
1305 1308 start_rev, end_rev);
1306 1309 return -1;
1307 1310 }
1308 1311 return (end_offset - start_offset) + (int64_t)end_size;
1309 1312 }
1310 1313
1311 1314 /* returns endidx so that revs[startidx:endidx] has no empty trailing revs */
1312 1315 static Py_ssize_t trim_endidx(indexObject *self, const Py_ssize_t *revs,
1313 1316 Py_ssize_t startidx, Py_ssize_t endidx)
1314 1317 {
1315 1318 int length;
1316 1319 while (endidx > 1 && endidx > startidx) {
1317 1320 length = index_get_length(self, revs[endidx - 1]);
1318 1321 if (length < 0) {
1319 1322 return -1;
1320 1323 }
1321 1324 if (length != 0) {
1322 1325 break;
1323 1326 }
1324 1327 endidx -= 1;
1325 1328 }
1326 1329 return endidx;
1327 1330 }
1328 1331
1329 1332 struct Gap {
1330 1333 int64_t size;
1331 1334 Py_ssize_t idx;
1332 1335 };
1333 1336
1334 1337 static int gap_compare(const void *left, const void *right)
1335 1338 {
1336 1339 const struct Gap *l_left = ((const struct Gap *)left);
1337 1340 const struct Gap *l_right = ((const struct Gap *)right);
1338 1341 if (l_left->size < l_right->size) {
1339 1342 return -1;
1340 1343 } else if (l_left->size > l_right->size) {
1341 1344 return 1;
1342 1345 }
1343 1346 return 0;
1344 1347 }
1345 1348 static int Py_ssize_t_compare(const void *left, const void *right)
1346 1349 {
1347 1350 const Py_ssize_t l_left = *(const Py_ssize_t *)left;
1348 1351 const Py_ssize_t l_right = *(const Py_ssize_t *)right;
1349 1352 if (l_left < l_right) {
1350 1353 return -1;
1351 1354 } else if (l_left > l_right) {
1352 1355 return 1;
1353 1356 }
1354 1357 return 0;
1355 1358 }
1356 1359
1357 1360 static PyObject *index_slicechunktodensity(indexObject *self, PyObject *args)
1358 1361 {
1359 1362 /* method arguments */
1360 1363 PyObject *list_revs = NULL; /* revisions in the chain */
1361 1364 double targetdensity = 0; /* min density to achieve */
1362 1365 Py_ssize_t mingapsize = 0; /* threshold to ignore gaps */
1363 1366
1364 1367 /* other core variables */
1365 1368 Py_ssize_t idxlen = index_length(self);
1366 1369 Py_ssize_t i; /* used for various iteration */
1367 1370 PyObject *result = NULL; /* the final return of the function */
1368 1371
1369 1372 /* generic information about the delta chain being slice */
1370 1373 Py_ssize_t num_revs = 0; /* size of the full delta chain */
1371 1374 Py_ssize_t *revs = NULL; /* native array of revision in the chain */
1372 1375 int64_t chainpayload = 0; /* sum of all delta in the chain */
1373 1376 int64_t deltachainspan = 0; /* distance from first byte to last byte */
1374 1377
1375 1378 /* variable used for slicing the delta chain */
1376 1379 int64_t readdata = 0; /* amount of data currently planned to be read */
1377 1380 double density = 0; /* ration of payload data compared to read ones */
1378 1381 int64_t previous_end;
1379 1382 struct Gap *gaps = NULL; /* array of notable gap in the chain */
1380 1383 Py_ssize_t num_gaps =
1381 1384 0; /* total number of notable gap recorded so far */
1382 1385 Py_ssize_t *selected_indices = NULL; /* indices of gap skipped over */
1383 1386 Py_ssize_t num_selected = 0; /* number of gaps skipped */
1384 1387 PyObject *chunk = NULL; /* individual slice */
1385 1388 PyObject *allchunks = NULL; /* all slices */
1386 1389 Py_ssize_t previdx;
1387 1390
1388 1391 /* parsing argument */
1389 1392 if (!PyArg_ParseTuple(args, "O!dn", &PyList_Type, &list_revs,
1390 1393 &targetdensity, &mingapsize)) {
1391 1394 goto bail;
1392 1395 }
1393 1396
1394 1397 /* If the delta chain contains a single element, we do not need slicing
1395 1398 */
1396 1399 num_revs = PyList_GET_SIZE(list_revs);
1397 1400 if (num_revs <= 1) {
1398 1401 result = PyTuple_Pack(1, list_revs);
1399 1402 goto done;
1400 1403 }
1401 1404
1402 1405 /* Turn the python list into a native integer array (for efficiency) */
1403 1406 revs = (Py_ssize_t *)calloc(num_revs, sizeof(Py_ssize_t));
1404 1407 if (revs == NULL) {
1405 1408 PyErr_NoMemory();
1406 1409 goto bail;
1407 1410 }
1408 1411 for (i = 0; i < num_revs; i++) {
1409 1412 Py_ssize_t revnum = PyInt_AsLong(PyList_GET_ITEM(list_revs, i));
1410 1413 if (revnum == -1 && PyErr_Occurred()) {
1411 1414 goto bail;
1412 1415 }
1413 1416 if (revnum < nullrev || revnum >= idxlen) {
1414 1417 PyErr_Format(PyExc_IndexError,
1415 1418 "index out of range: %zd", revnum);
1416 1419 goto bail;
1417 1420 }
1418 1421 revs[i] = revnum;
1419 1422 }
1420 1423
1421 1424 /* Compute and check various property of the unsliced delta chain */
1422 1425 deltachainspan = index_segment_span(self, revs[0], revs[num_revs - 1]);
1423 1426 if (deltachainspan < 0) {
1424 1427 goto bail;
1425 1428 }
1426 1429
1427 1430 if (deltachainspan <= mingapsize) {
1428 1431 result = PyTuple_Pack(1, list_revs);
1429 1432 goto done;
1430 1433 }
1431 1434 chainpayload = 0;
1432 1435 for (i = 0; i < num_revs; i++) {
1433 1436 int tmp = index_get_length(self, revs[i]);
1434 1437 if (tmp < 0) {
1435 1438 goto bail;
1436 1439 }
1437 1440 chainpayload += tmp;
1438 1441 }
1439 1442
1440 1443 readdata = deltachainspan;
1441 1444 density = 1.0;
1442 1445
1443 1446 if (0 < deltachainspan) {
1444 1447 density = (double)chainpayload / (double)deltachainspan;
1445 1448 }
1446 1449
1447 1450 if (density >= targetdensity) {
1448 1451 result = PyTuple_Pack(1, list_revs);
1449 1452 goto done;
1450 1453 }
1451 1454
1452 1455 /* if chain is too sparse, look for relevant gaps */
1453 1456 gaps = (struct Gap *)calloc(num_revs, sizeof(struct Gap));
1454 1457 if (gaps == NULL) {
1455 1458 PyErr_NoMemory();
1456 1459 goto bail;
1457 1460 }
1458 1461
1459 1462 previous_end = -1;
1460 1463 for (i = 0; i < num_revs; i++) {
1461 1464 int64_t revstart;
1462 1465 int revsize;
1463 1466 revstart = index_get_start(self, revs[i]);
1464 1467 if (revstart < 0) {
1465 1468 goto bail;
1466 1469 };
1467 1470 revsize = index_get_length(self, revs[i]);
1468 1471 if (revsize < 0) {
1469 1472 goto bail;
1470 1473 };
1471 1474 if (revsize == 0) {
1472 1475 continue;
1473 1476 }
1474 1477 if (previous_end >= 0) {
1475 1478 int64_t gapsize = revstart - previous_end;
1476 1479 if (gapsize > mingapsize) {
1477 1480 gaps[num_gaps].size = gapsize;
1478 1481 gaps[num_gaps].idx = i;
1479 1482 num_gaps += 1;
1480 1483 }
1481 1484 }
1482 1485 previous_end = revstart + revsize;
1483 1486 }
1484 1487 if (num_gaps == 0) {
1485 1488 result = PyTuple_Pack(1, list_revs);
1486 1489 goto done;
1487 1490 }
1488 1491 qsort(gaps, num_gaps, sizeof(struct Gap), &gap_compare);
1489 1492
1490 1493 /* Slice the largest gap first, they improve the density the most */
1491 1494 selected_indices =
1492 1495 (Py_ssize_t *)malloc((num_gaps + 1) * sizeof(Py_ssize_t));
1493 1496 if (selected_indices == NULL) {
1494 1497 PyErr_NoMemory();
1495 1498 goto bail;
1496 1499 }
1497 1500
1498 1501 for (i = num_gaps - 1; i >= 0; i--) {
1499 1502 selected_indices[num_selected] = gaps[i].idx;
1500 1503 readdata -= gaps[i].size;
1501 1504 num_selected += 1;
1502 1505 if (readdata <= 0) {
1503 1506 density = 1.0;
1504 1507 } else {
1505 1508 density = (double)chainpayload / (double)readdata;
1506 1509 }
1507 1510 if (density >= targetdensity) {
1508 1511 break;
1509 1512 }
1510 1513 }
1511 1514 qsort(selected_indices, num_selected, sizeof(Py_ssize_t),
1512 1515 &Py_ssize_t_compare);
1513 1516
1514 1517 /* create the resulting slice */
1515 1518 allchunks = PyList_New(0);
1516 1519 if (allchunks == NULL) {
1517 1520 goto bail;
1518 1521 }
1519 1522 previdx = 0;
1520 1523 selected_indices[num_selected] = num_revs;
1521 1524 for (i = 0; i <= num_selected; i++) {
1522 1525 Py_ssize_t idx = selected_indices[i];
1523 1526 Py_ssize_t endidx = trim_endidx(self, revs, previdx, idx);
1524 1527 if (endidx < 0) {
1525 1528 goto bail;
1526 1529 }
1527 1530 if (previdx < endidx) {
1528 1531 chunk = PyList_GetSlice(list_revs, previdx, endidx);
1529 1532 if (chunk == NULL) {
1530 1533 goto bail;
1531 1534 }
1532 1535 if (PyList_Append(allchunks, chunk) == -1) {
1533 1536 goto bail;
1534 1537 }
1535 1538 Py_DECREF(chunk);
1536 1539 chunk = NULL;
1537 1540 }
1538 1541 previdx = idx;
1539 1542 }
1540 1543 result = allchunks;
1541 1544 goto done;
1542 1545
1543 1546 bail:
1544 1547 Py_XDECREF(allchunks);
1545 1548 Py_XDECREF(chunk);
1546 1549 done:
1547 1550 free(revs);
1548 1551 free(gaps);
1549 1552 free(selected_indices);
1550 1553 return result;
1551 1554 }
1552 1555
1553 1556 static inline int nt_level(const char *node, Py_ssize_t level)
1554 1557 {
1555 1558 int v = node[level >> 1];
1556 1559 if (!(level & 1))
1557 1560 v >>= 4;
1558 1561 return v & 0xf;
1559 1562 }
1560 1563
1561 1564 /*
1562 1565 * Return values:
1563 1566 *
1564 1567 * -4: match is ambiguous (multiple candidates)
1565 1568 * -2: not found
1566 1569 * rest: valid rev
1567 1570 */
1568 1571 static int nt_find(nodetree *self, const char *node, Py_ssize_t nodelen,
1569 1572 int hex)
1570 1573 {
1571 1574 int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level;
1572 1575 int level, maxlevel, off;
1573 1576
1574 1577 if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0)
1575 1578 return -1;
1576 1579
1577 1580 if (hex)
1578 1581 maxlevel = nodelen > 40 ? 40 : (int)nodelen;
1579 1582 else
1580 1583 maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2);
1581 1584
1582 1585 for (level = off = 0; level < maxlevel; level++) {
1583 1586 int k = getnybble(node, level);
1584 1587 nodetreenode *n = &self->nodes[off];
1585 1588 int v = n->children[k];
1586 1589
1587 1590 if (v < 0) {
1588 1591 const char *n;
1589 1592 Py_ssize_t i;
1590 1593
1591 1594 v = -(v + 2);
1592 1595 n = index_node(self->index, v);
1593 1596 if (n == NULL)
1594 1597 return -2;
1595 1598 for (i = level; i < maxlevel; i++)
1596 1599 if (getnybble(node, i) != nt_level(n, i))
1597 1600 return -2;
1598 1601 return v;
1599 1602 }
1600 1603 if (v == 0)
1601 1604 return -2;
1602 1605 off = v;
1603 1606 }
1604 1607 /* multiple matches against an ambiguous prefix */
1605 1608 return -4;
1606 1609 }
1607 1610
1608 1611 static int nt_new(nodetree *self)
1609 1612 {
1610 1613 if (self->length == self->capacity) {
1611 1614 unsigned newcapacity;
1612 1615 nodetreenode *newnodes;
1613 1616 newcapacity = self->capacity * 2;
1614 1617 if (newcapacity >= INT_MAX / sizeof(nodetreenode)) {
1615 1618 PyErr_SetString(PyExc_MemoryError,
1616 1619 "overflow in nt_new");
1617 1620 return -1;
1618 1621 }
1619 1622 newnodes =
1620 1623 realloc(self->nodes, newcapacity * sizeof(nodetreenode));
1621 1624 if (newnodes == NULL) {
1622 1625 PyErr_SetString(PyExc_MemoryError, "out of memory");
1623 1626 return -1;
1624 1627 }
1625 1628 self->capacity = newcapacity;
1626 1629 self->nodes = newnodes;
1627 1630 memset(&self->nodes[self->length], 0,
1628 1631 sizeof(nodetreenode) * (self->capacity - self->length));
1629 1632 }
1630 1633 return self->length++;
1631 1634 }
1632 1635
1633 1636 static int nt_insert(nodetree *self, const char *node, int rev)
1634 1637 {
1635 1638 int level = 0;
1636 1639 int off = 0;
1637 1640
1638 1641 while (level < 40) {
1639 1642 int k = nt_level(node, level);
1640 1643 nodetreenode *n;
1641 1644 int v;
1642 1645
1643 1646 n = &self->nodes[off];
1644 1647 v = n->children[k];
1645 1648
1646 1649 if (v == 0) {
1647 1650 n->children[k] = -rev - 2;
1648 1651 return 0;
1649 1652 }
1650 1653 if (v < 0) {
1651 1654 const char *oldnode =
1652 1655 index_node_existing(self->index, -(v + 2));
1653 1656 int noff;
1654 1657
1655 1658 if (oldnode == NULL)
1656 1659 return -1;
1657 1660 if (!memcmp(oldnode, node, 20)) {
1658 1661 n->children[k] = -rev - 2;
1659 1662 return 0;
1660 1663 }
1661 1664 noff = nt_new(self);
1662 1665 if (noff == -1)
1663 1666 return -1;
1664 1667 /* self->nodes may have been changed by realloc */
1665 1668 self->nodes[off].children[k] = noff;
1666 1669 off = noff;
1667 1670 n = &self->nodes[off];
1668 1671 n->children[nt_level(oldnode, ++level)] = v;
1669 1672 if (level > self->depth)
1670 1673 self->depth = level;
1671 1674 self->splits += 1;
1672 1675 } else {
1673 1676 level += 1;
1674 1677 off = v;
1675 1678 }
1676 1679 }
1677 1680
1678 1681 return -1;
1679 1682 }
1680 1683
1681 1684 static PyObject *ntobj_insert(nodetreeObject *self, PyObject *args)
1682 1685 {
1683 1686 Py_ssize_t rev;
1684 1687 const char *node;
1685 1688 Py_ssize_t length;
1686 1689 if (!PyArg_ParseTuple(args, "n", &rev))
1687 1690 return NULL;
1688 1691 length = index_length(self->nt.index);
1689 1692 if (rev < 0 || rev >= length) {
1690 1693 PyErr_SetString(PyExc_ValueError, "revlog index out of range");
1691 1694 return NULL;
1692 1695 }
1693 1696 node = index_node_existing(self->nt.index, rev);
1694 1697 if (nt_insert(&self->nt, node, (int)rev) == -1)
1695 1698 return NULL;
1696 1699 Py_RETURN_NONE;
1697 1700 }
1698 1701
1699 1702 static int nt_delete_node(nodetree *self, const char *node)
1700 1703 {
1701 1704 /* rev==-2 happens to get encoded as 0, which is interpreted as not set
1702 1705 */
1703 1706 return nt_insert(self, node, -2);
1704 1707 }
1705 1708
1706 1709 static int nt_init(nodetree *self, indexObject *index, unsigned capacity)
1707 1710 {
1708 1711 /* Initialize before overflow-checking to avoid nt_dealloc() crash. */
1709 1712 self->nodes = NULL;
1710 1713
1711 1714 self->index = index;
1712 1715 /* The input capacity is in terms of revisions, while the field is in
1713 1716 * terms of nodetree nodes. */
1714 1717 self->capacity = (capacity < 4 ? 4 : capacity / 2);
1715 1718 self->depth = 0;
1716 1719 self->splits = 0;
1717 1720 if ((size_t)self->capacity > INT_MAX / sizeof(nodetreenode)) {
1718 1721 PyErr_SetString(PyExc_ValueError, "overflow in init_nt");
1719 1722 return -1;
1720 1723 }
1721 1724 self->nodes = calloc(self->capacity, sizeof(nodetreenode));
1722 1725 if (self->nodes == NULL) {
1723 1726 PyErr_NoMemory();
1724 1727 return -1;
1725 1728 }
1726 1729 self->length = 1;
1727 1730 return 0;
1728 1731 }
1729 1732
1730 1733 static int ntobj_init(nodetreeObject *self, PyObject *args)
1731 1734 {
1732 1735 PyObject *index;
1733 1736 unsigned capacity;
1734 1737 if (!PyArg_ParseTuple(args, "O!I", &HgRevlogIndex_Type, &index,
1735 1738 &capacity))
1736 1739 return -1;
1737 1740 Py_INCREF(index);
1738 1741 return nt_init(&self->nt, (indexObject *)index, capacity);
1739 1742 }
1740 1743
1741 1744 static int nt_partialmatch(nodetree *self, const char *node, Py_ssize_t nodelen)
1742 1745 {
1743 1746 return nt_find(self, node, nodelen, 1);
1744 1747 }
1745 1748
1746 1749 /*
1747 1750 * Find the length of the shortest unique prefix of node.
1748 1751 *
1749 1752 * Return values:
1750 1753 *
1751 1754 * -3: error (exception set)
1752 1755 * -2: not found (no exception set)
1753 1756 * rest: length of shortest prefix
1754 1757 */
1755 1758 static int nt_shortest(nodetree *self, const char *node)
1756 1759 {
1757 1760 int level, off;
1758 1761
1759 1762 for (level = off = 0; level < 40; level++) {
1760 1763 int k, v;
1761 1764 nodetreenode *n = &self->nodes[off];
1762 1765 k = nt_level(node, level);
1763 1766 v = n->children[k];
1764 1767 if (v < 0) {
1765 1768 const char *n;
1766 1769 v = -(v + 2);
1767 1770 n = index_node_existing(self->index, v);
1768 1771 if (n == NULL)
1769 1772 return -3;
1770 1773 if (memcmp(node, n, 20) != 0)
1771 1774 /*
1772 1775 * Found a unique prefix, but it wasn't for the
1773 1776 * requested node (i.e the requested node does
1774 1777 * not exist).
1775 1778 */
1776 1779 return -2;
1777 1780 return level + 1;
1778 1781 }
1779 1782 if (v == 0)
1780 1783 return -2;
1781 1784 off = v;
1782 1785 }
1783 1786 /*
1784 1787 * The node was still not unique after 40 hex digits, so this won't
1785 1788 * happen. Also, if we get here, then there's a programming error in
1786 1789 * this file that made us insert a node longer than 40 hex digits.
1787 1790 */
1788 1791 PyErr_SetString(PyExc_Exception, "broken node tree");
1789 1792 return -3;
1790 1793 }
1791 1794
1792 1795 static PyObject *ntobj_shortest(nodetreeObject *self, PyObject *args)
1793 1796 {
1794 1797 PyObject *val;
1795 1798 char *node;
1796 1799 int length;
1797 1800
1798 1801 if (!PyArg_ParseTuple(args, "O", &val))
1799 1802 return NULL;
1800 1803 if (node_check(val, &node) == -1)
1801 1804 return NULL;
1802 1805
1803 1806 length = nt_shortest(&self->nt, node);
1804 1807 if (length == -3)
1805 1808 return NULL;
1806 1809 if (length == -2) {
1807 1810 raise_revlog_error();
1808 1811 return NULL;
1809 1812 }
1810 1813 return PyInt_FromLong(length);
1811 1814 }
1812 1815
1813 1816 static void nt_dealloc(nodetree *self)
1814 1817 {
1815 1818 free(self->nodes);
1816 1819 self->nodes = NULL;
1817 1820 }
1818 1821
1819 1822 static void ntobj_dealloc(nodetreeObject *self)
1820 1823 {
1821 1824 Py_XDECREF(self->nt.index);
1822 1825 nt_dealloc(&self->nt);
1823 1826 PyObject_Del(self);
1824 1827 }
1825 1828
1826 1829 static PyMethodDef ntobj_methods[] = {
1827 1830 {"insert", (PyCFunction)ntobj_insert, METH_VARARGS,
1828 1831 "insert an index entry"},
1829 1832 {"shortest", (PyCFunction)ntobj_shortest, METH_VARARGS,
1830 1833 "find length of shortest hex nodeid of a binary ID"},
1831 1834 {NULL} /* Sentinel */
1832 1835 };
1833 1836
1834 1837 static PyTypeObject nodetreeType = {
1835 1838 PyVarObject_HEAD_INIT(NULL, 0) /* header */
1836 1839 "parsers.nodetree", /* tp_name */
1837 1840 sizeof(nodetreeObject), /* tp_basicsize */
1838 1841 0, /* tp_itemsize */
1839 1842 (destructor)ntobj_dealloc, /* tp_dealloc */
1840 1843 0, /* tp_print */
1841 1844 0, /* tp_getattr */
1842 1845 0, /* tp_setattr */
1843 1846 0, /* tp_compare */
1844 1847 0, /* tp_repr */
1845 1848 0, /* tp_as_number */
1846 1849 0, /* tp_as_sequence */
1847 1850 0, /* tp_as_mapping */
1848 1851 0, /* tp_hash */
1849 1852 0, /* tp_call */
1850 1853 0, /* tp_str */
1851 1854 0, /* tp_getattro */
1852 1855 0, /* tp_setattro */
1853 1856 0, /* tp_as_buffer */
1854 1857 Py_TPFLAGS_DEFAULT, /* tp_flags */
1855 1858 "nodetree", /* tp_doc */
1856 1859 0, /* tp_traverse */
1857 1860 0, /* tp_clear */
1858 1861 0, /* tp_richcompare */
1859 1862 0, /* tp_weaklistoffset */
1860 1863 0, /* tp_iter */
1861 1864 0, /* tp_iternext */
1862 1865 ntobj_methods, /* tp_methods */
1863 1866 0, /* tp_members */
1864 1867 0, /* tp_getset */
1865 1868 0, /* tp_base */
1866 1869 0, /* tp_dict */
1867 1870 0, /* tp_descr_get */
1868 1871 0, /* tp_descr_set */
1869 1872 0, /* tp_dictoffset */
1870 1873 (initproc)ntobj_init, /* tp_init */
1871 1874 0, /* tp_alloc */
1872 1875 };
1873 1876
1874 1877 static int index_init_nt(indexObject *self)
1875 1878 {
1876 1879 if (!self->ntinitialized) {
1877 1880 if (nt_init(&self->nt, self, (int)self->raw_length) == -1) {
1878 1881 nt_dealloc(&self->nt);
1879 1882 return -1;
1880 1883 }
1881 1884 if (nt_insert(&self->nt, nullid, -1) == -1) {
1882 1885 nt_dealloc(&self->nt);
1883 1886 return -1;
1884 1887 }
1885 1888 self->ntinitialized = 1;
1886 1889 self->ntrev = (int)index_length(self);
1887 1890 self->ntlookups = 1;
1888 1891 self->ntmisses = 0;
1889 1892 }
1890 1893 return 0;
1891 1894 }
1892 1895
1893 1896 /*
1894 1897 * Return values:
1895 1898 *
1896 1899 * -3: error (exception set)
1897 1900 * -2: not found (no exception set)
1898 1901 * rest: valid rev
1899 1902 */
1900 1903 static int index_find_node(indexObject *self, const char *node,
1901 1904 Py_ssize_t nodelen)
1902 1905 {
1903 1906 int rev;
1904 1907
1905 1908 if (index_init_nt(self) == -1)
1906 1909 return -3;
1907 1910
1908 1911 self->ntlookups++;
1909 1912 rev = nt_find(&self->nt, node, nodelen, 0);
1910 1913 if (rev >= -1)
1911 1914 return rev;
1912 1915
1913 1916 /*
1914 1917 * For the first handful of lookups, we scan the entire index,
1915 1918 * and cache only the matching nodes. This optimizes for cases
1916 1919 * like "hg tip", where only a few nodes are accessed.
1917 1920 *
1918 1921 * After that, we cache every node we visit, using a single
1919 1922 * scan amortized over multiple lookups. This gives the best
1920 1923 * bulk performance, e.g. for "hg log".
1921 1924 */
1922 1925 if (self->ntmisses++ < 4) {
1923 1926 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1924 1927 const char *n = index_node_existing(self, rev);
1925 1928 if (n == NULL)
1926 1929 return -3;
1927 1930 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1928 1931 if (nt_insert(&self->nt, n, rev) == -1)
1929 1932 return -3;
1930 1933 break;
1931 1934 }
1932 1935 }
1933 1936 } else {
1934 1937 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1935 1938 const char *n = index_node_existing(self, rev);
1936 1939 if (n == NULL)
1937 1940 return -3;
1938 1941 if (nt_insert(&self->nt, n, rev) == -1) {
1939 1942 self->ntrev = rev + 1;
1940 1943 return -3;
1941 1944 }
1942 1945 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1943 1946 break;
1944 1947 }
1945 1948 }
1946 1949 self->ntrev = rev;
1947 1950 }
1948 1951
1949 1952 if (rev >= 0)
1950 1953 return rev;
1951 1954 return -2;
1952 1955 }
1953 1956
1954 1957 static PyObject *index_getitem(indexObject *self, PyObject *value)
1955 1958 {
1956 1959 char *node;
1957 1960 int rev;
1958 1961
1959 1962 if (PyInt_Check(value)) {
1960 1963 long idx;
1961 1964 if (!pylong_to_long(value, &idx)) {
1962 1965 return NULL;
1963 1966 }
1964 1967 return index_get(self, idx);
1965 1968 }
1966 1969
1967 1970 if (node_check(value, &node) == -1)
1968 1971 return NULL;
1969 1972 rev = index_find_node(self, node, 20);
1970 1973 if (rev >= -1)
1971 1974 return PyInt_FromLong(rev);
1972 1975 if (rev == -2)
1973 1976 raise_revlog_error();
1974 1977 return NULL;
1975 1978 }
1976 1979
1977 1980 /*
1978 1981 * Fully populate the radix tree.
1979 1982 */
1980 1983 static int index_populate_nt(indexObject *self)
1981 1984 {
1982 1985 int rev;
1983 1986 if (self->ntrev > 0) {
1984 1987 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1985 1988 const char *n = index_node_existing(self, rev);
1986 1989 if (n == NULL)
1987 1990 return -1;
1988 1991 if (nt_insert(&self->nt, n, rev) == -1)
1989 1992 return -1;
1990 1993 }
1991 1994 self->ntrev = -1;
1992 1995 }
1993 1996 return 0;
1994 1997 }
1995 1998
1996 1999 static PyObject *index_partialmatch(indexObject *self, PyObject *args)
1997 2000 {
1998 2001 const char *fullnode;
1999 2002 Py_ssize_t nodelen;
2000 2003 char *node;
2001 2004 int rev, i;
2002 2005
2003 2006 if (!PyArg_ParseTuple(args, PY23("s#", "y#"), &node, &nodelen))
2004 2007 return NULL;
2005 2008
2006 2009 if (nodelen < 1) {
2007 2010 PyErr_SetString(PyExc_ValueError, "key too short");
2008 2011 return NULL;
2009 2012 }
2010 2013
2011 2014 if (nodelen > 40) {
2012 2015 PyErr_SetString(PyExc_ValueError, "key too long");
2013 2016 return NULL;
2014 2017 }
2015 2018
2016 2019 for (i = 0; i < nodelen; i++)
2017 2020 hexdigit(node, i);
2018 2021 if (PyErr_Occurred()) {
2019 2022 /* input contains non-hex characters */
2020 2023 PyErr_Clear();
2021 2024 Py_RETURN_NONE;
2022 2025 }
2023 2026
2024 2027 if (index_init_nt(self) == -1)
2025 2028 return NULL;
2026 2029 if (index_populate_nt(self) == -1)
2027 2030 return NULL;
2028 2031 rev = nt_partialmatch(&self->nt, node, nodelen);
2029 2032
2030 2033 switch (rev) {
2031 2034 case -4:
2032 2035 raise_revlog_error();
2033 2036 return NULL;
2034 2037 case -2:
2035 2038 Py_RETURN_NONE;
2036 2039 case -1:
2037 2040 return PyBytes_FromStringAndSize(nullid, 20);
2038 2041 }
2039 2042
2040 2043 fullnode = index_node_existing(self, rev);
2041 2044 if (fullnode == NULL) {
2042 2045 return NULL;
2043 2046 }
2044 2047 return PyBytes_FromStringAndSize(fullnode, 20);
2045 2048 }
2046 2049
2047 2050 static PyObject *index_shortest(indexObject *self, PyObject *args)
2048 2051 {
2049 2052 PyObject *val;
2050 2053 char *node;
2051 2054 int length;
2052 2055
2053 2056 if (!PyArg_ParseTuple(args, "O", &val))
2054 2057 return NULL;
2055 2058 if (node_check(val, &node) == -1)
2056 2059 return NULL;
2057 2060
2058 2061 self->ntlookups++;
2059 2062 if (index_init_nt(self) == -1)
2060 2063 return NULL;
2061 2064 if (index_populate_nt(self) == -1)
2062 2065 return NULL;
2063 2066 length = nt_shortest(&self->nt, node);
2064 2067 if (length == -3)
2065 2068 return NULL;
2066 2069 if (length == -2) {
2067 2070 raise_revlog_error();
2068 2071 return NULL;
2069 2072 }
2070 2073 return PyInt_FromLong(length);
2071 2074 }
2072 2075
2073 2076 static PyObject *index_m_get(indexObject *self, PyObject *args)
2074 2077 {
2075 2078 PyObject *val;
2076 2079 char *node;
2077 2080 int rev;
2078 2081
2079 2082 if (!PyArg_ParseTuple(args, "O", &val))
2080 2083 return NULL;
2081 2084 if (node_check(val, &node) == -1)
2082 2085 return NULL;
2083 2086 rev = index_find_node(self, node, 20);
2084 2087 if (rev == -3)
2085 2088 return NULL;
2086 2089 if (rev == -2)
2087 2090 Py_RETURN_NONE;
2088 2091 return PyInt_FromLong(rev);
2089 2092 }
2090 2093
2091 2094 static int index_contains(indexObject *self, PyObject *value)
2092 2095 {
2093 2096 char *node;
2094 2097
2095 2098 if (PyInt_Check(value)) {
2096 2099 long rev;
2097 2100 if (!pylong_to_long(value, &rev)) {
2098 2101 return -1;
2099 2102 }
2100 2103 return rev >= -1 && rev < index_length(self);
2101 2104 }
2102 2105
2103 2106 if (node_check(value, &node) == -1)
2104 2107 return -1;
2105 2108
2106 2109 switch (index_find_node(self, node, 20)) {
2107 2110 case -3:
2108 2111 return -1;
2109 2112 case -2:
2110 2113 return 0;
2111 2114 default:
2112 2115 return 1;
2113 2116 }
2114 2117 }
2115 2118
2116 2119 static PyObject *index_m_has_node(indexObject *self, PyObject *args)
2117 2120 {
2118 2121 int ret = index_contains(self, args);
2119 2122 if (ret < 0)
2120 2123 return NULL;
2121 2124 return PyBool_FromLong((long)ret);
2122 2125 }
2123 2126
2124 2127 static PyObject *index_m_rev(indexObject *self, PyObject *val)
2125 2128 {
2126 2129 char *node;
2127 2130 int rev;
2128 2131
2129 2132 if (node_check(val, &node) == -1)
2130 2133 return NULL;
2131 2134 rev = index_find_node(self, node, 20);
2132 2135 if (rev >= -1)
2133 2136 return PyInt_FromLong(rev);
2134 2137 if (rev == -2)
2135 2138 raise_revlog_error();
2136 2139 return NULL;
2137 2140 }
2138 2141
2139 2142 typedef uint64_t bitmask;
2140 2143
2141 2144 /*
2142 2145 * Given a disjoint set of revs, return all candidates for the
2143 2146 * greatest common ancestor. In revset notation, this is the set
2144 2147 * "heads(::a and ::b and ...)"
2145 2148 */
2146 2149 static PyObject *find_gca_candidates(indexObject *self, const int *revs,
2147 2150 int revcount)
2148 2151 {
2149 2152 const bitmask allseen = (1ull << revcount) - 1;
2150 2153 const bitmask poison = 1ull << revcount;
2151 2154 PyObject *gca = PyList_New(0);
2152 2155 int i, v, interesting;
2153 2156 int maxrev = -1;
2154 2157 bitmask sp;
2155 2158 bitmask *seen;
2156 2159
2157 2160 if (gca == NULL)
2158 2161 return PyErr_NoMemory();
2159 2162
2160 2163 for (i = 0; i < revcount; i++) {
2161 2164 if (revs[i] > maxrev)
2162 2165 maxrev = revs[i];
2163 2166 }
2164 2167
2165 2168 seen = calloc(sizeof(*seen), maxrev + 1);
2166 2169 if (seen == NULL) {
2167 2170 Py_DECREF(gca);
2168 2171 return PyErr_NoMemory();
2169 2172 }
2170 2173
2171 2174 for (i = 0; i < revcount; i++)
2172 2175 seen[revs[i]] = 1ull << i;
2173 2176
2174 2177 interesting = revcount;
2175 2178
2176 2179 for (v = maxrev; v >= 0 && interesting; v--) {
2177 2180 bitmask sv = seen[v];
2178 2181 int parents[2];
2179 2182
2180 2183 if (!sv)
2181 2184 continue;
2182 2185
2183 2186 if (sv < poison) {
2184 2187 interesting -= 1;
2185 2188 if (sv == allseen) {
2186 2189 PyObject *obj = PyInt_FromLong(v);
2187 2190 if (obj == NULL)
2188 2191 goto bail;
2189 2192 if (PyList_Append(gca, obj) == -1) {
2190 2193 Py_DECREF(obj);
2191 2194 goto bail;
2192 2195 }
2193 2196 sv |= poison;
2194 2197 for (i = 0; i < revcount; i++) {
2195 2198 if (revs[i] == v)
2196 2199 goto done;
2197 2200 }
2198 2201 }
2199 2202 }
2200 2203 if (index_get_parents(self, v, parents, maxrev) < 0)
2201 2204 goto bail;
2202 2205
2203 2206 for (i = 0; i < 2; i++) {
2204 2207 int p = parents[i];
2205 2208 if (p == -1)
2206 2209 continue;
2207 2210 sp = seen[p];
2208 2211 if (sv < poison) {
2209 2212 if (sp == 0) {
2210 2213 seen[p] = sv;
2211 2214 interesting++;
2212 2215 } else if (sp != sv)
2213 2216 seen[p] |= sv;
2214 2217 } else {
2215 2218 if (sp && sp < poison)
2216 2219 interesting--;
2217 2220 seen[p] = sv;
2218 2221 }
2219 2222 }
2220 2223 }
2221 2224
2222 2225 done:
2223 2226 free(seen);
2224 2227 return gca;
2225 2228 bail:
2226 2229 free(seen);
2227 2230 Py_XDECREF(gca);
2228 2231 return NULL;
2229 2232 }
2230 2233
2231 2234 /*
2232 2235 * Given a disjoint set of revs, return the subset with the longest
2233 2236 * path to the root.
2234 2237 */
2235 2238 static PyObject *find_deepest(indexObject *self, PyObject *revs)
2236 2239 {
2237 2240 const Py_ssize_t revcount = PyList_GET_SIZE(revs);
2238 2241 static const Py_ssize_t capacity = 24;
2239 2242 int *depth, *interesting = NULL;
2240 2243 int i, j, v, ninteresting;
2241 2244 PyObject *dict = NULL, *keys = NULL;
2242 2245 long *seen = NULL;
2243 2246 int maxrev = -1;
2244 2247 long final;
2245 2248
2246 2249 if (revcount > capacity) {
2247 2250 PyErr_Format(PyExc_OverflowError,
2248 2251 "bitset size (%ld) > capacity (%ld)",
2249 2252 (long)revcount, (long)capacity);
2250 2253 return NULL;
2251 2254 }
2252 2255
2253 2256 for (i = 0; i < revcount; i++) {
2254 2257 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
2255 2258 if (n > maxrev)
2256 2259 maxrev = n;
2257 2260 }
2258 2261
2259 2262 depth = calloc(sizeof(*depth), maxrev + 1);
2260 2263 if (depth == NULL)
2261 2264 return PyErr_NoMemory();
2262 2265
2263 2266 seen = calloc(sizeof(*seen), maxrev + 1);
2264 2267 if (seen == NULL) {
2265 2268 PyErr_NoMemory();
2266 2269 goto bail;
2267 2270 }
2268 2271
2269 2272 interesting = calloc(sizeof(*interesting), ((size_t)1) << revcount);
2270 2273 if (interesting == NULL) {
2271 2274 PyErr_NoMemory();
2272 2275 goto bail;
2273 2276 }
2274 2277
2275 2278 if (PyList_Sort(revs) == -1)
2276 2279 goto bail;
2277 2280
2278 2281 for (i = 0; i < revcount; i++) {
2279 2282 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
2280 2283 long b = 1l << i;
2281 2284 depth[n] = 1;
2282 2285 seen[n] = b;
2283 2286 interesting[b] = 1;
2284 2287 }
2285 2288
2286 2289 /* invariant: ninteresting is the number of non-zero entries in
2287 2290 * interesting. */
2288 2291 ninteresting = (int)revcount;
2289 2292
2290 2293 for (v = maxrev; v >= 0 && ninteresting > 1; v--) {
2291 2294 int dv = depth[v];
2292 2295 int parents[2];
2293 2296 long sv;
2294 2297
2295 2298 if (dv == 0)
2296 2299 continue;
2297 2300
2298 2301 sv = seen[v];
2299 2302 if (index_get_parents(self, v, parents, maxrev) < 0)
2300 2303 goto bail;
2301 2304
2302 2305 for (i = 0; i < 2; i++) {
2303 2306 int p = parents[i];
2304 2307 long sp;
2305 2308 int dp;
2306 2309
2307 2310 if (p == -1)
2308 2311 continue;
2309 2312
2310 2313 dp = depth[p];
2311 2314 sp = seen[p];
2312 2315 if (dp <= dv) {
2313 2316 depth[p] = dv + 1;
2314 2317 if (sp != sv) {
2315 2318 interesting[sv] += 1;
2316 2319 seen[p] = sv;
2317 2320 if (sp) {
2318 2321 interesting[sp] -= 1;
2319 2322 if (interesting[sp] == 0)
2320 2323 ninteresting -= 1;
2321 2324 }
2322 2325 }
2323 2326 } else if (dv == dp - 1) {
2324 2327 long nsp = sp | sv;
2325 2328 if (nsp == sp)
2326 2329 continue;
2327 2330 seen[p] = nsp;
2328 2331 interesting[sp] -= 1;
2329 2332 if (interesting[sp] == 0)
2330 2333 ninteresting -= 1;
2331 2334 if (interesting[nsp] == 0)
2332 2335 ninteresting += 1;
2333 2336 interesting[nsp] += 1;
2334 2337 }
2335 2338 }
2336 2339 interesting[sv] -= 1;
2337 2340 if (interesting[sv] == 0)
2338 2341 ninteresting -= 1;
2339 2342 }
2340 2343
2341 2344 final = 0;
2342 2345 j = ninteresting;
2343 2346 for (i = 0; i < (int)(2 << revcount) && j > 0; i++) {
2344 2347 if (interesting[i] == 0)
2345 2348 continue;
2346 2349 final |= i;
2347 2350 j -= 1;
2348 2351 }
2349 2352 if (final == 0) {
2350 2353 keys = PyList_New(0);
2351 2354 goto bail;
2352 2355 }
2353 2356
2354 2357 dict = PyDict_New();
2355 2358 if (dict == NULL)
2356 2359 goto bail;
2357 2360
2358 2361 for (i = 0; i < revcount; i++) {
2359 2362 PyObject *key;
2360 2363
2361 2364 if ((final & (1 << i)) == 0)
2362 2365 continue;
2363 2366
2364 2367 key = PyList_GET_ITEM(revs, i);
2365 2368 Py_INCREF(key);
2366 2369 Py_INCREF(Py_None);
2367 2370 if (PyDict_SetItem(dict, key, Py_None) == -1) {
2368 2371 Py_DECREF(key);
2369 2372 Py_DECREF(Py_None);
2370 2373 goto bail;
2371 2374 }
2372 2375 }
2373 2376
2374 2377 keys = PyDict_Keys(dict);
2375 2378
2376 2379 bail:
2377 2380 free(depth);
2378 2381 free(seen);
2379 2382 free(interesting);
2380 2383 Py_XDECREF(dict);
2381 2384
2382 2385 return keys;
2383 2386 }
2384 2387
2385 2388 /*
2386 2389 * Given a (possibly overlapping) set of revs, return all the
2387 2390 * common ancestors heads: heads(::args[0] and ::a[1] and ...)
2388 2391 */
2389 2392 static PyObject *index_commonancestorsheads(indexObject *self, PyObject *args)
2390 2393 {
2391 2394 PyObject *ret = NULL;
2392 2395 Py_ssize_t argcount, i, len;
2393 2396 bitmask repeat = 0;
2394 2397 int revcount = 0;
2395 2398 int *revs;
2396 2399
2397 2400 argcount = PySequence_Length(args);
2398 2401 revs = PyMem_Malloc(argcount * sizeof(*revs));
2399 2402 if (argcount > 0 && revs == NULL)
2400 2403 return PyErr_NoMemory();
2401 2404 len = index_length(self);
2402 2405
2403 2406 for (i = 0; i < argcount; i++) {
2404 2407 static const int capacity = 24;
2405 2408 PyObject *obj = PySequence_GetItem(args, i);
2406 2409 bitmask x;
2407 2410 long val;
2408 2411
2409 2412 if (!PyInt_Check(obj)) {
2410 2413 PyErr_SetString(PyExc_TypeError,
2411 2414 "arguments must all be ints");
2412 2415 Py_DECREF(obj);
2413 2416 goto bail;
2414 2417 }
2415 2418 val = PyInt_AsLong(obj);
2416 2419 Py_DECREF(obj);
2417 2420 if (val == -1) {
2418 2421 ret = PyList_New(0);
2419 2422 goto done;
2420 2423 }
2421 2424 if (val < 0 || val >= len) {
2422 2425 PyErr_SetString(PyExc_IndexError, "index out of range");
2423 2426 goto bail;
2424 2427 }
2425 2428 /* this cheesy bloom filter lets us avoid some more
2426 2429 * expensive duplicate checks in the common set-is-disjoint
2427 2430 * case */
2428 2431 x = 1ull << (val & 0x3f);
2429 2432 if (repeat & x) {
2430 2433 int k;
2431 2434 for (k = 0; k < revcount; k++) {
2432 2435 if (val == revs[k])
2433 2436 goto duplicate;
2434 2437 }
2435 2438 } else
2436 2439 repeat |= x;
2437 2440 if (revcount >= capacity) {
2438 2441 PyErr_Format(PyExc_OverflowError,
2439 2442 "bitset size (%d) > capacity (%d)",
2440 2443 revcount, capacity);
2441 2444 goto bail;
2442 2445 }
2443 2446 revs[revcount++] = (int)val;
2444 2447 duplicate:;
2445 2448 }
2446 2449
2447 2450 if (revcount == 0) {
2448 2451 ret = PyList_New(0);
2449 2452 goto done;
2450 2453 }
2451 2454 if (revcount == 1) {
2452 2455 PyObject *obj;
2453 2456 ret = PyList_New(1);
2454 2457 if (ret == NULL)
2455 2458 goto bail;
2456 2459 obj = PyInt_FromLong(revs[0]);
2457 2460 if (obj == NULL)
2458 2461 goto bail;
2459 2462 PyList_SET_ITEM(ret, 0, obj);
2460 2463 goto done;
2461 2464 }
2462 2465
2463 2466 ret = find_gca_candidates(self, revs, revcount);
2464 2467 if (ret == NULL)
2465 2468 goto bail;
2466 2469
2467 2470 done:
2468 2471 PyMem_Free(revs);
2469 2472 return ret;
2470 2473
2471 2474 bail:
2472 2475 PyMem_Free(revs);
2473 2476 Py_XDECREF(ret);
2474 2477 return NULL;
2475 2478 }
2476 2479
2477 2480 /*
2478 2481 * Given a (possibly overlapping) set of revs, return the greatest
2479 2482 * common ancestors: those with the longest path to the root.
2480 2483 */
2481 2484 static PyObject *index_ancestors(indexObject *self, PyObject *args)
2482 2485 {
2483 2486 PyObject *ret;
2484 2487 PyObject *gca = index_commonancestorsheads(self, args);
2485 2488 if (gca == NULL)
2486 2489 return NULL;
2487 2490
2488 2491 if (PyList_GET_SIZE(gca) <= 1) {
2489 2492 return gca;
2490 2493 }
2491 2494
2492 2495 ret = find_deepest(self, gca);
2493 2496 Py_DECREF(gca);
2494 2497 return ret;
2495 2498 }
2496 2499
2497 2500 /*
2498 2501 * Invalidate any trie entries introduced by added revs.
2499 2502 */
2500 2503 static void index_invalidate_added(indexObject *self, Py_ssize_t start)
2501 2504 {
2502 2505 Py_ssize_t i, len = PyList_GET_SIZE(self->added);
2503 2506
2504 2507 for (i = start; i < len; i++) {
2505 2508 PyObject *tuple = PyList_GET_ITEM(self->added, i);
2506 2509 PyObject *node = PyTuple_GET_ITEM(tuple, 7);
2507 2510
2508 2511 nt_delete_node(&self->nt, PyBytes_AS_STRING(node));
2509 2512 }
2510 2513
2511 2514 if (start == 0)
2512 2515 Py_CLEAR(self->added);
2513 2516 }
2514 2517
2515 2518 /*
2516 2519 * Delete a numeric range of revs, which must be at the end of the
2517 2520 * range.
2518 2521 */
2519 2522 static int index_slice_del(indexObject *self, PyObject *item)
2520 2523 {
2521 2524 Py_ssize_t start, stop, step, slicelength;
2522 2525 Py_ssize_t length = index_length(self) + 1;
2523 2526 int ret = 0;
2524 2527
2525 2528 /* Argument changed from PySliceObject* to PyObject* in Python 3. */
2526 2529 #ifdef IS_PY3K
2527 2530 if (PySlice_GetIndicesEx(item, length, &start, &stop, &step,
2528 2531 &slicelength) < 0)
2529 2532 #else
2530 2533 if (PySlice_GetIndicesEx((PySliceObject *)item, length, &start, &stop,
2531 2534 &step, &slicelength) < 0)
2532 2535 #endif
2533 2536 return -1;
2534 2537
2535 2538 if (slicelength <= 0)
2536 2539 return 0;
2537 2540
2538 2541 if ((step < 0 && start < stop) || (step > 0 && start > stop))
2539 2542 stop = start;
2540 2543
2541 2544 if (step < 0) {
2542 2545 stop = start + 1;
2543 2546 start = stop + step * (slicelength - 1) - 1;
2544 2547 step = -step;
2545 2548 }
2546 2549
2547 2550 if (step != 1) {
2548 2551 PyErr_SetString(PyExc_ValueError,
2549 2552 "revlog index delete requires step size of 1");
2550 2553 return -1;
2551 2554 }
2552 2555
2553 2556 if (stop != length - 1) {
2554 2557 PyErr_SetString(PyExc_IndexError,
2555 2558 "revlog index deletion indices are invalid");
2556 2559 return -1;
2557 2560 }
2558 2561
2559 2562 if (start < self->length) {
2560 2563 if (self->ntinitialized) {
2561 2564 Py_ssize_t i;
2562 2565
2563 2566 for (i = start; i < self->length; i++) {
2564 2567 const char *node = index_node_existing(self, i);
2565 2568 if (node == NULL)
2566 2569 return -1;
2567 2570
2568 2571 nt_delete_node(&self->nt, node);
2569 2572 }
2570 2573 if (self->added)
2571 2574 index_invalidate_added(self, 0);
2572 2575 if (self->ntrev > start)
2573 2576 self->ntrev = (int)start;
2574 2577 } else if (self->added) {
2575 2578 Py_CLEAR(self->added);
2576 2579 }
2577 2580
2578 2581 self->length = start;
2579 2582 if (start < self->raw_length) {
2580 2583 if (self->cache) {
2581 2584 Py_ssize_t i;
2582 2585 for (i = start; i < self->raw_length; i++)
2583 2586 Py_CLEAR(self->cache[i]);
2584 2587 }
2585 2588 self->raw_length = start;
2586 2589 }
2587 2590 goto done;
2588 2591 }
2589 2592
2590 2593 if (self->ntinitialized) {
2591 2594 index_invalidate_added(self, start - self->length);
2592 2595 if (self->ntrev > start)
2593 2596 self->ntrev = (int)start;
2594 2597 }
2595 2598 if (self->added)
2596 2599 ret = PyList_SetSlice(self->added, start - self->length,
2597 2600 PyList_GET_SIZE(self->added), NULL);
2598 2601 done:
2599 2602 Py_CLEAR(self->headrevs);
2600 2603 return ret;
2601 2604 }
2602 2605
2603 2606 /*
2604 2607 * Supported ops:
2605 2608 *
2606 2609 * slice deletion
2607 2610 * string assignment (extend node->rev mapping)
2608 2611 * string deletion (shrink node->rev mapping)
2609 2612 */
2610 2613 static int index_assign_subscript(indexObject *self, PyObject *item,
2611 2614 PyObject *value)
2612 2615 {
2613 2616 char *node;
2614 2617 long rev;
2615 2618
2616 2619 if (PySlice_Check(item) && value == NULL)
2617 2620 return index_slice_del(self, item);
2618 2621
2619 2622 if (node_check(item, &node) == -1)
2620 2623 return -1;
2621 2624
2622 2625 if (value == NULL)
2623 2626 return self->ntinitialized ? nt_delete_node(&self->nt, node)
2624 2627 : 0;
2625 2628 rev = PyInt_AsLong(value);
2626 2629 if (rev > INT_MAX || rev < 0) {
2627 2630 if (!PyErr_Occurred())
2628 2631 PyErr_SetString(PyExc_ValueError, "rev out of range");
2629 2632 return -1;
2630 2633 }
2631 2634
2632 2635 if (index_init_nt(self) == -1)
2633 2636 return -1;
2634 2637 return nt_insert(&self->nt, node, (int)rev);
2635 2638 }
2636 2639
2637 2640 /*
2638 2641 * Find all RevlogNG entries in an index that has inline data. Update
2639 2642 * the optional "offsets" table with those entries.
2640 2643 */
2641 2644 static Py_ssize_t inline_scan(indexObject *self, const char **offsets)
2642 2645 {
2643 2646 const char *data = (const char *)self->buf.buf;
2644 2647 Py_ssize_t pos = 0;
2645 2648 Py_ssize_t end = self->buf.len;
2646 2649 long incr = v1_hdrsize;
2647 2650 Py_ssize_t len = 0;
2648 2651
2649 2652 while (pos + v1_hdrsize <= end && pos >= 0) {
2650 2653 uint32_t comp_len;
2651 2654 /* 3rd element of header is length of compressed inline data */
2652 2655 comp_len = getbe32(data + pos + 8);
2653 2656 incr = v1_hdrsize + comp_len;
2654 2657 if (offsets)
2655 2658 offsets[len] = data + pos;
2656 2659 len++;
2657 2660 pos += incr;
2658 2661 }
2659 2662
2660 2663 if (pos != end) {
2661 2664 if (!PyErr_Occurred())
2662 2665 PyErr_SetString(PyExc_ValueError, "corrupt index file");
2663 2666 return -1;
2664 2667 }
2665 2668
2666 2669 return len;
2667 2670 }
2668 2671
2669 2672 static int index_init(indexObject *self, PyObject *args)
2670 2673 {
2671 2674 PyObject *data_obj, *inlined_obj;
2672 2675 Py_ssize_t size;
2673 2676
2674 2677 /* Initialize before argument-checking to avoid index_dealloc() crash.
2675 2678 */
2676 2679 self->raw_length = 0;
2677 2680 self->added = NULL;
2678 2681 self->cache = NULL;
2679 2682 self->data = NULL;
2680 2683 memset(&self->buf, 0, sizeof(self->buf));
2681 2684 self->headrevs = NULL;
2682 2685 self->filteredrevs = Py_None;
2683 2686 Py_INCREF(Py_None);
2684 2687 self->ntinitialized = 0;
2685 2688 self->offsets = NULL;
2686 2689
2687 2690 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
2688 2691 return -1;
2689 2692 if (!PyObject_CheckBuffer(data_obj)) {
2690 2693 PyErr_SetString(PyExc_TypeError,
2691 2694 "data does not support buffer interface");
2692 2695 return -1;
2693 2696 }
2694 2697
2695 2698 if (PyObject_GetBuffer(data_obj, &self->buf, PyBUF_SIMPLE) == -1)
2696 2699 return -1;
2697 2700 size = self->buf.len;
2698 2701
2699 2702 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
2700 2703 self->data = data_obj;
2701 2704
2702 2705 self->ntlookups = self->ntmisses = 0;
2703 2706 self->ntrev = -1;
2704 2707 Py_INCREF(self->data);
2705 2708
2706 2709 if (self->inlined) {
2707 2710 Py_ssize_t len = inline_scan(self, NULL);
2708 2711 if (len == -1)
2709 2712 goto bail;
2710 2713 self->raw_length = len;
2711 2714 self->length = len;
2712 2715 } else {
2713 2716 if (size % v1_hdrsize) {
2714 2717 PyErr_SetString(PyExc_ValueError, "corrupt index file");
2715 2718 goto bail;
2716 2719 }
2717 2720 self->raw_length = size / v1_hdrsize;
2718 2721 self->length = self->raw_length;
2719 2722 }
2720 2723
2721 2724 return 0;
2722 2725 bail:
2723 2726 return -1;
2724 2727 }
2725 2728
2726 2729 static PyObject *index_nodemap(indexObject *self)
2727 2730 {
2728 2731 Py_INCREF(self);
2729 2732 return (PyObject *)self;
2730 2733 }
2731 2734
2732 2735 static void _index_clearcaches(indexObject *self)
2733 2736 {
2734 2737 if (self->cache) {
2735 2738 Py_ssize_t i;
2736 2739
2737 2740 for (i = 0; i < self->raw_length; i++)
2738 2741 Py_CLEAR(self->cache[i]);
2739 2742 free(self->cache);
2740 2743 self->cache = NULL;
2741 2744 }
2742 2745 if (self->offsets) {
2743 2746 PyMem_Free((void *)self->offsets);
2744 2747 self->offsets = NULL;
2745 2748 }
2746 2749 if (self->ntinitialized) {
2747 2750 nt_dealloc(&self->nt);
2748 2751 }
2749 2752 self->ntinitialized = 0;
2750 2753 Py_CLEAR(self->headrevs);
2751 2754 }
2752 2755
2753 2756 static PyObject *index_clearcaches(indexObject *self)
2754 2757 {
2755 2758 _index_clearcaches(self);
2756 2759 self->ntrev = -1;
2757 2760 self->ntlookups = self->ntmisses = 0;
2758 2761 Py_RETURN_NONE;
2759 2762 }
2760 2763
2761 2764 static void index_dealloc(indexObject *self)
2762 2765 {
2763 2766 _index_clearcaches(self);
2764 2767 Py_XDECREF(self->filteredrevs);
2765 2768 if (self->buf.buf) {
2766 2769 PyBuffer_Release(&self->buf);
2767 2770 memset(&self->buf, 0, sizeof(self->buf));
2768 2771 }
2769 2772 Py_XDECREF(self->data);
2770 2773 Py_XDECREF(self->added);
2771 2774 PyObject_Del(self);
2772 2775 }
2773 2776
2774 2777 static PySequenceMethods index_sequence_methods = {
2775 2778 (lenfunc)index_length, /* sq_length */
2776 2779 0, /* sq_concat */
2777 2780 0, /* sq_repeat */
2778 2781 (ssizeargfunc)index_get, /* sq_item */
2779 2782 0, /* sq_slice */
2780 2783 0, /* sq_ass_item */
2781 2784 0, /* sq_ass_slice */
2782 2785 (objobjproc)index_contains, /* sq_contains */
2783 2786 };
2784 2787
2785 2788 static PyMappingMethods index_mapping_methods = {
2786 2789 (lenfunc)index_length, /* mp_length */
2787 2790 (binaryfunc)index_getitem, /* mp_subscript */
2788 2791 (objobjargproc)index_assign_subscript, /* mp_ass_subscript */
2789 2792 };
2790 2793
2791 2794 static PyMethodDef index_methods[] = {
2792 2795 {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS,
2793 2796 "return the gca set of the given revs"},
2794 2797 {"commonancestorsheads", (PyCFunction)index_commonancestorsheads,
2795 2798 METH_VARARGS,
2796 2799 "return the heads of the common ancestors of the given revs"},
2797 2800 {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS,
2798 2801 "clear the index caches"},
2799 2802 {"get", (PyCFunction)index_m_get, METH_VARARGS, "get an index entry"},
2800 2803 {"get_rev", (PyCFunction)index_m_get, METH_VARARGS,
2801 2804 "return `rev` associated with a node or None"},
2802 2805 {"has_node", (PyCFunction)index_m_has_node, METH_O,
2803 2806 "return True if the node exist in the index"},
2804 2807 {"rev", (PyCFunction)index_m_rev, METH_O,
2805 2808 "return `rev` associated with a node or raise RevlogError"},
2806 2809 {"computephasesmapsets", (PyCFunction)compute_phases_map_sets, METH_VARARGS,
2807 2810 "compute phases"},
2808 2811 {"reachableroots2", (PyCFunction)reachableroots2, METH_VARARGS,
2809 2812 "reachableroots"},
2810 2813 {"headrevs", (PyCFunction)index_headrevs, METH_VARARGS,
2811 2814 "get head revisions"}, /* Can do filtering since 3.2 */
2812 2815 {"headrevsfiltered", (PyCFunction)index_headrevs, METH_VARARGS,
2813 2816 "get filtered head revisions"}, /* Can always do filtering */
2814 2817 {"issnapshot", (PyCFunction)index_issnapshot, METH_O,
2815 2818 "True if the object is a snapshot"},
2816 2819 {"findsnapshots", (PyCFunction)index_findsnapshots, METH_VARARGS,
2817 2820 "Gather snapshot data in a cache dict"},
2818 2821 {"deltachain", (PyCFunction)index_deltachain, METH_VARARGS,
2819 2822 "determine revisions with deltas to reconstruct fulltext"},
2820 2823 {"slicechunktodensity", (PyCFunction)index_slicechunktodensity,
2821 2824 METH_VARARGS, "determine revisions with deltas to reconstruct fulltext"},
2822 2825 {"append", (PyCFunction)index_append, METH_O, "append an index entry"},
2823 2826 {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS,
2824 2827 "match a potentially ambiguous node ID"},
2825 2828 {"shortest", (PyCFunction)index_shortest, METH_VARARGS,
2826 2829 "find length of shortest hex nodeid of a binary ID"},
2827 2830 {"stats", (PyCFunction)index_stats, METH_NOARGS, "stats for the index"},
2828 2831 {NULL} /* Sentinel */
2829 2832 };
2830 2833
2831 2834 static PyGetSetDef index_getset[] = {
2832 2835 {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL},
2833 2836 {NULL} /* Sentinel */
2834 2837 };
2835 2838
2836 2839 PyTypeObject HgRevlogIndex_Type = {
2837 2840 PyVarObject_HEAD_INIT(NULL, 0) /* header */
2838 2841 "parsers.index", /* tp_name */
2839 2842 sizeof(indexObject), /* tp_basicsize */
2840 2843 0, /* tp_itemsize */
2841 2844 (destructor)index_dealloc, /* tp_dealloc */
2842 2845 0, /* tp_print */
2843 2846 0, /* tp_getattr */
2844 2847 0, /* tp_setattr */
2845 2848 0, /* tp_compare */
2846 2849 0, /* tp_repr */
2847 2850 0, /* tp_as_number */
2848 2851 &index_sequence_methods, /* tp_as_sequence */
2849 2852 &index_mapping_methods, /* tp_as_mapping */
2850 2853 0, /* tp_hash */
2851 2854 0, /* tp_call */
2852 2855 0, /* tp_str */
2853 2856 0, /* tp_getattro */
2854 2857 0, /* tp_setattro */
2855 2858 0, /* tp_as_buffer */
2856 2859 Py_TPFLAGS_DEFAULT, /* tp_flags */
2857 2860 "revlog index", /* tp_doc */
2858 2861 0, /* tp_traverse */
2859 2862 0, /* tp_clear */
2860 2863 0, /* tp_richcompare */
2861 2864 0, /* tp_weaklistoffset */
2862 2865 0, /* tp_iter */
2863 2866 0, /* tp_iternext */
2864 2867 index_methods, /* tp_methods */
2865 2868 0, /* tp_members */
2866 2869 index_getset, /* tp_getset */
2867 2870 0, /* tp_base */
2868 2871 0, /* tp_dict */
2869 2872 0, /* tp_descr_get */
2870 2873 0, /* tp_descr_set */
2871 2874 0, /* tp_dictoffset */
2872 2875 (initproc)index_init, /* tp_init */
2873 2876 0, /* tp_alloc */
2874 2877 };
2875 2878
2876 2879 /*
2877 2880 * returns a tuple of the form (index, index, cache) with elements as
2878 2881 * follows:
2879 2882 *
2880 2883 * index: an index object that lazily parses RevlogNG records
2881 2884 * cache: if data is inlined, a tuple (0, index_file_content), else None
2882 2885 * index_file_content could be a string, or a buffer
2883 2886 *
2884 2887 * added complications are for backwards compatibility
2885 2888 */
2886 2889 PyObject *parse_index2(PyObject *self, PyObject *args)
2887 2890 {
2888 2891 PyObject *cache = NULL;
2889 2892 indexObject *idx;
2890 2893 int ret;
2891 2894
2892 2895 idx = PyObject_New(indexObject, &HgRevlogIndex_Type);
2893 2896 if (idx == NULL)
2894 2897 goto bail;
2895 2898
2896 2899 ret = index_init(idx, args);
2897 2900 if (ret == -1)
2898 2901 goto bail;
2899 2902
2900 2903 if (idx->inlined) {
2901 2904 cache = Py_BuildValue("iO", 0, idx->data);
2902 2905 if (cache == NULL)
2903 2906 goto bail;
2904 2907 } else {
2905 2908 cache = Py_None;
2906 2909 Py_INCREF(cache);
2907 2910 }
2908 2911
2909 2912 return Py_BuildValue("NN", idx, cache);
2910 2913
2911 2914 bail:
2912 2915 Py_XDECREF(idx);
2913 2916 Py_XDECREF(cache);
2914 2917 return NULL;
2915 2918 }
2916 2919
2917 2920 static Revlog_CAPI CAPI = {
2918 2921 /* increment the abi_version field upon each change in the Revlog_CAPI
2919 2922 struct or in the ABI of the listed functions */
2920 2923 2,
2921 2924 index_length,
2922 2925 index_node,
2923 2926 HgRevlogIndex_GetParents,
2924 2927 };
2925 2928
2926 2929 void revlog_module_init(PyObject *mod)
2927 2930 {
2928 2931 PyObject *caps = NULL;
2929 2932 HgRevlogIndex_Type.tp_new = PyType_GenericNew;
2930 2933 if (PyType_Ready(&HgRevlogIndex_Type) < 0)
2931 2934 return;
2932 2935 Py_INCREF(&HgRevlogIndex_Type);
2933 2936 PyModule_AddObject(mod, "index", (PyObject *)&HgRevlogIndex_Type);
2934 2937
2935 2938 nodetreeType.tp_new = PyType_GenericNew;
2936 2939 if (PyType_Ready(&nodetreeType) < 0)
2937 2940 return;
2938 2941 Py_INCREF(&nodetreeType);
2939 2942 PyModule_AddObject(mod, "nodetree", (PyObject *)&nodetreeType);
2940 2943
2941 2944 if (!nullentry) {
2942 2945 nullentry =
2943 2946 Py_BuildValue(PY23("iiiiiiis#", "iiiiiiiy#"), 0, 0, 0, -1,
2944 2947 -1, -1, -1, nullid, (Py_ssize_t)20);
2945 2948 }
2946 2949 if (nullentry)
2947 2950 PyObject_GC_UnTrack(nullentry);
2948 2951
2949 2952 caps = PyCapsule_New(&CAPI, "mercurial.cext.parsers.revlog_CAPI", NULL);
2950 2953 if (caps != NULL)
2951 2954 PyModule_AddObject(mod, "revlog_CAPI", caps);
2952 2955 }
General Comments 0
You need to be logged in to leave comments. Login now