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