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