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