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