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