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