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