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