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