##// END OF EJS Templates
parsers: do not cache RevlogError type (issue4451)...
Gregory Szorc -
r25561:50a6c3c5 stable
parent child Browse files
Show More
@@ -1,2649 +1,2642 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 char lowertable[128] = {
39 39 '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
40 40 '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
41 41 '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
42 42 '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f',
43 43 '\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27',
44 44 '\x28', '\x29', '\x2a', '\x2b', '\x2c', '\x2d', '\x2e', '\x2f',
45 45 '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37',
46 46 '\x38', '\x39', '\x3a', '\x3b', '\x3c', '\x3d', '\x3e', '\x3f',
47 47 '\x40',
48 48 '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67', /* A-G */
49 49 '\x68', '\x69', '\x6a', '\x6b', '\x6c', '\x6d', '\x6e', '\x6f', /* H-O */
50 50 '\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77', /* P-W */
51 51 '\x78', '\x79', '\x7a', /* X-Z */
52 52 '\x5b', '\x5c', '\x5d', '\x5e', '\x5f',
53 53 '\x60', '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67',
54 54 '\x68', '\x69', '\x6a', '\x6b', '\x6c', '\x6d', '\x6e', '\x6f',
55 55 '\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77',
56 56 '\x78', '\x79', '\x7a', '\x7b', '\x7c', '\x7d', '\x7e', '\x7f'
57 57 };
58 58
59 59 static char uppertable[128] = {
60 60 '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
61 61 '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
62 62 '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
63 63 '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f',
64 64 '\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27',
65 65 '\x28', '\x29', '\x2a', '\x2b', '\x2c', '\x2d', '\x2e', '\x2f',
66 66 '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37',
67 67 '\x38', '\x39', '\x3a', '\x3b', '\x3c', '\x3d', '\x3e', '\x3f',
68 68 '\x40', '\x41', '\x42', '\x43', '\x44', '\x45', '\x46', '\x47',
69 69 '\x48', '\x49', '\x4a', '\x4b', '\x4c', '\x4d', '\x4e', '\x4f',
70 70 '\x50', '\x51', '\x52', '\x53', '\x54', '\x55', '\x56', '\x57',
71 71 '\x58', '\x59', '\x5a', '\x5b', '\x5c', '\x5d', '\x5e', '\x5f',
72 72 '\x60',
73 73 '\x41', '\x42', '\x43', '\x44', '\x45', '\x46', '\x47', /* a-g */
74 74 '\x48', '\x49', '\x4a', '\x4b', '\x4c', '\x4d', '\x4e', '\x4f', /* h-o */
75 75 '\x50', '\x51', '\x52', '\x53', '\x54', '\x55', '\x56', '\x57', /* p-w */
76 76 '\x58', '\x59', '\x5a', /* x-z */
77 77 '\x7b', '\x7c', '\x7d', '\x7e', '\x7f'
78 78 };
79 79
80 80 static inline int hexdigit(const char *p, Py_ssize_t off)
81 81 {
82 82 int8_t val = hextable[(unsigned char)p[off]];
83 83
84 84 if (val >= 0) {
85 85 return val;
86 86 }
87 87
88 88 PyErr_SetString(PyExc_ValueError, "input contains non-hex character");
89 89 return 0;
90 90 }
91 91
92 92 /*
93 93 * Turn a hex-encoded string into binary.
94 94 */
95 95 PyObject *unhexlify(const char *str, int len)
96 96 {
97 97 PyObject *ret;
98 98 char *d;
99 99 int i;
100 100
101 101 ret = PyBytes_FromStringAndSize(NULL, len / 2);
102 102
103 103 if (!ret)
104 104 return NULL;
105 105
106 106 d = PyBytes_AsString(ret);
107 107
108 108 for (i = 0; i < len;) {
109 109 int hi = hexdigit(str, i++);
110 110 int lo = hexdigit(str, i++);
111 111 *d++ = (hi << 4) | lo;
112 112 }
113 113
114 114 return ret;
115 115 }
116 116
117 117 static inline PyObject *_asciitransform(PyObject *str_obj,
118 118 const char table[128],
119 119 PyObject *fallback_fn)
120 120 {
121 121 char *str, *newstr;
122 122 Py_ssize_t i, len;
123 123 PyObject *newobj = NULL;
124 124 PyObject *ret = NULL;
125 125
126 126 str = PyBytes_AS_STRING(str_obj);
127 127 len = PyBytes_GET_SIZE(str_obj);
128 128
129 129 newobj = PyBytes_FromStringAndSize(NULL, len);
130 130 if (!newobj)
131 131 goto quit;
132 132
133 133 newstr = PyBytes_AS_STRING(newobj);
134 134
135 135 for (i = 0; i < len; i++) {
136 136 char c = str[i];
137 137 if (c & 0x80) {
138 138 if (fallback_fn != NULL) {
139 139 ret = PyObject_CallFunctionObjArgs(fallback_fn,
140 140 str_obj, NULL);
141 141 } else {
142 142 PyObject *err = PyUnicodeDecodeError_Create(
143 143 "ascii", str, len, i, (i + 1),
144 144 "unexpected code byte");
145 145 PyErr_SetObject(PyExc_UnicodeDecodeError, err);
146 146 Py_XDECREF(err);
147 147 }
148 148 goto quit;
149 149 }
150 150 newstr[i] = table[(unsigned char)c];
151 151 }
152 152
153 153 ret = newobj;
154 154 Py_INCREF(ret);
155 155 quit:
156 156 Py_XDECREF(newobj);
157 157 return ret;
158 158 }
159 159
160 160 static PyObject *asciilower(PyObject *self, PyObject *args)
161 161 {
162 162 PyObject *str_obj;
163 163 if (!PyArg_ParseTuple(args, "O!:asciilower", &PyBytes_Type, &str_obj))
164 164 return NULL;
165 165 return _asciitransform(str_obj, lowertable, NULL);
166 166 }
167 167
168 168 static PyObject *asciiupper(PyObject *self, PyObject *args)
169 169 {
170 170 PyObject *str_obj;
171 171 if (!PyArg_ParseTuple(args, "O!:asciiupper", &PyBytes_Type, &str_obj))
172 172 return NULL;
173 173 return _asciitransform(str_obj, uppertable, NULL);
174 174 }
175 175
176 176 static PyObject *make_file_foldmap(PyObject *self, PyObject *args)
177 177 {
178 178 PyObject *dmap, *spec_obj, *normcase_fallback;
179 179 PyObject *file_foldmap = NULL;
180 180 enum normcase_spec spec;
181 181 PyObject *k, *v;
182 182 dirstateTupleObject *tuple;
183 183 Py_ssize_t pos = 0;
184 184 const char *table;
185 185
186 186 if (!PyArg_ParseTuple(args, "O!O!O!:make_file_foldmap",
187 187 &PyDict_Type, &dmap,
188 188 &PyInt_Type, &spec_obj,
189 189 &PyFunction_Type, &normcase_fallback))
190 190 goto quit;
191 191
192 192 spec = (int)PyInt_AS_LONG(spec_obj);
193 193 switch (spec) {
194 194 case NORMCASE_LOWER:
195 195 table = lowertable;
196 196 break;
197 197 case NORMCASE_UPPER:
198 198 table = uppertable;
199 199 break;
200 200 case NORMCASE_OTHER:
201 201 table = NULL;
202 202 break;
203 203 default:
204 204 PyErr_SetString(PyExc_TypeError, "invalid normcasespec");
205 205 goto quit;
206 206 }
207 207
208 208 #if PY_VERSION_HEX >= 0x02060000
209 209 /* _PyDict_NewPresized expects a minused parameter, but it actually
210 210 creates a dictionary that's the nearest power of two bigger than the
211 211 parameter. For example, with the initial minused = 1000, the
212 212 dictionary created has size 1024. Of course in a lot of cases that
213 213 can be greater than the maximum load factor Python's dict object
214 214 expects (= 2/3), so as soon as we cross the threshold we'll resize
215 215 anyway. So create a dictionary that's 3/2 the size. Also add some
216 216 more to deal with additions outside this function. */
217 217 file_foldmap = _PyDict_NewPresized((PyDict_Size(dmap) / 5) * 8);
218 218 #else
219 219 file_foldmap = PyDict_New();
220 220 #endif
221 221
222 222 if (file_foldmap == NULL)
223 223 goto quit;
224 224
225 225 while (PyDict_Next(dmap, &pos, &k, &v)) {
226 226 if (!dirstate_tuple_check(v)) {
227 227 PyErr_SetString(PyExc_TypeError,
228 228 "expected a dirstate tuple");
229 229 goto quit;
230 230 }
231 231
232 232 tuple = (dirstateTupleObject *)v;
233 233 if (tuple->state != 'r') {
234 234 PyObject *normed;
235 235 if (table != NULL) {
236 236 normed = _asciitransform(k, table,
237 237 normcase_fallback);
238 238 } else {
239 239 normed = PyObject_CallFunctionObjArgs(
240 240 normcase_fallback, k, NULL);
241 241 }
242 242
243 243 if (normed == NULL)
244 244 goto quit;
245 245 if (PyDict_SetItem(file_foldmap, normed, k) == -1)
246 246 goto quit;
247 247 }
248 248 }
249 249 return file_foldmap;
250 250 quit:
251 251 Py_XDECREF(file_foldmap);
252 252 return NULL;
253 253 }
254 254
255 255 /*
256 256 * This code assumes that a manifest is stitched together with newline
257 257 * ('\n') characters.
258 258 */
259 259 static PyObject *parse_manifest(PyObject *self, PyObject *args)
260 260 {
261 261 PyObject *mfdict, *fdict;
262 262 char *str, *start, *end;
263 263 int len;
264 264
265 265 if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest",
266 266 &PyDict_Type, &mfdict,
267 267 &PyDict_Type, &fdict,
268 268 &str, &len))
269 269 goto quit;
270 270
271 271 start = str;
272 272 end = str + len;
273 273 while (start < end) {
274 274 PyObject *file = NULL, *node = NULL;
275 275 PyObject *flags = NULL;
276 276 char *zero = NULL, *newline = NULL;
277 277 ptrdiff_t nlen;
278 278
279 279 zero = memchr(start, '\0', end - start);
280 280 if (!zero) {
281 281 PyErr_SetString(PyExc_ValueError,
282 282 "manifest entry has no separator");
283 283 goto quit;
284 284 }
285 285
286 286 newline = memchr(zero + 1, '\n', end - (zero + 1));
287 287 if (!newline) {
288 288 PyErr_SetString(PyExc_ValueError,
289 289 "manifest contains trailing garbage");
290 290 goto quit;
291 291 }
292 292
293 293 file = PyBytes_FromStringAndSize(start, zero - start);
294 294
295 295 if (!file)
296 296 goto bail;
297 297
298 298 nlen = newline - zero - 1;
299 299
300 300 node = unhexlify(zero + 1, nlen > 40 ? 40 : (int)nlen);
301 301 if (!node)
302 302 goto bail;
303 303
304 304 if (nlen > 40) {
305 305 flags = PyBytes_FromStringAndSize(zero + 41,
306 306 nlen - 40);
307 307 if (!flags)
308 308 goto bail;
309 309
310 310 if (PyDict_SetItem(fdict, file, flags) == -1)
311 311 goto bail;
312 312 }
313 313
314 314 if (PyDict_SetItem(mfdict, file, node) == -1)
315 315 goto bail;
316 316
317 317 start = newline + 1;
318 318
319 319 Py_XDECREF(flags);
320 320 Py_XDECREF(node);
321 321 Py_XDECREF(file);
322 322 continue;
323 323 bail:
324 324 Py_XDECREF(flags);
325 325 Py_XDECREF(node);
326 326 Py_XDECREF(file);
327 327 goto quit;
328 328 }
329 329
330 330 Py_INCREF(Py_None);
331 331 return Py_None;
332 332 quit:
333 333 return NULL;
334 334 }
335 335
336 336 static inline dirstateTupleObject *make_dirstate_tuple(char state, int mode,
337 337 int size, int mtime)
338 338 {
339 339 dirstateTupleObject *t = PyObject_New(dirstateTupleObject,
340 340 &dirstateTupleType);
341 341 if (!t)
342 342 return NULL;
343 343 t->state = state;
344 344 t->mode = mode;
345 345 t->size = size;
346 346 t->mtime = mtime;
347 347 return t;
348 348 }
349 349
350 350 static PyObject *dirstate_tuple_new(PyTypeObject *subtype, PyObject *args,
351 351 PyObject *kwds)
352 352 {
353 353 /* We do all the initialization here and not a tp_init function because
354 354 * dirstate_tuple is immutable. */
355 355 dirstateTupleObject *t;
356 356 char state;
357 357 int size, mode, mtime;
358 358 if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime))
359 359 return NULL;
360 360
361 361 t = (dirstateTupleObject *)subtype->tp_alloc(subtype, 1);
362 362 if (!t)
363 363 return NULL;
364 364 t->state = state;
365 365 t->mode = mode;
366 366 t->size = size;
367 367 t->mtime = mtime;
368 368
369 369 return (PyObject *)t;
370 370 }
371 371
372 372 static void dirstate_tuple_dealloc(PyObject *o)
373 373 {
374 374 PyObject_Del(o);
375 375 }
376 376
377 377 static Py_ssize_t dirstate_tuple_length(PyObject *o)
378 378 {
379 379 return 4;
380 380 }
381 381
382 382 static PyObject *dirstate_tuple_item(PyObject *o, Py_ssize_t i)
383 383 {
384 384 dirstateTupleObject *t = (dirstateTupleObject *)o;
385 385 switch (i) {
386 386 case 0:
387 387 return PyBytes_FromStringAndSize(&t->state, 1);
388 388 case 1:
389 389 return PyInt_FromLong(t->mode);
390 390 case 2:
391 391 return PyInt_FromLong(t->size);
392 392 case 3:
393 393 return PyInt_FromLong(t->mtime);
394 394 default:
395 395 PyErr_SetString(PyExc_IndexError, "index out of range");
396 396 return NULL;
397 397 }
398 398 }
399 399
400 400 static PySequenceMethods dirstate_tuple_sq = {
401 401 dirstate_tuple_length, /* sq_length */
402 402 0, /* sq_concat */
403 403 0, /* sq_repeat */
404 404 dirstate_tuple_item, /* sq_item */
405 405 0, /* sq_ass_item */
406 406 0, /* sq_contains */
407 407 0, /* sq_inplace_concat */
408 408 0 /* sq_inplace_repeat */
409 409 };
410 410
411 411 PyTypeObject dirstateTupleType = {
412 412 PyVarObject_HEAD_INIT(NULL, 0)
413 413 "dirstate_tuple", /* tp_name */
414 414 sizeof(dirstateTupleObject),/* tp_basicsize */
415 415 0, /* tp_itemsize */
416 416 (destructor)dirstate_tuple_dealloc, /* tp_dealloc */
417 417 0, /* tp_print */
418 418 0, /* tp_getattr */
419 419 0, /* tp_setattr */
420 420 0, /* tp_compare */
421 421 0, /* tp_repr */
422 422 0, /* tp_as_number */
423 423 &dirstate_tuple_sq, /* tp_as_sequence */
424 424 0, /* tp_as_mapping */
425 425 0, /* tp_hash */
426 426 0, /* tp_call */
427 427 0, /* tp_str */
428 428 0, /* tp_getattro */
429 429 0, /* tp_setattro */
430 430 0, /* tp_as_buffer */
431 431 Py_TPFLAGS_DEFAULT, /* tp_flags */
432 432 "dirstate tuple", /* tp_doc */
433 433 0, /* tp_traverse */
434 434 0, /* tp_clear */
435 435 0, /* tp_richcompare */
436 436 0, /* tp_weaklistoffset */
437 437 0, /* tp_iter */
438 438 0, /* tp_iternext */
439 439 0, /* tp_methods */
440 440 0, /* tp_members */
441 441 0, /* tp_getset */
442 442 0, /* tp_base */
443 443 0, /* tp_dict */
444 444 0, /* tp_descr_get */
445 445 0, /* tp_descr_set */
446 446 0, /* tp_dictoffset */
447 447 0, /* tp_init */
448 448 0, /* tp_alloc */
449 449 dirstate_tuple_new, /* tp_new */
450 450 };
451 451
452 452 static PyObject *parse_dirstate(PyObject *self, PyObject *args)
453 453 {
454 454 PyObject *dmap, *cmap, *parents = NULL, *ret = NULL;
455 455 PyObject *fname = NULL, *cname = NULL, *entry = NULL;
456 456 char state, *cur, *str, *cpos;
457 457 int mode, size, mtime;
458 458 unsigned int flen, len, pos = 40;
459 459 int readlen;
460 460
461 461 if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate",
462 462 &PyDict_Type, &dmap,
463 463 &PyDict_Type, &cmap,
464 464 &str, &readlen))
465 465 goto quit;
466 466
467 467 if (readlen < 0)
468 468 goto quit;
469 469
470 470 len = readlen;
471 471
472 472 /* read parents */
473 473 if (len < 40)
474 474 goto quit;
475 475
476 476 parents = Py_BuildValue("s#s#", str, 20, str + 20, 20);
477 477 if (!parents)
478 478 goto quit;
479 479
480 480 /* read filenames */
481 481 while (pos >= 40 && pos < len) {
482 482 cur = str + pos;
483 483 /* unpack header */
484 484 state = *cur;
485 485 mode = getbe32(cur + 1);
486 486 size = getbe32(cur + 5);
487 487 mtime = getbe32(cur + 9);
488 488 flen = getbe32(cur + 13);
489 489 pos += 17;
490 490 cur += 17;
491 491 if (flen > len - pos) {
492 492 PyErr_SetString(PyExc_ValueError, "overflow in dirstate");
493 493 goto quit;
494 494 }
495 495
496 496 entry = (PyObject *)make_dirstate_tuple(state, mode, size,
497 497 mtime);
498 498 cpos = memchr(cur, 0, flen);
499 499 if (cpos) {
500 500 fname = PyBytes_FromStringAndSize(cur, cpos - cur);
501 501 cname = PyBytes_FromStringAndSize(cpos + 1,
502 502 flen - (cpos - cur) - 1);
503 503 if (!fname || !cname ||
504 504 PyDict_SetItem(cmap, fname, cname) == -1 ||
505 505 PyDict_SetItem(dmap, fname, entry) == -1)
506 506 goto quit;
507 507 Py_DECREF(cname);
508 508 } else {
509 509 fname = PyBytes_FromStringAndSize(cur, flen);
510 510 if (!fname ||
511 511 PyDict_SetItem(dmap, fname, entry) == -1)
512 512 goto quit;
513 513 }
514 514 Py_DECREF(fname);
515 515 Py_DECREF(entry);
516 516 fname = cname = entry = NULL;
517 517 pos += flen;
518 518 }
519 519
520 520 ret = parents;
521 521 Py_INCREF(ret);
522 522 quit:
523 523 Py_XDECREF(fname);
524 524 Py_XDECREF(cname);
525 525 Py_XDECREF(entry);
526 526 Py_XDECREF(parents);
527 527 return ret;
528 528 }
529 529
530 530 /*
531 531 * Efficiently pack a dirstate object into its on-disk format.
532 532 */
533 533 static PyObject *pack_dirstate(PyObject *self, PyObject *args)
534 534 {
535 535 PyObject *packobj = NULL;
536 536 PyObject *map, *copymap, *pl, *mtime_unset = NULL;
537 537 Py_ssize_t nbytes, pos, l;
538 538 PyObject *k, *v = NULL, *pn;
539 539 char *p, *s;
540 540 double now;
541 541
542 542 if (!PyArg_ParseTuple(args, "O!O!Od:pack_dirstate",
543 543 &PyDict_Type, &map, &PyDict_Type, &copymap,
544 544 &pl, &now))
545 545 return NULL;
546 546
547 547 if (!PySequence_Check(pl) || PySequence_Size(pl) != 2) {
548 548 PyErr_SetString(PyExc_TypeError, "expected 2-element sequence");
549 549 return NULL;
550 550 }
551 551
552 552 /* Figure out how much we need to allocate. */
553 553 for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) {
554 554 PyObject *c;
555 555 if (!PyString_Check(k)) {
556 556 PyErr_SetString(PyExc_TypeError, "expected string key");
557 557 goto bail;
558 558 }
559 559 nbytes += PyString_GET_SIZE(k) + 17;
560 560 c = PyDict_GetItem(copymap, k);
561 561 if (c) {
562 562 if (!PyString_Check(c)) {
563 563 PyErr_SetString(PyExc_TypeError,
564 564 "expected string key");
565 565 goto bail;
566 566 }
567 567 nbytes += PyString_GET_SIZE(c) + 1;
568 568 }
569 569 }
570 570
571 571 packobj = PyString_FromStringAndSize(NULL, nbytes);
572 572 if (packobj == NULL)
573 573 goto bail;
574 574
575 575 p = PyString_AS_STRING(packobj);
576 576
577 577 pn = PySequence_ITEM(pl, 0);
578 578 if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
579 579 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
580 580 goto bail;
581 581 }
582 582 memcpy(p, s, l);
583 583 p += 20;
584 584 pn = PySequence_ITEM(pl, 1);
585 585 if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
586 586 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
587 587 goto bail;
588 588 }
589 589 memcpy(p, s, l);
590 590 p += 20;
591 591
592 592 for (pos = 0; PyDict_Next(map, &pos, &k, &v); ) {
593 593 dirstateTupleObject *tuple;
594 594 char state;
595 595 uint32_t mode, size, mtime;
596 596 Py_ssize_t len, l;
597 597 PyObject *o;
598 598 char *t;
599 599
600 600 if (!dirstate_tuple_check(v)) {
601 601 PyErr_SetString(PyExc_TypeError,
602 602 "expected a dirstate tuple");
603 603 goto bail;
604 604 }
605 605 tuple = (dirstateTupleObject *)v;
606 606
607 607 state = tuple->state;
608 608 mode = tuple->mode;
609 609 size = tuple->size;
610 610 mtime = tuple->mtime;
611 611 if (state == 'n' && mtime == (uint32_t)now) {
612 612 /* See pure/parsers.py:pack_dirstate for why we do
613 613 * this. */
614 614 mtime = -1;
615 615 mtime_unset = (PyObject *)make_dirstate_tuple(
616 616 state, mode, size, mtime);
617 617 if (!mtime_unset)
618 618 goto bail;
619 619 if (PyDict_SetItem(map, k, mtime_unset) == -1)
620 620 goto bail;
621 621 Py_DECREF(mtime_unset);
622 622 mtime_unset = NULL;
623 623 }
624 624 *p++ = state;
625 625 putbe32(mode, p);
626 626 putbe32(size, p + 4);
627 627 putbe32(mtime, p + 8);
628 628 t = p + 12;
629 629 p += 16;
630 630 len = PyString_GET_SIZE(k);
631 631 memcpy(p, PyString_AS_STRING(k), len);
632 632 p += len;
633 633 o = PyDict_GetItem(copymap, k);
634 634 if (o) {
635 635 *p++ = '\0';
636 636 l = PyString_GET_SIZE(o);
637 637 memcpy(p, PyString_AS_STRING(o), l);
638 638 p += l;
639 639 len += l + 1;
640 640 }
641 641 putbe32((uint32_t)len, t);
642 642 }
643 643
644 644 pos = p - PyString_AS_STRING(packobj);
645 645 if (pos != nbytes) {
646 646 PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld",
647 647 (long)pos, (long)nbytes);
648 648 goto bail;
649 649 }
650 650
651 651 return packobj;
652 652 bail:
653 653 Py_XDECREF(mtime_unset);
654 654 Py_XDECREF(packobj);
655 655 Py_XDECREF(v);
656 656 return NULL;
657 657 }
658 658
659 659 /*
660 660 * A base-16 trie for fast node->rev mapping.
661 661 *
662 662 * Positive value is index of the next node in the trie
663 663 * Negative value is a leaf: -(rev + 1)
664 664 * Zero is empty
665 665 */
666 666 typedef struct {
667 667 int children[16];
668 668 } nodetree;
669 669
670 670 /*
671 671 * This class has two behaviours.
672 672 *
673 673 * When used in a list-like way (with integer keys), we decode an
674 674 * entry in a RevlogNG index file on demand. Our last entry is a
675 675 * sentinel, always a nullid. We have limited support for
676 676 * integer-keyed insert and delete, only at elements right before the
677 677 * sentinel.
678 678 *
679 679 * With string keys, we lazily perform a reverse mapping from node to
680 680 * rev, using a base-16 trie.
681 681 */
682 682 typedef struct {
683 683 PyObject_HEAD
684 684 /* Type-specific fields go here. */
685 685 PyObject *data; /* raw bytes of index */
686 686 PyObject **cache; /* cached tuples */
687 687 const char **offsets; /* populated on demand */
688 688 Py_ssize_t raw_length; /* original number of elements */
689 689 Py_ssize_t length; /* current number of elements */
690 690 PyObject *added; /* populated on demand */
691 691 PyObject *headrevs; /* cache, invalidated on changes */
692 692 PyObject *filteredrevs;/* filtered revs set */
693 693 nodetree *nt; /* base-16 trie */
694 694 int ntlength; /* # nodes in use */
695 695 int ntcapacity; /* # nodes allocated */
696 696 int ntdepth; /* maximum depth of tree */
697 697 int ntsplits; /* # splits performed */
698 698 int ntrev; /* last rev scanned */
699 699 int ntlookups; /* # lookups */
700 700 int ntmisses; /* # lookups that miss the cache */
701 701 int inlined;
702 702 } indexObject;
703 703
704 704 static Py_ssize_t index_length(const indexObject *self)
705 705 {
706 706 if (self->added == NULL)
707 707 return self->length;
708 708 return self->length + PyList_GET_SIZE(self->added);
709 709 }
710 710
711 711 static PyObject *nullentry;
712 712 static const char nullid[20];
713 713
714 714 static Py_ssize_t inline_scan(indexObject *self, const char **offsets);
715 715
716 716 #if LONG_MAX == 0x7fffffffL
717 717 static char *tuple_format = "Kiiiiiis#";
718 718 #else
719 719 static char *tuple_format = "kiiiiiis#";
720 720 #endif
721 721
722 722 /* A RevlogNG v1 index entry is 64 bytes long. */
723 723 static const long v1_hdrsize = 64;
724 724
725 725 /*
726 726 * Return a pointer to the beginning of a RevlogNG record.
727 727 */
728 728 static const char *index_deref(indexObject *self, Py_ssize_t pos)
729 729 {
730 730 if (self->inlined && pos > 0) {
731 731 if (self->offsets == NULL) {
732 732 self->offsets = malloc(self->raw_length *
733 733 sizeof(*self->offsets));
734 734 if (self->offsets == NULL)
735 735 return (const char *)PyErr_NoMemory();
736 736 inline_scan(self, self->offsets);
737 737 }
738 738 return self->offsets[pos];
739 739 }
740 740
741 741 return PyString_AS_STRING(self->data) + pos * v1_hdrsize;
742 742 }
743 743
744 744 /*
745 745 * RevlogNG format (all in big endian, data may be inlined):
746 746 * 6 bytes: offset
747 747 * 2 bytes: flags
748 748 * 4 bytes: compressed length
749 749 * 4 bytes: uncompressed length
750 750 * 4 bytes: base revision
751 751 * 4 bytes: link revision
752 752 * 4 bytes: parent 1 revision
753 753 * 4 bytes: parent 2 revision
754 754 * 32 bytes: nodeid (only 20 bytes used)
755 755 */
756 756 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
757 757 {
758 758 uint64_t offset_flags;
759 759 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
760 760 const char *c_node_id;
761 761 const char *data;
762 762 Py_ssize_t length = index_length(self);
763 763 PyObject *entry;
764 764
765 765 if (pos < 0)
766 766 pos += length;
767 767
768 768 if (pos < 0 || pos >= length) {
769 769 PyErr_SetString(PyExc_IndexError, "revlog index out of range");
770 770 return NULL;
771 771 }
772 772
773 773 if (pos == length - 1) {
774 774 Py_INCREF(nullentry);
775 775 return nullentry;
776 776 }
777 777
778 778 if (pos >= self->length - 1) {
779 779 PyObject *obj;
780 780 obj = PyList_GET_ITEM(self->added, pos - self->length + 1);
781 781 Py_INCREF(obj);
782 782 return obj;
783 783 }
784 784
785 785 if (self->cache) {
786 786 if (self->cache[pos]) {
787 787 Py_INCREF(self->cache[pos]);
788 788 return self->cache[pos];
789 789 }
790 790 } else {
791 791 self->cache = calloc(self->raw_length, sizeof(PyObject *));
792 792 if (self->cache == NULL)
793 793 return PyErr_NoMemory();
794 794 }
795 795
796 796 data = index_deref(self, pos);
797 797 if (data == NULL)
798 798 return NULL;
799 799
800 800 offset_flags = getbe32(data + 4);
801 801 if (pos == 0) /* mask out version number for the first entry */
802 802 offset_flags &= 0xFFFF;
803 803 else {
804 804 uint32_t offset_high = getbe32(data);
805 805 offset_flags |= ((uint64_t)offset_high) << 32;
806 806 }
807 807
808 808 comp_len = getbe32(data + 8);
809 809 uncomp_len = getbe32(data + 12);
810 810 base_rev = getbe32(data + 16);
811 811 link_rev = getbe32(data + 20);
812 812 parent_1 = getbe32(data + 24);
813 813 parent_2 = getbe32(data + 28);
814 814 c_node_id = data + 32;
815 815
816 816 entry = Py_BuildValue(tuple_format, offset_flags, comp_len,
817 817 uncomp_len, base_rev, link_rev,
818 818 parent_1, parent_2, c_node_id, 20);
819 819
820 820 if (entry) {
821 821 PyObject_GC_UnTrack(entry);
822 822 Py_INCREF(entry);
823 823 }
824 824
825 825 self->cache[pos] = entry;
826 826
827 827 return entry;
828 828 }
829 829
830 830 /*
831 831 * Return the 20-byte SHA of the node corresponding to the given rev.
832 832 */
833 833 static const char *index_node(indexObject *self, Py_ssize_t pos)
834 834 {
835 835 Py_ssize_t length = index_length(self);
836 836 const char *data;
837 837
838 838 if (pos == length - 1 || pos == INT_MAX)
839 839 return nullid;
840 840
841 841 if (pos >= length)
842 842 return NULL;
843 843
844 844 if (pos >= self->length - 1) {
845 845 PyObject *tuple, *str;
846 846 tuple = PyList_GET_ITEM(self->added, pos - self->length + 1);
847 847 str = PyTuple_GetItem(tuple, 7);
848 848 return str ? PyString_AS_STRING(str) : NULL;
849 849 }
850 850
851 851 data = index_deref(self, pos);
852 852 return data ? data + 32 : NULL;
853 853 }
854 854
855 855 static int nt_insert(indexObject *self, const char *node, int rev);
856 856
857 857 static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen)
858 858 {
859 859 if (PyString_AsStringAndSize(obj, node, nodelen) == -1)
860 860 return -1;
861 861 if (*nodelen == 20)
862 862 return 0;
863 863 PyErr_SetString(PyExc_ValueError, "20-byte hash required");
864 864 return -1;
865 865 }
866 866
867 867 static PyObject *index_insert(indexObject *self, PyObject *args)
868 868 {
869 869 PyObject *obj;
870 870 char *node;
871 871 int index;
872 872 Py_ssize_t len, nodelen;
873 873
874 874 if (!PyArg_ParseTuple(args, "iO", &index, &obj))
875 875 return NULL;
876 876
877 877 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
878 878 PyErr_SetString(PyExc_TypeError, "8-tuple required");
879 879 return NULL;
880 880 }
881 881
882 882 if (node_check(PyTuple_GET_ITEM(obj, 7), &node, &nodelen) == -1)
883 883 return NULL;
884 884
885 885 len = index_length(self);
886 886
887 887 if (index < 0)
888 888 index += len;
889 889
890 890 if (index != len - 1) {
891 891 PyErr_SetString(PyExc_IndexError,
892 892 "insert only supported at index -1");
893 893 return NULL;
894 894 }
895 895
896 896 if (self->added == NULL) {
897 897 self->added = PyList_New(0);
898 898 if (self->added == NULL)
899 899 return NULL;
900 900 }
901 901
902 902 if (PyList_Append(self->added, obj) == -1)
903 903 return NULL;
904 904
905 905 if (self->nt)
906 906 nt_insert(self, node, index);
907 907
908 908 Py_CLEAR(self->headrevs);
909 909 Py_RETURN_NONE;
910 910 }
911 911
912 912 static void _index_clearcaches(indexObject *self)
913 913 {
914 914 if (self->cache) {
915 915 Py_ssize_t i;
916 916
917 917 for (i = 0; i < self->raw_length; i++)
918 918 Py_CLEAR(self->cache[i]);
919 919 free(self->cache);
920 920 self->cache = NULL;
921 921 }
922 922 if (self->offsets) {
923 923 free(self->offsets);
924 924 self->offsets = NULL;
925 925 }
926 926 if (self->nt) {
927 927 free(self->nt);
928 928 self->nt = NULL;
929 929 }
930 930 Py_CLEAR(self->headrevs);
931 931 }
932 932
933 933 static PyObject *index_clearcaches(indexObject *self)
934 934 {
935 935 _index_clearcaches(self);
936 936 self->ntlength = self->ntcapacity = 0;
937 937 self->ntdepth = self->ntsplits = 0;
938 938 self->ntrev = -1;
939 939 self->ntlookups = self->ntmisses = 0;
940 940 Py_RETURN_NONE;
941 941 }
942 942
943 943 static PyObject *index_stats(indexObject *self)
944 944 {
945 945 PyObject *obj = PyDict_New();
946 946 PyObject *t = NULL;
947 947
948 948 if (obj == NULL)
949 949 return NULL;
950 950
951 951 #define istat(__n, __d) \
952 952 t = PyInt_FromSsize_t(self->__n); \
953 953 if (!t) \
954 954 goto bail; \
955 955 if (PyDict_SetItemString(obj, __d, t) == -1) \
956 956 goto bail; \
957 957 Py_DECREF(t);
958 958
959 959 if (self->added) {
960 960 Py_ssize_t len = PyList_GET_SIZE(self->added);
961 961 t = PyInt_FromSsize_t(len);
962 962 if (!t)
963 963 goto bail;
964 964 if (PyDict_SetItemString(obj, "index entries added", t) == -1)
965 965 goto bail;
966 966 Py_DECREF(t);
967 967 }
968 968
969 969 if (self->raw_length != self->length - 1)
970 970 istat(raw_length, "revs on disk");
971 971 istat(length, "revs in memory");
972 972 istat(ntcapacity, "node trie capacity");
973 973 istat(ntdepth, "node trie depth");
974 974 istat(ntlength, "node trie count");
975 975 istat(ntlookups, "node trie lookups");
976 976 istat(ntmisses, "node trie misses");
977 977 istat(ntrev, "node trie last rev scanned");
978 978 istat(ntsplits, "node trie splits");
979 979
980 980 #undef istat
981 981
982 982 return obj;
983 983
984 984 bail:
985 985 Py_XDECREF(obj);
986 986 Py_XDECREF(t);
987 987 return NULL;
988 988 }
989 989
990 990 /*
991 991 * When we cache a list, we want to be sure the caller can't mutate
992 992 * the cached copy.
993 993 */
994 994 static PyObject *list_copy(PyObject *list)
995 995 {
996 996 Py_ssize_t len = PyList_GET_SIZE(list);
997 997 PyObject *newlist = PyList_New(len);
998 998 Py_ssize_t i;
999 999
1000 1000 if (newlist == NULL)
1001 1001 return NULL;
1002 1002
1003 1003 for (i = 0; i < len; i++) {
1004 1004 PyObject *obj = PyList_GET_ITEM(list, i);
1005 1005 Py_INCREF(obj);
1006 1006 PyList_SET_ITEM(newlist, i, obj);
1007 1007 }
1008 1008
1009 1009 return newlist;
1010 1010 }
1011 1011
1012 1012 /* arg should be Py_ssize_t but Python 2.4 do not support the n format */
1013 1013 static int check_filter(PyObject *filter, unsigned long arg) {
1014 1014 if (filter) {
1015 1015 PyObject *arglist, *result;
1016 1016 int isfiltered;
1017 1017
1018 1018 arglist = Py_BuildValue("(k)", arg);
1019 1019 if (!arglist) {
1020 1020 return -1;
1021 1021 }
1022 1022
1023 1023 result = PyEval_CallObject(filter, arglist);
1024 1024 Py_DECREF(arglist);
1025 1025 if (!result) {
1026 1026 return -1;
1027 1027 }
1028 1028
1029 1029 /* PyObject_IsTrue returns 1 if true, 0 if false, -1 if error,
1030 1030 * same as this function, so we can just return it directly.*/
1031 1031 isfiltered = PyObject_IsTrue(result);
1032 1032 Py_DECREF(result);
1033 1033 return isfiltered;
1034 1034 } else {
1035 1035 return 0;
1036 1036 }
1037 1037 }
1038 1038
1039 1039 static Py_ssize_t add_roots_get_min(indexObject *self, PyObject *list,
1040 1040 Py_ssize_t marker, char *phases)
1041 1041 {
1042 1042 PyObject *iter = NULL;
1043 1043 PyObject *iter_item = NULL;
1044 1044 Py_ssize_t min_idx = index_length(self) + 1;
1045 1045 long iter_item_long;
1046 1046
1047 1047 if (PyList_GET_SIZE(list) != 0) {
1048 1048 iter = PyObject_GetIter(list);
1049 1049 if (iter == NULL)
1050 1050 return -2;
1051 1051 while ((iter_item = PyIter_Next(iter)))
1052 1052 {
1053 1053 iter_item_long = PyInt_AS_LONG(iter_item);
1054 1054 Py_DECREF(iter_item);
1055 1055 if (iter_item_long < min_idx)
1056 1056 min_idx = iter_item_long;
1057 1057 phases[iter_item_long] = marker;
1058 1058 }
1059 1059 Py_DECREF(iter);
1060 1060 }
1061 1061
1062 1062 return min_idx;
1063 1063 }
1064 1064
1065 1065 static inline void set_phase_from_parents(char *phases, int parent_1,
1066 1066 int parent_2, Py_ssize_t i)
1067 1067 {
1068 1068 if (parent_1 >= 0 && phases[parent_1] > phases[i])
1069 1069 phases[i] = phases[parent_1];
1070 1070 if (parent_2 >= 0 && phases[parent_2] > phases[i])
1071 1071 phases[i] = phases[parent_2];
1072 1072 }
1073 1073
1074 1074 static PyObject *compute_phases(indexObject *self, PyObject *args)
1075 1075 {
1076 1076 PyObject *roots = Py_None;
1077 1077 PyObject *phaseslist = NULL;
1078 1078 PyObject *phaseroots = NULL;
1079 1079 PyObject *rev = NULL;
1080 1080 PyObject *p1 = NULL;
1081 1081 PyObject *p2 = NULL;
1082 1082 Py_ssize_t addlen = self->added ? PyList_GET_SIZE(self->added) : 0;
1083 1083 Py_ssize_t len = index_length(self) - 1;
1084 1084 Py_ssize_t numphase = 0;
1085 1085 Py_ssize_t minrevallphases = 0;
1086 1086 Py_ssize_t minrevphase = 0;
1087 1087 Py_ssize_t i = 0;
1088 1088 int parent_1, parent_2;
1089 1089 char *phases = NULL;
1090 1090 const char *data;
1091 1091
1092 1092 if (!PyArg_ParseTuple(args, "O", &roots))
1093 1093 goto release_none;
1094 1094 if (roots == NULL || !PyList_Check(roots))
1095 1095 goto release_none;
1096 1096
1097 1097 phases = calloc(len, 1); /* phase per rev: {0: public, 1: draft, 2: secret} */
1098 1098 if (phases == NULL)
1099 1099 goto release_none;
1100 1100 /* Put the phase information of all the roots in phases */
1101 1101 numphase = PyList_GET_SIZE(roots)+1;
1102 1102 minrevallphases = len + 1;
1103 1103 for (i = 0; i < numphase-1; i++) {
1104 1104 phaseroots = PyList_GET_ITEM(roots, i);
1105 1105 if (!PyList_Check(phaseroots))
1106 1106 goto release_phases;
1107 1107 minrevphase = add_roots_get_min(self, phaseroots, i+1, phases);
1108 1108 if (minrevphase == -2) /* Error from add_roots_get_min */
1109 1109 goto release_phases;
1110 1110 minrevallphases = MIN(minrevallphases, minrevphase);
1111 1111 }
1112 1112 /* Propagate the phase information from the roots to the revs */
1113 1113 if (minrevallphases != -1) {
1114 1114 for (i = minrevallphases; i < self->raw_length; i++) {
1115 1115 data = index_deref(self, i);
1116 1116 set_phase_from_parents(phases, getbe32(data+24), getbe32(data+28), i);
1117 1117 }
1118 1118 for (i = 0; i < addlen; i++) {
1119 1119 rev = PyList_GET_ITEM(self->added, i);
1120 1120 p1 = PyTuple_GET_ITEM(rev, 5);
1121 1121 p2 = PyTuple_GET_ITEM(rev, 6);
1122 1122 if (!PyInt_Check(p1) || !PyInt_Check(p2)) {
1123 1123 PyErr_SetString(PyExc_TypeError, "revlog parents are invalid");
1124 1124 goto release_phases;
1125 1125 }
1126 1126 parent_1 = (int)PyInt_AS_LONG(p1);
1127 1127 parent_2 = (int)PyInt_AS_LONG(p2);
1128 1128 set_phase_from_parents(phases, parent_1, parent_2, i+self->raw_length);
1129 1129 }
1130 1130 }
1131 1131 /* Transform phase list to a python list */
1132 1132 phaseslist = PyList_New(len);
1133 1133 if (phaseslist == NULL)
1134 1134 goto release_phases;
1135 1135 for (i = 0; i < len; i++)
1136 1136 PyList_SET_ITEM(phaseslist, i, PyInt_FromLong(phases[i]));
1137 1137
1138 1138 release_phases:
1139 1139 free(phases);
1140 1140 release_none:
1141 1141 return phaseslist;
1142 1142 }
1143 1143
1144 1144 static PyObject *index_headrevs(indexObject *self, PyObject *args)
1145 1145 {
1146 1146 Py_ssize_t i, len, addlen;
1147 1147 char *nothead = NULL;
1148 1148 PyObject *heads = NULL;
1149 1149 PyObject *filter = NULL;
1150 1150 PyObject *filteredrevs = Py_None;
1151 1151
1152 1152 if (!PyArg_ParseTuple(args, "|O", &filteredrevs)) {
1153 1153 return NULL;
1154 1154 }
1155 1155
1156 1156 if (self->headrevs && filteredrevs == self->filteredrevs)
1157 1157 return list_copy(self->headrevs);
1158 1158
1159 1159 Py_DECREF(self->filteredrevs);
1160 1160 self->filteredrevs = filteredrevs;
1161 1161 Py_INCREF(filteredrevs);
1162 1162
1163 1163 if (filteredrevs != Py_None) {
1164 1164 filter = PyObject_GetAttrString(filteredrevs, "__contains__");
1165 1165 if (!filter) {
1166 1166 PyErr_SetString(PyExc_TypeError,
1167 1167 "filteredrevs has no attribute __contains__");
1168 1168 goto bail;
1169 1169 }
1170 1170 }
1171 1171
1172 1172 len = index_length(self) - 1;
1173 1173 heads = PyList_New(0);
1174 1174 if (heads == NULL)
1175 1175 goto bail;
1176 1176 if (len == 0) {
1177 1177 PyObject *nullid = PyInt_FromLong(-1);
1178 1178 if (nullid == NULL || PyList_Append(heads, nullid) == -1) {
1179 1179 Py_XDECREF(nullid);
1180 1180 goto bail;
1181 1181 }
1182 1182 goto done;
1183 1183 }
1184 1184
1185 1185 nothead = calloc(len, 1);
1186 1186 if (nothead == NULL)
1187 1187 goto bail;
1188 1188
1189 1189 for (i = 0; i < self->raw_length; i++) {
1190 1190 const char *data;
1191 1191 int parent_1, parent_2, isfiltered;
1192 1192
1193 1193 isfiltered = check_filter(filter, i);
1194 1194 if (isfiltered == -1) {
1195 1195 PyErr_SetString(PyExc_TypeError,
1196 1196 "unable to check filter");
1197 1197 goto bail;
1198 1198 }
1199 1199
1200 1200 if (isfiltered) {
1201 1201 nothead[i] = 1;
1202 1202 continue;
1203 1203 }
1204 1204
1205 1205 data = index_deref(self, i);
1206 1206 parent_1 = getbe32(data + 24);
1207 1207 parent_2 = getbe32(data + 28);
1208 1208
1209 1209 if (parent_1 >= 0)
1210 1210 nothead[parent_1] = 1;
1211 1211 if (parent_2 >= 0)
1212 1212 nothead[parent_2] = 1;
1213 1213 }
1214 1214
1215 1215 addlen = self->added ? PyList_GET_SIZE(self->added) : 0;
1216 1216
1217 1217 for (i = 0; i < addlen; i++) {
1218 1218 PyObject *rev = PyList_GET_ITEM(self->added, i);
1219 1219 PyObject *p1 = PyTuple_GET_ITEM(rev, 5);
1220 1220 PyObject *p2 = PyTuple_GET_ITEM(rev, 6);
1221 1221 long parent_1, parent_2;
1222 1222 int isfiltered;
1223 1223
1224 1224 if (!PyInt_Check(p1) || !PyInt_Check(p2)) {
1225 1225 PyErr_SetString(PyExc_TypeError,
1226 1226 "revlog parents are invalid");
1227 1227 goto bail;
1228 1228 }
1229 1229
1230 1230 isfiltered = check_filter(filter, i);
1231 1231 if (isfiltered == -1) {
1232 1232 PyErr_SetString(PyExc_TypeError,
1233 1233 "unable to check filter");
1234 1234 goto bail;
1235 1235 }
1236 1236
1237 1237 if (isfiltered) {
1238 1238 nothead[i] = 1;
1239 1239 continue;
1240 1240 }
1241 1241
1242 1242 parent_1 = PyInt_AS_LONG(p1);
1243 1243 parent_2 = PyInt_AS_LONG(p2);
1244 1244 if (parent_1 >= 0)
1245 1245 nothead[parent_1] = 1;
1246 1246 if (parent_2 >= 0)
1247 1247 nothead[parent_2] = 1;
1248 1248 }
1249 1249
1250 1250 for (i = 0; i < len; i++) {
1251 1251 PyObject *head;
1252 1252
1253 1253 if (nothead[i])
1254 1254 continue;
1255 1255 head = PyInt_FromSsize_t(i);
1256 1256 if (head == NULL || PyList_Append(heads, head) == -1) {
1257 1257 Py_XDECREF(head);
1258 1258 goto bail;
1259 1259 }
1260 1260 }
1261 1261
1262 1262 done:
1263 1263 self->headrevs = heads;
1264 1264 Py_XDECREF(filter);
1265 1265 free(nothead);
1266 1266 return list_copy(self->headrevs);
1267 1267 bail:
1268 1268 Py_XDECREF(filter);
1269 1269 Py_XDECREF(heads);
1270 1270 free(nothead);
1271 1271 return NULL;
1272 1272 }
1273 1273
1274 1274 static inline int nt_level(const char *node, Py_ssize_t level)
1275 1275 {
1276 1276 int v = node[level>>1];
1277 1277 if (!(level & 1))
1278 1278 v >>= 4;
1279 1279 return v & 0xf;
1280 1280 }
1281 1281
1282 1282 /*
1283 1283 * Return values:
1284 1284 *
1285 1285 * -4: match is ambiguous (multiple candidates)
1286 1286 * -2: not found
1287 1287 * rest: valid rev
1288 1288 */
1289 1289 static int nt_find(indexObject *self, const char *node, Py_ssize_t nodelen,
1290 1290 int hex)
1291 1291 {
1292 1292 int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level;
1293 1293 int level, maxlevel, off;
1294 1294
1295 1295 if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0)
1296 1296 return -1;
1297 1297
1298 1298 if (self->nt == NULL)
1299 1299 return -2;
1300 1300
1301 1301 if (hex)
1302 1302 maxlevel = nodelen > 40 ? 40 : (int)nodelen;
1303 1303 else
1304 1304 maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2);
1305 1305
1306 1306 for (level = off = 0; level < maxlevel; level++) {
1307 1307 int k = getnybble(node, level);
1308 1308 nodetree *n = &self->nt[off];
1309 1309 int v = n->children[k];
1310 1310
1311 1311 if (v < 0) {
1312 1312 const char *n;
1313 1313 Py_ssize_t i;
1314 1314
1315 1315 v = -(v + 1);
1316 1316 n = index_node(self, v);
1317 1317 if (n == NULL)
1318 1318 return -2;
1319 1319 for (i = level; i < maxlevel; i++)
1320 1320 if (getnybble(node, i) != nt_level(n, i))
1321 1321 return -2;
1322 1322 return v;
1323 1323 }
1324 1324 if (v == 0)
1325 1325 return -2;
1326 1326 off = v;
1327 1327 }
1328 1328 /* multiple matches against an ambiguous prefix */
1329 1329 return -4;
1330 1330 }
1331 1331
1332 1332 static int nt_new(indexObject *self)
1333 1333 {
1334 1334 if (self->ntlength == self->ntcapacity) {
1335 1335 if (self->ntcapacity >= INT_MAX / (sizeof(nodetree) * 2)) {
1336 1336 PyErr_SetString(PyExc_MemoryError,
1337 1337 "overflow in nt_new");
1338 1338 return -1;
1339 1339 }
1340 1340 self->ntcapacity *= 2;
1341 1341 self->nt = realloc(self->nt,
1342 1342 self->ntcapacity * sizeof(nodetree));
1343 1343 if (self->nt == NULL) {
1344 1344 PyErr_SetString(PyExc_MemoryError, "out of memory");
1345 1345 return -1;
1346 1346 }
1347 1347 memset(&self->nt[self->ntlength], 0,
1348 1348 sizeof(nodetree) * (self->ntcapacity - self->ntlength));
1349 1349 }
1350 1350 return self->ntlength++;
1351 1351 }
1352 1352
1353 1353 static int nt_insert(indexObject *self, const char *node, int rev)
1354 1354 {
1355 1355 int level = 0;
1356 1356 int off = 0;
1357 1357
1358 1358 while (level < 40) {
1359 1359 int k = nt_level(node, level);
1360 1360 nodetree *n;
1361 1361 int v;
1362 1362
1363 1363 n = &self->nt[off];
1364 1364 v = n->children[k];
1365 1365
1366 1366 if (v == 0) {
1367 1367 n->children[k] = -rev - 1;
1368 1368 return 0;
1369 1369 }
1370 1370 if (v < 0) {
1371 1371 const char *oldnode = index_node(self, -(v + 1));
1372 1372 int noff;
1373 1373
1374 1374 if (!oldnode || !memcmp(oldnode, node, 20)) {
1375 1375 n->children[k] = -rev - 1;
1376 1376 return 0;
1377 1377 }
1378 1378 noff = nt_new(self);
1379 1379 if (noff == -1)
1380 1380 return -1;
1381 1381 /* self->nt may have been changed by realloc */
1382 1382 self->nt[off].children[k] = noff;
1383 1383 off = noff;
1384 1384 n = &self->nt[off];
1385 1385 n->children[nt_level(oldnode, ++level)] = v;
1386 1386 if (level > self->ntdepth)
1387 1387 self->ntdepth = level;
1388 1388 self->ntsplits += 1;
1389 1389 } else {
1390 1390 level += 1;
1391 1391 off = v;
1392 1392 }
1393 1393 }
1394 1394
1395 1395 return -1;
1396 1396 }
1397 1397
1398 1398 static int nt_init(indexObject *self)
1399 1399 {
1400 1400 if (self->nt == NULL) {
1401 1401 if (self->raw_length > INT_MAX / sizeof(nodetree)) {
1402 1402 PyErr_SetString(PyExc_ValueError, "overflow in nt_init");
1403 1403 return -1;
1404 1404 }
1405 1405 self->ntcapacity = self->raw_length < 4
1406 1406 ? 4 : (int)self->raw_length / 2;
1407 1407
1408 1408 self->nt = calloc(self->ntcapacity, sizeof(nodetree));
1409 1409 if (self->nt == NULL) {
1410 1410 PyErr_NoMemory();
1411 1411 return -1;
1412 1412 }
1413 1413 self->ntlength = 1;
1414 1414 self->ntrev = (int)index_length(self) - 1;
1415 1415 self->ntlookups = 1;
1416 1416 self->ntmisses = 0;
1417 1417 if (nt_insert(self, nullid, INT_MAX) == -1)
1418 1418 return -1;
1419 1419 }
1420 1420 return 0;
1421 1421 }
1422 1422
1423 1423 /*
1424 1424 * Return values:
1425 1425 *
1426 1426 * -3: error (exception set)
1427 1427 * -2: not found (no exception set)
1428 1428 * rest: valid rev
1429 1429 */
1430 1430 static int index_find_node(indexObject *self,
1431 1431 const char *node, Py_ssize_t nodelen)
1432 1432 {
1433 1433 int rev;
1434 1434
1435 1435 self->ntlookups++;
1436 1436 rev = nt_find(self, node, nodelen, 0);
1437 1437 if (rev >= -1)
1438 1438 return rev;
1439 1439
1440 1440 if (nt_init(self) == -1)
1441 1441 return -3;
1442 1442
1443 1443 /*
1444 1444 * For the first handful of lookups, we scan the entire index,
1445 1445 * and cache only the matching nodes. This optimizes for cases
1446 1446 * like "hg tip", where only a few nodes are accessed.
1447 1447 *
1448 1448 * After that, we cache every node we visit, using a single
1449 1449 * scan amortized over multiple lookups. This gives the best
1450 1450 * bulk performance, e.g. for "hg log".
1451 1451 */
1452 1452 if (self->ntmisses++ < 4) {
1453 1453 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1454 1454 const char *n = index_node(self, rev);
1455 1455 if (n == NULL)
1456 1456 return -2;
1457 1457 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1458 1458 if (nt_insert(self, n, rev) == -1)
1459 1459 return -3;
1460 1460 break;
1461 1461 }
1462 1462 }
1463 1463 } else {
1464 1464 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1465 1465 const char *n = index_node(self, rev);
1466 1466 if (n == NULL) {
1467 1467 self->ntrev = rev + 1;
1468 1468 return -2;
1469 1469 }
1470 1470 if (nt_insert(self, n, rev) == -1) {
1471 1471 self->ntrev = rev + 1;
1472 1472 return -3;
1473 1473 }
1474 1474 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
1475 1475 break;
1476 1476 }
1477 1477 }
1478 1478 self->ntrev = rev;
1479 1479 }
1480 1480
1481 1481 if (rev >= 0)
1482 1482 return rev;
1483 1483 return -2;
1484 1484 }
1485 1485
1486 static PyObject *raise_revlog_error(void)
1486 static void raise_revlog_error(void)
1487 1487 {
1488 static PyObject *errclass;
1489 PyObject *mod = NULL, *errobj;
1490
1491 if (errclass == NULL) {
1492 PyObject *dict;
1488 PyObject *mod = NULL, *dict = NULL, *errclass = NULL;
1493 1489
1494 1490 mod = PyImport_ImportModule("mercurial.error");
1495 if (mod == NULL)
1496 goto classfail;
1491 if (mod == NULL) {
1492 goto cleanup;
1493 }
1497 1494
1498 1495 dict = PyModule_GetDict(mod);
1499 if (dict == NULL)
1500 goto classfail;
1496 if (dict == NULL) {
1497 goto cleanup;
1498 }
1499 Py_INCREF(dict);
1501 1500
1502 1501 errclass = PyDict_GetItemString(dict, "RevlogError");
1503 1502 if (errclass == NULL) {
1504 1503 PyErr_SetString(PyExc_SystemError,
1505 1504 "could not find RevlogError");
1506 goto classfail;
1507 }
1508 Py_INCREF(errclass);
1509 Py_DECREF(mod);
1505 goto cleanup;
1510 1506 }
1511 1507
1512 errobj = PyObject_CallFunction(errclass, NULL);
1513 if (errobj == NULL)
1514 return NULL;
1515 PyErr_SetObject(errclass, errobj);
1516 return errobj;
1508 /* value of exception is ignored by callers */
1509 PyErr_SetString(errclass, "RevlogError");
1517 1510
1518 classfail:
1511 cleanup:
1512 Py_XDECREF(dict);
1519 1513 Py_XDECREF(mod);
1520 return NULL;
1521 1514 }
1522 1515
1523 1516 static PyObject *index_getitem(indexObject *self, PyObject *value)
1524 1517 {
1525 1518 char *node;
1526 1519 Py_ssize_t nodelen;
1527 1520 int rev;
1528 1521
1529 1522 if (PyInt_Check(value))
1530 1523 return index_get(self, PyInt_AS_LONG(value));
1531 1524
1532 1525 if (node_check(value, &node, &nodelen) == -1)
1533 1526 return NULL;
1534 1527 rev = index_find_node(self, node, nodelen);
1535 1528 if (rev >= -1)
1536 1529 return PyInt_FromLong(rev);
1537 1530 if (rev == -2)
1538 1531 raise_revlog_error();
1539 1532 return NULL;
1540 1533 }
1541 1534
1542 1535 static int nt_partialmatch(indexObject *self, const char *node,
1543 1536 Py_ssize_t nodelen)
1544 1537 {
1545 1538 int rev;
1546 1539
1547 1540 if (nt_init(self) == -1)
1548 1541 return -3;
1549 1542
1550 1543 if (self->ntrev > 0) {
1551 1544 /* ensure that the radix tree is fully populated */
1552 1545 for (rev = self->ntrev - 1; rev >= 0; rev--) {
1553 1546 const char *n = index_node(self, rev);
1554 1547 if (n == NULL)
1555 1548 return -2;
1556 1549 if (nt_insert(self, n, rev) == -1)
1557 1550 return -3;
1558 1551 }
1559 1552 self->ntrev = rev;
1560 1553 }
1561 1554
1562 1555 return nt_find(self, node, nodelen, 1);
1563 1556 }
1564 1557
1565 1558 static PyObject *index_partialmatch(indexObject *self, PyObject *args)
1566 1559 {
1567 1560 const char *fullnode;
1568 1561 int nodelen;
1569 1562 char *node;
1570 1563 int rev, i;
1571 1564
1572 1565 if (!PyArg_ParseTuple(args, "s#", &node, &nodelen))
1573 1566 return NULL;
1574 1567
1575 1568 if (nodelen < 4) {
1576 1569 PyErr_SetString(PyExc_ValueError, "key too short");
1577 1570 return NULL;
1578 1571 }
1579 1572
1580 1573 if (nodelen > 40) {
1581 1574 PyErr_SetString(PyExc_ValueError, "key too long");
1582 1575 return NULL;
1583 1576 }
1584 1577
1585 1578 for (i = 0; i < nodelen; i++)
1586 1579 hexdigit(node, i);
1587 1580 if (PyErr_Occurred()) {
1588 1581 /* input contains non-hex characters */
1589 1582 PyErr_Clear();
1590 1583 Py_RETURN_NONE;
1591 1584 }
1592 1585
1593 1586 rev = nt_partialmatch(self, node, nodelen);
1594 1587
1595 1588 switch (rev) {
1596 1589 case -4:
1597 1590 raise_revlog_error();
1598 1591 case -3:
1599 1592 return NULL;
1600 1593 case -2:
1601 1594 Py_RETURN_NONE;
1602 1595 case -1:
1603 1596 return PyString_FromStringAndSize(nullid, 20);
1604 1597 }
1605 1598
1606 1599 fullnode = index_node(self, rev);
1607 1600 if (fullnode == NULL) {
1608 1601 PyErr_Format(PyExc_IndexError,
1609 1602 "could not access rev %d", rev);
1610 1603 return NULL;
1611 1604 }
1612 1605 return PyString_FromStringAndSize(fullnode, 20);
1613 1606 }
1614 1607
1615 1608 static PyObject *index_m_get(indexObject *self, PyObject *args)
1616 1609 {
1617 1610 Py_ssize_t nodelen;
1618 1611 PyObject *val;
1619 1612 char *node;
1620 1613 int rev;
1621 1614
1622 1615 if (!PyArg_ParseTuple(args, "O", &val))
1623 1616 return NULL;
1624 1617 if (node_check(val, &node, &nodelen) == -1)
1625 1618 return NULL;
1626 1619 rev = index_find_node(self, node, nodelen);
1627 1620 if (rev == -3)
1628 1621 return NULL;
1629 1622 if (rev == -2)
1630 1623 Py_RETURN_NONE;
1631 1624 return PyInt_FromLong(rev);
1632 1625 }
1633 1626
1634 1627 static int index_contains(indexObject *self, PyObject *value)
1635 1628 {
1636 1629 char *node;
1637 1630 Py_ssize_t nodelen;
1638 1631
1639 1632 if (PyInt_Check(value)) {
1640 1633 long rev = PyInt_AS_LONG(value);
1641 1634 return rev >= -1 && rev < index_length(self);
1642 1635 }
1643 1636
1644 1637 if (node_check(value, &node, &nodelen) == -1)
1645 1638 return -1;
1646 1639
1647 1640 switch (index_find_node(self, node, nodelen)) {
1648 1641 case -3:
1649 1642 return -1;
1650 1643 case -2:
1651 1644 return 0;
1652 1645 default:
1653 1646 return 1;
1654 1647 }
1655 1648 }
1656 1649
1657 1650 static inline void index_get_parents(indexObject *self, int rev, int *ps)
1658 1651 {
1659 1652 if (rev >= self->length - 1) {
1660 1653 PyObject *tuple = PyList_GET_ITEM(self->added,
1661 1654 rev - self->length + 1);
1662 1655 ps[0] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 5));
1663 1656 ps[1] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 6));
1664 1657 } else {
1665 1658 const char *data = index_deref(self, rev);
1666 1659 ps[0] = getbe32(data + 24);
1667 1660 ps[1] = getbe32(data + 28);
1668 1661 }
1669 1662 }
1670 1663
1671 1664 typedef uint64_t bitmask;
1672 1665
1673 1666 /*
1674 1667 * Given a disjoint set of revs, return all candidates for the
1675 1668 * greatest common ancestor. In revset notation, this is the set
1676 1669 * "heads(::a and ::b and ...)"
1677 1670 */
1678 1671 static PyObject *find_gca_candidates(indexObject *self, const int *revs,
1679 1672 int revcount)
1680 1673 {
1681 1674 const bitmask allseen = (1ull << revcount) - 1;
1682 1675 const bitmask poison = 1ull << revcount;
1683 1676 PyObject *gca = PyList_New(0);
1684 1677 int i, v, interesting;
1685 1678 int maxrev = -1;
1686 1679 bitmask sp;
1687 1680 bitmask *seen;
1688 1681
1689 1682 if (gca == NULL)
1690 1683 return PyErr_NoMemory();
1691 1684
1692 1685 for (i = 0; i < revcount; i++) {
1693 1686 if (revs[i] > maxrev)
1694 1687 maxrev = revs[i];
1695 1688 }
1696 1689
1697 1690 seen = calloc(sizeof(*seen), maxrev + 1);
1698 1691 if (seen == NULL) {
1699 1692 Py_DECREF(gca);
1700 1693 return PyErr_NoMemory();
1701 1694 }
1702 1695
1703 1696 for (i = 0; i < revcount; i++)
1704 1697 seen[revs[i]] = 1ull << i;
1705 1698
1706 1699 interesting = revcount;
1707 1700
1708 1701 for (v = maxrev; v >= 0 && interesting; v--) {
1709 1702 bitmask sv = seen[v];
1710 1703 int parents[2];
1711 1704
1712 1705 if (!sv)
1713 1706 continue;
1714 1707
1715 1708 if (sv < poison) {
1716 1709 interesting -= 1;
1717 1710 if (sv == allseen) {
1718 1711 PyObject *obj = PyInt_FromLong(v);
1719 1712 if (obj == NULL)
1720 1713 goto bail;
1721 1714 if (PyList_Append(gca, obj) == -1) {
1722 1715 Py_DECREF(obj);
1723 1716 goto bail;
1724 1717 }
1725 1718 sv |= poison;
1726 1719 for (i = 0; i < revcount; i++) {
1727 1720 if (revs[i] == v)
1728 1721 goto done;
1729 1722 }
1730 1723 }
1731 1724 }
1732 1725 index_get_parents(self, v, parents);
1733 1726
1734 1727 for (i = 0; i < 2; i++) {
1735 1728 int p = parents[i];
1736 1729 if (p == -1)
1737 1730 continue;
1738 1731 sp = seen[p];
1739 1732 if (sv < poison) {
1740 1733 if (sp == 0) {
1741 1734 seen[p] = sv;
1742 1735 interesting++;
1743 1736 }
1744 1737 else if (sp != sv)
1745 1738 seen[p] |= sv;
1746 1739 } else {
1747 1740 if (sp && sp < poison)
1748 1741 interesting--;
1749 1742 seen[p] = sv;
1750 1743 }
1751 1744 }
1752 1745 }
1753 1746
1754 1747 done:
1755 1748 free(seen);
1756 1749 return gca;
1757 1750 bail:
1758 1751 free(seen);
1759 1752 Py_XDECREF(gca);
1760 1753 return NULL;
1761 1754 }
1762 1755
1763 1756 /*
1764 1757 * Given a disjoint set of revs, return the subset with the longest
1765 1758 * path to the root.
1766 1759 */
1767 1760 static PyObject *find_deepest(indexObject *self, PyObject *revs)
1768 1761 {
1769 1762 const Py_ssize_t revcount = PyList_GET_SIZE(revs);
1770 1763 static const Py_ssize_t capacity = 24;
1771 1764 int *depth, *interesting = NULL;
1772 1765 int i, j, v, ninteresting;
1773 1766 PyObject *dict = NULL, *keys = NULL;
1774 1767 long *seen = NULL;
1775 1768 int maxrev = -1;
1776 1769 long final;
1777 1770
1778 1771 if (revcount > capacity) {
1779 1772 PyErr_Format(PyExc_OverflowError,
1780 1773 "bitset size (%ld) > capacity (%ld)",
1781 1774 (long)revcount, (long)capacity);
1782 1775 return NULL;
1783 1776 }
1784 1777
1785 1778 for (i = 0; i < revcount; i++) {
1786 1779 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
1787 1780 if (n > maxrev)
1788 1781 maxrev = n;
1789 1782 }
1790 1783
1791 1784 depth = calloc(sizeof(*depth), maxrev + 1);
1792 1785 if (depth == NULL)
1793 1786 return PyErr_NoMemory();
1794 1787
1795 1788 seen = calloc(sizeof(*seen), maxrev + 1);
1796 1789 if (seen == NULL) {
1797 1790 PyErr_NoMemory();
1798 1791 goto bail;
1799 1792 }
1800 1793
1801 1794 interesting = calloc(sizeof(*interesting), 2 << revcount);
1802 1795 if (interesting == NULL) {
1803 1796 PyErr_NoMemory();
1804 1797 goto bail;
1805 1798 }
1806 1799
1807 1800 if (PyList_Sort(revs) == -1)
1808 1801 goto bail;
1809 1802
1810 1803 for (i = 0; i < revcount; i++) {
1811 1804 int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i));
1812 1805 long b = 1l << i;
1813 1806 depth[n] = 1;
1814 1807 seen[n] = b;
1815 1808 interesting[b] = 1;
1816 1809 }
1817 1810
1818 1811 ninteresting = (int)revcount;
1819 1812
1820 1813 for (v = maxrev; v >= 0 && ninteresting > 1; v--) {
1821 1814 int dv = depth[v];
1822 1815 int parents[2];
1823 1816 long sv;
1824 1817
1825 1818 if (dv == 0)
1826 1819 continue;
1827 1820
1828 1821 sv = seen[v];
1829 1822 index_get_parents(self, v, parents);
1830 1823
1831 1824 for (i = 0; i < 2; i++) {
1832 1825 int p = parents[i];
1833 1826 long nsp, sp;
1834 1827 int dp;
1835 1828
1836 1829 if (p == -1)
1837 1830 continue;
1838 1831
1839 1832 dp = depth[p];
1840 1833 nsp = sp = seen[p];
1841 1834 if (dp <= dv) {
1842 1835 depth[p] = dv + 1;
1843 1836 if (sp != sv) {
1844 1837 interesting[sv] += 1;
1845 1838 nsp = seen[p] = sv;
1846 1839 if (sp) {
1847 1840 interesting[sp] -= 1;
1848 1841 if (interesting[sp] == 0)
1849 1842 ninteresting -= 1;
1850 1843 }
1851 1844 }
1852 1845 }
1853 1846 else if (dv == dp - 1) {
1854 1847 nsp = sp | sv;
1855 1848 if (nsp == sp)
1856 1849 continue;
1857 1850 seen[p] = nsp;
1858 1851 interesting[sp] -= 1;
1859 1852 if (interesting[sp] == 0 && interesting[nsp] > 0)
1860 1853 ninteresting -= 1;
1861 1854 interesting[nsp] += 1;
1862 1855 }
1863 1856 }
1864 1857 interesting[sv] -= 1;
1865 1858 if (interesting[sv] == 0)
1866 1859 ninteresting -= 1;
1867 1860 }
1868 1861
1869 1862 final = 0;
1870 1863 j = ninteresting;
1871 1864 for (i = 0; i < (int)(2 << revcount) && j > 0; i++) {
1872 1865 if (interesting[i] == 0)
1873 1866 continue;
1874 1867 final |= i;
1875 1868 j -= 1;
1876 1869 }
1877 1870 if (final == 0) {
1878 1871 keys = PyList_New(0);
1879 1872 goto bail;
1880 1873 }
1881 1874
1882 1875 dict = PyDict_New();
1883 1876 if (dict == NULL)
1884 1877 goto bail;
1885 1878
1886 1879 for (i = 0; i < revcount; i++) {
1887 1880 PyObject *key;
1888 1881
1889 1882 if ((final & (1 << i)) == 0)
1890 1883 continue;
1891 1884
1892 1885 key = PyList_GET_ITEM(revs, i);
1893 1886 Py_INCREF(key);
1894 1887 Py_INCREF(Py_None);
1895 1888 if (PyDict_SetItem(dict, key, Py_None) == -1) {
1896 1889 Py_DECREF(key);
1897 1890 Py_DECREF(Py_None);
1898 1891 goto bail;
1899 1892 }
1900 1893 }
1901 1894
1902 1895 keys = PyDict_Keys(dict);
1903 1896
1904 1897 bail:
1905 1898 free(depth);
1906 1899 free(seen);
1907 1900 free(interesting);
1908 1901 Py_XDECREF(dict);
1909 1902
1910 1903 return keys;
1911 1904 }
1912 1905
1913 1906 /*
1914 1907 * Given a (possibly overlapping) set of revs, return all the
1915 1908 * common ancestors heads: heads(::args[0] and ::a[1] and ...)
1916 1909 */
1917 1910 static PyObject *index_commonancestorsheads(indexObject *self, PyObject *args)
1918 1911 {
1919 1912 PyObject *ret = NULL;
1920 1913 Py_ssize_t argcount, i, len;
1921 1914 bitmask repeat = 0;
1922 1915 int revcount = 0;
1923 1916 int *revs;
1924 1917
1925 1918 argcount = PySequence_Length(args);
1926 1919 revs = malloc(argcount * sizeof(*revs));
1927 1920 if (argcount > 0 && revs == NULL)
1928 1921 return PyErr_NoMemory();
1929 1922 len = index_length(self) - 1;
1930 1923
1931 1924 for (i = 0; i < argcount; i++) {
1932 1925 static const int capacity = 24;
1933 1926 PyObject *obj = PySequence_GetItem(args, i);
1934 1927 bitmask x;
1935 1928 long val;
1936 1929
1937 1930 if (!PyInt_Check(obj)) {
1938 1931 PyErr_SetString(PyExc_TypeError,
1939 1932 "arguments must all be ints");
1940 1933 Py_DECREF(obj);
1941 1934 goto bail;
1942 1935 }
1943 1936 val = PyInt_AsLong(obj);
1944 1937 Py_DECREF(obj);
1945 1938 if (val == -1) {
1946 1939 ret = PyList_New(0);
1947 1940 goto done;
1948 1941 }
1949 1942 if (val < 0 || val >= len) {
1950 1943 PyErr_SetString(PyExc_IndexError,
1951 1944 "index out of range");
1952 1945 goto bail;
1953 1946 }
1954 1947 /* this cheesy bloom filter lets us avoid some more
1955 1948 * expensive duplicate checks in the common set-is-disjoint
1956 1949 * case */
1957 1950 x = 1ull << (val & 0x3f);
1958 1951 if (repeat & x) {
1959 1952 int k;
1960 1953 for (k = 0; k < revcount; k++) {
1961 1954 if (val == revs[k])
1962 1955 goto duplicate;
1963 1956 }
1964 1957 }
1965 1958 else repeat |= x;
1966 1959 if (revcount >= capacity) {
1967 1960 PyErr_Format(PyExc_OverflowError,
1968 1961 "bitset size (%d) > capacity (%d)",
1969 1962 revcount, capacity);
1970 1963 goto bail;
1971 1964 }
1972 1965 revs[revcount++] = (int)val;
1973 1966 duplicate:;
1974 1967 }
1975 1968
1976 1969 if (revcount == 0) {
1977 1970 ret = PyList_New(0);
1978 1971 goto done;
1979 1972 }
1980 1973 if (revcount == 1) {
1981 1974 PyObject *obj;
1982 1975 ret = PyList_New(1);
1983 1976 if (ret == NULL)
1984 1977 goto bail;
1985 1978 obj = PyInt_FromLong(revs[0]);
1986 1979 if (obj == NULL)
1987 1980 goto bail;
1988 1981 PyList_SET_ITEM(ret, 0, obj);
1989 1982 goto done;
1990 1983 }
1991 1984
1992 1985 ret = find_gca_candidates(self, revs, revcount);
1993 1986 if (ret == NULL)
1994 1987 goto bail;
1995 1988
1996 1989 done:
1997 1990 free(revs);
1998 1991 return ret;
1999 1992
2000 1993 bail:
2001 1994 free(revs);
2002 1995 Py_XDECREF(ret);
2003 1996 return NULL;
2004 1997 }
2005 1998
2006 1999 /*
2007 2000 * Given a (possibly overlapping) set of revs, return the greatest
2008 2001 * common ancestors: those with the longest path to the root.
2009 2002 */
2010 2003 static PyObject *index_ancestors(indexObject *self, PyObject *args)
2011 2004 {
2012 2005 PyObject *gca = index_commonancestorsheads(self, args);
2013 2006 if (gca == NULL)
2014 2007 return NULL;
2015 2008
2016 2009 if (PyList_GET_SIZE(gca) <= 1) {
2017 2010 Py_INCREF(gca);
2018 2011 return gca;
2019 2012 }
2020 2013
2021 2014 return find_deepest(self, gca);
2022 2015 }
2023 2016
2024 2017 /*
2025 2018 * Invalidate any trie entries introduced by added revs.
2026 2019 */
2027 2020 static void nt_invalidate_added(indexObject *self, Py_ssize_t start)
2028 2021 {
2029 2022 Py_ssize_t i, len = PyList_GET_SIZE(self->added);
2030 2023
2031 2024 for (i = start; i < len; i++) {
2032 2025 PyObject *tuple = PyList_GET_ITEM(self->added, i);
2033 2026 PyObject *node = PyTuple_GET_ITEM(tuple, 7);
2034 2027
2035 2028 nt_insert(self, PyString_AS_STRING(node), -1);
2036 2029 }
2037 2030
2038 2031 if (start == 0)
2039 2032 Py_CLEAR(self->added);
2040 2033 }
2041 2034
2042 2035 /*
2043 2036 * Delete a numeric range of revs, which must be at the end of the
2044 2037 * range, but exclude the sentinel nullid entry.
2045 2038 */
2046 2039 static int index_slice_del(indexObject *self, PyObject *item)
2047 2040 {
2048 2041 Py_ssize_t start, stop, step, slicelength;
2049 2042 Py_ssize_t length = index_length(self);
2050 2043 int ret = 0;
2051 2044
2052 2045 if (PySlice_GetIndicesEx((PySliceObject*)item, length,
2053 2046 &start, &stop, &step, &slicelength) < 0)
2054 2047 return -1;
2055 2048
2056 2049 if (slicelength <= 0)
2057 2050 return 0;
2058 2051
2059 2052 if ((step < 0 && start < stop) || (step > 0 && start > stop))
2060 2053 stop = start;
2061 2054
2062 2055 if (step < 0) {
2063 2056 stop = start + 1;
2064 2057 start = stop + step*(slicelength - 1) - 1;
2065 2058 step = -step;
2066 2059 }
2067 2060
2068 2061 if (step != 1) {
2069 2062 PyErr_SetString(PyExc_ValueError,
2070 2063 "revlog index delete requires step size of 1");
2071 2064 return -1;
2072 2065 }
2073 2066
2074 2067 if (stop != length - 1) {
2075 2068 PyErr_SetString(PyExc_IndexError,
2076 2069 "revlog index deletion indices are invalid");
2077 2070 return -1;
2078 2071 }
2079 2072
2080 2073 if (start < self->length - 1) {
2081 2074 if (self->nt) {
2082 2075 Py_ssize_t i;
2083 2076
2084 2077 for (i = start + 1; i < self->length - 1; i++) {
2085 2078 const char *node = index_node(self, i);
2086 2079
2087 2080 if (node)
2088 2081 nt_insert(self, node, -1);
2089 2082 }
2090 2083 if (self->added)
2091 2084 nt_invalidate_added(self, 0);
2092 2085 if (self->ntrev > start)
2093 2086 self->ntrev = (int)start;
2094 2087 }
2095 2088 self->length = start + 1;
2096 2089 if (start < self->raw_length) {
2097 2090 if (self->cache) {
2098 2091 Py_ssize_t i;
2099 2092 for (i = start; i < self->raw_length; i++)
2100 2093 Py_CLEAR(self->cache[i]);
2101 2094 }
2102 2095 self->raw_length = start;
2103 2096 }
2104 2097 goto done;
2105 2098 }
2106 2099
2107 2100 if (self->nt) {
2108 2101 nt_invalidate_added(self, start - self->length + 1);
2109 2102 if (self->ntrev > start)
2110 2103 self->ntrev = (int)start;
2111 2104 }
2112 2105 if (self->added)
2113 2106 ret = PyList_SetSlice(self->added, start - self->length + 1,
2114 2107 PyList_GET_SIZE(self->added), NULL);
2115 2108 done:
2116 2109 Py_CLEAR(self->headrevs);
2117 2110 return ret;
2118 2111 }
2119 2112
2120 2113 /*
2121 2114 * Supported ops:
2122 2115 *
2123 2116 * slice deletion
2124 2117 * string assignment (extend node->rev mapping)
2125 2118 * string deletion (shrink node->rev mapping)
2126 2119 */
2127 2120 static int index_assign_subscript(indexObject *self, PyObject *item,
2128 2121 PyObject *value)
2129 2122 {
2130 2123 char *node;
2131 2124 Py_ssize_t nodelen;
2132 2125 long rev;
2133 2126
2134 2127 if (PySlice_Check(item) && value == NULL)
2135 2128 return index_slice_del(self, item);
2136 2129
2137 2130 if (node_check(item, &node, &nodelen) == -1)
2138 2131 return -1;
2139 2132
2140 2133 if (value == NULL)
2141 2134 return self->nt ? nt_insert(self, node, -1) : 0;
2142 2135 rev = PyInt_AsLong(value);
2143 2136 if (rev > INT_MAX || rev < 0) {
2144 2137 if (!PyErr_Occurred())
2145 2138 PyErr_SetString(PyExc_ValueError, "rev out of range");
2146 2139 return -1;
2147 2140 }
2148 2141
2149 2142 if (nt_init(self) == -1)
2150 2143 return -1;
2151 2144 return nt_insert(self, node, (int)rev);
2152 2145 }
2153 2146
2154 2147 /*
2155 2148 * Find all RevlogNG entries in an index that has inline data. Update
2156 2149 * the optional "offsets" table with those entries.
2157 2150 */
2158 2151 static Py_ssize_t inline_scan(indexObject *self, const char **offsets)
2159 2152 {
2160 2153 const char *data = PyString_AS_STRING(self->data);
2161 2154 Py_ssize_t pos = 0;
2162 2155 Py_ssize_t end = PyString_GET_SIZE(self->data);
2163 2156 long incr = v1_hdrsize;
2164 2157 Py_ssize_t len = 0;
2165 2158
2166 2159 while (pos + v1_hdrsize <= end && pos >= 0) {
2167 2160 uint32_t comp_len;
2168 2161 /* 3rd element of header is length of compressed inline data */
2169 2162 comp_len = getbe32(data + pos + 8);
2170 2163 incr = v1_hdrsize + comp_len;
2171 2164 if (offsets)
2172 2165 offsets[len] = data + pos;
2173 2166 len++;
2174 2167 pos += incr;
2175 2168 }
2176 2169
2177 2170 if (pos != end) {
2178 2171 if (!PyErr_Occurred())
2179 2172 PyErr_SetString(PyExc_ValueError, "corrupt index file");
2180 2173 return -1;
2181 2174 }
2182 2175
2183 2176 return len;
2184 2177 }
2185 2178
2186 2179 static int index_init(indexObject *self, PyObject *args)
2187 2180 {
2188 2181 PyObject *data_obj, *inlined_obj;
2189 2182 Py_ssize_t size;
2190 2183
2191 2184 /* Initialize before argument-checking to avoid index_dealloc() crash. */
2192 2185 self->raw_length = 0;
2193 2186 self->added = NULL;
2194 2187 self->cache = NULL;
2195 2188 self->data = NULL;
2196 2189 self->headrevs = NULL;
2197 2190 self->filteredrevs = Py_None;
2198 2191 Py_INCREF(Py_None);
2199 2192 self->nt = NULL;
2200 2193 self->offsets = NULL;
2201 2194
2202 2195 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
2203 2196 return -1;
2204 2197 if (!PyString_Check(data_obj)) {
2205 2198 PyErr_SetString(PyExc_TypeError, "data is not a string");
2206 2199 return -1;
2207 2200 }
2208 2201 size = PyString_GET_SIZE(data_obj);
2209 2202
2210 2203 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
2211 2204 self->data = data_obj;
2212 2205
2213 2206 self->ntlength = self->ntcapacity = 0;
2214 2207 self->ntdepth = self->ntsplits = 0;
2215 2208 self->ntlookups = self->ntmisses = 0;
2216 2209 self->ntrev = -1;
2217 2210 Py_INCREF(self->data);
2218 2211
2219 2212 if (self->inlined) {
2220 2213 Py_ssize_t len = inline_scan(self, NULL);
2221 2214 if (len == -1)
2222 2215 goto bail;
2223 2216 self->raw_length = len;
2224 2217 self->length = len + 1;
2225 2218 } else {
2226 2219 if (size % v1_hdrsize) {
2227 2220 PyErr_SetString(PyExc_ValueError, "corrupt index file");
2228 2221 goto bail;
2229 2222 }
2230 2223 self->raw_length = size / v1_hdrsize;
2231 2224 self->length = self->raw_length + 1;
2232 2225 }
2233 2226
2234 2227 return 0;
2235 2228 bail:
2236 2229 return -1;
2237 2230 }
2238 2231
2239 2232 static PyObject *index_nodemap(indexObject *self)
2240 2233 {
2241 2234 Py_INCREF(self);
2242 2235 return (PyObject *)self;
2243 2236 }
2244 2237
2245 2238 static void index_dealloc(indexObject *self)
2246 2239 {
2247 2240 _index_clearcaches(self);
2248 2241 Py_XDECREF(self->filteredrevs);
2249 2242 Py_XDECREF(self->data);
2250 2243 Py_XDECREF(self->added);
2251 2244 PyObject_Del(self);
2252 2245 }
2253 2246
2254 2247 static PySequenceMethods index_sequence_methods = {
2255 2248 (lenfunc)index_length, /* sq_length */
2256 2249 0, /* sq_concat */
2257 2250 0, /* sq_repeat */
2258 2251 (ssizeargfunc)index_get, /* sq_item */
2259 2252 0, /* sq_slice */
2260 2253 0, /* sq_ass_item */
2261 2254 0, /* sq_ass_slice */
2262 2255 (objobjproc)index_contains, /* sq_contains */
2263 2256 };
2264 2257
2265 2258 static PyMappingMethods index_mapping_methods = {
2266 2259 (lenfunc)index_length, /* mp_length */
2267 2260 (binaryfunc)index_getitem, /* mp_subscript */
2268 2261 (objobjargproc)index_assign_subscript, /* mp_ass_subscript */
2269 2262 };
2270 2263
2271 2264 static PyMethodDef index_methods[] = {
2272 2265 {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS,
2273 2266 "return the gca set of the given revs"},
2274 2267 {"commonancestorsheads", (PyCFunction)index_commonancestorsheads,
2275 2268 METH_VARARGS,
2276 2269 "return the heads of the common ancestors of the given revs"},
2277 2270 {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS,
2278 2271 "clear the index caches"},
2279 2272 {"get", (PyCFunction)index_m_get, METH_VARARGS,
2280 2273 "get an index entry"},
2281 2274 {"computephases", (PyCFunction)compute_phases, METH_VARARGS,
2282 2275 "compute phases"},
2283 2276 {"headrevs", (PyCFunction)index_headrevs, METH_VARARGS,
2284 2277 "get head revisions"}, /* Can do filtering since 3.2 */
2285 2278 {"headrevsfiltered", (PyCFunction)index_headrevs, METH_VARARGS,
2286 2279 "get filtered head revisions"}, /* Can always do filtering */
2287 2280 {"insert", (PyCFunction)index_insert, METH_VARARGS,
2288 2281 "insert an index entry"},
2289 2282 {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS,
2290 2283 "match a potentially ambiguous node ID"},
2291 2284 {"stats", (PyCFunction)index_stats, METH_NOARGS,
2292 2285 "stats for the index"},
2293 2286 {NULL} /* Sentinel */
2294 2287 };
2295 2288
2296 2289 static PyGetSetDef index_getset[] = {
2297 2290 {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL},
2298 2291 {NULL} /* Sentinel */
2299 2292 };
2300 2293
2301 2294 static PyTypeObject indexType = {
2302 2295 PyObject_HEAD_INIT(NULL)
2303 2296 0, /* ob_size */
2304 2297 "parsers.index", /* tp_name */
2305 2298 sizeof(indexObject), /* tp_basicsize */
2306 2299 0, /* tp_itemsize */
2307 2300 (destructor)index_dealloc, /* tp_dealloc */
2308 2301 0, /* tp_print */
2309 2302 0, /* tp_getattr */
2310 2303 0, /* tp_setattr */
2311 2304 0, /* tp_compare */
2312 2305 0, /* tp_repr */
2313 2306 0, /* tp_as_number */
2314 2307 &index_sequence_methods, /* tp_as_sequence */
2315 2308 &index_mapping_methods, /* tp_as_mapping */
2316 2309 0, /* tp_hash */
2317 2310 0, /* tp_call */
2318 2311 0, /* tp_str */
2319 2312 0, /* tp_getattro */
2320 2313 0, /* tp_setattro */
2321 2314 0, /* tp_as_buffer */
2322 2315 Py_TPFLAGS_DEFAULT, /* tp_flags */
2323 2316 "revlog index", /* tp_doc */
2324 2317 0, /* tp_traverse */
2325 2318 0, /* tp_clear */
2326 2319 0, /* tp_richcompare */
2327 2320 0, /* tp_weaklistoffset */
2328 2321 0, /* tp_iter */
2329 2322 0, /* tp_iternext */
2330 2323 index_methods, /* tp_methods */
2331 2324 0, /* tp_members */
2332 2325 index_getset, /* tp_getset */
2333 2326 0, /* tp_base */
2334 2327 0, /* tp_dict */
2335 2328 0, /* tp_descr_get */
2336 2329 0, /* tp_descr_set */
2337 2330 0, /* tp_dictoffset */
2338 2331 (initproc)index_init, /* tp_init */
2339 2332 0, /* tp_alloc */
2340 2333 };
2341 2334
2342 2335 /*
2343 2336 * returns a tuple of the form (index, index, cache) with elements as
2344 2337 * follows:
2345 2338 *
2346 2339 * index: an index object that lazily parses RevlogNG records
2347 2340 * cache: if data is inlined, a tuple (index_file_content, 0), else None
2348 2341 *
2349 2342 * added complications are for backwards compatibility
2350 2343 */
2351 2344 static PyObject *parse_index2(PyObject *self, PyObject *args)
2352 2345 {
2353 2346 PyObject *tuple = NULL, *cache = NULL;
2354 2347 indexObject *idx;
2355 2348 int ret;
2356 2349
2357 2350 idx = PyObject_New(indexObject, &indexType);
2358 2351 if (idx == NULL)
2359 2352 goto bail;
2360 2353
2361 2354 ret = index_init(idx, args);
2362 2355 if (ret == -1)
2363 2356 goto bail;
2364 2357
2365 2358 if (idx->inlined) {
2366 2359 cache = Py_BuildValue("iO", 0, idx->data);
2367 2360 if (cache == NULL)
2368 2361 goto bail;
2369 2362 } else {
2370 2363 cache = Py_None;
2371 2364 Py_INCREF(cache);
2372 2365 }
2373 2366
2374 2367 tuple = Py_BuildValue("NN", idx, cache);
2375 2368 if (!tuple)
2376 2369 goto bail;
2377 2370 return tuple;
2378 2371
2379 2372 bail:
2380 2373 Py_XDECREF(idx);
2381 2374 Py_XDECREF(cache);
2382 2375 Py_XDECREF(tuple);
2383 2376 return NULL;
2384 2377 }
2385 2378
2386 2379 #define BUMPED_FIX 1
2387 2380 #define USING_SHA_256 2
2388 2381
2389 2382 static PyObject *readshas(
2390 2383 const char *source, unsigned char num, Py_ssize_t hashwidth)
2391 2384 {
2392 2385 int i;
2393 2386 PyObject *list = PyTuple_New(num);
2394 2387 if (list == NULL) {
2395 2388 return NULL;
2396 2389 }
2397 2390 for (i = 0; i < num; i++) {
2398 2391 PyObject *hash = PyString_FromStringAndSize(source, hashwidth);
2399 2392 if (hash == NULL) {
2400 2393 Py_DECREF(list);
2401 2394 return NULL;
2402 2395 }
2403 2396 PyTuple_SetItem(list, i, hash);
2404 2397 source += hashwidth;
2405 2398 }
2406 2399 return list;
2407 2400 }
2408 2401
2409 2402 static PyObject *fm1readmarker(const char *data, uint32_t *msize)
2410 2403 {
2411 2404 const char *meta;
2412 2405
2413 2406 double mtime;
2414 2407 int16_t tz;
2415 2408 uint16_t flags;
2416 2409 unsigned char nsuccs, nparents, nmetadata;
2417 2410 Py_ssize_t hashwidth = 20;
2418 2411
2419 2412 PyObject *prec = NULL, *parents = NULL, *succs = NULL;
2420 2413 PyObject *metadata = NULL, *ret = NULL;
2421 2414 int i;
2422 2415
2423 2416 *msize = getbe32(data);
2424 2417 data += 4;
2425 2418 mtime = getbefloat64(data);
2426 2419 data += 8;
2427 2420 tz = getbeint16(data);
2428 2421 data += 2;
2429 2422 flags = getbeuint16(data);
2430 2423 data += 2;
2431 2424
2432 2425 if (flags & USING_SHA_256) {
2433 2426 hashwidth = 32;
2434 2427 }
2435 2428
2436 2429 nsuccs = (unsigned char)(*data++);
2437 2430 nparents = (unsigned char)(*data++);
2438 2431 nmetadata = (unsigned char)(*data++);
2439 2432
2440 2433 prec = PyString_FromStringAndSize(data, hashwidth);
2441 2434 data += hashwidth;
2442 2435 if (prec == NULL) {
2443 2436 goto bail;
2444 2437 }
2445 2438
2446 2439 succs = readshas(data, nsuccs, hashwidth);
2447 2440 if (succs == NULL) {
2448 2441 goto bail;
2449 2442 }
2450 2443 data += nsuccs * hashwidth;
2451 2444
2452 2445 if (nparents == 1 || nparents == 2) {
2453 2446 parents = readshas(data, nparents, hashwidth);
2454 2447 if (parents == NULL) {
2455 2448 goto bail;
2456 2449 }
2457 2450 data += nparents * hashwidth;
2458 2451 } else {
2459 2452 parents = Py_None;
2460 2453 }
2461 2454
2462 2455 meta = data + (2 * nmetadata);
2463 2456 metadata = PyTuple_New(nmetadata);
2464 2457 if (metadata == NULL) {
2465 2458 goto bail;
2466 2459 }
2467 2460 for (i = 0; i < nmetadata; i++) {
2468 2461 PyObject *tmp, *left = NULL, *right = NULL;
2469 2462 Py_ssize_t metasize = (unsigned char)(*data++);
2470 2463 left = PyString_FromStringAndSize(meta, metasize);
2471 2464 meta += metasize;
2472 2465 metasize = (unsigned char)(*data++);
2473 2466 right = PyString_FromStringAndSize(meta, metasize);
2474 2467 meta += metasize;
2475 2468 if (!left || !right) {
2476 2469 Py_XDECREF(left);
2477 2470 Py_XDECREF(right);
2478 2471 goto bail;
2479 2472 }
2480 2473 tmp = PyTuple_Pack(2, left, right);
2481 2474 Py_DECREF(left);
2482 2475 Py_DECREF(right);
2483 2476 if (!tmp) {
2484 2477 goto bail;
2485 2478 }
2486 2479 PyTuple_SetItem(metadata, i, tmp);
2487 2480 }
2488 2481 ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags,
2489 2482 metadata, mtime, (int)tz * 60, parents);
2490 2483 bail:
2491 2484 Py_XDECREF(prec);
2492 2485 Py_XDECREF(succs);
2493 2486 Py_XDECREF(metadata);
2494 2487 if (parents != Py_None)
2495 2488 Py_XDECREF(parents);
2496 2489 return ret;
2497 2490 }
2498 2491
2499 2492
2500 2493 static PyObject *fm1readmarkers(PyObject *self, PyObject *args) {
2501 2494 const char *data;
2502 2495 Py_ssize_t datalen;
2503 2496 /* only unsigned long because python 2.4, should be Py_ssize_t */
2504 2497 unsigned long offset, stop;
2505 2498 PyObject *markers = NULL;
2506 2499
2507 2500 /* replace kk with nn when we drop Python 2.4 */
2508 2501 if (!PyArg_ParseTuple(args, "s#kk", &data, &datalen, &offset, &stop)) {
2509 2502 return NULL;
2510 2503 }
2511 2504 data += offset;
2512 2505 markers = PyList_New(0);
2513 2506 if (!markers) {
2514 2507 return NULL;
2515 2508 }
2516 2509 while (offset < stop) {
2517 2510 uint32_t msize;
2518 2511 int error;
2519 2512 PyObject *record = fm1readmarker(data, &msize);
2520 2513 if (!record) {
2521 2514 goto bail;
2522 2515 }
2523 2516 error = PyList_Append(markers, record);
2524 2517 Py_DECREF(record);
2525 2518 if (error) {
2526 2519 goto bail;
2527 2520 }
2528 2521 data += msize;
2529 2522 offset += msize;
2530 2523 }
2531 2524 return markers;
2532 2525 bail:
2533 2526 Py_DECREF(markers);
2534 2527 return NULL;
2535 2528 }
2536 2529
2537 2530 static char parsers_doc[] = "Efficient content parsing.";
2538 2531
2539 2532 PyObject *encodedir(PyObject *self, PyObject *args);
2540 2533 PyObject *pathencode(PyObject *self, PyObject *args);
2541 2534 PyObject *lowerencode(PyObject *self, PyObject *args);
2542 2535
2543 2536 static PyMethodDef methods[] = {
2544 2537 {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"},
2545 2538 {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"},
2546 2539 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
2547 2540 {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"},
2548 2541 {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"},
2549 2542 {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"},
2550 2543 {"make_file_foldmap", make_file_foldmap, METH_VARARGS,
2551 2544 "make file foldmap\n"},
2552 2545 {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"},
2553 2546 {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"},
2554 2547 {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"},
2555 2548 {"fm1readmarkers", fm1readmarkers, METH_VARARGS,
2556 2549 "parse v1 obsolete markers\n"},
2557 2550 {NULL, NULL}
2558 2551 };
2559 2552
2560 2553 void dirs_module_init(PyObject *mod);
2561 2554 void manifest_module_init(PyObject *mod);
2562 2555
2563 2556 static void module_init(PyObject *mod)
2564 2557 {
2565 2558 /* This module constant has two purposes. First, it lets us unit test
2566 2559 * the ImportError raised without hard-coding any error text. This
2567 2560 * means we can change the text in the future without breaking tests,
2568 2561 * even across changesets without a recompile. Second, its presence
2569 2562 * can be used to determine whether the version-checking logic is
2570 2563 * present, which also helps in testing across changesets without a
2571 2564 * recompile. Note that this means the pure-Python version of parsers
2572 2565 * should not have this module constant. */
2573 2566 PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext);
2574 2567
2575 2568 dirs_module_init(mod);
2576 2569 manifest_module_init(mod);
2577 2570
2578 2571 indexType.tp_new = PyType_GenericNew;
2579 2572 if (PyType_Ready(&indexType) < 0 ||
2580 2573 PyType_Ready(&dirstateTupleType) < 0)
2581 2574 return;
2582 2575 Py_INCREF(&indexType);
2583 2576 PyModule_AddObject(mod, "index", (PyObject *)&indexType);
2584 2577 Py_INCREF(&dirstateTupleType);
2585 2578 PyModule_AddObject(mod, "dirstatetuple",
2586 2579 (PyObject *)&dirstateTupleType);
2587 2580
2588 2581 nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0,
2589 2582 -1, -1, -1, -1, nullid, 20);
2590 2583 if (nullentry)
2591 2584 PyObject_GC_UnTrack(nullentry);
2592 2585 }
2593 2586
2594 2587 static int check_python_version(void)
2595 2588 {
2596 2589 PyObject *sys = PyImport_ImportModule("sys"), *ver;
2597 2590 long hexversion;
2598 2591 if (!sys)
2599 2592 return -1;
2600 2593 ver = PyObject_GetAttrString(sys, "hexversion");
2601 2594 Py_DECREF(sys);
2602 2595 if (!ver)
2603 2596 return -1;
2604 2597 hexversion = PyInt_AsLong(ver);
2605 2598 Py_DECREF(ver);
2606 2599 /* sys.hexversion is a 32-bit number by default, so the -1 case
2607 2600 * should only occur in unusual circumstances (e.g. if sys.hexversion
2608 2601 * is manually set to an invalid value). */
2609 2602 if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) {
2610 2603 PyErr_Format(PyExc_ImportError, "%s: The Mercurial extension "
2611 2604 "modules were compiled with Python " PY_VERSION ", but "
2612 2605 "Mercurial is currently using Python with sys.hexversion=%ld: "
2613 2606 "Python %s\n at: %s", versionerrortext, hexversion,
2614 2607 Py_GetVersion(), Py_GetProgramFullPath());
2615 2608 return -1;
2616 2609 }
2617 2610 return 0;
2618 2611 }
2619 2612
2620 2613 #ifdef IS_PY3K
2621 2614 static struct PyModuleDef parsers_module = {
2622 2615 PyModuleDef_HEAD_INIT,
2623 2616 "parsers",
2624 2617 parsers_doc,
2625 2618 -1,
2626 2619 methods
2627 2620 };
2628 2621
2629 2622 PyMODINIT_FUNC PyInit_parsers(void)
2630 2623 {
2631 2624 PyObject *mod;
2632 2625
2633 2626 if (check_python_version() == -1)
2634 2627 return;
2635 2628 mod = PyModule_Create(&parsers_module);
2636 2629 module_init(mod);
2637 2630 return mod;
2638 2631 }
2639 2632 #else
2640 2633 PyMODINIT_FUNC initparsers(void)
2641 2634 {
2642 2635 PyObject *mod;
2643 2636
2644 2637 if (check_python_version() == -1)
2645 2638 return;
2646 2639 mod = Py_InitModule3("parsers", methods, parsers_doc);
2647 2640 module_init(mod);
2648 2641 }
2649 2642 #endif
General Comments 0
You need to be logged in to leave comments. Login now