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