##// END OF EJS Templates
dirstate-item: `dirstate_item_from_v1_data` replaces make_dirstate_item...
marmoute -
r48757:3337eec2 default
parent child Browse files
Show More
@@ -1,1160 +1,1145 b''
1 1 /*
2 2 parsers.c - efficient content parsing
3 3
4 4 Copyright 2008 Olivia Mackall <olivia@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 #define PY_SSIZE_T_CLEAN
11 11 #include <Python.h>
12 12 #include <ctype.h>
13 13 #include <stddef.h>
14 14 #include <string.h>
15 15
16 16 #include "bitmanipulation.h"
17 17 #include "charencode.h"
18 18 #include "util.h"
19 19
20 20 #ifdef IS_PY3K
21 21 /* The mapping of Python types is meant to be temporary to get Python
22 22 * 3 to compile. We should remove this once Python 3 support is fully
23 23 * supported and proper types are used in the extensions themselves. */
24 24 #define PyInt_Check PyLong_Check
25 25 #define PyInt_FromLong PyLong_FromLong
26 26 #define PyInt_FromSsize_t PyLong_FromSsize_t
27 27 #define PyInt_AsLong PyLong_AsLong
28 28 #endif
29 29
30 30 static const char *const versionerrortext = "Python minor version mismatch";
31 31
32 32 static const int dirstate_v1_from_p2 = -2;
33 33 static const int dirstate_v1_nonnormal = -1;
34 34 static const int ambiguous_time = -1;
35 35
36 36 static PyObject *dict_new_presized(PyObject *self, PyObject *args)
37 37 {
38 38 Py_ssize_t expected_size;
39 39
40 40 if (!PyArg_ParseTuple(args, "n:make_presized_dict", &expected_size)) {
41 41 return NULL;
42 42 }
43 43
44 44 return _dict_new_presized(expected_size);
45 45 }
46 46
47 static inline dirstateItemObject *make_dirstate_item(char state, int mode,
48 int size, int mtime)
49 {
50 dirstateItemObject *t =
51 PyObject_New(dirstateItemObject, &dirstateItemType);
52 if (!t) {
53 return NULL;
54 }
55 t->state = state;
56 t->mode = mode;
57 t->size = size;
58 t->mtime = mtime;
59 return t;
60 }
61
62 47 static PyObject *dirstate_item_new(PyTypeObject *subtype, PyObject *args,
63 48 PyObject *kwds)
64 49 {
65 50 /* We do all the initialization here and not a tp_init function because
66 51 * dirstate_item is immutable. */
67 52 dirstateItemObject *t;
68 53 int wc_tracked;
69 54 int p1_tracked;
70 55 int p2_tracked;
71 56 int merged;
72 57 int clean_p1;
73 58 int clean_p2;
74 59 int possibly_dirty;
75 60 PyObject *parentfiledata;
76 61 static char *keywords_name[] = {
77 62 "wc_tracked", "p1_tracked", "p2_tracked",
78 63 "merged", "clean_p1", "clean_p2",
79 64 "possibly_dirty", "parentfiledata", NULL,
80 65 };
81 66 wc_tracked = 0;
82 67 p1_tracked = 0;
83 68 p2_tracked = 0;
84 69 merged = 0;
85 70 clean_p1 = 0;
86 71 clean_p2 = 0;
87 72 possibly_dirty = 0;
88 73 parentfiledata = Py_None;
89 74 if (!PyArg_ParseTupleAndKeywords(args, kwds, "iiiiiiiO", keywords_name,
90 75 &wc_tracked, &p1_tracked, &p2_tracked,
91 76 &merged, &clean_p1, &clean_p2,
92 77 &possibly_dirty, &parentfiledata
93 78
94 79 )) {
95 80 return NULL;
96 81 }
97 82 if (merged && (clean_p1 || clean_p2)) {
98 83 PyErr_SetString(PyExc_RuntimeError,
99 84 "`merged` argument incompatible with "
100 85 "`clean_p1`/`clean_p2`");
101 86 return NULL;
102 87 }
103 88 t = (dirstateItemObject *)subtype->tp_alloc(subtype, 1);
104 89 if (!t) {
105 90 return NULL;
106 91 }
107 92 t->state = 'r';
108 93 t->mode = 0;
109 94 t->size = dirstate_v1_nonnormal;
110 95 t->mtime = ambiguous_time;
111 96 if (!(p1_tracked || p2_tracked || wc_tracked)) {
112 97 /* Nothing special to do, file is untracked */
113 98 } else if (merged) {
114 99 t->state = 'm';
115 100 t->size = dirstate_v1_from_p2;
116 101 t->mtime = ambiguous_time;
117 102 } else if (!(p1_tracked || p2_tracked) && wc_tracked) {
118 103 t->state = 'a';
119 104 t->size = dirstate_v1_nonnormal;
120 105 t->mtime = ambiguous_time;
121 106 } else if ((p1_tracked || p2_tracked) && !wc_tracked) {
122 107 t->state = 'r';
123 108 t->size = 0;
124 109 t->mtime = 0;
125 110 } else if (clean_p2 && wc_tracked) {
126 111 t->state = 'n';
127 112 t->size = dirstate_v1_from_p2;
128 113 t->mtime = ambiguous_time;
129 114 } else if (!p1_tracked && p2_tracked && wc_tracked) {
130 115 t->state = 'n';
131 116 t->size = dirstate_v1_from_p2;
132 117 t->mtime = ambiguous_time;
133 118 } else if (possibly_dirty) {
134 119 t->state = 'n';
135 120 t->size = dirstate_v1_nonnormal;
136 121 t->mtime = ambiguous_time;
137 122 } else if (wc_tracked) {
138 123 /* this is a "normal" file */
139 124 if (parentfiledata == Py_None) {
140 125 PyErr_SetString(
141 126 PyExc_RuntimeError,
142 127 "failed to pass parentfiledata for a normal file");
143 128 return NULL;
144 129 }
145 130 if (!PyTuple_CheckExact(parentfiledata)) {
146 131 PyErr_SetString(
147 132 PyExc_TypeError,
148 133 "parentfiledata should be a Tuple or None");
149 134 return NULL;
150 135 }
151 136 t->state = 'n';
152 137 t->mode =
153 138 (int)PyLong_AsLong(PyTuple_GetItem(parentfiledata, 0));
154 139 t->size =
155 140 (int)PyLong_AsLong(PyTuple_GetItem(parentfiledata, 1));
156 141 t->mtime =
157 142 (int)PyLong_AsLong(PyTuple_GetItem(parentfiledata, 2));
158 143 } else {
159 144 PyErr_SetString(PyExc_RuntimeError, "unreachable");
160 145 return NULL;
161 146 }
162 147 return (PyObject *)t;
163 148 }
164 149
165 150 static void dirstate_item_dealloc(PyObject *o)
166 151 {
167 152 PyObject_Del(o);
168 153 }
169 154
170 155 static PyObject *dirstate_item_v1_state(dirstateItemObject *self)
171 156 {
172 157 return PyBytes_FromStringAndSize(&self->state, 1);
173 158 };
174 159
175 160 static PyObject *dirstate_item_v1_mode(dirstateItemObject *self)
176 161 {
177 162 return PyInt_FromLong(self->mode);
178 163 };
179 164
180 165 static PyObject *dirstate_item_v1_size(dirstateItemObject *self)
181 166 {
182 167 return PyInt_FromLong(self->size);
183 168 };
184 169
185 170 static PyObject *dirstate_item_v1_mtime(dirstateItemObject *self)
186 171 {
187 172 return PyInt_FromLong(self->mtime);
188 173 };
189 174
190 175 static PyObject *dirstate_item_need_delay(dirstateItemObject *self,
191 176 PyObject *value)
192 177 {
193 178 long now;
194 179 if (!pylong_to_long(value, &now)) {
195 180 return NULL;
196 181 }
197 182 if (self->state == 'n' && self->mtime == now) {
198 183 Py_RETURN_TRUE;
199 184 } else {
200 185 Py_RETURN_FALSE;
201 186 }
202 187 };
203 188
204 /* This will never change since it's bound to V1, unlike `make_dirstate_item`
189 /* This will never change since it's bound to V1
205 190 */
206 191 static inline dirstateItemObject *
207 192 dirstate_item_from_v1_data(char state, int mode, int size, int mtime)
208 193 {
209 194 dirstateItemObject *t =
210 195 PyObject_New(dirstateItemObject, &dirstateItemType);
211 196 if (!t) {
212 197 return NULL;
213 198 }
214 199 t->state = state;
215 200 t->mode = mode;
216 201 t->size = size;
217 202 t->mtime = mtime;
218 203 return t;
219 204 }
220 205
221 206 /* This will never change since it's bound to V1, unlike `dirstate_item_new` */
222 207 static PyObject *dirstate_item_from_v1_meth(PyTypeObject *subtype,
223 208 PyObject *args)
224 209 {
225 210 /* We do all the initialization here and not a tp_init function because
226 211 * dirstate_item is immutable. */
227 212 dirstateItemObject *t;
228 213 char state;
229 214 int size, mode, mtime;
230 215 if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime)) {
231 216 return NULL;
232 217 }
233 218
234 219 t = (dirstateItemObject *)subtype->tp_alloc(subtype, 1);
235 220 if (!t) {
236 221 return NULL;
237 222 }
238 223 t->state = state;
239 224 t->mode = mode;
240 225 t->size = size;
241 226 t->mtime = mtime;
242 227
243 228 return (PyObject *)t;
244 229 };
245 230
246 231 /* constructor to help legacy API to build a new "added" item
247 232
248 233 Should eventually be removed */
249 234 static PyObject *dirstate_item_new_added(PyTypeObject *subtype)
250 235 {
251 236 dirstateItemObject *t;
252 237 t = (dirstateItemObject *)subtype->tp_alloc(subtype, 1);
253 238 if (!t) {
254 239 return NULL;
255 240 }
256 241 t->state = 'a';
257 242 t->mode = 0;
258 243 t->size = dirstate_v1_nonnormal;
259 244 t->mtime = ambiguous_time;
260 245 return (PyObject *)t;
261 246 };
262 247
263 248 /* constructor to help legacy API to build a new "merged" item
264 249
265 250 Should eventually be removed */
266 251 static PyObject *dirstate_item_new_merged(PyTypeObject *subtype)
267 252 {
268 253 dirstateItemObject *t;
269 254 t = (dirstateItemObject *)subtype->tp_alloc(subtype, 1);
270 255 if (!t) {
271 256 return NULL;
272 257 }
273 258 t->state = 'm';
274 259 t->mode = 0;
275 260 t->size = dirstate_v1_from_p2;
276 261 t->mtime = ambiguous_time;
277 262 return (PyObject *)t;
278 263 };
279 264
280 265 /* constructor to help legacy API to build a new "from_p2" item
281 266
282 267 Should eventually be removed */
283 268 static PyObject *dirstate_item_new_from_p2(PyTypeObject *subtype)
284 269 {
285 270 /* We do all the initialization here and not a tp_init function because
286 271 * dirstate_item is immutable. */
287 272 dirstateItemObject *t;
288 273 t = (dirstateItemObject *)subtype->tp_alloc(subtype, 1);
289 274 if (!t) {
290 275 return NULL;
291 276 }
292 277 t->state = 'n';
293 278 t->mode = 0;
294 279 t->size = dirstate_v1_from_p2;
295 280 t->mtime = ambiguous_time;
296 281 return (PyObject *)t;
297 282 };
298 283
299 284 /* constructor to help legacy API to build a new "possibly" item
300 285
301 286 Should eventually be removed */
302 287 static PyObject *dirstate_item_new_possibly_dirty(PyTypeObject *subtype)
303 288 {
304 289 /* We do all the initialization here and not a tp_init function because
305 290 * dirstate_item is immutable. */
306 291 dirstateItemObject *t;
307 292 t = (dirstateItemObject *)subtype->tp_alloc(subtype, 1);
308 293 if (!t) {
309 294 return NULL;
310 295 }
311 296 t->state = 'n';
312 297 t->mode = 0;
313 298 t->size = dirstate_v1_nonnormal;
314 299 t->mtime = ambiguous_time;
315 300 return (PyObject *)t;
316 301 };
317 302
318 303 /* constructor to help legacy API to build a new "normal" item
319 304
320 305 Should eventually be removed */
321 306 static PyObject *dirstate_item_new_normal(PyTypeObject *subtype, PyObject *args)
322 307 {
323 308 /* We do all the initialization here and not a tp_init function because
324 309 * dirstate_item is immutable. */
325 310 dirstateItemObject *t;
326 311 int size, mode, mtime;
327 312 if (!PyArg_ParseTuple(args, "iii", &mode, &size, &mtime)) {
328 313 return NULL;
329 314 }
330 315
331 316 t = (dirstateItemObject *)subtype->tp_alloc(subtype, 1);
332 317 if (!t) {
333 318 return NULL;
334 319 }
335 320 t->state = 'n';
336 321 t->mode = mode;
337 322 t->size = size;
338 323 t->mtime = mtime;
339 324 return (PyObject *)t;
340 325 };
341 326
342 327 /* This means the next status call will have to actually check its content
343 328 to make sure it is correct. */
344 329 static PyObject *dirstate_item_set_possibly_dirty(dirstateItemObject *self)
345 330 {
346 331 self->mtime = ambiguous_time;
347 332 Py_RETURN_NONE;
348 333 }
349 334
350 335 static PyObject *dirstate_item_set_untracked(dirstateItemObject *self)
351 336 {
352 337 if (self->state == 'm') {
353 338 self->size = dirstate_v1_nonnormal;
354 339 } else if (self->state == 'n' && self->size == dirstate_v1_from_p2) {
355 340 self->size = dirstate_v1_from_p2;
356 341 } else {
357 342 self->size = 0;
358 343 }
359 344 self->state = 'r';
360 345 self->mode = 0;
361 346 self->mtime = 0;
362 347 Py_RETURN_NONE;
363 348 }
364 349
365 350 static PyMethodDef dirstate_item_methods[] = {
366 351 {"v1_state", (PyCFunction)dirstate_item_v1_state, METH_NOARGS,
367 352 "return a \"state\" suitable for v1 serialization"},
368 353 {"v1_mode", (PyCFunction)dirstate_item_v1_mode, METH_NOARGS,
369 354 "return a \"mode\" suitable for v1 serialization"},
370 355 {"v1_size", (PyCFunction)dirstate_item_v1_size, METH_NOARGS,
371 356 "return a \"size\" suitable for v1 serialization"},
372 357 {"v1_mtime", (PyCFunction)dirstate_item_v1_mtime, METH_NOARGS,
373 358 "return a \"mtime\" suitable for v1 serialization"},
374 359 {"need_delay", (PyCFunction)dirstate_item_need_delay, METH_O,
375 360 "True if the stored mtime would be ambiguous with the current time"},
376 361 {"from_v1_data", (PyCFunction)dirstate_item_from_v1_meth,
377 362 METH_VARARGS | METH_CLASS, "build a new DirstateItem object from V1 data"},
378 363 {"new_added", (PyCFunction)dirstate_item_new_added,
379 364 METH_NOARGS | METH_CLASS,
380 365 "constructor to help legacy API to build a new \"added\" item"},
381 366 {"new_merged", (PyCFunction)dirstate_item_new_merged,
382 367 METH_NOARGS | METH_CLASS,
383 368 "constructor to help legacy API to build a new \"merged\" item"},
384 369 {"new_from_p2", (PyCFunction)dirstate_item_new_from_p2,
385 370 METH_NOARGS | METH_CLASS,
386 371 "constructor to help legacy API to build a new \"from_p2\" item"},
387 372 {"new_possibly_dirty", (PyCFunction)dirstate_item_new_possibly_dirty,
388 373 METH_NOARGS | METH_CLASS,
389 374 "constructor to help legacy API to build a new \"possibly_dirty\" item"},
390 375 {"new_normal", (PyCFunction)dirstate_item_new_normal,
391 376 METH_VARARGS | METH_CLASS,
392 377 "constructor to help legacy API to build a new \"normal\" item"},
393 378 {"set_possibly_dirty", (PyCFunction)dirstate_item_set_possibly_dirty,
394 379 METH_NOARGS, "mark a file as \"possibly dirty\""},
395 380 {"set_untracked", (PyCFunction)dirstate_item_set_untracked, METH_NOARGS,
396 381 "mark a file as \"untracked\""},
397 382 {NULL} /* Sentinel */
398 383 };
399 384
400 385 static PyObject *dirstate_item_get_mode(dirstateItemObject *self)
401 386 {
402 387 return PyInt_FromLong(self->mode);
403 388 };
404 389
405 390 static PyObject *dirstate_item_get_size(dirstateItemObject *self)
406 391 {
407 392 return PyInt_FromLong(self->size);
408 393 };
409 394
410 395 static PyObject *dirstate_item_get_mtime(dirstateItemObject *self)
411 396 {
412 397 return PyInt_FromLong(self->mtime);
413 398 };
414 399
415 400 static PyObject *dirstate_item_get_state(dirstateItemObject *self)
416 401 {
417 402 return PyBytes_FromStringAndSize(&self->state, 1);
418 403 };
419 404
420 405 static PyObject *dirstate_item_get_tracked(dirstateItemObject *self)
421 406 {
422 407 if (self->state == 'a' || self->state == 'm' || self->state == 'n') {
423 408 Py_RETURN_TRUE;
424 409 } else {
425 410 Py_RETURN_FALSE;
426 411 }
427 412 };
428 413
429 414 static PyObject *dirstate_item_get_added(dirstateItemObject *self)
430 415 {
431 416 if (self->state == 'a') {
432 417 Py_RETURN_TRUE;
433 418 } else {
434 419 Py_RETURN_FALSE;
435 420 }
436 421 };
437 422
438 423 static PyObject *dirstate_item_get_merged(dirstateItemObject *self)
439 424 {
440 425 if (self->state == 'm') {
441 426 Py_RETURN_TRUE;
442 427 } else {
443 428 Py_RETURN_FALSE;
444 429 }
445 430 };
446 431
447 432 static PyObject *dirstate_item_get_merged_removed(dirstateItemObject *self)
448 433 {
449 434 if (self->state == 'r' && self->size == dirstate_v1_nonnormal) {
450 435 Py_RETURN_TRUE;
451 436 } else {
452 437 Py_RETURN_FALSE;
453 438 }
454 439 };
455 440
456 441 static PyObject *dirstate_item_get_from_p2(dirstateItemObject *self)
457 442 {
458 443 if (self->state == 'n' && self->size == dirstate_v1_from_p2) {
459 444 Py_RETURN_TRUE;
460 445 } else {
461 446 Py_RETURN_FALSE;
462 447 }
463 448 };
464 449
465 450 static PyObject *dirstate_item_get_from_p2_removed(dirstateItemObject *self)
466 451 {
467 452 if (self->state == 'r' && self->size == dirstate_v1_from_p2) {
468 453 Py_RETURN_TRUE;
469 454 } else {
470 455 Py_RETURN_FALSE;
471 456 }
472 457 };
473 458
474 459 static PyObject *dirstate_item_get_removed(dirstateItemObject *self)
475 460 {
476 461 if (self->state == 'r') {
477 462 Py_RETURN_TRUE;
478 463 } else {
479 464 Py_RETURN_FALSE;
480 465 }
481 466 };
482 467
483 468 static PyObject *dm_nonnormal(dirstateItemObject *self)
484 469 {
485 470 if (self->state != 'n' || self->mtime == ambiguous_time) {
486 471 Py_RETURN_TRUE;
487 472 } else {
488 473 Py_RETURN_FALSE;
489 474 }
490 475 };
491 476 static PyObject *dm_otherparent(dirstateItemObject *self)
492 477 {
493 478 if (self->size == dirstate_v1_from_p2) {
494 479 Py_RETURN_TRUE;
495 480 } else {
496 481 Py_RETURN_FALSE;
497 482 }
498 483 };
499 484
500 485 static PyGetSetDef dirstate_item_getset[] = {
501 486 {"mode", (getter)dirstate_item_get_mode, NULL, "mode", NULL},
502 487 {"size", (getter)dirstate_item_get_size, NULL, "size", NULL},
503 488 {"mtime", (getter)dirstate_item_get_mtime, NULL, "mtime", NULL},
504 489 {"state", (getter)dirstate_item_get_state, NULL, "state", NULL},
505 490 {"tracked", (getter)dirstate_item_get_tracked, NULL, "tracked", NULL},
506 491 {"added", (getter)dirstate_item_get_added, NULL, "added", NULL},
507 492 {"merged_removed", (getter)dirstate_item_get_merged_removed, NULL,
508 493 "merged_removed", NULL},
509 494 {"merged", (getter)dirstate_item_get_merged, NULL, "merged", NULL},
510 495 {"from_p2_removed", (getter)dirstate_item_get_from_p2_removed, NULL,
511 496 "from_p2_removed", NULL},
512 497 {"from_p2", (getter)dirstate_item_get_from_p2, NULL, "from_p2", NULL},
513 498 {"removed", (getter)dirstate_item_get_removed, NULL, "removed", NULL},
514 499 {"dm_nonnormal", (getter)dm_nonnormal, NULL, "dm_nonnormal", NULL},
515 500 {"dm_otherparent", (getter)dm_otherparent, NULL, "dm_otherparent", NULL},
516 501 {NULL} /* Sentinel */
517 502 };
518 503
519 504 PyTypeObject dirstateItemType = {
520 505 PyVarObject_HEAD_INIT(NULL, 0) /* header */
521 506 "dirstate_tuple", /* tp_name */
522 507 sizeof(dirstateItemObject), /* tp_basicsize */
523 508 0, /* tp_itemsize */
524 509 (destructor)dirstate_item_dealloc, /* tp_dealloc */
525 510 0, /* tp_print */
526 511 0, /* tp_getattr */
527 512 0, /* tp_setattr */
528 513 0, /* tp_compare */
529 514 0, /* tp_repr */
530 515 0, /* tp_as_number */
531 516 0, /* tp_as_sequence */
532 517 0, /* tp_as_mapping */
533 518 0, /* tp_hash */
534 519 0, /* tp_call */
535 520 0, /* tp_str */
536 521 0, /* tp_getattro */
537 522 0, /* tp_setattro */
538 523 0, /* tp_as_buffer */
539 524 Py_TPFLAGS_DEFAULT, /* tp_flags */
540 525 "dirstate tuple", /* tp_doc */
541 526 0, /* tp_traverse */
542 527 0, /* tp_clear */
543 528 0, /* tp_richcompare */
544 529 0, /* tp_weaklistoffset */
545 530 0, /* tp_iter */
546 531 0, /* tp_iternext */
547 532 dirstate_item_methods, /* tp_methods */
548 533 0, /* tp_members */
549 534 dirstate_item_getset, /* tp_getset */
550 535 0, /* tp_base */
551 536 0, /* tp_dict */
552 537 0, /* tp_descr_get */
553 538 0, /* tp_descr_set */
554 539 0, /* tp_dictoffset */
555 540 0, /* tp_init */
556 541 0, /* tp_alloc */
557 542 dirstate_item_new, /* tp_new */
558 543 };
559 544
560 545 static PyObject *parse_dirstate(PyObject *self, PyObject *args)
561 546 {
562 547 PyObject *dmap, *cmap, *parents = NULL, *ret = NULL;
563 548 PyObject *fname = NULL, *cname = NULL, *entry = NULL;
564 549 char state, *cur, *str, *cpos;
565 550 int mode, size, mtime;
566 551 unsigned int flen, pos = 40;
567 552 Py_ssize_t len = 40;
568 553 Py_ssize_t readlen;
569 554
570 555 if (!PyArg_ParseTuple(
571 556 args, PY23("O!O!s#:parse_dirstate", "O!O!y#:parse_dirstate"),
572 557 &PyDict_Type, &dmap, &PyDict_Type, &cmap, &str, &readlen)) {
573 558 goto quit;
574 559 }
575 560
576 561 len = readlen;
577 562
578 563 /* read parents */
579 564 if (len < 40) {
580 565 PyErr_SetString(PyExc_ValueError,
581 566 "too little data for parents");
582 567 goto quit;
583 568 }
584 569
585 570 parents = Py_BuildValue(PY23("s#s#", "y#y#"), str, (Py_ssize_t)20,
586 571 str + 20, (Py_ssize_t)20);
587 572 if (!parents) {
588 573 goto quit;
589 574 }
590 575
591 576 /* read filenames */
592 577 while (pos >= 40 && pos < len) {
593 578 if (pos + 17 > len) {
594 579 PyErr_SetString(PyExc_ValueError,
595 580 "overflow in dirstate");
596 581 goto quit;
597 582 }
598 583 cur = str + pos;
599 584 /* unpack header */
600 585 state = *cur;
601 586 mode = getbe32(cur + 1);
602 587 size = getbe32(cur + 5);
603 588 mtime = getbe32(cur + 9);
604 589 flen = getbe32(cur + 13);
605 590 pos += 17;
606 591 cur += 17;
607 592 if (flen > len - pos) {
608 593 PyErr_SetString(PyExc_ValueError,
609 594 "overflow in dirstate");
610 595 goto quit;
611 596 }
612 597
613 598 entry = (PyObject *)dirstate_item_from_v1_data(state, mode,
614 599 size, mtime);
615 600 cpos = memchr(cur, 0, flen);
616 601 if (cpos) {
617 602 fname = PyBytes_FromStringAndSize(cur, cpos - cur);
618 603 cname = PyBytes_FromStringAndSize(
619 604 cpos + 1, flen - (cpos - cur) - 1);
620 605 if (!fname || !cname ||
621 606 PyDict_SetItem(cmap, fname, cname) == -1 ||
622 607 PyDict_SetItem(dmap, fname, entry) == -1) {
623 608 goto quit;
624 609 }
625 610 Py_DECREF(cname);
626 611 } else {
627 612 fname = PyBytes_FromStringAndSize(cur, flen);
628 613 if (!fname ||
629 614 PyDict_SetItem(dmap, fname, entry) == -1) {
630 615 goto quit;
631 616 }
632 617 }
633 618 Py_DECREF(fname);
634 619 Py_DECREF(entry);
635 620 fname = cname = entry = NULL;
636 621 pos += flen;
637 622 }
638 623
639 624 ret = parents;
640 625 Py_INCREF(ret);
641 626 quit:
642 627 Py_XDECREF(fname);
643 628 Py_XDECREF(cname);
644 629 Py_XDECREF(entry);
645 630 Py_XDECREF(parents);
646 631 return ret;
647 632 }
648 633
649 634 /*
650 635 * Build a set of non-normal and other parent entries from the dirstate dmap
651 636 */
652 637 static PyObject *nonnormalotherparententries(PyObject *self, PyObject *args)
653 638 {
654 639 PyObject *dmap, *fname, *v;
655 640 PyObject *nonnset = NULL, *otherpset = NULL, *result = NULL;
656 641 Py_ssize_t pos;
657 642
658 643 if (!PyArg_ParseTuple(args, "O!:nonnormalentries", &PyDict_Type,
659 644 &dmap)) {
660 645 goto bail;
661 646 }
662 647
663 648 nonnset = PySet_New(NULL);
664 649 if (nonnset == NULL) {
665 650 goto bail;
666 651 }
667 652
668 653 otherpset = PySet_New(NULL);
669 654 if (otherpset == NULL) {
670 655 goto bail;
671 656 }
672 657
673 658 pos = 0;
674 659 while (PyDict_Next(dmap, &pos, &fname, &v)) {
675 660 dirstateItemObject *t;
676 661 if (!dirstate_tuple_check(v)) {
677 662 PyErr_SetString(PyExc_TypeError,
678 663 "expected a dirstate tuple");
679 664 goto bail;
680 665 }
681 666 t = (dirstateItemObject *)v;
682 667
683 668 if (t->state == 'n' && t->size == -2) {
684 669 if (PySet_Add(otherpset, fname) == -1) {
685 670 goto bail;
686 671 }
687 672 }
688 673
689 674 if (t->state == 'n' && t->mtime != -1) {
690 675 continue;
691 676 }
692 677 if (PySet_Add(nonnset, fname) == -1) {
693 678 goto bail;
694 679 }
695 680 }
696 681
697 682 result = Py_BuildValue("(OO)", nonnset, otherpset);
698 683 if (result == NULL) {
699 684 goto bail;
700 685 }
701 686 Py_DECREF(nonnset);
702 687 Py_DECREF(otherpset);
703 688 return result;
704 689 bail:
705 690 Py_XDECREF(nonnset);
706 691 Py_XDECREF(otherpset);
707 692 Py_XDECREF(result);
708 693 return NULL;
709 694 }
710 695
711 696 /*
712 697 * Efficiently pack a dirstate object into its on-disk format.
713 698 */
714 699 static PyObject *pack_dirstate(PyObject *self, PyObject *args)
715 700 {
716 701 PyObject *packobj = NULL;
717 702 PyObject *map, *copymap, *pl, *mtime_unset = NULL;
718 703 Py_ssize_t nbytes, pos, l;
719 704 PyObject *k, *v = NULL, *pn;
720 705 char *p, *s;
721 706 int now;
722 707
723 708 if (!PyArg_ParseTuple(args, "O!O!O!i:pack_dirstate", &PyDict_Type, &map,
724 709 &PyDict_Type, &copymap, &PyTuple_Type, &pl,
725 710 &now)) {
726 711 return NULL;
727 712 }
728 713
729 714 if (PyTuple_Size(pl) != 2) {
730 715 PyErr_SetString(PyExc_TypeError, "expected 2-element tuple");
731 716 return NULL;
732 717 }
733 718
734 719 /* Figure out how much we need to allocate. */
735 720 for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) {
736 721 PyObject *c;
737 722 if (!PyBytes_Check(k)) {
738 723 PyErr_SetString(PyExc_TypeError, "expected string key");
739 724 goto bail;
740 725 }
741 726 nbytes += PyBytes_GET_SIZE(k) + 17;
742 727 c = PyDict_GetItem(copymap, k);
743 728 if (c) {
744 729 if (!PyBytes_Check(c)) {
745 730 PyErr_SetString(PyExc_TypeError,
746 731 "expected string key");
747 732 goto bail;
748 733 }
749 734 nbytes += PyBytes_GET_SIZE(c) + 1;
750 735 }
751 736 }
752 737
753 738 packobj = PyBytes_FromStringAndSize(NULL, nbytes);
754 739 if (packobj == NULL) {
755 740 goto bail;
756 741 }
757 742
758 743 p = PyBytes_AS_STRING(packobj);
759 744
760 745 pn = PyTuple_GET_ITEM(pl, 0);
761 746 if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
762 747 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
763 748 goto bail;
764 749 }
765 750 memcpy(p, s, l);
766 751 p += 20;
767 752 pn = PyTuple_GET_ITEM(pl, 1);
768 753 if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
769 754 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
770 755 goto bail;
771 756 }
772 757 memcpy(p, s, l);
773 758 p += 20;
774 759
775 760 for (pos = 0; PyDict_Next(map, &pos, &k, &v);) {
776 761 dirstateItemObject *tuple;
777 762 char state;
778 763 int mode, size, mtime;
779 764 Py_ssize_t len, l;
780 765 PyObject *o;
781 766 char *t;
782 767
783 768 if (!dirstate_tuple_check(v)) {
784 769 PyErr_SetString(PyExc_TypeError,
785 770 "expected a dirstate tuple");
786 771 goto bail;
787 772 }
788 773 tuple = (dirstateItemObject *)v;
789 774
790 775 state = tuple->state;
791 776 mode = tuple->mode;
792 777 size = tuple->size;
793 778 mtime = tuple->mtime;
794 779 if (state == 'n' && mtime == now) {
795 780 /* See pure/parsers.py:pack_dirstate for why we do
796 781 * this. */
797 782 mtime = -1;
798 mtime_unset = (PyObject *)make_dirstate_item(
783 mtime_unset = (PyObject *)dirstate_item_from_v1_data(
799 784 state, mode, size, mtime);
800 785 if (!mtime_unset) {
801 786 goto bail;
802 787 }
803 788 if (PyDict_SetItem(map, k, mtime_unset) == -1) {
804 789 goto bail;
805 790 }
806 791 Py_DECREF(mtime_unset);
807 792 mtime_unset = NULL;
808 793 }
809 794 *p++ = state;
810 795 putbe32((uint32_t)mode, p);
811 796 putbe32((uint32_t)size, p + 4);
812 797 putbe32((uint32_t)mtime, p + 8);
813 798 t = p + 12;
814 799 p += 16;
815 800 len = PyBytes_GET_SIZE(k);
816 801 memcpy(p, PyBytes_AS_STRING(k), len);
817 802 p += len;
818 803 o = PyDict_GetItem(copymap, k);
819 804 if (o) {
820 805 *p++ = '\0';
821 806 l = PyBytes_GET_SIZE(o);
822 807 memcpy(p, PyBytes_AS_STRING(o), l);
823 808 p += l;
824 809 len += l + 1;
825 810 }
826 811 putbe32((uint32_t)len, t);
827 812 }
828 813
829 814 pos = p - PyBytes_AS_STRING(packobj);
830 815 if (pos != nbytes) {
831 816 PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld",
832 817 (long)pos, (long)nbytes);
833 818 goto bail;
834 819 }
835 820
836 821 return packobj;
837 822 bail:
838 823 Py_XDECREF(mtime_unset);
839 824 Py_XDECREF(packobj);
840 825 Py_XDECREF(v);
841 826 return NULL;
842 827 }
843 828
844 829 #define BUMPED_FIX 1
845 830 #define USING_SHA_256 2
846 831 #define FM1_HEADER_SIZE (4 + 8 + 2 + 2 + 1 + 1 + 1)
847 832
848 833 static PyObject *readshas(const char *source, unsigned char num,
849 834 Py_ssize_t hashwidth)
850 835 {
851 836 int i;
852 837 PyObject *list = PyTuple_New(num);
853 838 if (list == NULL) {
854 839 return NULL;
855 840 }
856 841 for (i = 0; i < num; i++) {
857 842 PyObject *hash = PyBytes_FromStringAndSize(source, hashwidth);
858 843 if (hash == NULL) {
859 844 Py_DECREF(list);
860 845 return NULL;
861 846 }
862 847 PyTuple_SET_ITEM(list, i, hash);
863 848 source += hashwidth;
864 849 }
865 850 return list;
866 851 }
867 852
868 853 static PyObject *fm1readmarker(const char *databegin, const char *dataend,
869 854 uint32_t *msize)
870 855 {
871 856 const char *data = databegin;
872 857 const char *meta;
873 858
874 859 double mtime;
875 860 int16_t tz;
876 861 uint16_t flags;
877 862 unsigned char nsuccs, nparents, nmetadata;
878 863 Py_ssize_t hashwidth = 20;
879 864
880 865 PyObject *prec = NULL, *parents = NULL, *succs = NULL;
881 866 PyObject *metadata = NULL, *ret = NULL;
882 867 int i;
883 868
884 869 if (data + FM1_HEADER_SIZE > dataend) {
885 870 goto overflow;
886 871 }
887 872
888 873 *msize = getbe32(data);
889 874 data += 4;
890 875 mtime = getbefloat64(data);
891 876 data += 8;
892 877 tz = getbeint16(data);
893 878 data += 2;
894 879 flags = getbeuint16(data);
895 880 data += 2;
896 881
897 882 if (flags & USING_SHA_256) {
898 883 hashwidth = 32;
899 884 }
900 885
901 886 nsuccs = (unsigned char)(*data++);
902 887 nparents = (unsigned char)(*data++);
903 888 nmetadata = (unsigned char)(*data++);
904 889
905 890 if (databegin + *msize > dataend) {
906 891 goto overflow;
907 892 }
908 893 dataend = databegin + *msize; /* narrow down to marker size */
909 894
910 895 if (data + hashwidth > dataend) {
911 896 goto overflow;
912 897 }
913 898 prec = PyBytes_FromStringAndSize(data, hashwidth);
914 899 data += hashwidth;
915 900 if (prec == NULL) {
916 901 goto bail;
917 902 }
918 903
919 904 if (data + nsuccs * hashwidth > dataend) {
920 905 goto overflow;
921 906 }
922 907 succs = readshas(data, nsuccs, hashwidth);
923 908 if (succs == NULL) {
924 909 goto bail;
925 910 }
926 911 data += nsuccs * hashwidth;
927 912
928 913 if (nparents == 1 || nparents == 2) {
929 914 if (data + nparents * hashwidth > dataend) {
930 915 goto overflow;
931 916 }
932 917 parents = readshas(data, nparents, hashwidth);
933 918 if (parents == NULL) {
934 919 goto bail;
935 920 }
936 921 data += nparents * hashwidth;
937 922 } else {
938 923 parents = Py_None;
939 924 Py_INCREF(parents);
940 925 }
941 926
942 927 if (data + 2 * nmetadata > dataend) {
943 928 goto overflow;
944 929 }
945 930 meta = data + (2 * nmetadata);
946 931 metadata = PyTuple_New(nmetadata);
947 932 if (metadata == NULL) {
948 933 goto bail;
949 934 }
950 935 for (i = 0; i < nmetadata; i++) {
951 936 PyObject *tmp, *left = NULL, *right = NULL;
952 937 Py_ssize_t leftsize = (unsigned char)(*data++);
953 938 Py_ssize_t rightsize = (unsigned char)(*data++);
954 939 if (meta + leftsize + rightsize > dataend) {
955 940 goto overflow;
956 941 }
957 942 left = PyBytes_FromStringAndSize(meta, leftsize);
958 943 meta += leftsize;
959 944 right = PyBytes_FromStringAndSize(meta, rightsize);
960 945 meta += rightsize;
961 946 tmp = PyTuple_New(2);
962 947 if (!left || !right || !tmp) {
963 948 Py_XDECREF(left);
964 949 Py_XDECREF(right);
965 950 Py_XDECREF(tmp);
966 951 goto bail;
967 952 }
968 953 PyTuple_SET_ITEM(tmp, 0, left);
969 954 PyTuple_SET_ITEM(tmp, 1, right);
970 955 PyTuple_SET_ITEM(metadata, i, tmp);
971 956 }
972 957 ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags, metadata, mtime,
973 958 (int)tz * 60, parents);
974 959 goto bail; /* return successfully */
975 960
976 961 overflow:
977 962 PyErr_SetString(PyExc_ValueError, "overflow in obsstore");
978 963 bail:
979 964 Py_XDECREF(prec);
980 965 Py_XDECREF(succs);
981 966 Py_XDECREF(metadata);
982 967 Py_XDECREF(parents);
983 968 return ret;
984 969 }
985 970
986 971 static PyObject *fm1readmarkers(PyObject *self, PyObject *args)
987 972 {
988 973 const char *data, *dataend;
989 974 Py_ssize_t datalen, offset, stop;
990 975 PyObject *markers = NULL;
991 976
992 977 if (!PyArg_ParseTuple(args, PY23("s#nn", "y#nn"), &data, &datalen,
993 978 &offset, &stop)) {
994 979 return NULL;
995 980 }
996 981 if (offset < 0) {
997 982 PyErr_SetString(PyExc_ValueError,
998 983 "invalid negative offset in fm1readmarkers");
999 984 return NULL;
1000 985 }
1001 986 if (stop > datalen) {
1002 987 PyErr_SetString(
1003 988 PyExc_ValueError,
1004 989 "stop longer than data length in fm1readmarkers");
1005 990 return NULL;
1006 991 }
1007 992 dataend = data + datalen;
1008 993 data += offset;
1009 994 markers = PyList_New(0);
1010 995 if (!markers) {
1011 996 return NULL;
1012 997 }
1013 998 while (offset < stop) {
1014 999 uint32_t msize;
1015 1000 int error;
1016 1001 PyObject *record = fm1readmarker(data, dataend, &msize);
1017 1002 if (!record) {
1018 1003 goto bail;
1019 1004 }
1020 1005 error = PyList_Append(markers, record);
1021 1006 Py_DECREF(record);
1022 1007 if (error) {
1023 1008 goto bail;
1024 1009 }
1025 1010 data += msize;
1026 1011 offset += msize;
1027 1012 }
1028 1013 return markers;
1029 1014 bail:
1030 1015 Py_DECREF(markers);
1031 1016 return NULL;
1032 1017 }
1033 1018
1034 1019 static char parsers_doc[] = "Efficient content parsing.";
1035 1020
1036 1021 PyObject *encodedir(PyObject *self, PyObject *args);
1037 1022 PyObject *pathencode(PyObject *self, PyObject *args);
1038 1023 PyObject *lowerencode(PyObject *self, PyObject *args);
1039 1024 PyObject *parse_index2(PyObject *self, PyObject *args, PyObject *kwargs);
1040 1025
1041 1026 static PyMethodDef methods[] = {
1042 1027 {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"},
1043 1028 {"nonnormalotherparententries", nonnormalotherparententries, METH_VARARGS,
1044 1029 "create a set containing non-normal and other parent entries of given "
1045 1030 "dirstate\n"},
1046 1031 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
1047 1032 {"parse_index2", (PyCFunction)parse_index2, METH_VARARGS | METH_KEYWORDS,
1048 1033 "parse a revlog index\n"},
1049 1034 {"isasciistr", isasciistr, METH_VARARGS, "check if an ASCII string\n"},
1050 1035 {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"},
1051 1036 {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"},
1052 1037 {"dict_new_presized", dict_new_presized, METH_VARARGS,
1053 1038 "construct a dict with an expected size\n"},
1054 1039 {"make_file_foldmap", make_file_foldmap, METH_VARARGS,
1055 1040 "make file foldmap\n"},
1056 1041 {"jsonescapeu8fast", jsonescapeu8fast, METH_VARARGS,
1057 1042 "escape a UTF-8 byte string to JSON (fast path)\n"},
1058 1043 {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"},
1059 1044 {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"},
1060 1045 {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"},
1061 1046 {"fm1readmarkers", fm1readmarkers, METH_VARARGS,
1062 1047 "parse v1 obsolete markers\n"},
1063 1048 {NULL, NULL}};
1064 1049
1065 1050 void dirs_module_init(PyObject *mod);
1066 1051 void manifest_module_init(PyObject *mod);
1067 1052 void revlog_module_init(PyObject *mod);
1068 1053
1069 1054 static const int version = 20;
1070 1055
1071 1056 static void module_init(PyObject *mod)
1072 1057 {
1073 1058 PyObject *capsule = NULL;
1074 1059 PyModule_AddIntConstant(mod, "version", version);
1075 1060
1076 1061 /* This module constant has two purposes. First, it lets us unit test
1077 1062 * the ImportError raised without hard-coding any error text. This
1078 1063 * means we can change the text in the future without breaking tests,
1079 1064 * even across changesets without a recompile. Second, its presence
1080 1065 * can be used to determine whether the version-checking logic is
1081 1066 * present, which also helps in testing across changesets without a
1082 1067 * recompile. Note that this means the pure-Python version of parsers
1083 1068 * should not have this module constant. */
1084 1069 PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext);
1085 1070
1086 1071 dirs_module_init(mod);
1087 1072 manifest_module_init(mod);
1088 1073 revlog_module_init(mod);
1089 1074
1090 1075 capsule = PyCapsule_New(
1091 make_dirstate_item,
1076 dirstate_item_from_v1_data,
1092 1077 "mercurial.cext.parsers.make_dirstate_item_CAPI", NULL);
1093 1078 if (capsule != NULL)
1094 1079 PyModule_AddObject(mod, "make_dirstate_item_CAPI", capsule);
1095 1080
1096 1081 if (PyType_Ready(&dirstateItemType) < 0) {
1097 1082 return;
1098 1083 }
1099 1084 Py_INCREF(&dirstateItemType);
1100 1085 PyModule_AddObject(mod, "DirstateItem", (PyObject *)&dirstateItemType);
1101 1086 }
1102 1087
1103 1088 static int check_python_version(void)
1104 1089 {
1105 1090 PyObject *sys = PyImport_ImportModule("sys"), *ver;
1106 1091 long hexversion;
1107 1092 if (!sys) {
1108 1093 return -1;
1109 1094 }
1110 1095 ver = PyObject_GetAttrString(sys, "hexversion");
1111 1096 Py_DECREF(sys);
1112 1097 if (!ver) {
1113 1098 return -1;
1114 1099 }
1115 1100 hexversion = PyInt_AsLong(ver);
1116 1101 Py_DECREF(ver);
1117 1102 /* sys.hexversion is a 32-bit number by default, so the -1 case
1118 1103 * should only occur in unusual circumstances (e.g. if sys.hexversion
1119 1104 * is manually set to an invalid value). */
1120 1105 if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) {
1121 1106 PyErr_Format(PyExc_ImportError,
1122 1107 "%s: The Mercurial extension "
1123 1108 "modules were compiled with Python " PY_VERSION
1124 1109 ", but "
1125 1110 "Mercurial is currently using Python with "
1126 1111 "sys.hexversion=%ld: "
1127 1112 "Python %s\n at: %s",
1128 1113 versionerrortext, hexversion, Py_GetVersion(),
1129 1114 Py_GetProgramFullPath());
1130 1115 return -1;
1131 1116 }
1132 1117 return 0;
1133 1118 }
1134 1119
1135 1120 #ifdef IS_PY3K
1136 1121 static struct PyModuleDef parsers_module = {PyModuleDef_HEAD_INIT, "parsers",
1137 1122 parsers_doc, -1, methods};
1138 1123
1139 1124 PyMODINIT_FUNC PyInit_parsers(void)
1140 1125 {
1141 1126 PyObject *mod;
1142 1127
1143 1128 if (check_python_version() == -1)
1144 1129 return NULL;
1145 1130 mod = PyModule_Create(&parsers_module);
1146 1131 module_init(mod);
1147 1132 return mod;
1148 1133 }
1149 1134 #else
1150 1135 PyMODINIT_FUNC initparsers(void)
1151 1136 {
1152 1137 PyObject *mod;
1153 1138
1154 1139 if (check_python_version() == -1) {
1155 1140 return;
1156 1141 }
1157 1142 mod = Py_InitModule3("parsers", methods, parsers_doc);
1158 1143 module_init(mod);
1159 1144 }
1160 1145 #endif
General Comments 0
You need to be logged in to leave comments. Login now