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