Show More
@@ -1,794 +1,794 | |||
|
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 "bitmanipulation.h" |
|
16 | 16 | #include "charencode.h" |
|
17 | 17 | #include "util.h" |
|
18 | 18 | |
|
19 | 19 | #ifdef IS_PY3K |
|
20 | 20 | /* The mapping of Python types is meant to be temporary to get Python |
|
21 | 21 | * 3 to compile. We should remove this once Python 3 support is fully |
|
22 | 22 | * supported and proper types are used in the extensions themselves. */ |
|
23 | 23 | #define PyInt_Check PyLong_Check |
|
24 | 24 | #define PyInt_FromLong PyLong_FromLong |
|
25 | 25 | #define PyInt_FromSsize_t PyLong_FromSsize_t |
|
26 | 26 | #define PyInt_AsLong PyLong_AsLong |
|
27 | 27 | #endif |
|
28 | 28 | |
|
29 | 29 | static const char *const versionerrortext = "Python minor version mismatch"; |
|
30 | 30 | |
|
31 | 31 | static PyObject *dict_new_presized(PyObject *self, PyObject *args) |
|
32 | 32 | { |
|
33 | 33 | Py_ssize_t expected_size; |
|
34 | 34 | |
|
35 | 35 | if (!PyArg_ParseTuple(args, "n:make_presized_dict", &expected_size)) |
|
36 | 36 | return NULL; |
|
37 | 37 | |
|
38 | 38 | return _dict_new_presized(expected_size); |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | /* |
|
42 | 42 | * This code assumes that a manifest is stitched together with newline |
|
43 | 43 | * ('\n') characters. |
|
44 | 44 | */ |
|
45 | 45 | static PyObject *parse_manifest(PyObject *self, PyObject *args) |
|
46 | 46 | { |
|
47 | 47 | PyObject *mfdict, *fdict; |
|
48 | 48 | char *str, *start, *end; |
|
49 | 49 | int len; |
|
50 | 50 | |
|
51 | 51 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest", &PyDict_Type, |
|
52 | 52 | &mfdict, &PyDict_Type, &fdict, &str, &len)) |
|
53 | 53 | goto quit; |
|
54 | 54 | |
|
55 | 55 | start = str; |
|
56 | 56 | end = str + len; |
|
57 | 57 | while (start < end) { |
|
58 | 58 | PyObject *file = NULL, *node = NULL; |
|
59 | 59 | PyObject *flags = NULL; |
|
60 | 60 | char *zero = NULL, *newline = NULL; |
|
61 | 61 | ptrdiff_t nlen; |
|
62 | 62 | |
|
63 | 63 | zero = memchr(start, '\0', end - start); |
|
64 | 64 | if (!zero) { |
|
65 | 65 | PyErr_SetString(PyExc_ValueError, |
|
66 | 66 | "manifest entry has no separator"); |
|
67 | 67 | goto quit; |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | newline = memchr(zero + 1, '\n', end - (zero + 1)); |
|
71 | 71 | if (!newline) { |
|
72 | 72 | PyErr_SetString(PyExc_ValueError, |
|
73 | 73 | "manifest contains trailing garbage"); |
|
74 | 74 | goto quit; |
|
75 | 75 | } |
|
76 | 76 | |
|
77 | 77 | file = PyBytes_FromStringAndSize(start, zero - start); |
|
78 | 78 | |
|
79 | 79 | if (!file) |
|
80 | 80 | goto bail; |
|
81 | 81 | |
|
82 | 82 | nlen = newline - zero - 1; |
|
83 | 83 | |
|
84 | 84 | node = unhexlify(zero + 1, nlen > 40 ? 40 : (Py_ssize_t)nlen); |
|
85 | 85 | if (!node) |
|
86 | 86 | goto bail; |
|
87 | 87 | |
|
88 | 88 | if (nlen > 40) { |
|
89 | 89 | flags = PyBytes_FromStringAndSize(zero + 41, nlen - 40); |
|
90 | 90 | if (!flags) |
|
91 | 91 | goto bail; |
|
92 | 92 | |
|
93 | 93 | if (PyDict_SetItem(fdict, file, flags) == -1) |
|
94 | 94 | goto bail; |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | 97 | if (PyDict_SetItem(mfdict, file, node) == -1) |
|
98 | 98 | goto bail; |
|
99 | 99 | |
|
100 | 100 | start = newline + 1; |
|
101 | 101 | |
|
102 | 102 | Py_XDECREF(flags); |
|
103 | 103 | Py_XDECREF(node); |
|
104 | 104 | Py_XDECREF(file); |
|
105 | 105 | continue; |
|
106 | 106 | bail: |
|
107 | 107 | Py_XDECREF(flags); |
|
108 | 108 | Py_XDECREF(node); |
|
109 | 109 | Py_XDECREF(file); |
|
110 | 110 | goto quit; |
|
111 | 111 | } |
|
112 | 112 | |
|
113 | 113 | Py_INCREF(Py_None); |
|
114 | 114 | return Py_None; |
|
115 | 115 | quit: |
|
116 | 116 | return NULL; |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | static inline dirstateTupleObject *make_dirstate_tuple(char state, int mode, |
|
120 | 120 | int size, int mtime) |
|
121 | 121 | { |
|
122 | 122 | dirstateTupleObject *t = |
|
123 | 123 | PyObject_New(dirstateTupleObject, &dirstateTupleType); |
|
124 | 124 | if (!t) |
|
125 | 125 | return NULL; |
|
126 | 126 | t->state = state; |
|
127 | 127 | t->mode = mode; |
|
128 | 128 | t->size = size; |
|
129 | 129 | t->mtime = mtime; |
|
130 | 130 | return t; |
|
131 | 131 | } |
|
132 | 132 | |
|
133 | 133 | static PyObject *dirstate_tuple_new(PyTypeObject *subtype, PyObject *args, |
|
134 | 134 | PyObject *kwds) |
|
135 | 135 | { |
|
136 | 136 | /* We do all the initialization here and not a tp_init function because |
|
137 | 137 | * dirstate_tuple is immutable. */ |
|
138 | 138 | dirstateTupleObject *t; |
|
139 | 139 | char state; |
|
140 | 140 | int size, mode, mtime; |
|
141 | 141 | if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime)) |
|
142 | 142 | return NULL; |
|
143 | 143 | |
|
144 | 144 | t = (dirstateTupleObject *)subtype->tp_alloc(subtype, 1); |
|
145 | 145 | if (!t) |
|
146 | 146 | return NULL; |
|
147 | 147 | t->state = state; |
|
148 | 148 | t->mode = mode; |
|
149 | 149 | t->size = size; |
|
150 | 150 | t->mtime = mtime; |
|
151 | 151 | |
|
152 | 152 | return (PyObject *)t; |
|
153 | 153 | } |
|
154 | 154 | |
|
155 | 155 | static void dirstate_tuple_dealloc(PyObject *o) |
|
156 | 156 | { |
|
157 | 157 | PyObject_Del(o); |
|
158 | 158 | } |
|
159 | 159 | |
|
160 | 160 | static Py_ssize_t dirstate_tuple_length(PyObject *o) |
|
161 | 161 | { |
|
162 | 162 | return 4; |
|
163 | 163 | } |
|
164 | 164 | |
|
165 | 165 | static PyObject *dirstate_tuple_item(PyObject *o, Py_ssize_t i) |
|
166 | 166 | { |
|
167 | 167 | dirstateTupleObject *t = (dirstateTupleObject *)o; |
|
168 | 168 | switch (i) { |
|
169 | 169 | case 0: |
|
170 | 170 | return PyBytes_FromStringAndSize(&t->state, 1); |
|
171 | 171 | case 1: |
|
172 | 172 | return PyInt_FromLong(t->mode); |
|
173 | 173 | case 2: |
|
174 | 174 | return PyInt_FromLong(t->size); |
|
175 | 175 | case 3: |
|
176 | 176 | return PyInt_FromLong(t->mtime); |
|
177 | 177 | default: |
|
178 | 178 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
|
179 | 179 | return NULL; |
|
180 | 180 | } |
|
181 | 181 | } |
|
182 | 182 | |
|
183 | 183 | static PySequenceMethods dirstate_tuple_sq = { |
|
184 | 184 | dirstate_tuple_length, /* sq_length */ |
|
185 | 185 | 0, /* sq_concat */ |
|
186 | 186 | 0, /* sq_repeat */ |
|
187 | 187 | dirstate_tuple_item, /* sq_item */ |
|
188 | 188 | 0, /* sq_ass_item */ |
|
189 | 189 | 0, /* sq_contains */ |
|
190 | 190 | 0, /* sq_inplace_concat */ |
|
191 | 191 | 0 /* sq_inplace_repeat */ |
|
192 | 192 | }; |
|
193 | 193 | |
|
194 | 194 | PyTypeObject dirstateTupleType = { |
|
195 | 195 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ |
|
196 | 196 | "dirstate_tuple", /* tp_name */ |
|
197 | 197 | sizeof(dirstateTupleObject), /* tp_basicsize */ |
|
198 | 198 | 0, /* tp_itemsize */ |
|
199 | 199 | (destructor)dirstate_tuple_dealloc, /* tp_dealloc */ |
|
200 | 200 | 0, /* tp_print */ |
|
201 | 201 | 0, /* tp_getattr */ |
|
202 | 202 | 0, /* tp_setattr */ |
|
203 | 203 | 0, /* tp_compare */ |
|
204 | 204 | 0, /* tp_repr */ |
|
205 | 205 | 0, /* tp_as_number */ |
|
206 | 206 | &dirstate_tuple_sq, /* tp_as_sequence */ |
|
207 | 207 | 0, /* tp_as_mapping */ |
|
208 | 208 | 0, /* tp_hash */ |
|
209 | 209 | 0, /* tp_call */ |
|
210 | 210 | 0, /* tp_str */ |
|
211 | 211 | 0, /* tp_getattro */ |
|
212 | 212 | 0, /* tp_setattro */ |
|
213 | 213 | 0, /* tp_as_buffer */ |
|
214 | 214 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
215 | 215 | "dirstate tuple", /* tp_doc */ |
|
216 | 216 | 0, /* tp_traverse */ |
|
217 | 217 | 0, /* tp_clear */ |
|
218 | 218 | 0, /* tp_richcompare */ |
|
219 | 219 | 0, /* tp_weaklistoffset */ |
|
220 | 220 | 0, /* tp_iter */ |
|
221 | 221 | 0, /* tp_iternext */ |
|
222 | 222 | 0, /* tp_methods */ |
|
223 | 223 | 0, /* tp_members */ |
|
224 | 224 | 0, /* tp_getset */ |
|
225 | 225 | 0, /* tp_base */ |
|
226 | 226 | 0, /* tp_dict */ |
|
227 | 227 | 0, /* tp_descr_get */ |
|
228 | 228 | 0, /* tp_descr_set */ |
|
229 | 229 | 0, /* tp_dictoffset */ |
|
230 | 230 | 0, /* tp_init */ |
|
231 | 231 | 0, /* tp_alloc */ |
|
232 | 232 | dirstate_tuple_new, /* tp_new */ |
|
233 | 233 | }; |
|
234 | 234 | |
|
235 | 235 | static PyObject *parse_dirstate(PyObject *self, PyObject *args) |
|
236 | 236 | { |
|
237 | 237 | PyObject *dmap, *cmap, *parents = NULL, *ret = NULL; |
|
238 | 238 | PyObject *fname = NULL, *cname = NULL, *entry = NULL; |
|
239 | 239 | char state, *cur, *str, *cpos; |
|
240 | 240 | int mode, size, mtime; |
|
241 | 241 | unsigned int flen, len, pos = 40; |
|
242 | 242 | int readlen; |
|
243 | 243 | |
|
244 | 244 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate", &PyDict_Type, |
|
245 | 245 | &dmap, &PyDict_Type, &cmap, &str, &readlen)) |
|
246 | 246 | goto quit; |
|
247 | 247 | |
|
248 | 248 | len = readlen; |
|
249 | 249 | |
|
250 | 250 | /* read parents */ |
|
251 | 251 | if (len < 40) { |
|
252 | 252 | PyErr_SetString(PyExc_ValueError, |
|
253 | 253 | "too little data for parents"); |
|
254 | 254 | goto quit; |
|
255 | 255 | } |
|
256 | 256 | |
|
257 | 257 | parents = Py_BuildValue("s#s#", str, 20, str + 20, 20); |
|
258 | 258 | if (!parents) |
|
259 | 259 | goto quit; |
|
260 | 260 | |
|
261 | 261 | /* read filenames */ |
|
262 | 262 | while (pos >= 40 && pos < len) { |
|
263 | 263 | if (pos + 17 > len) { |
|
264 | 264 | PyErr_SetString(PyExc_ValueError, |
|
265 | 265 | "overflow in dirstate"); |
|
266 | 266 | goto quit; |
|
267 | 267 | } |
|
268 | 268 | cur = str + pos; |
|
269 | 269 | /* unpack header */ |
|
270 | 270 | state = *cur; |
|
271 | 271 | mode = getbe32(cur + 1); |
|
272 | 272 | size = getbe32(cur + 5); |
|
273 | 273 | mtime = getbe32(cur + 9); |
|
274 | 274 | flen = getbe32(cur + 13); |
|
275 | 275 | pos += 17; |
|
276 | 276 | cur += 17; |
|
277 | 277 | if (flen > len - pos) { |
|
278 | 278 | PyErr_SetString(PyExc_ValueError, |
|
279 | 279 | "overflow in dirstate"); |
|
280 | 280 | goto quit; |
|
281 | 281 | } |
|
282 | 282 | |
|
283 | 283 | entry = |
|
284 | 284 | (PyObject *)make_dirstate_tuple(state, mode, size, mtime); |
|
285 | 285 | cpos = memchr(cur, 0, flen); |
|
286 | 286 | if (cpos) { |
|
287 | 287 | fname = PyBytes_FromStringAndSize(cur, cpos - cur); |
|
288 | 288 | cname = PyBytes_FromStringAndSize( |
|
289 | 289 | cpos + 1, flen - (cpos - cur) - 1); |
|
290 | 290 | if (!fname || !cname || |
|
291 | 291 | PyDict_SetItem(cmap, fname, cname) == -1 || |
|
292 | 292 | PyDict_SetItem(dmap, fname, entry) == -1) |
|
293 | 293 | goto quit; |
|
294 | 294 | Py_DECREF(cname); |
|
295 | 295 | } else { |
|
296 | 296 | fname = PyBytes_FromStringAndSize(cur, flen); |
|
297 | 297 | if (!fname || PyDict_SetItem(dmap, fname, entry) == -1) |
|
298 | 298 | goto quit; |
|
299 | 299 | } |
|
300 | 300 | Py_DECREF(fname); |
|
301 | 301 | Py_DECREF(entry); |
|
302 | 302 | fname = cname = entry = NULL; |
|
303 | 303 | pos += flen; |
|
304 | 304 | } |
|
305 | 305 | |
|
306 | 306 | ret = parents; |
|
307 | 307 | Py_INCREF(ret); |
|
308 | 308 | quit: |
|
309 | 309 | Py_XDECREF(fname); |
|
310 | 310 | Py_XDECREF(cname); |
|
311 | 311 | Py_XDECREF(entry); |
|
312 | 312 | Py_XDECREF(parents); |
|
313 | 313 | return ret; |
|
314 | 314 | } |
|
315 | 315 | |
|
316 | 316 | /* |
|
317 | 317 | * Build a set of non-normal and other parent entries from the dirstate dmap |
|
318 | 318 | */ |
|
319 | 319 | static PyObject *nonnormalotherparententries(PyObject *self, PyObject *args) |
|
320 | 320 | { |
|
321 | 321 | PyObject *dmap, *fname, *v; |
|
322 | 322 | PyObject *nonnset = NULL, *otherpset = NULL, *result = NULL; |
|
323 | 323 | Py_ssize_t pos; |
|
324 | 324 | |
|
325 | 325 | if (!PyArg_ParseTuple(args, "O!:nonnormalentries", &PyDict_Type, &dmap)) |
|
326 | 326 | goto bail; |
|
327 | 327 | |
|
328 | 328 | nonnset = PySet_New(NULL); |
|
329 | 329 | if (nonnset == NULL) |
|
330 | 330 | goto bail; |
|
331 | 331 | |
|
332 | 332 | otherpset = PySet_New(NULL); |
|
333 | 333 | if (otherpset == NULL) |
|
334 | 334 | goto bail; |
|
335 | 335 | |
|
336 | 336 | pos = 0; |
|
337 | 337 | while (PyDict_Next(dmap, &pos, &fname, &v)) { |
|
338 | 338 | dirstateTupleObject *t; |
|
339 | 339 | if (!dirstate_tuple_check(v)) { |
|
340 | 340 | PyErr_SetString(PyExc_TypeError, |
|
341 | 341 | "expected a dirstate tuple"); |
|
342 | 342 | goto bail; |
|
343 | 343 | } |
|
344 | 344 | t = (dirstateTupleObject *)v; |
|
345 | 345 | |
|
346 | 346 | if (t->state == 'n' && t->size == -2) { |
|
347 | 347 | if (PySet_Add(otherpset, fname) == -1) { |
|
348 | 348 | goto bail; |
|
349 | 349 | } |
|
350 | 350 | } |
|
351 | 351 | |
|
352 | 352 | if (t->state == 'n' && t->mtime != -1) |
|
353 | 353 | continue; |
|
354 | 354 | if (PySet_Add(nonnset, fname) == -1) |
|
355 | 355 | goto bail; |
|
356 | 356 | } |
|
357 | 357 | |
|
358 | 358 | result = Py_BuildValue("(OO)", nonnset, otherpset); |
|
359 | 359 | if (result == NULL) |
|
360 | 360 | goto bail; |
|
361 | 361 | Py_DECREF(nonnset); |
|
362 | 362 | Py_DECREF(otherpset); |
|
363 | 363 | return result; |
|
364 | 364 | bail: |
|
365 | 365 | Py_XDECREF(nonnset); |
|
366 | 366 | Py_XDECREF(otherpset); |
|
367 | 367 | Py_XDECREF(result); |
|
368 | 368 | return NULL; |
|
369 | 369 | } |
|
370 | 370 | |
|
371 | 371 | /* |
|
372 | 372 | * Efficiently pack a dirstate object into its on-disk format. |
|
373 | 373 | */ |
|
374 | 374 | static PyObject *pack_dirstate(PyObject *self, PyObject *args) |
|
375 | 375 | { |
|
376 | 376 | PyObject *packobj = NULL; |
|
377 | 377 | PyObject *map, *copymap, *pl, *mtime_unset = NULL; |
|
378 | 378 | Py_ssize_t nbytes, pos, l; |
|
379 | 379 | PyObject *k, *v = NULL, *pn; |
|
380 | 380 | char *p, *s; |
|
381 | 381 | int now; |
|
382 | 382 | |
|
383 | 383 | if (!PyArg_ParseTuple(args, "O!O!Oi:pack_dirstate", &PyDict_Type, &map, |
|
384 | 384 | &PyDict_Type, ©map, &pl, &now)) |
|
385 | 385 | return NULL; |
|
386 | 386 | |
|
387 | 387 | if (!PySequence_Check(pl) || PySequence_Size(pl) != 2) { |
|
388 | 388 | PyErr_SetString(PyExc_TypeError, "expected 2-element sequence"); |
|
389 | 389 | return NULL; |
|
390 | 390 | } |
|
391 | 391 | |
|
392 | 392 | /* Figure out how much we need to allocate. */ |
|
393 | 393 | for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) { |
|
394 | 394 | PyObject *c; |
|
395 | 395 | if (!PyBytes_Check(k)) { |
|
396 | 396 | PyErr_SetString(PyExc_TypeError, "expected string key"); |
|
397 | 397 | goto bail; |
|
398 | 398 | } |
|
399 | 399 | nbytes += PyBytes_GET_SIZE(k) + 17; |
|
400 | 400 | c = PyDict_GetItem(copymap, k); |
|
401 | 401 | if (c) { |
|
402 | 402 | if (!PyBytes_Check(c)) { |
|
403 | 403 | PyErr_SetString(PyExc_TypeError, |
|
404 | 404 | "expected string key"); |
|
405 | 405 | goto bail; |
|
406 | 406 | } |
|
407 | 407 | nbytes += PyBytes_GET_SIZE(c) + 1; |
|
408 | 408 | } |
|
409 | 409 | } |
|
410 | 410 | |
|
411 | 411 | packobj = PyBytes_FromStringAndSize(NULL, nbytes); |
|
412 | 412 | if (packobj == NULL) |
|
413 | 413 | goto bail; |
|
414 | 414 | |
|
415 | 415 | p = PyBytes_AS_STRING(packobj); |
|
416 | 416 | |
|
417 | 417 | pn = PySequence_ITEM(pl, 0); |
|
418 | 418 | if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { |
|
419 | 419 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); |
|
420 | 420 | goto bail; |
|
421 | 421 | } |
|
422 | 422 | memcpy(p, s, l); |
|
423 | 423 | p += 20; |
|
424 | 424 | pn = PySequence_ITEM(pl, 1); |
|
425 | 425 | if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { |
|
426 | 426 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); |
|
427 | 427 | goto bail; |
|
428 | 428 | } |
|
429 | 429 | memcpy(p, s, l); |
|
430 | 430 | p += 20; |
|
431 | 431 | |
|
432 | 432 | for (pos = 0; PyDict_Next(map, &pos, &k, &v);) { |
|
433 | 433 | dirstateTupleObject *tuple; |
|
434 | 434 | char state; |
|
435 | 435 | int mode, size, mtime; |
|
436 | 436 | Py_ssize_t len, l; |
|
437 | 437 | PyObject *o; |
|
438 | 438 | char *t; |
|
439 | 439 | |
|
440 | 440 | if (!dirstate_tuple_check(v)) { |
|
441 | 441 | PyErr_SetString(PyExc_TypeError, |
|
442 | 442 | "expected a dirstate tuple"); |
|
443 | 443 | goto bail; |
|
444 | 444 | } |
|
445 | 445 | tuple = (dirstateTupleObject *)v; |
|
446 | 446 | |
|
447 | 447 | state = tuple->state; |
|
448 | 448 | mode = tuple->mode; |
|
449 | 449 | size = tuple->size; |
|
450 | 450 | mtime = tuple->mtime; |
|
451 | 451 | if (state == 'n' && mtime == now) { |
|
452 | 452 | /* See pure/parsers.py:pack_dirstate for why we do |
|
453 | 453 | * this. */ |
|
454 | 454 | mtime = -1; |
|
455 | 455 | mtime_unset = (PyObject *)make_dirstate_tuple( |
|
456 | 456 | state, mode, size, mtime); |
|
457 | 457 | if (!mtime_unset) |
|
458 | 458 | goto bail; |
|
459 | 459 | if (PyDict_SetItem(map, k, mtime_unset) == -1) |
|
460 | 460 | goto bail; |
|
461 | 461 | Py_DECREF(mtime_unset); |
|
462 | 462 | mtime_unset = NULL; |
|
463 | 463 | } |
|
464 | 464 | *p++ = state; |
|
465 | 465 | putbe32((uint32_t)mode, p); |
|
466 | 466 | putbe32((uint32_t)size, p + 4); |
|
467 | 467 | putbe32((uint32_t)mtime, p + 8); |
|
468 | 468 | t = p + 12; |
|
469 | 469 | p += 16; |
|
470 | 470 | len = PyBytes_GET_SIZE(k); |
|
471 | 471 | memcpy(p, PyBytes_AS_STRING(k), len); |
|
472 | 472 | p += len; |
|
473 | 473 | o = PyDict_GetItem(copymap, k); |
|
474 | 474 | if (o) { |
|
475 | 475 | *p++ = '\0'; |
|
476 | 476 | l = PyBytes_GET_SIZE(o); |
|
477 | 477 | memcpy(p, PyBytes_AS_STRING(o), l); |
|
478 | 478 | p += l; |
|
479 | 479 | len += l + 1; |
|
480 | 480 | } |
|
481 | 481 | putbe32((uint32_t)len, t); |
|
482 | 482 | } |
|
483 | 483 | |
|
484 | 484 | pos = p - PyBytes_AS_STRING(packobj); |
|
485 | 485 | if (pos != nbytes) { |
|
486 | 486 | PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld", |
|
487 | 487 | (long)pos, (long)nbytes); |
|
488 | 488 | goto bail; |
|
489 | 489 | } |
|
490 | 490 | |
|
491 | 491 | return packobj; |
|
492 | 492 | bail: |
|
493 | 493 | Py_XDECREF(mtime_unset); |
|
494 | 494 | Py_XDECREF(packobj); |
|
495 | 495 | Py_XDECREF(v); |
|
496 | 496 | return NULL; |
|
497 | 497 | } |
|
498 | 498 | |
|
499 | 499 | #define BUMPED_FIX 1 |
|
500 | 500 | #define USING_SHA_256 2 |
|
501 | 501 | #define FM1_HEADER_SIZE (4 + 8 + 2 + 2 + 1 + 1 + 1) |
|
502 | 502 | |
|
503 | 503 | static PyObject *readshas(const char *source, unsigned char num, |
|
504 | 504 | Py_ssize_t hashwidth) |
|
505 | 505 | { |
|
506 | 506 | int i; |
|
507 | 507 | PyObject *list = PyTuple_New(num); |
|
508 | 508 | if (list == NULL) { |
|
509 | 509 | return NULL; |
|
510 | 510 | } |
|
511 | 511 | for (i = 0; i < num; i++) { |
|
512 | 512 | PyObject *hash = PyBytes_FromStringAndSize(source, hashwidth); |
|
513 | 513 | if (hash == NULL) { |
|
514 | 514 | Py_DECREF(list); |
|
515 | 515 | return NULL; |
|
516 | 516 | } |
|
517 | 517 | PyTuple_SET_ITEM(list, i, hash); |
|
518 | 518 | source += hashwidth; |
|
519 | 519 | } |
|
520 | 520 | return list; |
|
521 | 521 | } |
|
522 | 522 | |
|
523 | 523 | static PyObject *fm1readmarker(const char *databegin, const char *dataend, |
|
524 | 524 | uint32_t *msize) |
|
525 | 525 | { |
|
526 | 526 | const char *data = databegin; |
|
527 | 527 | const char *meta; |
|
528 | 528 | |
|
529 | 529 | double mtime; |
|
530 | 530 | int16_t tz; |
|
531 | 531 | uint16_t flags; |
|
532 | 532 | unsigned char nsuccs, nparents, nmetadata; |
|
533 | 533 | Py_ssize_t hashwidth = 20; |
|
534 | 534 | |
|
535 | 535 | PyObject *prec = NULL, *parents = NULL, *succs = NULL; |
|
536 | 536 | PyObject *metadata = NULL, *ret = NULL; |
|
537 | 537 | int i; |
|
538 | 538 | |
|
539 | 539 | if (data + FM1_HEADER_SIZE > dataend) { |
|
540 | 540 | goto overflow; |
|
541 | 541 | } |
|
542 | 542 | |
|
543 | 543 | *msize = getbe32(data); |
|
544 | 544 | data += 4; |
|
545 | 545 | mtime = getbefloat64(data); |
|
546 | 546 | data += 8; |
|
547 | 547 | tz = getbeint16(data); |
|
548 | 548 | data += 2; |
|
549 | 549 | flags = getbeuint16(data); |
|
550 | 550 | data += 2; |
|
551 | 551 | |
|
552 | 552 | if (flags & USING_SHA_256) { |
|
553 | 553 | hashwidth = 32; |
|
554 | 554 | } |
|
555 | 555 | |
|
556 | 556 | nsuccs = (unsigned char)(*data++); |
|
557 | 557 | nparents = (unsigned char)(*data++); |
|
558 | 558 | nmetadata = (unsigned char)(*data++); |
|
559 | 559 | |
|
560 | 560 | if (databegin + *msize > dataend) { |
|
561 | 561 | goto overflow; |
|
562 | 562 | } |
|
563 | 563 | dataend = databegin + *msize; /* narrow down to marker size */ |
|
564 | 564 | |
|
565 | 565 | if (data + hashwidth > dataend) { |
|
566 | 566 | goto overflow; |
|
567 | 567 | } |
|
568 | 568 | prec = PyBytes_FromStringAndSize(data, hashwidth); |
|
569 | 569 | data += hashwidth; |
|
570 | 570 | if (prec == NULL) { |
|
571 | 571 | goto bail; |
|
572 | 572 | } |
|
573 | 573 | |
|
574 | 574 | if (data + nsuccs * hashwidth > dataend) { |
|
575 | 575 | goto overflow; |
|
576 | 576 | } |
|
577 | 577 | succs = readshas(data, nsuccs, hashwidth); |
|
578 | 578 | if (succs == NULL) { |
|
579 | 579 | goto bail; |
|
580 | 580 | } |
|
581 | 581 | data += nsuccs * hashwidth; |
|
582 | 582 | |
|
583 | 583 | if (nparents == 1 || nparents == 2) { |
|
584 | 584 | if (data + nparents * hashwidth > dataend) { |
|
585 | 585 | goto overflow; |
|
586 | 586 | } |
|
587 | 587 | parents = readshas(data, nparents, hashwidth); |
|
588 | 588 | if (parents == NULL) { |
|
589 | 589 | goto bail; |
|
590 | 590 | } |
|
591 | 591 | data += nparents * hashwidth; |
|
592 | 592 | } else { |
|
593 | 593 | parents = Py_None; |
|
594 | 594 | Py_INCREF(parents); |
|
595 | 595 | } |
|
596 | 596 | |
|
597 | 597 | if (data + 2 * nmetadata > dataend) { |
|
598 | 598 | goto overflow; |
|
599 | 599 | } |
|
600 | 600 | meta = data + (2 * nmetadata); |
|
601 | 601 | metadata = PyTuple_New(nmetadata); |
|
602 | 602 | if (metadata == NULL) { |
|
603 | 603 | goto bail; |
|
604 | 604 | } |
|
605 | 605 | for (i = 0; i < nmetadata; i++) { |
|
606 | 606 | PyObject *tmp, *left = NULL, *right = NULL; |
|
607 | 607 | Py_ssize_t leftsize = (unsigned char)(*data++); |
|
608 | 608 | Py_ssize_t rightsize = (unsigned char)(*data++); |
|
609 | 609 | if (meta + leftsize + rightsize > dataend) { |
|
610 | 610 | goto overflow; |
|
611 | 611 | } |
|
612 | 612 | left = PyBytes_FromStringAndSize(meta, leftsize); |
|
613 | 613 | meta += leftsize; |
|
614 | 614 | right = PyBytes_FromStringAndSize(meta, rightsize); |
|
615 | 615 | meta += rightsize; |
|
616 | 616 | tmp = PyTuple_New(2); |
|
617 | 617 | if (!left || !right || !tmp) { |
|
618 | 618 | Py_XDECREF(left); |
|
619 | 619 | Py_XDECREF(right); |
|
620 | 620 | Py_XDECREF(tmp); |
|
621 | 621 | goto bail; |
|
622 | 622 | } |
|
623 | 623 | PyTuple_SET_ITEM(tmp, 0, left); |
|
624 | 624 | PyTuple_SET_ITEM(tmp, 1, right); |
|
625 | 625 | PyTuple_SET_ITEM(metadata, i, tmp); |
|
626 | 626 | } |
|
627 | 627 | ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags, metadata, mtime, |
|
628 | 628 | (int)tz * 60, parents); |
|
629 | 629 | goto bail; /* return successfully */ |
|
630 | 630 | |
|
631 | 631 | overflow: |
|
632 | 632 | PyErr_SetString(PyExc_ValueError, "overflow in obsstore"); |
|
633 | 633 | bail: |
|
634 | 634 | Py_XDECREF(prec); |
|
635 | 635 | Py_XDECREF(succs); |
|
636 | 636 | Py_XDECREF(metadata); |
|
637 | 637 | Py_XDECREF(parents); |
|
638 | 638 | return ret; |
|
639 | 639 | } |
|
640 | 640 | |
|
641 | 641 | static PyObject *fm1readmarkers(PyObject *self, PyObject *args) |
|
642 | 642 | { |
|
643 | 643 | const char *data, *dataend; |
|
644 | 644 | int datalen; |
|
645 | 645 | Py_ssize_t offset, stop; |
|
646 | 646 | PyObject *markers = NULL; |
|
647 | 647 | |
|
648 | 648 | if (!PyArg_ParseTuple(args, "s#nn", &data, &datalen, &offset, &stop)) { |
|
649 | 649 | return NULL; |
|
650 | 650 | } |
|
651 | 651 | dataend = data + datalen; |
|
652 | 652 | data += offset; |
|
653 | 653 | markers = PyList_New(0); |
|
654 | 654 | if (!markers) { |
|
655 | 655 | return NULL; |
|
656 | 656 | } |
|
657 | 657 | while (offset < stop) { |
|
658 | 658 | uint32_t msize; |
|
659 | 659 | int error; |
|
660 | 660 | PyObject *record = fm1readmarker(data, dataend, &msize); |
|
661 | 661 | if (!record) { |
|
662 | 662 | goto bail; |
|
663 | 663 | } |
|
664 | 664 | error = PyList_Append(markers, record); |
|
665 | 665 | Py_DECREF(record); |
|
666 | 666 | if (error) { |
|
667 | 667 | goto bail; |
|
668 | 668 | } |
|
669 | 669 | data += msize; |
|
670 | 670 | offset += msize; |
|
671 | 671 | } |
|
672 | 672 | return markers; |
|
673 | 673 | bail: |
|
674 | 674 | Py_DECREF(markers); |
|
675 | 675 | return NULL; |
|
676 | 676 | } |
|
677 | 677 | |
|
678 | 678 | static char parsers_doc[] = "Efficient content parsing."; |
|
679 | 679 | |
|
680 | 680 | PyObject *encodedir(PyObject *self, PyObject *args); |
|
681 | 681 | PyObject *pathencode(PyObject *self, PyObject *args); |
|
682 | 682 | PyObject *lowerencode(PyObject *self, PyObject *args); |
|
683 | 683 | PyObject *parse_index2(PyObject *self, PyObject *args); |
|
684 | 684 | |
|
685 | 685 | static PyMethodDef methods[] = { |
|
686 | 686 | {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"}, |
|
687 | 687 | {"nonnormalotherparententries", nonnormalotherparententries, METH_VARARGS, |
|
688 | 688 | "create a set containing non-normal and other parent entries of given " |
|
689 | 689 | "dirstate\n"}, |
|
690 | 690 | {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"}, |
|
691 | 691 | {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"}, |
|
692 | 692 | {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"}, |
|
693 | 693 | {"isasciistr", isasciistr, METH_VARARGS, "check if an ASCII string\n"}, |
|
694 | 694 | {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"}, |
|
695 | 695 | {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"}, |
|
696 | 696 | {"dict_new_presized", dict_new_presized, METH_VARARGS, |
|
697 | 697 | "construct a dict with an expected size\n"}, |
|
698 | 698 | {"make_file_foldmap", make_file_foldmap, METH_VARARGS, |
|
699 | 699 | "make file foldmap\n"}, |
|
700 | 700 | {"jsonescapeu8fast", jsonescapeu8fast, METH_VARARGS, |
|
701 | 701 | "escape a UTF-8 byte string to JSON (fast path)\n"}, |
|
702 | 702 | {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"}, |
|
703 | 703 | {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"}, |
|
704 | 704 | {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"}, |
|
705 | 705 | {"fm1readmarkers", fm1readmarkers, METH_VARARGS, |
|
706 | 706 | "parse v1 obsolete markers\n"}, |
|
707 | 707 | {NULL, NULL}}; |
|
708 | 708 | |
|
709 | 709 | void dirs_module_init(PyObject *mod); |
|
710 | 710 | void manifest_module_init(PyObject *mod); |
|
711 | 711 | void revlog_module_init(PyObject *mod); |
|
712 | 712 | |
|
713 |
static const int version = |
|
|
713 | static const int version = 4; | |
|
714 | 714 | |
|
715 | 715 | static void module_init(PyObject *mod) |
|
716 | 716 | { |
|
717 | 717 | PyModule_AddIntConstant(mod, "version", version); |
|
718 | 718 | |
|
719 | 719 | /* This module constant has two purposes. First, it lets us unit test |
|
720 | 720 | * the ImportError raised without hard-coding any error text. This |
|
721 | 721 | * means we can change the text in the future without breaking tests, |
|
722 | 722 | * even across changesets without a recompile. Second, its presence |
|
723 | 723 | * can be used to determine whether the version-checking logic is |
|
724 | 724 | * present, which also helps in testing across changesets without a |
|
725 | 725 | * recompile. Note that this means the pure-Python version of parsers |
|
726 | 726 | * should not have this module constant. */ |
|
727 | 727 | PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext); |
|
728 | 728 | |
|
729 | 729 | dirs_module_init(mod); |
|
730 | 730 | manifest_module_init(mod); |
|
731 | 731 | revlog_module_init(mod); |
|
732 | 732 | |
|
733 | 733 | if (PyType_Ready(&dirstateTupleType) < 0) |
|
734 | 734 | return; |
|
735 | 735 | Py_INCREF(&dirstateTupleType); |
|
736 | 736 | PyModule_AddObject(mod, "dirstatetuple", |
|
737 | 737 | (PyObject *)&dirstateTupleType); |
|
738 | 738 | } |
|
739 | 739 | |
|
740 | 740 | static int check_python_version(void) |
|
741 | 741 | { |
|
742 | 742 | PyObject *sys = PyImport_ImportModule("sys"), *ver; |
|
743 | 743 | long hexversion; |
|
744 | 744 | if (!sys) |
|
745 | 745 | return -1; |
|
746 | 746 | ver = PyObject_GetAttrString(sys, "hexversion"); |
|
747 | 747 | Py_DECREF(sys); |
|
748 | 748 | if (!ver) |
|
749 | 749 | return -1; |
|
750 | 750 | hexversion = PyInt_AsLong(ver); |
|
751 | 751 | Py_DECREF(ver); |
|
752 | 752 | /* sys.hexversion is a 32-bit number by default, so the -1 case |
|
753 | 753 | * should only occur in unusual circumstances (e.g. if sys.hexversion |
|
754 | 754 | * is manually set to an invalid value). */ |
|
755 | 755 | if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) { |
|
756 | 756 | PyErr_Format(PyExc_ImportError, |
|
757 | 757 | "%s: The Mercurial extension " |
|
758 | 758 | "modules were compiled with Python " PY_VERSION |
|
759 | 759 | ", but " |
|
760 | 760 | "Mercurial is currently using Python with " |
|
761 | 761 | "sys.hexversion=%ld: " |
|
762 | 762 | "Python %s\n at: %s", |
|
763 | 763 | versionerrortext, hexversion, Py_GetVersion(), |
|
764 | 764 | Py_GetProgramFullPath()); |
|
765 | 765 | return -1; |
|
766 | 766 | } |
|
767 | 767 | return 0; |
|
768 | 768 | } |
|
769 | 769 | |
|
770 | 770 | #ifdef IS_PY3K |
|
771 | 771 | static struct PyModuleDef parsers_module = {PyModuleDef_HEAD_INIT, "parsers", |
|
772 | 772 | parsers_doc, -1, methods}; |
|
773 | 773 | |
|
774 | 774 | PyMODINIT_FUNC PyInit_parsers(void) |
|
775 | 775 | { |
|
776 | 776 | PyObject *mod; |
|
777 | 777 | |
|
778 | 778 | if (check_python_version() == -1) |
|
779 | 779 | return NULL; |
|
780 | 780 | mod = PyModule_Create(&parsers_module); |
|
781 | 781 | module_init(mod); |
|
782 | 782 | return mod; |
|
783 | 783 | } |
|
784 | 784 | #else |
|
785 | 785 | PyMODINIT_FUNC initparsers(void) |
|
786 | 786 | { |
|
787 | 787 | PyObject *mod; |
|
788 | 788 | |
|
789 | 789 | if (check_python_version() == -1) |
|
790 | 790 | return; |
|
791 | 791 | mod = Py_InitModule3("parsers", methods, parsers_doc); |
|
792 | 792 | module_init(mod); |
|
793 | 793 | } |
|
794 | 794 | #endif |
@@ -1,2090 +1,2084 | |||
|
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 <assert.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_AS_LONG PyLong_AS_LONG |
|
28 | 28 | #define PyInt_AsLong PyLong_AsLong |
|
29 | 29 | #endif |
|
30 | 30 | |
|
31 | 31 | /* |
|
32 | 32 | * A base-16 trie for fast node->rev mapping. |
|
33 | 33 | * |
|
34 | 34 | * Positive value is index of the next node in the trie |
|
35 | 35 | * Negative value is a leaf: -(rev + 1) |
|
36 | 36 | * Zero is empty |
|
37 | 37 | */ |
|
38 | 38 | typedef struct { |
|
39 | 39 | int children[16]; |
|
40 | 40 | } nodetree; |
|
41 | 41 | |
|
42 | 42 | /* |
|
43 | 43 | * This class has two behaviors. |
|
44 | 44 | * |
|
45 | 45 | * When used in a list-like way (with integer keys), we decode an |
|
46 | 46 | * entry in a RevlogNG index file on demand. Our last entry is a |
|
47 | 47 | * sentinel, always a nullid. We have limited support for |
|
48 | 48 | * integer-keyed insert and delete, only at elements right before the |
|
49 | 49 | * sentinel. |
|
50 | 50 | * |
|
51 | 51 | * With string keys, we lazily perform a reverse mapping from node to |
|
52 | 52 | * rev, using a base-16 trie. |
|
53 | 53 | */ |
|
54 | 54 | typedef struct { |
|
55 | 55 | PyObject_HEAD |
|
56 | 56 | /* Type-specific fields go here. */ |
|
57 | 57 | PyObject *data; /* raw bytes of index */ |
|
58 | 58 | Py_buffer buf; /* buffer of data */ |
|
59 | 59 | PyObject **cache; /* cached tuples */ |
|
60 | 60 | const char **offsets; /* populated on demand */ |
|
61 | 61 | Py_ssize_t raw_length; /* original number of elements */ |
|
62 | 62 | Py_ssize_t length; /* current number of elements */ |
|
63 | 63 | PyObject *added; /* populated on demand */ |
|
64 | 64 | PyObject *headrevs; /* cache, invalidated on changes */ |
|
65 | 65 | PyObject *filteredrevs;/* filtered revs set */ |
|
66 | 66 | nodetree *nt; /* base-16 trie */ |
|
67 | 67 | unsigned ntlength; /* # nodes in use */ |
|
68 | 68 | unsigned ntcapacity; /* # nodes allocated */ |
|
69 | 69 | int ntdepth; /* maximum depth of tree */ |
|
70 | 70 | int ntsplits; /* # splits performed */ |
|
71 | 71 | int ntrev; /* last rev scanned */ |
|
72 | 72 | int ntlookups; /* # lookups */ |
|
73 | 73 | int ntmisses; /* # lookups that miss the cache */ |
|
74 | 74 | int inlined; |
|
75 | 75 | } indexObject; |
|
76 | 76 | |
|
77 | 77 | static Py_ssize_t index_length(const indexObject *self) |
|
78 | 78 | { |
|
79 | 79 | if (self->added == NULL) |
|
80 | 80 | return self->length; |
|
81 | 81 | return self->length + PyList_GET_SIZE(self->added); |
|
82 | 82 | } |
|
83 | 83 | |
|
84 | 84 | static PyObject *nullentry; |
|
85 | 85 | static const char nullid[20]; |
|
86 | 86 | |
|
87 | 87 | static Py_ssize_t inline_scan(indexObject *self, const char **offsets); |
|
88 | 88 | |
|
89 | 89 | #if LONG_MAX == 0x7fffffffL |
|
90 | 90 | static char *tuple_format = "Kiiiiiis#"; |
|
91 | 91 | #else |
|
92 | 92 | static char *tuple_format = "kiiiiiis#"; |
|
93 | 93 | #endif |
|
94 | 94 | |
|
95 | 95 | /* A RevlogNG v1 index entry is 64 bytes long. */ |
|
96 | 96 | static const long v1_hdrsize = 64; |
|
97 | 97 | |
|
98 | 98 | /* |
|
99 | 99 | * Return a pointer to the beginning of a RevlogNG record. |
|
100 | 100 | */ |
|
101 | 101 | static const char *index_deref(indexObject *self, Py_ssize_t pos) |
|
102 | 102 | { |
|
103 | 103 | if (self->inlined && pos > 0) { |
|
104 | 104 | if (self->offsets == NULL) { |
|
105 | 105 | self->offsets = PyMem_Malloc(self->raw_length * |
|
106 | 106 | sizeof(*self->offsets)); |
|
107 | 107 | if (self->offsets == NULL) |
|
108 | 108 | return (const char *)PyErr_NoMemory(); |
|
109 | 109 | inline_scan(self, self->offsets); |
|
110 | 110 | } |
|
111 | 111 | return self->offsets[pos]; |
|
112 | 112 | } |
|
113 | 113 | |
|
114 | 114 | return (const char *)(self->buf.buf) + pos * v1_hdrsize; |
|
115 | 115 | } |
|
116 | 116 | |
|
117 | 117 | static inline int index_get_parents(indexObject *self, Py_ssize_t rev, |
|
118 | 118 | int *ps, int maxrev) |
|
119 | 119 | { |
|
120 | 120 | if (rev >= self->length - 1) { |
|
121 | 121 | PyObject *tuple = PyList_GET_ITEM(self->added, |
|
122 | 122 | rev - self->length + 1); |
|
123 | 123 | ps[0] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 5)); |
|
124 | 124 | ps[1] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 6)); |
|
125 | 125 | } else { |
|
126 | 126 | const char *data = index_deref(self, rev); |
|
127 | 127 | ps[0] = getbe32(data + 24); |
|
128 | 128 | ps[1] = getbe32(data + 28); |
|
129 | 129 | } |
|
130 | 130 | /* If index file is corrupted, ps[] may point to invalid revisions. So |
|
131 | 131 | * there is a risk of buffer overflow to trust them unconditionally. */ |
|
132 | 132 | if (ps[0] > maxrev || ps[1] > maxrev) { |
|
133 | 133 | PyErr_SetString(PyExc_ValueError, "parent out of range"); |
|
134 | 134 | return -1; |
|
135 | 135 | } |
|
136 | 136 | return 0; |
|
137 | 137 | } |
|
138 | 138 | |
|
139 | 139 | |
|
140 | 140 | /* |
|
141 | 141 | * RevlogNG format (all in big endian, data may be inlined): |
|
142 | 142 | * 6 bytes: offset |
|
143 | 143 | * 2 bytes: flags |
|
144 | 144 | * 4 bytes: compressed length |
|
145 | 145 | * 4 bytes: uncompressed length |
|
146 | 146 | * 4 bytes: base revision |
|
147 | 147 | * 4 bytes: link revision |
|
148 | 148 | * 4 bytes: parent 1 revision |
|
149 | 149 | * 4 bytes: parent 2 revision |
|
150 | 150 | * 32 bytes: nodeid (only 20 bytes used) |
|
151 | 151 | */ |
|
152 | 152 | static PyObject *index_get(indexObject *self, Py_ssize_t pos) |
|
153 | 153 | { |
|
154 | 154 | uint64_t offset_flags; |
|
155 | 155 | int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2; |
|
156 | 156 | const char *c_node_id; |
|
157 | 157 | const char *data; |
|
158 | 158 | Py_ssize_t length = index_length(self); |
|
159 | 159 | PyObject *entry; |
|
160 | 160 | |
|
161 | 161 | if (pos < 0) |
|
162 | 162 | pos += length; |
|
163 | 163 | |
|
164 | 164 | if (pos < 0 || pos >= length) { |
|
165 | 165 | PyErr_SetString(PyExc_IndexError, "revlog index out of range"); |
|
166 | 166 | return NULL; |
|
167 | 167 | } |
|
168 | 168 | |
|
169 | 169 | if (pos == length - 1) { |
|
170 | 170 | Py_INCREF(nullentry); |
|
171 | 171 | return nullentry; |
|
172 | 172 | } |
|
173 | 173 | |
|
174 | 174 | if (pos >= self->length - 1) { |
|
175 | 175 | PyObject *obj; |
|
176 | 176 | obj = PyList_GET_ITEM(self->added, pos - self->length + 1); |
|
177 | 177 | Py_INCREF(obj); |
|
178 | 178 | return obj; |
|
179 | 179 | } |
|
180 | 180 | |
|
181 | 181 | if (self->cache) { |
|
182 | 182 | if (self->cache[pos]) { |
|
183 | 183 | Py_INCREF(self->cache[pos]); |
|
184 | 184 | return self->cache[pos]; |
|
185 | 185 | } |
|
186 | 186 | } else { |
|
187 | 187 | self->cache = calloc(self->raw_length, sizeof(PyObject *)); |
|
188 | 188 | if (self->cache == NULL) |
|
189 | 189 | return PyErr_NoMemory(); |
|
190 | 190 | } |
|
191 | 191 | |
|
192 | 192 | data = index_deref(self, pos); |
|
193 | 193 | if (data == NULL) |
|
194 | 194 | return NULL; |
|
195 | 195 | |
|
196 | 196 | offset_flags = getbe32(data + 4); |
|
197 | 197 | if (pos == 0) /* mask out version number for the first entry */ |
|
198 | 198 | offset_flags &= 0xFFFF; |
|
199 | 199 | else { |
|
200 | 200 | uint32_t offset_high = getbe32(data); |
|
201 | 201 | offset_flags |= ((uint64_t)offset_high) << 32; |
|
202 | 202 | } |
|
203 | 203 | |
|
204 | 204 | comp_len = getbe32(data + 8); |
|
205 | 205 | uncomp_len = getbe32(data + 12); |
|
206 | 206 | base_rev = getbe32(data + 16); |
|
207 | 207 | link_rev = getbe32(data + 20); |
|
208 | 208 | parent_1 = getbe32(data + 24); |
|
209 | 209 | parent_2 = getbe32(data + 28); |
|
210 | 210 | c_node_id = data + 32; |
|
211 | 211 | |
|
212 | 212 | entry = Py_BuildValue(tuple_format, offset_flags, comp_len, |
|
213 | 213 | uncomp_len, base_rev, link_rev, |
|
214 | 214 | parent_1, parent_2, c_node_id, 20); |
|
215 | 215 | |
|
216 | 216 | if (entry) { |
|
217 | 217 | PyObject_GC_UnTrack(entry); |
|
218 | 218 | Py_INCREF(entry); |
|
219 | 219 | } |
|
220 | 220 | |
|
221 | 221 | self->cache[pos] = entry; |
|
222 | 222 | |
|
223 | 223 | return entry; |
|
224 | 224 | } |
|
225 | 225 | |
|
226 | 226 | /* |
|
227 | 227 | * Return the 20-byte SHA of the node corresponding to the given rev. |
|
228 | 228 | */ |
|
229 | 229 | static const char *index_node(indexObject *self, Py_ssize_t pos) |
|
230 | 230 | { |
|
231 | 231 | Py_ssize_t length = index_length(self); |
|
232 | 232 | const char *data; |
|
233 | 233 | |
|
234 | 234 | if (pos == length - 1 || pos == INT_MAX) |
|
235 | 235 | return nullid; |
|
236 | 236 | |
|
237 | 237 | if (pos >= length) |
|
238 | 238 | return NULL; |
|
239 | 239 | |
|
240 | 240 | if (pos >= self->length - 1) { |
|
241 | 241 | PyObject *tuple, *str; |
|
242 | 242 | tuple = PyList_GET_ITEM(self->added, pos - self->length + 1); |
|
243 | 243 | str = PyTuple_GetItem(tuple, 7); |
|
244 | 244 | return str ? PyBytes_AS_STRING(str) : NULL; |
|
245 | 245 | } |
|
246 | 246 | |
|
247 | 247 | data = index_deref(self, pos); |
|
248 | 248 | return data ? data + 32 : NULL; |
|
249 | 249 | } |
|
250 | 250 | |
|
251 | 251 | static int nt_insert(indexObject *self, const char *node, int rev); |
|
252 | 252 | |
|
253 | 253 | static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen) |
|
254 | 254 | { |
|
255 | 255 | if (PyBytes_AsStringAndSize(obj, node, nodelen) == -1) |
|
256 | 256 | return -1; |
|
257 | 257 | if (*nodelen == 20) |
|
258 | 258 | return 0; |
|
259 | 259 | PyErr_SetString(PyExc_ValueError, "20-byte hash required"); |
|
260 | 260 | return -1; |
|
261 | 261 | } |
|
262 | 262 | |
|
263 | 263 | static PyObject *index_insert(indexObject *self, PyObject *args) |
|
264 | 264 | { |
|
265 | 265 | PyObject *obj; |
|
266 | 266 | char *node; |
|
267 | 267 | int index; |
|
268 | 268 | Py_ssize_t len, nodelen; |
|
269 | 269 | |
|
270 | 270 | if (!PyArg_ParseTuple(args, "iO", &index, &obj)) |
|
271 | 271 | return NULL; |
|
272 | 272 | |
|
273 | 273 | if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) { |
|
274 | 274 | PyErr_SetString(PyExc_TypeError, "8-tuple required"); |
|
275 | 275 | return NULL; |
|
276 | 276 | } |
|
277 | 277 | |
|
278 | 278 | if (node_check(PyTuple_GET_ITEM(obj, 7), &node, &nodelen) == -1) |
|
279 | 279 | return NULL; |
|
280 | 280 | |
|
281 | 281 | len = index_length(self); |
|
282 | 282 | |
|
283 | 283 | if (index < 0) |
|
284 | 284 | index += len; |
|
285 | 285 | |
|
286 | 286 | if (index != len - 1) { |
|
287 | 287 | PyErr_SetString(PyExc_IndexError, |
|
288 | 288 | "insert only supported at index -1"); |
|
289 | 289 | return NULL; |
|
290 | 290 | } |
|
291 | 291 | |
|
292 | 292 | if (self->added == NULL) { |
|
293 | 293 | self->added = PyList_New(0); |
|
294 | 294 | if (self->added == NULL) |
|
295 | 295 | return NULL; |
|
296 | 296 | } |
|
297 | 297 | |
|
298 | 298 | if (PyList_Append(self->added, obj) == -1) |
|
299 | 299 | return NULL; |
|
300 | 300 | |
|
301 | 301 | if (self->nt) |
|
302 | 302 | nt_insert(self, node, index); |
|
303 | 303 | |
|
304 | 304 | Py_CLEAR(self->headrevs); |
|
305 | 305 | Py_RETURN_NONE; |
|
306 | 306 | } |
|
307 | 307 | |
|
308 | 308 | static void _index_clearcaches(indexObject *self) |
|
309 | 309 | { |
|
310 | 310 | if (self->cache) { |
|
311 | 311 | Py_ssize_t i; |
|
312 | 312 | |
|
313 | 313 | for (i = 0; i < self->raw_length; i++) |
|
314 | 314 | Py_CLEAR(self->cache[i]); |
|
315 | 315 | free(self->cache); |
|
316 | 316 | self->cache = NULL; |
|
317 | 317 | } |
|
318 | 318 | if (self->offsets) { |
|
319 | 319 | PyMem_Free(self->offsets); |
|
320 | 320 | self->offsets = NULL; |
|
321 | 321 | } |
|
322 | 322 | if (self->nt) { |
|
323 | 323 | free(self->nt); |
|
324 | 324 | self->nt = NULL; |
|
325 | 325 | } |
|
326 | 326 | Py_CLEAR(self->headrevs); |
|
327 | 327 | } |
|
328 | 328 | |
|
329 | 329 | static PyObject *index_clearcaches(indexObject *self) |
|
330 | 330 | { |
|
331 | 331 | _index_clearcaches(self); |
|
332 | 332 | self->ntlength = self->ntcapacity = 0; |
|
333 | 333 | self->ntdepth = self->ntsplits = 0; |
|
334 | 334 | self->ntrev = -1; |
|
335 | 335 | self->ntlookups = self->ntmisses = 0; |
|
336 | 336 | Py_RETURN_NONE; |
|
337 | 337 | } |
|
338 | 338 | |
|
339 | 339 | static PyObject *index_stats(indexObject *self) |
|
340 | 340 | { |
|
341 | 341 | PyObject *obj = PyDict_New(); |
|
342 | 342 | PyObject *t = NULL; |
|
343 | 343 | |
|
344 | 344 | if (obj == NULL) |
|
345 | 345 | return NULL; |
|
346 | 346 | |
|
347 | 347 | #define istat(__n, __d) \ |
|
348 | 348 | do { \ |
|
349 | 349 | t = PyInt_FromSsize_t(self->__n); \ |
|
350 | 350 | if (!t) \ |
|
351 | 351 | goto bail; \ |
|
352 | 352 | if (PyDict_SetItemString(obj, __d, t) == -1) \ |
|
353 | 353 | goto bail; \ |
|
354 | 354 | Py_DECREF(t); \ |
|
355 | 355 | } while (0) |
|
356 | 356 | |
|
357 | 357 | if (self->added) { |
|
358 | 358 | Py_ssize_t len = PyList_GET_SIZE(self->added); |
|
359 | 359 | t = PyInt_FromSsize_t(len); |
|
360 | 360 | if (!t) |
|
361 | 361 | goto bail; |
|
362 | 362 | if (PyDict_SetItemString(obj, "index entries added", t) == -1) |
|
363 | 363 | goto bail; |
|
364 | 364 | Py_DECREF(t); |
|
365 | 365 | } |
|
366 | 366 | |
|
367 | 367 | if (self->raw_length != self->length - 1) |
|
368 | 368 | istat(raw_length, "revs on disk"); |
|
369 | 369 | istat(length, "revs in memory"); |
|
370 | 370 | istat(ntcapacity, "node trie capacity"); |
|
371 | 371 | istat(ntdepth, "node trie depth"); |
|
372 | 372 | istat(ntlength, "node trie count"); |
|
373 | 373 | istat(ntlookups, "node trie lookups"); |
|
374 | 374 | istat(ntmisses, "node trie misses"); |
|
375 | 375 | istat(ntrev, "node trie last rev scanned"); |
|
376 | 376 | istat(ntsplits, "node trie splits"); |
|
377 | 377 | |
|
378 | 378 | #undef istat |
|
379 | 379 | |
|
380 | 380 | return obj; |
|
381 | 381 | |
|
382 | 382 | bail: |
|
383 | 383 | Py_XDECREF(obj); |
|
384 | 384 | Py_XDECREF(t); |
|
385 | 385 | return NULL; |
|
386 | 386 | } |
|
387 | 387 | |
|
388 | 388 | /* |
|
389 | 389 | * When we cache a list, we want to be sure the caller can't mutate |
|
390 | 390 | * the cached copy. |
|
391 | 391 | */ |
|
392 | 392 | static PyObject *list_copy(PyObject *list) |
|
393 | 393 | { |
|
394 | 394 | Py_ssize_t len = PyList_GET_SIZE(list); |
|
395 | 395 | PyObject *newlist = PyList_New(len); |
|
396 | 396 | Py_ssize_t i; |
|
397 | 397 | |
|
398 | 398 | if (newlist == NULL) |
|
399 | 399 | return NULL; |
|
400 | 400 | |
|
401 | 401 | for (i = 0; i < len; i++) { |
|
402 | 402 | PyObject *obj = PyList_GET_ITEM(list, i); |
|
403 | 403 | Py_INCREF(obj); |
|
404 | 404 | PyList_SET_ITEM(newlist, i, obj); |
|
405 | 405 | } |
|
406 | 406 | |
|
407 | 407 | return newlist; |
|
408 | 408 | } |
|
409 | 409 | |
|
410 | 410 | static int check_filter(PyObject *filter, Py_ssize_t arg) |
|
411 | 411 | { |
|
412 | 412 | if (filter) { |
|
413 | 413 | PyObject *arglist, *result; |
|
414 | 414 | int isfiltered; |
|
415 | 415 | |
|
416 | 416 | arglist = Py_BuildValue("(n)", arg); |
|
417 | 417 | if (!arglist) { |
|
418 | 418 | return -1; |
|
419 | 419 | } |
|
420 | 420 | |
|
421 | 421 | result = PyEval_CallObject(filter, arglist); |
|
422 | 422 | Py_DECREF(arglist); |
|
423 | 423 | if (!result) { |
|
424 | 424 | return -1; |
|
425 | 425 | } |
|
426 | 426 | |
|
427 | 427 | /* PyObject_IsTrue returns 1 if true, 0 if false, -1 if error, |
|
428 | 428 | * same as this function, so we can just return it directly.*/ |
|
429 | 429 | isfiltered = PyObject_IsTrue(result); |
|
430 | 430 | Py_DECREF(result); |
|
431 | 431 | return isfiltered; |
|
432 | 432 | } else { |
|
433 | 433 | return 0; |
|
434 | 434 | } |
|
435 | 435 | } |
|
436 | 436 | |
|
437 | 437 | static Py_ssize_t add_roots_get_min(indexObject *self, PyObject *list, |
|
438 | 438 | Py_ssize_t marker, char *phases) |
|
439 | 439 | { |
|
440 | 440 | PyObject *iter = NULL; |
|
441 | 441 | PyObject *iter_item = NULL; |
|
442 | 442 | Py_ssize_t min_idx = index_length(self) + 1; |
|
443 | 443 | long iter_item_long; |
|
444 | 444 | |
|
445 | 445 | if (PyList_GET_SIZE(list) != 0) { |
|
446 | 446 | iter = PyObject_GetIter(list); |
|
447 | 447 | if (iter == NULL) |
|
448 | 448 | return -2; |
|
449 | 449 | while ((iter_item = PyIter_Next(iter))) { |
|
450 | 450 | iter_item_long = PyInt_AS_LONG(iter_item); |
|
451 | 451 | Py_DECREF(iter_item); |
|
452 | 452 | if (iter_item_long < min_idx) |
|
453 | 453 | min_idx = iter_item_long; |
|
454 | 454 | phases[iter_item_long] = marker; |
|
455 | 455 | } |
|
456 | 456 | Py_DECREF(iter); |
|
457 | 457 | } |
|
458 | 458 | |
|
459 | 459 | return min_idx; |
|
460 | 460 | } |
|
461 | 461 | |
|
462 | 462 | static inline void set_phase_from_parents(char *phases, int parent_1, |
|
463 | 463 | int parent_2, Py_ssize_t i) |
|
464 | 464 | { |
|
465 | 465 | if (parent_1 >= 0 && phases[parent_1] > phases[i]) |
|
466 | 466 | phases[i] = phases[parent_1]; |
|
467 | 467 | if (parent_2 >= 0 && phases[parent_2] > phases[i]) |
|
468 | 468 | phases[i] = phases[parent_2]; |
|
469 | 469 | } |
|
470 | 470 | |
|
471 | 471 | static PyObject *reachableroots2(indexObject *self, PyObject *args) |
|
472 | 472 | { |
|
473 | 473 | |
|
474 | 474 | /* Input */ |
|
475 | 475 | long minroot; |
|
476 | 476 | PyObject *includepatharg = NULL; |
|
477 | 477 | int includepath = 0; |
|
478 | 478 | /* heads and roots are lists */ |
|
479 | 479 | PyObject *heads = NULL; |
|
480 | 480 | PyObject *roots = NULL; |
|
481 | 481 | PyObject *reachable = NULL; |
|
482 | 482 | |
|
483 | 483 | PyObject *val; |
|
484 | 484 | Py_ssize_t len = index_length(self) - 1; |
|
485 | 485 | long revnum; |
|
486 | 486 | Py_ssize_t k; |
|
487 | 487 | Py_ssize_t i; |
|
488 | 488 | Py_ssize_t l; |
|
489 | 489 | int r; |
|
490 | 490 | int parents[2]; |
|
491 | 491 | |
|
492 | 492 | /* Internal data structure: |
|
493 | 493 | * tovisit: array of length len+1 (all revs + nullrev), filled upto lentovisit |
|
494 | 494 | * revstates: array of length len+1 (all revs + nullrev) */ |
|
495 | 495 | int *tovisit = NULL; |
|
496 | 496 | long lentovisit = 0; |
|
497 | 497 | enum { RS_SEEN = 1, RS_ROOT = 2, RS_REACHABLE = 4 }; |
|
498 | 498 | char *revstates = NULL; |
|
499 | 499 | |
|
500 | 500 | /* Get arguments */ |
|
501 | 501 | if (!PyArg_ParseTuple(args, "lO!O!O!", &minroot, &PyList_Type, &heads, |
|
502 | 502 | &PyList_Type, &roots, |
|
503 | 503 | &PyBool_Type, &includepatharg)) |
|
504 | 504 | goto bail; |
|
505 | 505 | |
|
506 | 506 | if (includepatharg == Py_True) |
|
507 | 507 | includepath = 1; |
|
508 | 508 | |
|
509 | 509 | /* Initialize return set */ |
|
510 | 510 | reachable = PyList_New(0); |
|
511 | 511 | if (reachable == NULL) |
|
512 | 512 | goto bail; |
|
513 | 513 | |
|
514 | 514 | /* Initialize internal datastructures */ |
|
515 | 515 | tovisit = (int *)malloc((len + 1) * sizeof(int)); |
|
516 | 516 | if (tovisit == NULL) { |
|
517 | 517 | PyErr_NoMemory(); |
|
518 | 518 | goto bail; |
|
519 | 519 | } |
|
520 | 520 | |
|
521 | 521 | revstates = (char *)calloc(len + 1, 1); |
|
522 | 522 | if (revstates == NULL) { |
|
523 | 523 | PyErr_NoMemory(); |
|
524 | 524 | goto bail; |
|
525 | 525 | } |
|
526 | 526 | |
|
527 | 527 | l = PyList_GET_SIZE(roots); |
|
528 | 528 | for (i = 0; i < l; i++) { |
|
529 | 529 | revnum = PyInt_AsLong(PyList_GET_ITEM(roots, i)); |
|
530 | 530 | if (revnum == -1 && PyErr_Occurred()) |
|
531 | 531 | goto bail; |
|
532 | 532 | /* If root is out of range, e.g. wdir(), it must be unreachable |
|
533 | 533 | * from heads. So we can just ignore it. */ |
|
534 | 534 | if (revnum + 1 < 0 || revnum + 1 >= len + 1) |
|
535 | 535 | continue; |
|
536 | 536 | revstates[revnum + 1] |= RS_ROOT; |
|
537 | 537 | } |
|
538 | 538 | |
|
539 | 539 | /* Populate tovisit with all the heads */ |
|
540 | 540 | l = PyList_GET_SIZE(heads); |
|
541 | 541 | for (i = 0; i < l; i++) { |
|
542 | 542 | revnum = PyInt_AsLong(PyList_GET_ITEM(heads, i)); |
|
543 | 543 | if (revnum == -1 && PyErr_Occurred()) |
|
544 | 544 | goto bail; |
|
545 | 545 | if (revnum + 1 < 0 || revnum + 1 >= len + 1) { |
|
546 | 546 | PyErr_SetString(PyExc_IndexError, "head out of range"); |
|
547 | 547 | goto bail; |
|
548 | 548 | } |
|
549 | 549 | if (!(revstates[revnum + 1] & RS_SEEN)) { |
|
550 | 550 | tovisit[lentovisit++] = (int)revnum; |
|
551 | 551 | revstates[revnum + 1] |= RS_SEEN; |
|
552 | 552 | } |
|
553 | 553 | } |
|
554 | 554 | |
|
555 | 555 | /* Visit the tovisit list and find the reachable roots */ |
|
556 | 556 | k = 0; |
|
557 | 557 | while (k < lentovisit) { |
|
558 | 558 | /* Add the node to reachable if it is a root*/ |
|
559 | 559 | revnum = tovisit[k++]; |
|
560 | 560 | if (revstates[revnum + 1] & RS_ROOT) { |
|
561 | 561 | revstates[revnum + 1] |= RS_REACHABLE; |
|
562 | 562 | val = PyInt_FromLong(revnum); |
|
563 | 563 | if (val == NULL) |
|
564 | 564 | goto bail; |
|
565 | 565 | r = PyList_Append(reachable, val); |
|
566 | 566 | Py_DECREF(val); |
|
567 | 567 | if (r < 0) |
|
568 | 568 | goto bail; |
|
569 | 569 | if (includepath == 0) |
|
570 | 570 | continue; |
|
571 | 571 | } |
|
572 | 572 | |
|
573 | 573 | /* Add its parents to the list of nodes to visit */ |
|
574 | 574 | if (revnum == -1) |
|
575 | 575 | continue; |
|
576 | 576 | r = index_get_parents(self, revnum, parents, (int)len - 1); |
|
577 | 577 | if (r < 0) |
|
578 | 578 | goto bail; |
|
579 | 579 | for (i = 0; i < 2; i++) { |
|
580 | 580 | if (!(revstates[parents[i] + 1] & RS_SEEN) |
|
581 | 581 | && parents[i] >= minroot) { |
|
582 | 582 | tovisit[lentovisit++] = parents[i]; |
|
583 | 583 | revstates[parents[i] + 1] |= RS_SEEN; |
|
584 | 584 | } |
|
585 | 585 | } |
|
586 | 586 | } |
|
587 | 587 | |
|
588 | 588 | /* Find all the nodes in between the roots we found and the heads |
|
589 | 589 | * and add them to the reachable set */ |
|
590 | 590 | if (includepath == 1) { |
|
591 | 591 | long minidx = minroot; |
|
592 | 592 | if (minidx < 0) |
|
593 | 593 | minidx = 0; |
|
594 | 594 | for (i = minidx; i < len; i++) { |
|
595 | 595 | if (!(revstates[i + 1] & RS_SEEN)) |
|
596 | 596 | continue; |
|
597 | 597 | r = index_get_parents(self, i, parents, (int)len - 1); |
|
598 | 598 | /* Corrupted index file, error is set from |
|
599 | 599 | * index_get_parents */ |
|
600 | 600 | if (r < 0) |
|
601 | 601 | goto bail; |
|
602 | 602 | if (((revstates[parents[0] + 1] | |
|
603 | 603 | revstates[parents[1] + 1]) & RS_REACHABLE) |
|
604 | 604 | && !(revstates[i + 1] & RS_REACHABLE)) { |
|
605 | 605 | revstates[i + 1] |= RS_REACHABLE; |
|
606 | 606 | val = PyInt_FromLong(i); |
|
607 | 607 | if (val == NULL) |
|
608 | 608 | goto bail; |
|
609 | 609 | r = PyList_Append(reachable, val); |
|
610 | 610 | Py_DECREF(val); |
|
611 | 611 | if (r < 0) |
|
612 | 612 | goto bail; |
|
613 | 613 | } |
|
614 | 614 | } |
|
615 | 615 | } |
|
616 | 616 | |
|
617 | 617 | free(revstates); |
|
618 | 618 | free(tovisit); |
|
619 | 619 | return reachable; |
|
620 | 620 | bail: |
|
621 | 621 | Py_XDECREF(reachable); |
|
622 | 622 | free(revstates); |
|
623 | 623 | free(tovisit); |
|
624 | 624 | return NULL; |
|
625 | 625 | } |
|
626 | 626 | |
|
627 | 627 | static PyObject *compute_phases_map_sets(indexObject *self, PyObject *args) |
|
628 | 628 | { |
|
629 | 629 | PyObject *roots = Py_None; |
|
630 | 630 | PyObject *ret = NULL; |
|
631 |
PyObject *phases |
|
|
631 | PyObject *phasessize = NULL; | |
|
632 | 632 | PyObject *phaseroots = NULL; |
|
633 | 633 | PyObject *phaseset = NULL; |
|
634 | 634 | PyObject *phasessetlist = NULL; |
|
635 | 635 | PyObject *rev = NULL; |
|
636 | 636 | Py_ssize_t len = index_length(self) - 1; |
|
637 | 637 | Py_ssize_t numphase = 0; |
|
638 | 638 | Py_ssize_t minrevallphases = 0; |
|
639 | 639 | Py_ssize_t minrevphase = 0; |
|
640 | 640 | Py_ssize_t i = 0; |
|
641 | 641 | char *phases = NULL; |
|
642 | 642 | long phase; |
|
643 | 643 | |
|
644 | 644 | if (!PyArg_ParseTuple(args, "O", &roots)) |
|
645 | 645 | goto done; |
|
646 | 646 | if (roots == NULL || !PyList_Check(roots)) |
|
647 | 647 | goto done; |
|
648 | 648 | |
|
649 | 649 | phases = calloc(len, 1); /* phase per rev: {0: public, 1: draft, 2: secret} */ |
|
650 | 650 | if (phases == NULL) { |
|
651 | 651 | PyErr_NoMemory(); |
|
652 | 652 | goto done; |
|
653 | 653 | } |
|
654 | 654 | /* Put the phase information of all the roots in phases */ |
|
655 | 655 | numphase = PyList_GET_SIZE(roots)+1; |
|
656 | 656 | minrevallphases = len + 1; |
|
657 | 657 | phasessetlist = PyList_New(numphase); |
|
658 | 658 | if (phasessetlist == NULL) |
|
659 | 659 | goto done; |
|
660 | 660 | |
|
661 | 661 | PyList_SET_ITEM(phasessetlist, 0, Py_None); |
|
662 | 662 | Py_INCREF(Py_None); |
|
663 | 663 | |
|
664 | 664 | for (i = 0; i < numphase-1; i++) { |
|
665 | 665 | phaseroots = PyList_GET_ITEM(roots, i); |
|
666 | 666 | phaseset = PySet_New(NULL); |
|
667 | 667 | if (phaseset == NULL) |
|
668 | 668 | goto release; |
|
669 | 669 | PyList_SET_ITEM(phasessetlist, i+1, phaseset); |
|
670 | 670 | if (!PyList_Check(phaseroots)) |
|
671 | 671 | goto release; |
|
672 | 672 | minrevphase = add_roots_get_min(self, phaseroots, i+1, phases); |
|
673 | 673 | if (minrevphase == -2) /* Error from add_roots_get_min */ |
|
674 | 674 | goto release; |
|
675 | 675 | minrevallphases = MIN(minrevallphases, minrevphase); |
|
676 | 676 | } |
|
677 | 677 | /* Propagate the phase information from the roots to the revs */ |
|
678 | 678 | if (minrevallphases != -1) { |
|
679 | 679 | int parents[2]; |
|
680 | 680 | for (i = minrevallphases; i < len; i++) { |
|
681 | 681 | if (index_get_parents(self, i, parents, |
|
682 | 682 | (int)len - 1) < 0) |
|
683 | 683 | goto release; |
|
684 | 684 | set_phase_from_parents(phases, parents[0], parents[1], i); |
|
685 | 685 | } |
|
686 | 686 | } |
|
687 | 687 | /* Transform phase list to a python list */ |
|
688 |
phases |
|
|
689 |
if (phases |
|
|
688 | phasessize = PyInt_FromLong(len); | |
|
689 | if (phasessize == NULL) | |
|
690 | 690 | goto release; |
|
691 | 691 | for (i = 0; i < len; i++) { |
|
692 | PyObject *phaseval; | |
|
693 | ||
|
694 | 692 | phase = phases[i]; |
|
695 | 693 | /* We only store the sets of phase for non public phase, the public phase |
|
696 | 694 | * is computed as a difference */ |
|
697 | 695 | if (phase != 0) { |
|
698 | 696 | phaseset = PyList_GET_ITEM(phasessetlist, phase); |
|
699 | 697 | rev = PyInt_FromLong(i); |
|
700 | 698 | if (rev == NULL) |
|
701 | 699 | goto release; |
|
702 | 700 | PySet_Add(phaseset, rev); |
|
703 | 701 | Py_XDECREF(rev); |
|
704 | 702 | } |
|
705 | phaseval = PyInt_FromLong(phase); | |
|
706 | if (phaseval == NULL) | |
|
707 | goto release; | |
|
708 | PyList_SET_ITEM(phaseslist, i, phaseval); | |
|
709 | 703 | } |
|
710 |
ret = PyTuple_Pack(2, phases |
|
|
704 | ret = PyTuple_Pack(2, phasessize, phasessetlist); | |
|
711 | 705 | |
|
712 | 706 | release: |
|
713 |
Py_XDECREF(phases |
|
|
707 | Py_XDECREF(phasessize); | |
|
714 | 708 | Py_XDECREF(phasessetlist); |
|
715 | 709 | done: |
|
716 | 710 | free(phases); |
|
717 | 711 | return ret; |
|
718 | 712 | } |
|
719 | 713 | |
|
720 | 714 | static PyObject *index_headrevs(indexObject *self, PyObject *args) |
|
721 | 715 | { |
|
722 | 716 | Py_ssize_t i, j, len; |
|
723 | 717 | char *nothead = NULL; |
|
724 | 718 | PyObject *heads = NULL; |
|
725 | 719 | PyObject *filter = NULL; |
|
726 | 720 | PyObject *filteredrevs = Py_None; |
|
727 | 721 | |
|
728 | 722 | if (!PyArg_ParseTuple(args, "|O", &filteredrevs)) { |
|
729 | 723 | return NULL; |
|
730 | 724 | } |
|
731 | 725 | |
|
732 | 726 | if (self->headrevs && filteredrevs == self->filteredrevs) |
|
733 | 727 | return list_copy(self->headrevs); |
|
734 | 728 | |
|
735 | 729 | Py_DECREF(self->filteredrevs); |
|
736 | 730 | self->filteredrevs = filteredrevs; |
|
737 | 731 | Py_INCREF(filteredrevs); |
|
738 | 732 | |
|
739 | 733 | if (filteredrevs != Py_None) { |
|
740 | 734 | filter = PyObject_GetAttrString(filteredrevs, "__contains__"); |
|
741 | 735 | if (!filter) { |
|
742 | 736 | PyErr_SetString(PyExc_TypeError, |
|
743 | 737 | "filteredrevs has no attribute __contains__"); |
|
744 | 738 | goto bail; |
|
745 | 739 | } |
|
746 | 740 | } |
|
747 | 741 | |
|
748 | 742 | len = index_length(self) - 1; |
|
749 | 743 | heads = PyList_New(0); |
|
750 | 744 | if (heads == NULL) |
|
751 | 745 | goto bail; |
|
752 | 746 | if (len == 0) { |
|
753 | 747 | PyObject *nullid = PyInt_FromLong(-1); |
|
754 | 748 | if (nullid == NULL || PyList_Append(heads, nullid) == -1) { |
|
755 | 749 | Py_XDECREF(nullid); |
|
756 | 750 | goto bail; |
|
757 | 751 | } |
|
758 | 752 | goto done; |
|
759 | 753 | } |
|
760 | 754 | |
|
761 | 755 | nothead = calloc(len, 1); |
|
762 | 756 | if (nothead == NULL) { |
|
763 | 757 | PyErr_NoMemory(); |
|
764 | 758 | goto bail; |
|
765 | 759 | } |
|
766 | 760 | |
|
767 | 761 | for (i = len - 1; i >= 0; i--) { |
|
768 | 762 | int isfiltered; |
|
769 | 763 | int parents[2]; |
|
770 | 764 | |
|
771 | 765 | /* If nothead[i] == 1, it means we've seen an unfiltered child of this |
|
772 | 766 | * node already, and therefore this node is not filtered. So we can skip |
|
773 | 767 | * the expensive check_filter step. |
|
774 | 768 | */ |
|
775 | 769 | if (nothead[i] != 1) { |
|
776 | 770 | isfiltered = check_filter(filter, i); |
|
777 | 771 | if (isfiltered == -1) { |
|
778 | 772 | PyErr_SetString(PyExc_TypeError, |
|
779 | 773 | "unable to check filter"); |
|
780 | 774 | goto bail; |
|
781 | 775 | } |
|
782 | 776 | |
|
783 | 777 | if (isfiltered) { |
|
784 | 778 | nothead[i] = 1; |
|
785 | 779 | continue; |
|
786 | 780 | } |
|
787 | 781 | } |
|
788 | 782 | |
|
789 | 783 | if (index_get_parents(self, i, parents, (int)len - 1) < 0) |
|
790 | 784 | goto bail; |
|
791 | 785 | for (j = 0; j < 2; j++) { |
|
792 | 786 | if (parents[j] >= 0) |
|
793 | 787 | nothead[parents[j]] = 1; |
|
794 | 788 | } |
|
795 | 789 | } |
|
796 | 790 | |
|
797 | 791 | for (i = 0; i < len; i++) { |
|
798 | 792 | PyObject *head; |
|
799 | 793 | |
|
800 | 794 | if (nothead[i]) |
|
801 | 795 | continue; |
|
802 | 796 | head = PyInt_FromSsize_t(i); |
|
803 | 797 | if (head == NULL || PyList_Append(heads, head) == -1) { |
|
804 | 798 | Py_XDECREF(head); |
|
805 | 799 | goto bail; |
|
806 | 800 | } |
|
807 | 801 | } |
|
808 | 802 | |
|
809 | 803 | done: |
|
810 | 804 | self->headrevs = heads; |
|
811 | 805 | Py_XDECREF(filter); |
|
812 | 806 | free(nothead); |
|
813 | 807 | return list_copy(self->headrevs); |
|
814 | 808 | bail: |
|
815 | 809 | Py_XDECREF(filter); |
|
816 | 810 | Py_XDECREF(heads); |
|
817 | 811 | free(nothead); |
|
818 | 812 | return NULL; |
|
819 | 813 | } |
|
820 | 814 | |
|
821 | 815 | /** |
|
822 | 816 | * Obtain the base revision index entry. |
|
823 | 817 | * |
|
824 | 818 | * Callers must ensure that rev >= 0 or illegal memory access may occur. |
|
825 | 819 | */ |
|
826 | 820 | static inline int index_baserev(indexObject *self, int rev) |
|
827 | 821 | { |
|
828 | 822 | const char *data; |
|
829 | 823 | |
|
830 | 824 | if (rev >= self->length - 1) { |
|
831 | 825 | PyObject *tuple = PyList_GET_ITEM(self->added, |
|
832 | 826 | rev - self->length + 1); |
|
833 | 827 | return (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 3)); |
|
834 | 828 | } |
|
835 | 829 | else { |
|
836 | 830 | data = index_deref(self, rev); |
|
837 | 831 | if (data == NULL) { |
|
838 | 832 | return -2; |
|
839 | 833 | } |
|
840 | 834 | |
|
841 | 835 | return getbe32(data + 16); |
|
842 | 836 | } |
|
843 | 837 | } |
|
844 | 838 | |
|
845 | 839 | static PyObject *index_deltachain(indexObject *self, PyObject *args) |
|
846 | 840 | { |
|
847 | 841 | int rev, generaldelta; |
|
848 | 842 | PyObject *stoparg; |
|
849 | 843 | int stoprev, iterrev, baserev = -1; |
|
850 | 844 | int stopped; |
|
851 | 845 | PyObject *chain = NULL, *result = NULL; |
|
852 | 846 | const Py_ssize_t length = index_length(self); |
|
853 | 847 | |
|
854 | 848 | if (!PyArg_ParseTuple(args, "iOi", &rev, &stoparg, &generaldelta)) { |
|
855 | 849 | return NULL; |
|
856 | 850 | } |
|
857 | 851 | |
|
858 | 852 | if (PyInt_Check(stoparg)) { |
|
859 | 853 | stoprev = (int)PyInt_AsLong(stoparg); |
|
860 | 854 | if (stoprev == -1 && PyErr_Occurred()) { |
|
861 | 855 | return NULL; |
|
862 | 856 | } |
|
863 | 857 | } |
|
864 | 858 | else if (stoparg == Py_None) { |
|
865 | 859 | stoprev = -2; |
|
866 | 860 | } |
|
867 | 861 | else { |
|
868 | 862 | PyErr_SetString(PyExc_ValueError, |
|
869 | 863 | "stoprev must be integer or None"); |
|
870 | 864 | return NULL; |
|
871 | 865 | } |
|
872 | 866 | |
|
873 | 867 | if (rev < 0 || rev >= length - 1) { |
|
874 | 868 | PyErr_SetString(PyExc_ValueError, "revlog index out of range"); |
|
875 | 869 | return NULL; |
|
876 | 870 | } |
|
877 | 871 | |
|
878 | 872 | chain = PyList_New(0); |
|
879 | 873 | if (chain == NULL) { |
|
880 | 874 | return NULL; |
|
881 | 875 | } |
|
882 | 876 | |
|
883 | 877 | baserev = index_baserev(self, rev); |
|
884 | 878 | |
|
885 | 879 | /* This should never happen. */ |
|
886 | 880 | if (baserev <= -2) { |
|
887 | 881 | /* Error should be set by index_deref() */ |
|
888 | 882 | assert(PyErr_Occurred()); |
|
889 | 883 | goto bail; |
|
890 | 884 | } |
|
891 | 885 | |
|
892 | 886 | iterrev = rev; |
|
893 | 887 | |
|
894 | 888 | while (iterrev != baserev && iterrev != stoprev) { |
|
895 | 889 | PyObject *value = PyInt_FromLong(iterrev); |
|
896 | 890 | if (value == NULL) { |
|
897 | 891 | goto bail; |
|
898 | 892 | } |
|
899 | 893 | if (PyList_Append(chain, value)) { |
|
900 | 894 | Py_DECREF(value); |
|
901 | 895 | goto bail; |
|
902 | 896 | } |
|
903 | 897 | Py_DECREF(value); |
|
904 | 898 | |
|
905 | 899 | if (generaldelta) { |
|
906 | 900 | iterrev = baserev; |
|
907 | 901 | } |
|
908 | 902 | else { |
|
909 | 903 | iterrev--; |
|
910 | 904 | } |
|
911 | 905 | |
|
912 | 906 | if (iterrev < 0) { |
|
913 | 907 | break; |
|
914 | 908 | } |
|
915 | 909 | |
|
916 | 910 | if (iterrev >= length - 1) { |
|
917 | 911 | PyErr_SetString(PyExc_IndexError, "revision outside index"); |
|
918 | 912 | return NULL; |
|
919 | 913 | } |
|
920 | 914 | |
|
921 | 915 | baserev = index_baserev(self, iterrev); |
|
922 | 916 | |
|
923 | 917 | /* This should never happen. */ |
|
924 | 918 | if (baserev <= -2) { |
|
925 | 919 | /* Error should be set by index_deref() */ |
|
926 | 920 | assert(PyErr_Occurred()); |
|
927 | 921 | goto bail; |
|
928 | 922 | } |
|
929 | 923 | } |
|
930 | 924 | |
|
931 | 925 | if (iterrev == stoprev) { |
|
932 | 926 | stopped = 1; |
|
933 | 927 | } |
|
934 | 928 | else { |
|
935 | 929 | PyObject *value = PyInt_FromLong(iterrev); |
|
936 | 930 | if (value == NULL) { |
|
937 | 931 | goto bail; |
|
938 | 932 | } |
|
939 | 933 | if (PyList_Append(chain, value)) { |
|
940 | 934 | Py_DECREF(value); |
|
941 | 935 | goto bail; |
|
942 | 936 | } |
|
943 | 937 | Py_DECREF(value); |
|
944 | 938 | |
|
945 | 939 | stopped = 0; |
|
946 | 940 | } |
|
947 | 941 | |
|
948 | 942 | if (PyList_Reverse(chain)) { |
|
949 | 943 | goto bail; |
|
950 | 944 | } |
|
951 | 945 | |
|
952 | 946 | result = Py_BuildValue("OO", chain, stopped ? Py_True : Py_False); |
|
953 | 947 | Py_DECREF(chain); |
|
954 | 948 | return result; |
|
955 | 949 | |
|
956 | 950 | bail: |
|
957 | 951 | Py_DECREF(chain); |
|
958 | 952 | return NULL; |
|
959 | 953 | } |
|
960 | 954 | |
|
961 | 955 | static inline int nt_level(const char *node, Py_ssize_t level) |
|
962 | 956 | { |
|
963 | 957 | int v = node[level>>1]; |
|
964 | 958 | if (!(level & 1)) |
|
965 | 959 | v >>= 4; |
|
966 | 960 | return v & 0xf; |
|
967 | 961 | } |
|
968 | 962 | |
|
969 | 963 | /* |
|
970 | 964 | * Return values: |
|
971 | 965 | * |
|
972 | 966 | * -4: match is ambiguous (multiple candidates) |
|
973 | 967 | * -2: not found |
|
974 | 968 | * rest: valid rev |
|
975 | 969 | */ |
|
976 | 970 | static int nt_find(indexObject *self, const char *node, Py_ssize_t nodelen, |
|
977 | 971 | int hex) |
|
978 | 972 | { |
|
979 | 973 | int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level; |
|
980 | 974 | int level, maxlevel, off; |
|
981 | 975 | |
|
982 | 976 | if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0) |
|
983 | 977 | return -1; |
|
984 | 978 | |
|
985 | 979 | if (self->nt == NULL) |
|
986 | 980 | return -2; |
|
987 | 981 | |
|
988 | 982 | if (hex) |
|
989 | 983 | maxlevel = nodelen > 40 ? 40 : (int)nodelen; |
|
990 | 984 | else |
|
991 | 985 | maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2); |
|
992 | 986 | |
|
993 | 987 | for (level = off = 0; level < maxlevel; level++) { |
|
994 | 988 | int k = getnybble(node, level); |
|
995 | 989 | nodetree *n = &self->nt[off]; |
|
996 | 990 | int v = n->children[k]; |
|
997 | 991 | |
|
998 | 992 | if (v < 0) { |
|
999 | 993 | const char *n; |
|
1000 | 994 | Py_ssize_t i; |
|
1001 | 995 | |
|
1002 | 996 | v = -(v + 1); |
|
1003 | 997 | n = index_node(self, v); |
|
1004 | 998 | if (n == NULL) |
|
1005 | 999 | return -2; |
|
1006 | 1000 | for (i = level; i < maxlevel; i++) |
|
1007 | 1001 | if (getnybble(node, i) != nt_level(n, i)) |
|
1008 | 1002 | return -2; |
|
1009 | 1003 | return v; |
|
1010 | 1004 | } |
|
1011 | 1005 | if (v == 0) |
|
1012 | 1006 | return -2; |
|
1013 | 1007 | off = v; |
|
1014 | 1008 | } |
|
1015 | 1009 | /* multiple matches against an ambiguous prefix */ |
|
1016 | 1010 | return -4; |
|
1017 | 1011 | } |
|
1018 | 1012 | |
|
1019 | 1013 | static int nt_new(indexObject *self) |
|
1020 | 1014 | { |
|
1021 | 1015 | if (self->ntlength == self->ntcapacity) { |
|
1022 | 1016 | if (self->ntcapacity >= INT_MAX / (sizeof(nodetree) * 2)) { |
|
1023 | 1017 | PyErr_SetString(PyExc_MemoryError, |
|
1024 | 1018 | "overflow in nt_new"); |
|
1025 | 1019 | return -1; |
|
1026 | 1020 | } |
|
1027 | 1021 | self->ntcapacity *= 2; |
|
1028 | 1022 | self->nt = realloc(self->nt, |
|
1029 | 1023 | self->ntcapacity * sizeof(nodetree)); |
|
1030 | 1024 | if (self->nt == NULL) { |
|
1031 | 1025 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
|
1032 | 1026 | return -1; |
|
1033 | 1027 | } |
|
1034 | 1028 | memset(&self->nt[self->ntlength], 0, |
|
1035 | 1029 | sizeof(nodetree) * (self->ntcapacity - self->ntlength)); |
|
1036 | 1030 | } |
|
1037 | 1031 | return self->ntlength++; |
|
1038 | 1032 | } |
|
1039 | 1033 | |
|
1040 | 1034 | static int nt_insert(indexObject *self, const char *node, int rev) |
|
1041 | 1035 | { |
|
1042 | 1036 | int level = 0; |
|
1043 | 1037 | int off = 0; |
|
1044 | 1038 | |
|
1045 | 1039 | while (level < 40) { |
|
1046 | 1040 | int k = nt_level(node, level); |
|
1047 | 1041 | nodetree *n; |
|
1048 | 1042 | int v; |
|
1049 | 1043 | |
|
1050 | 1044 | n = &self->nt[off]; |
|
1051 | 1045 | v = n->children[k]; |
|
1052 | 1046 | |
|
1053 | 1047 | if (v == 0) { |
|
1054 | 1048 | n->children[k] = -rev - 1; |
|
1055 | 1049 | return 0; |
|
1056 | 1050 | } |
|
1057 | 1051 | if (v < 0) { |
|
1058 | 1052 | const char *oldnode = index_node(self, -(v + 1)); |
|
1059 | 1053 | int noff; |
|
1060 | 1054 | |
|
1061 | 1055 | if (!oldnode || !memcmp(oldnode, node, 20)) { |
|
1062 | 1056 | n->children[k] = -rev - 1; |
|
1063 | 1057 | return 0; |
|
1064 | 1058 | } |
|
1065 | 1059 | noff = nt_new(self); |
|
1066 | 1060 | if (noff == -1) |
|
1067 | 1061 | return -1; |
|
1068 | 1062 | /* self->nt may have been changed by realloc */ |
|
1069 | 1063 | self->nt[off].children[k] = noff; |
|
1070 | 1064 | off = noff; |
|
1071 | 1065 | n = &self->nt[off]; |
|
1072 | 1066 | n->children[nt_level(oldnode, ++level)] = v; |
|
1073 | 1067 | if (level > self->ntdepth) |
|
1074 | 1068 | self->ntdepth = level; |
|
1075 | 1069 | self->ntsplits += 1; |
|
1076 | 1070 | } else { |
|
1077 | 1071 | level += 1; |
|
1078 | 1072 | off = v; |
|
1079 | 1073 | } |
|
1080 | 1074 | } |
|
1081 | 1075 | |
|
1082 | 1076 | return -1; |
|
1083 | 1077 | } |
|
1084 | 1078 | |
|
1085 | 1079 | static int nt_init(indexObject *self) |
|
1086 | 1080 | { |
|
1087 | 1081 | if (self->nt == NULL) { |
|
1088 | 1082 | if ((size_t)self->raw_length > INT_MAX / sizeof(nodetree)) { |
|
1089 | 1083 | PyErr_SetString(PyExc_ValueError, "overflow in nt_init"); |
|
1090 | 1084 | return -1; |
|
1091 | 1085 | } |
|
1092 | 1086 | self->ntcapacity = self->raw_length < 4 |
|
1093 | 1087 | ? 4 : (int)self->raw_length / 2; |
|
1094 | 1088 | |
|
1095 | 1089 | self->nt = calloc(self->ntcapacity, sizeof(nodetree)); |
|
1096 | 1090 | if (self->nt == NULL) { |
|
1097 | 1091 | PyErr_NoMemory(); |
|
1098 | 1092 | return -1; |
|
1099 | 1093 | } |
|
1100 | 1094 | self->ntlength = 1; |
|
1101 | 1095 | self->ntrev = (int)index_length(self) - 1; |
|
1102 | 1096 | self->ntlookups = 1; |
|
1103 | 1097 | self->ntmisses = 0; |
|
1104 | 1098 | if (nt_insert(self, nullid, INT_MAX) == -1) |
|
1105 | 1099 | return -1; |
|
1106 | 1100 | } |
|
1107 | 1101 | return 0; |
|
1108 | 1102 | } |
|
1109 | 1103 | |
|
1110 | 1104 | /* |
|
1111 | 1105 | * Return values: |
|
1112 | 1106 | * |
|
1113 | 1107 | * -3: error (exception set) |
|
1114 | 1108 | * -2: not found (no exception set) |
|
1115 | 1109 | * rest: valid rev |
|
1116 | 1110 | */ |
|
1117 | 1111 | static int index_find_node(indexObject *self, |
|
1118 | 1112 | const char *node, Py_ssize_t nodelen) |
|
1119 | 1113 | { |
|
1120 | 1114 | int rev; |
|
1121 | 1115 | |
|
1122 | 1116 | self->ntlookups++; |
|
1123 | 1117 | rev = nt_find(self, node, nodelen, 0); |
|
1124 | 1118 | if (rev >= -1) |
|
1125 | 1119 | return rev; |
|
1126 | 1120 | |
|
1127 | 1121 | if (nt_init(self) == -1) |
|
1128 | 1122 | return -3; |
|
1129 | 1123 | |
|
1130 | 1124 | /* |
|
1131 | 1125 | * For the first handful of lookups, we scan the entire index, |
|
1132 | 1126 | * and cache only the matching nodes. This optimizes for cases |
|
1133 | 1127 | * like "hg tip", where only a few nodes are accessed. |
|
1134 | 1128 | * |
|
1135 | 1129 | * After that, we cache every node we visit, using a single |
|
1136 | 1130 | * scan amortized over multiple lookups. This gives the best |
|
1137 | 1131 | * bulk performance, e.g. for "hg log". |
|
1138 | 1132 | */ |
|
1139 | 1133 | if (self->ntmisses++ < 4) { |
|
1140 | 1134 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
1141 | 1135 | const char *n = index_node(self, rev); |
|
1142 | 1136 | if (n == NULL) |
|
1143 | 1137 | return -2; |
|
1144 | 1138 | if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) { |
|
1145 | 1139 | if (nt_insert(self, n, rev) == -1) |
|
1146 | 1140 | return -3; |
|
1147 | 1141 | break; |
|
1148 | 1142 | } |
|
1149 | 1143 | } |
|
1150 | 1144 | } else { |
|
1151 | 1145 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
1152 | 1146 | const char *n = index_node(self, rev); |
|
1153 | 1147 | if (n == NULL) { |
|
1154 | 1148 | self->ntrev = rev + 1; |
|
1155 | 1149 | return -2; |
|
1156 | 1150 | } |
|
1157 | 1151 | if (nt_insert(self, n, rev) == -1) { |
|
1158 | 1152 | self->ntrev = rev + 1; |
|
1159 | 1153 | return -3; |
|
1160 | 1154 | } |
|
1161 | 1155 | if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) { |
|
1162 | 1156 | break; |
|
1163 | 1157 | } |
|
1164 | 1158 | } |
|
1165 | 1159 | self->ntrev = rev; |
|
1166 | 1160 | } |
|
1167 | 1161 | |
|
1168 | 1162 | if (rev >= 0) |
|
1169 | 1163 | return rev; |
|
1170 | 1164 | return -2; |
|
1171 | 1165 | } |
|
1172 | 1166 | |
|
1173 | 1167 | static void raise_revlog_error(void) |
|
1174 | 1168 | { |
|
1175 | 1169 | PyObject *mod = NULL, *dict = NULL, *errclass = NULL; |
|
1176 | 1170 | |
|
1177 | 1171 | mod = PyImport_ImportModule("mercurial.error"); |
|
1178 | 1172 | if (mod == NULL) { |
|
1179 | 1173 | goto cleanup; |
|
1180 | 1174 | } |
|
1181 | 1175 | |
|
1182 | 1176 | dict = PyModule_GetDict(mod); |
|
1183 | 1177 | if (dict == NULL) { |
|
1184 | 1178 | goto cleanup; |
|
1185 | 1179 | } |
|
1186 | 1180 | Py_INCREF(dict); |
|
1187 | 1181 | |
|
1188 | 1182 | errclass = PyDict_GetItemString(dict, "RevlogError"); |
|
1189 | 1183 | if (errclass == NULL) { |
|
1190 | 1184 | PyErr_SetString(PyExc_SystemError, |
|
1191 | 1185 | "could not find RevlogError"); |
|
1192 | 1186 | goto cleanup; |
|
1193 | 1187 | } |
|
1194 | 1188 | |
|
1195 | 1189 | /* value of exception is ignored by callers */ |
|
1196 | 1190 | PyErr_SetString(errclass, "RevlogError"); |
|
1197 | 1191 | |
|
1198 | 1192 | cleanup: |
|
1199 | 1193 | Py_XDECREF(dict); |
|
1200 | 1194 | Py_XDECREF(mod); |
|
1201 | 1195 | } |
|
1202 | 1196 | |
|
1203 | 1197 | static PyObject *index_getitem(indexObject *self, PyObject *value) |
|
1204 | 1198 | { |
|
1205 | 1199 | char *node; |
|
1206 | 1200 | Py_ssize_t nodelen; |
|
1207 | 1201 | int rev; |
|
1208 | 1202 | |
|
1209 | 1203 | if (PyInt_Check(value)) |
|
1210 | 1204 | return index_get(self, PyInt_AS_LONG(value)); |
|
1211 | 1205 | |
|
1212 | 1206 | if (node_check(value, &node, &nodelen) == -1) |
|
1213 | 1207 | return NULL; |
|
1214 | 1208 | rev = index_find_node(self, node, nodelen); |
|
1215 | 1209 | if (rev >= -1) |
|
1216 | 1210 | return PyInt_FromLong(rev); |
|
1217 | 1211 | if (rev == -2) |
|
1218 | 1212 | raise_revlog_error(); |
|
1219 | 1213 | return NULL; |
|
1220 | 1214 | } |
|
1221 | 1215 | |
|
1222 | 1216 | static int nt_partialmatch(indexObject *self, const char *node, |
|
1223 | 1217 | Py_ssize_t nodelen) |
|
1224 | 1218 | { |
|
1225 | 1219 | int rev; |
|
1226 | 1220 | |
|
1227 | 1221 | if (nt_init(self) == -1) |
|
1228 | 1222 | return -3; |
|
1229 | 1223 | |
|
1230 | 1224 | if (self->ntrev > 0) { |
|
1231 | 1225 | /* ensure that the radix tree is fully populated */ |
|
1232 | 1226 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
1233 | 1227 | const char *n = index_node(self, rev); |
|
1234 | 1228 | if (n == NULL) |
|
1235 | 1229 | return -2; |
|
1236 | 1230 | if (nt_insert(self, n, rev) == -1) |
|
1237 | 1231 | return -3; |
|
1238 | 1232 | } |
|
1239 | 1233 | self->ntrev = rev; |
|
1240 | 1234 | } |
|
1241 | 1235 | |
|
1242 | 1236 | return nt_find(self, node, nodelen, 1); |
|
1243 | 1237 | } |
|
1244 | 1238 | |
|
1245 | 1239 | static PyObject *index_partialmatch(indexObject *self, PyObject *args) |
|
1246 | 1240 | { |
|
1247 | 1241 | const char *fullnode; |
|
1248 | 1242 | int nodelen; |
|
1249 | 1243 | char *node; |
|
1250 | 1244 | int rev, i; |
|
1251 | 1245 | |
|
1252 | 1246 | if (!PyArg_ParseTuple(args, "s#", &node, &nodelen)) |
|
1253 | 1247 | return NULL; |
|
1254 | 1248 | |
|
1255 | 1249 | if (nodelen < 4) { |
|
1256 | 1250 | PyErr_SetString(PyExc_ValueError, "key too short"); |
|
1257 | 1251 | return NULL; |
|
1258 | 1252 | } |
|
1259 | 1253 | |
|
1260 | 1254 | if (nodelen > 40) { |
|
1261 | 1255 | PyErr_SetString(PyExc_ValueError, "key too long"); |
|
1262 | 1256 | return NULL; |
|
1263 | 1257 | } |
|
1264 | 1258 | |
|
1265 | 1259 | for (i = 0; i < nodelen; i++) |
|
1266 | 1260 | hexdigit(node, i); |
|
1267 | 1261 | if (PyErr_Occurred()) { |
|
1268 | 1262 | /* input contains non-hex characters */ |
|
1269 | 1263 | PyErr_Clear(); |
|
1270 | 1264 | Py_RETURN_NONE; |
|
1271 | 1265 | } |
|
1272 | 1266 | |
|
1273 | 1267 | rev = nt_partialmatch(self, node, nodelen); |
|
1274 | 1268 | |
|
1275 | 1269 | switch (rev) { |
|
1276 | 1270 | case -4: |
|
1277 | 1271 | raise_revlog_error(); |
|
1278 | 1272 | case -3: |
|
1279 | 1273 | return NULL; |
|
1280 | 1274 | case -2: |
|
1281 | 1275 | Py_RETURN_NONE; |
|
1282 | 1276 | case -1: |
|
1283 | 1277 | return PyBytes_FromStringAndSize(nullid, 20); |
|
1284 | 1278 | } |
|
1285 | 1279 | |
|
1286 | 1280 | fullnode = index_node(self, rev); |
|
1287 | 1281 | if (fullnode == NULL) { |
|
1288 | 1282 | PyErr_Format(PyExc_IndexError, |
|
1289 | 1283 | "could not access rev %d", rev); |
|
1290 | 1284 | return NULL; |
|
1291 | 1285 | } |
|
1292 | 1286 | return PyBytes_FromStringAndSize(fullnode, 20); |
|
1293 | 1287 | } |
|
1294 | 1288 | |
|
1295 | 1289 | static PyObject *index_m_get(indexObject *self, PyObject *args) |
|
1296 | 1290 | { |
|
1297 | 1291 | Py_ssize_t nodelen; |
|
1298 | 1292 | PyObject *val; |
|
1299 | 1293 | char *node; |
|
1300 | 1294 | int rev; |
|
1301 | 1295 | |
|
1302 | 1296 | if (!PyArg_ParseTuple(args, "O", &val)) |
|
1303 | 1297 | return NULL; |
|
1304 | 1298 | if (node_check(val, &node, &nodelen) == -1) |
|
1305 | 1299 | return NULL; |
|
1306 | 1300 | rev = index_find_node(self, node, nodelen); |
|
1307 | 1301 | if (rev == -3) |
|
1308 | 1302 | return NULL; |
|
1309 | 1303 | if (rev == -2) |
|
1310 | 1304 | Py_RETURN_NONE; |
|
1311 | 1305 | return PyInt_FromLong(rev); |
|
1312 | 1306 | } |
|
1313 | 1307 | |
|
1314 | 1308 | static int index_contains(indexObject *self, PyObject *value) |
|
1315 | 1309 | { |
|
1316 | 1310 | char *node; |
|
1317 | 1311 | Py_ssize_t nodelen; |
|
1318 | 1312 | |
|
1319 | 1313 | if (PyInt_Check(value)) { |
|
1320 | 1314 | long rev = PyInt_AS_LONG(value); |
|
1321 | 1315 | return rev >= -1 && rev < index_length(self); |
|
1322 | 1316 | } |
|
1323 | 1317 | |
|
1324 | 1318 | if (node_check(value, &node, &nodelen) == -1) |
|
1325 | 1319 | return -1; |
|
1326 | 1320 | |
|
1327 | 1321 | switch (index_find_node(self, node, nodelen)) { |
|
1328 | 1322 | case -3: |
|
1329 | 1323 | return -1; |
|
1330 | 1324 | case -2: |
|
1331 | 1325 | return 0; |
|
1332 | 1326 | default: |
|
1333 | 1327 | return 1; |
|
1334 | 1328 | } |
|
1335 | 1329 | } |
|
1336 | 1330 | |
|
1337 | 1331 | typedef uint64_t bitmask; |
|
1338 | 1332 | |
|
1339 | 1333 | /* |
|
1340 | 1334 | * Given a disjoint set of revs, return all candidates for the |
|
1341 | 1335 | * greatest common ancestor. In revset notation, this is the set |
|
1342 | 1336 | * "heads(::a and ::b and ...)" |
|
1343 | 1337 | */ |
|
1344 | 1338 | static PyObject *find_gca_candidates(indexObject *self, const int *revs, |
|
1345 | 1339 | int revcount) |
|
1346 | 1340 | { |
|
1347 | 1341 | const bitmask allseen = (1ull << revcount) - 1; |
|
1348 | 1342 | const bitmask poison = 1ull << revcount; |
|
1349 | 1343 | PyObject *gca = PyList_New(0); |
|
1350 | 1344 | int i, v, interesting; |
|
1351 | 1345 | int maxrev = -1; |
|
1352 | 1346 | bitmask sp; |
|
1353 | 1347 | bitmask *seen; |
|
1354 | 1348 | |
|
1355 | 1349 | if (gca == NULL) |
|
1356 | 1350 | return PyErr_NoMemory(); |
|
1357 | 1351 | |
|
1358 | 1352 | for (i = 0; i < revcount; i++) { |
|
1359 | 1353 | if (revs[i] > maxrev) |
|
1360 | 1354 | maxrev = revs[i]; |
|
1361 | 1355 | } |
|
1362 | 1356 | |
|
1363 | 1357 | seen = calloc(sizeof(*seen), maxrev + 1); |
|
1364 | 1358 | if (seen == NULL) { |
|
1365 | 1359 | Py_DECREF(gca); |
|
1366 | 1360 | return PyErr_NoMemory(); |
|
1367 | 1361 | } |
|
1368 | 1362 | |
|
1369 | 1363 | for (i = 0; i < revcount; i++) |
|
1370 | 1364 | seen[revs[i]] = 1ull << i; |
|
1371 | 1365 | |
|
1372 | 1366 | interesting = revcount; |
|
1373 | 1367 | |
|
1374 | 1368 | for (v = maxrev; v >= 0 && interesting; v--) { |
|
1375 | 1369 | bitmask sv = seen[v]; |
|
1376 | 1370 | int parents[2]; |
|
1377 | 1371 | |
|
1378 | 1372 | if (!sv) |
|
1379 | 1373 | continue; |
|
1380 | 1374 | |
|
1381 | 1375 | if (sv < poison) { |
|
1382 | 1376 | interesting -= 1; |
|
1383 | 1377 | if (sv == allseen) { |
|
1384 | 1378 | PyObject *obj = PyInt_FromLong(v); |
|
1385 | 1379 | if (obj == NULL) |
|
1386 | 1380 | goto bail; |
|
1387 | 1381 | if (PyList_Append(gca, obj) == -1) { |
|
1388 | 1382 | Py_DECREF(obj); |
|
1389 | 1383 | goto bail; |
|
1390 | 1384 | } |
|
1391 | 1385 | sv |= poison; |
|
1392 | 1386 | for (i = 0; i < revcount; i++) { |
|
1393 | 1387 | if (revs[i] == v) |
|
1394 | 1388 | goto done; |
|
1395 | 1389 | } |
|
1396 | 1390 | } |
|
1397 | 1391 | } |
|
1398 | 1392 | if (index_get_parents(self, v, parents, maxrev) < 0) |
|
1399 | 1393 | goto bail; |
|
1400 | 1394 | |
|
1401 | 1395 | for (i = 0; i < 2; i++) { |
|
1402 | 1396 | int p = parents[i]; |
|
1403 | 1397 | if (p == -1) |
|
1404 | 1398 | continue; |
|
1405 | 1399 | sp = seen[p]; |
|
1406 | 1400 | if (sv < poison) { |
|
1407 | 1401 | if (sp == 0) { |
|
1408 | 1402 | seen[p] = sv; |
|
1409 | 1403 | interesting++; |
|
1410 | 1404 | } |
|
1411 | 1405 | else if (sp != sv) |
|
1412 | 1406 | seen[p] |= sv; |
|
1413 | 1407 | } else { |
|
1414 | 1408 | if (sp && sp < poison) |
|
1415 | 1409 | interesting--; |
|
1416 | 1410 | seen[p] = sv; |
|
1417 | 1411 | } |
|
1418 | 1412 | } |
|
1419 | 1413 | } |
|
1420 | 1414 | |
|
1421 | 1415 | done: |
|
1422 | 1416 | free(seen); |
|
1423 | 1417 | return gca; |
|
1424 | 1418 | bail: |
|
1425 | 1419 | free(seen); |
|
1426 | 1420 | Py_XDECREF(gca); |
|
1427 | 1421 | return NULL; |
|
1428 | 1422 | } |
|
1429 | 1423 | |
|
1430 | 1424 | /* |
|
1431 | 1425 | * Given a disjoint set of revs, return the subset with the longest |
|
1432 | 1426 | * path to the root. |
|
1433 | 1427 | */ |
|
1434 | 1428 | static PyObject *find_deepest(indexObject *self, PyObject *revs) |
|
1435 | 1429 | { |
|
1436 | 1430 | const Py_ssize_t revcount = PyList_GET_SIZE(revs); |
|
1437 | 1431 | static const Py_ssize_t capacity = 24; |
|
1438 | 1432 | int *depth, *interesting = NULL; |
|
1439 | 1433 | int i, j, v, ninteresting; |
|
1440 | 1434 | PyObject *dict = NULL, *keys = NULL; |
|
1441 | 1435 | long *seen = NULL; |
|
1442 | 1436 | int maxrev = -1; |
|
1443 | 1437 | long final; |
|
1444 | 1438 | |
|
1445 | 1439 | if (revcount > capacity) { |
|
1446 | 1440 | PyErr_Format(PyExc_OverflowError, |
|
1447 | 1441 | "bitset size (%ld) > capacity (%ld)", |
|
1448 | 1442 | (long)revcount, (long)capacity); |
|
1449 | 1443 | return NULL; |
|
1450 | 1444 | } |
|
1451 | 1445 | |
|
1452 | 1446 | for (i = 0; i < revcount; i++) { |
|
1453 | 1447 | int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i)); |
|
1454 | 1448 | if (n > maxrev) |
|
1455 | 1449 | maxrev = n; |
|
1456 | 1450 | } |
|
1457 | 1451 | |
|
1458 | 1452 | depth = calloc(sizeof(*depth), maxrev + 1); |
|
1459 | 1453 | if (depth == NULL) |
|
1460 | 1454 | return PyErr_NoMemory(); |
|
1461 | 1455 | |
|
1462 | 1456 | seen = calloc(sizeof(*seen), maxrev + 1); |
|
1463 | 1457 | if (seen == NULL) { |
|
1464 | 1458 | PyErr_NoMemory(); |
|
1465 | 1459 | goto bail; |
|
1466 | 1460 | } |
|
1467 | 1461 | |
|
1468 | 1462 | interesting = calloc(sizeof(*interesting), 1 << revcount); |
|
1469 | 1463 | if (interesting == NULL) { |
|
1470 | 1464 | PyErr_NoMemory(); |
|
1471 | 1465 | goto bail; |
|
1472 | 1466 | } |
|
1473 | 1467 | |
|
1474 | 1468 | if (PyList_Sort(revs) == -1) |
|
1475 | 1469 | goto bail; |
|
1476 | 1470 | |
|
1477 | 1471 | for (i = 0; i < revcount; i++) { |
|
1478 | 1472 | int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i)); |
|
1479 | 1473 | long b = 1l << i; |
|
1480 | 1474 | depth[n] = 1; |
|
1481 | 1475 | seen[n] = b; |
|
1482 | 1476 | interesting[b] = 1; |
|
1483 | 1477 | } |
|
1484 | 1478 | |
|
1485 | 1479 | /* invariant: ninteresting is the number of non-zero entries in |
|
1486 | 1480 | * interesting. */ |
|
1487 | 1481 | ninteresting = (int)revcount; |
|
1488 | 1482 | |
|
1489 | 1483 | for (v = maxrev; v >= 0 && ninteresting > 1; v--) { |
|
1490 | 1484 | int dv = depth[v]; |
|
1491 | 1485 | int parents[2]; |
|
1492 | 1486 | long sv; |
|
1493 | 1487 | |
|
1494 | 1488 | if (dv == 0) |
|
1495 | 1489 | continue; |
|
1496 | 1490 | |
|
1497 | 1491 | sv = seen[v]; |
|
1498 | 1492 | if (index_get_parents(self, v, parents, maxrev) < 0) |
|
1499 | 1493 | goto bail; |
|
1500 | 1494 | |
|
1501 | 1495 | for (i = 0; i < 2; i++) { |
|
1502 | 1496 | int p = parents[i]; |
|
1503 | 1497 | long sp; |
|
1504 | 1498 | int dp; |
|
1505 | 1499 | |
|
1506 | 1500 | if (p == -1) |
|
1507 | 1501 | continue; |
|
1508 | 1502 | |
|
1509 | 1503 | dp = depth[p]; |
|
1510 | 1504 | sp = seen[p]; |
|
1511 | 1505 | if (dp <= dv) { |
|
1512 | 1506 | depth[p] = dv + 1; |
|
1513 | 1507 | if (sp != sv) { |
|
1514 | 1508 | interesting[sv] += 1; |
|
1515 | 1509 | seen[p] = sv; |
|
1516 | 1510 | if (sp) { |
|
1517 | 1511 | interesting[sp] -= 1; |
|
1518 | 1512 | if (interesting[sp] == 0) |
|
1519 | 1513 | ninteresting -= 1; |
|
1520 | 1514 | } |
|
1521 | 1515 | } |
|
1522 | 1516 | } |
|
1523 | 1517 | else if (dv == dp - 1) { |
|
1524 | 1518 | long nsp = sp | sv; |
|
1525 | 1519 | if (nsp == sp) |
|
1526 | 1520 | continue; |
|
1527 | 1521 | seen[p] = nsp; |
|
1528 | 1522 | interesting[sp] -= 1; |
|
1529 | 1523 | if (interesting[sp] == 0) |
|
1530 | 1524 | ninteresting -= 1; |
|
1531 | 1525 | if (interesting[nsp] == 0) |
|
1532 | 1526 | ninteresting += 1; |
|
1533 | 1527 | interesting[nsp] += 1; |
|
1534 | 1528 | } |
|
1535 | 1529 | } |
|
1536 | 1530 | interesting[sv] -= 1; |
|
1537 | 1531 | if (interesting[sv] == 0) |
|
1538 | 1532 | ninteresting -= 1; |
|
1539 | 1533 | } |
|
1540 | 1534 | |
|
1541 | 1535 | final = 0; |
|
1542 | 1536 | j = ninteresting; |
|
1543 | 1537 | for (i = 0; i < (int)(2 << revcount) && j > 0; i++) { |
|
1544 | 1538 | if (interesting[i] == 0) |
|
1545 | 1539 | continue; |
|
1546 | 1540 | final |= i; |
|
1547 | 1541 | j -= 1; |
|
1548 | 1542 | } |
|
1549 | 1543 | if (final == 0) { |
|
1550 | 1544 | keys = PyList_New(0); |
|
1551 | 1545 | goto bail; |
|
1552 | 1546 | } |
|
1553 | 1547 | |
|
1554 | 1548 | dict = PyDict_New(); |
|
1555 | 1549 | if (dict == NULL) |
|
1556 | 1550 | goto bail; |
|
1557 | 1551 | |
|
1558 | 1552 | for (i = 0; i < revcount; i++) { |
|
1559 | 1553 | PyObject *key; |
|
1560 | 1554 | |
|
1561 | 1555 | if ((final & (1 << i)) == 0) |
|
1562 | 1556 | continue; |
|
1563 | 1557 | |
|
1564 | 1558 | key = PyList_GET_ITEM(revs, i); |
|
1565 | 1559 | Py_INCREF(key); |
|
1566 | 1560 | Py_INCREF(Py_None); |
|
1567 | 1561 | if (PyDict_SetItem(dict, key, Py_None) == -1) { |
|
1568 | 1562 | Py_DECREF(key); |
|
1569 | 1563 | Py_DECREF(Py_None); |
|
1570 | 1564 | goto bail; |
|
1571 | 1565 | } |
|
1572 | 1566 | } |
|
1573 | 1567 | |
|
1574 | 1568 | keys = PyDict_Keys(dict); |
|
1575 | 1569 | |
|
1576 | 1570 | bail: |
|
1577 | 1571 | free(depth); |
|
1578 | 1572 | free(seen); |
|
1579 | 1573 | free(interesting); |
|
1580 | 1574 | Py_XDECREF(dict); |
|
1581 | 1575 | |
|
1582 | 1576 | return keys; |
|
1583 | 1577 | } |
|
1584 | 1578 | |
|
1585 | 1579 | /* |
|
1586 | 1580 | * Given a (possibly overlapping) set of revs, return all the |
|
1587 | 1581 | * common ancestors heads: heads(::args[0] and ::a[1] and ...) |
|
1588 | 1582 | */ |
|
1589 | 1583 | static PyObject *index_commonancestorsheads(indexObject *self, PyObject *args) |
|
1590 | 1584 | { |
|
1591 | 1585 | PyObject *ret = NULL; |
|
1592 | 1586 | Py_ssize_t argcount, i, len; |
|
1593 | 1587 | bitmask repeat = 0; |
|
1594 | 1588 | int revcount = 0; |
|
1595 | 1589 | int *revs; |
|
1596 | 1590 | |
|
1597 | 1591 | argcount = PySequence_Length(args); |
|
1598 | 1592 | revs = PyMem_Malloc(argcount * sizeof(*revs)); |
|
1599 | 1593 | if (argcount > 0 && revs == NULL) |
|
1600 | 1594 | return PyErr_NoMemory(); |
|
1601 | 1595 | len = index_length(self) - 1; |
|
1602 | 1596 | |
|
1603 | 1597 | for (i = 0; i < argcount; i++) { |
|
1604 | 1598 | static const int capacity = 24; |
|
1605 | 1599 | PyObject *obj = PySequence_GetItem(args, i); |
|
1606 | 1600 | bitmask x; |
|
1607 | 1601 | long val; |
|
1608 | 1602 | |
|
1609 | 1603 | if (!PyInt_Check(obj)) { |
|
1610 | 1604 | PyErr_SetString(PyExc_TypeError, |
|
1611 | 1605 | "arguments must all be ints"); |
|
1612 | 1606 | Py_DECREF(obj); |
|
1613 | 1607 | goto bail; |
|
1614 | 1608 | } |
|
1615 | 1609 | val = PyInt_AsLong(obj); |
|
1616 | 1610 | Py_DECREF(obj); |
|
1617 | 1611 | if (val == -1) { |
|
1618 | 1612 | ret = PyList_New(0); |
|
1619 | 1613 | goto done; |
|
1620 | 1614 | } |
|
1621 | 1615 | if (val < 0 || val >= len) { |
|
1622 | 1616 | PyErr_SetString(PyExc_IndexError, |
|
1623 | 1617 | "index out of range"); |
|
1624 | 1618 | goto bail; |
|
1625 | 1619 | } |
|
1626 | 1620 | /* this cheesy bloom filter lets us avoid some more |
|
1627 | 1621 | * expensive duplicate checks in the common set-is-disjoint |
|
1628 | 1622 | * case */ |
|
1629 | 1623 | x = 1ull << (val & 0x3f); |
|
1630 | 1624 | if (repeat & x) { |
|
1631 | 1625 | int k; |
|
1632 | 1626 | for (k = 0; k < revcount; k++) { |
|
1633 | 1627 | if (val == revs[k]) |
|
1634 | 1628 | goto duplicate; |
|
1635 | 1629 | } |
|
1636 | 1630 | } |
|
1637 | 1631 | else repeat |= x; |
|
1638 | 1632 | if (revcount >= capacity) { |
|
1639 | 1633 | PyErr_Format(PyExc_OverflowError, |
|
1640 | 1634 | "bitset size (%d) > capacity (%d)", |
|
1641 | 1635 | revcount, capacity); |
|
1642 | 1636 | goto bail; |
|
1643 | 1637 | } |
|
1644 | 1638 | revs[revcount++] = (int)val; |
|
1645 | 1639 | duplicate:; |
|
1646 | 1640 | } |
|
1647 | 1641 | |
|
1648 | 1642 | if (revcount == 0) { |
|
1649 | 1643 | ret = PyList_New(0); |
|
1650 | 1644 | goto done; |
|
1651 | 1645 | } |
|
1652 | 1646 | if (revcount == 1) { |
|
1653 | 1647 | PyObject *obj; |
|
1654 | 1648 | ret = PyList_New(1); |
|
1655 | 1649 | if (ret == NULL) |
|
1656 | 1650 | goto bail; |
|
1657 | 1651 | obj = PyInt_FromLong(revs[0]); |
|
1658 | 1652 | if (obj == NULL) |
|
1659 | 1653 | goto bail; |
|
1660 | 1654 | PyList_SET_ITEM(ret, 0, obj); |
|
1661 | 1655 | goto done; |
|
1662 | 1656 | } |
|
1663 | 1657 | |
|
1664 | 1658 | ret = find_gca_candidates(self, revs, revcount); |
|
1665 | 1659 | if (ret == NULL) |
|
1666 | 1660 | goto bail; |
|
1667 | 1661 | |
|
1668 | 1662 | done: |
|
1669 | 1663 | PyMem_Free(revs); |
|
1670 | 1664 | return ret; |
|
1671 | 1665 | |
|
1672 | 1666 | bail: |
|
1673 | 1667 | PyMem_Free(revs); |
|
1674 | 1668 | Py_XDECREF(ret); |
|
1675 | 1669 | return NULL; |
|
1676 | 1670 | } |
|
1677 | 1671 | |
|
1678 | 1672 | /* |
|
1679 | 1673 | * Given a (possibly overlapping) set of revs, return the greatest |
|
1680 | 1674 | * common ancestors: those with the longest path to the root. |
|
1681 | 1675 | */ |
|
1682 | 1676 | static PyObject *index_ancestors(indexObject *self, PyObject *args) |
|
1683 | 1677 | { |
|
1684 | 1678 | PyObject *ret; |
|
1685 | 1679 | PyObject *gca = index_commonancestorsheads(self, args); |
|
1686 | 1680 | if (gca == NULL) |
|
1687 | 1681 | return NULL; |
|
1688 | 1682 | |
|
1689 | 1683 | if (PyList_GET_SIZE(gca) <= 1) { |
|
1690 | 1684 | return gca; |
|
1691 | 1685 | } |
|
1692 | 1686 | |
|
1693 | 1687 | ret = find_deepest(self, gca); |
|
1694 | 1688 | Py_DECREF(gca); |
|
1695 | 1689 | return ret; |
|
1696 | 1690 | } |
|
1697 | 1691 | |
|
1698 | 1692 | /* |
|
1699 | 1693 | * Invalidate any trie entries introduced by added revs. |
|
1700 | 1694 | */ |
|
1701 | 1695 | static void nt_invalidate_added(indexObject *self, Py_ssize_t start) |
|
1702 | 1696 | { |
|
1703 | 1697 | Py_ssize_t i, len = PyList_GET_SIZE(self->added); |
|
1704 | 1698 | |
|
1705 | 1699 | for (i = start; i < len; i++) { |
|
1706 | 1700 | PyObject *tuple = PyList_GET_ITEM(self->added, i); |
|
1707 | 1701 | PyObject *node = PyTuple_GET_ITEM(tuple, 7); |
|
1708 | 1702 | |
|
1709 | 1703 | nt_insert(self, PyBytes_AS_STRING(node), -1); |
|
1710 | 1704 | } |
|
1711 | 1705 | |
|
1712 | 1706 | if (start == 0) |
|
1713 | 1707 | Py_CLEAR(self->added); |
|
1714 | 1708 | } |
|
1715 | 1709 | |
|
1716 | 1710 | /* |
|
1717 | 1711 | * Delete a numeric range of revs, which must be at the end of the |
|
1718 | 1712 | * range, but exclude the sentinel nullid entry. |
|
1719 | 1713 | */ |
|
1720 | 1714 | static int index_slice_del(indexObject *self, PyObject *item) |
|
1721 | 1715 | { |
|
1722 | 1716 | Py_ssize_t start, stop, step, slicelength; |
|
1723 | 1717 | Py_ssize_t length = index_length(self); |
|
1724 | 1718 | int ret = 0; |
|
1725 | 1719 | |
|
1726 | 1720 | /* Argument changed from PySliceObject* to PyObject* in Python 3. */ |
|
1727 | 1721 | #ifdef IS_PY3K |
|
1728 | 1722 | if (PySlice_GetIndicesEx(item, length, |
|
1729 | 1723 | #else |
|
1730 | 1724 | if (PySlice_GetIndicesEx((PySliceObject*)item, length, |
|
1731 | 1725 | #endif |
|
1732 | 1726 | &start, &stop, &step, &slicelength) < 0) |
|
1733 | 1727 | return -1; |
|
1734 | 1728 | |
|
1735 | 1729 | if (slicelength <= 0) |
|
1736 | 1730 | return 0; |
|
1737 | 1731 | |
|
1738 | 1732 | if ((step < 0 && start < stop) || (step > 0 && start > stop)) |
|
1739 | 1733 | stop = start; |
|
1740 | 1734 | |
|
1741 | 1735 | if (step < 0) { |
|
1742 | 1736 | stop = start + 1; |
|
1743 | 1737 | start = stop + step*(slicelength - 1) - 1; |
|
1744 | 1738 | step = -step; |
|
1745 | 1739 | } |
|
1746 | 1740 | |
|
1747 | 1741 | if (step != 1) { |
|
1748 | 1742 | PyErr_SetString(PyExc_ValueError, |
|
1749 | 1743 | "revlog index delete requires step size of 1"); |
|
1750 | 1744 | return -1; |
|
1751 | 1745 | } |
|
1752 | 1746 | |
|
1753 | 1747 | if (stop != length - 1) { |
|
1754 | 1748 | PyErr_SetString(PyExc_IndexError, |
|
1755 | 1749 | "revlog index deletion indices are invalid"); |
|
1756 | 1750 | return -1; |
|
1757 | 1751 | } |
|
1758 | 1752 | |
|
1759 | 1753 | if (start < self->length - 1) { |
|
1760 | 1754 | if (self->nt) { |
|
1761 | 1755 | Py_ssize_t i; |
|
1762 | 1756 | |
|
1763 | 1757 | for (i = start + 1; i < self->length - 1; i++) { |
|
1764 | 1758 | const char *node = index_node(self, i); |
|
1765 | 1759 | |
|
1766 | 1760 | if (node) |
|
1767 | 1761 | nt_insert(self, node, -1); |
|
1768 | 1762 | } |
|
1769 | 1763 | if (self->added) |
|
1770 | 1764 | nt_invalidate_added(self, 0); |
|
1771 | 1765 | if (self->ntrev > start) |
|
1772 | 1766 | self->ntrev = (int)start; |
|
1773 | 1767 | } |
|
1774 | 1768 | self->length = start + 1; |
|
1775 | 1769 | if (start < self->raw_length) { |
|
1776 | 1770 | if (self->cache) { |
|
1777 | 1771 | Py_ssize_t i; |
|
1778 | 1772 | for (i = start; i < self->raw_length; i++) |
|
1779 | 1773 | Py_CLEAR(self->cache[i]); |
|
1780 | 1774 | } |
|
1781 | 1775 | self->raw_length = start; |
|
1782 | 1776 | } |
|
1783 | 1777 | goto done; |
|
1784 | 1778 | } |
|
1785 | 1779 | |
|
1786 | 1780 | if (self->nt) { |
|
1787 | 1781 | nt_invalidate_added(self, start - self->length + 1); |
|
1788 | 1782 | if (self->ntrev > start) |
|
1789 | 1783 | self->ntrev = (int)start; |
|
1790 | 1784 | } |
|
1791 | 1785 | if (self->added) |
|
1792 | 1786 | ret = PyList_SetSlice(self->added, start - self->length + 1, |
|
1793 | 1787 | PyList_GET_SIZE(self->added), NULL); |
|
1794 | 1788 | done: |
|
1795 | 1789 | Py_CLEAR(self->headrevs); |
|
1796 | 1790 | return ret; |
|
1797 | 1791 | } |
|
1798 | 1792 | |
|
1799 | 1793 | /* |
|
1800 | 1794 | * Supported ops: |
|
1801 | 1795 | * |
|
1802 | 1796 | * slice deletion |
|
1803 | 1797 | * string assignment (extend node->rev mapping) |
|
1804 | 1798 | * string deletion (shrink node->rev mapping) |
|
1805 | 1799 | */ |
|
1806 | 1800 | static int index_assign_subscript(indexObject *self, PyObject *item, |
|
1807 | 1801 | PyObject *value) |
|
1808 | 1802 | { |
|
1809 | 1803 | char *node; |
|
1810 | 1804 | Py_ssize_t nodelen; |
|
1811 | 1805 | long rev; |
|
1812 | 1806 | |
|
1813 | 1807 | if (PySlice_Check(item) && value == NULL) |
|
1814 | 1808 | return index_slice_del(self, item); |
|
1815 | 1809 | |
|
1816 | 1810 | if (node_check(item, &node, &nodelen) == -1) |
|
1817 | 1811 | return -1; |
|
1818 | 1812 | |
|
1819 | 1813 | if (value == NULL) |
|
1820 | 1814 | return self->nt ? nt_insert(self, node, -1) : 0; |
|
1821 | 1815 | rev = PyInt_AsLong(value); |
|
1822 | 1816 | if (rev > INT_MAX || rev < 0) { |
|
1823 | 1817 | if (!PyErr_Occurred()) |
|
1824 | 1818 | PyErr_SetString(PyExc_ValueError, "rev out of range"); |
|
1825 | 1819 | return -1; |
|
1826 | 1820 | } |
|
1827 | 1821 | |
|
1828 | 1822 | if (nt_init(self) == -1) |
|
1829 | 1823 | return -1; |
|
1830 | 1824 | return nt_insert(self, node, (int)rev); |
|
1831 | 1825 | } |
|
1832 | 1826 | |
|
1833 | 1827 | /* |
|
1834 | 1828 | * Find all RevlogNG entries in an index that has inline data. Update |
|
1835 | 1829 | * the optional "offsets" table with those entries. |
|
1836 | 1830 | */ |
|
1837 | 1831 | static Py_ssize_t inline_scan(indexObject *self, const char **offsets) |
|
1838 | 1832 | { |
|
1839 | 1833 | const char *data = (const char *)self->buf.buf; |
|
1840 | 1834 | Py_ssize_t pos = 0; |
|
1841 | 1835 | Py_ssize_t end = self->buf.len; |
|
1842 | 1836 | long incr = v1_hdrsize; |
|
1843 | 1837 | Py_ssize_t len = 0; |
|
1844 | 1838 | |
|
1845 | 1839 | while (pos + v1_hdrsize <= end && pos >= 0) { |
|
1846 | 1840 | uint32_t comp_len; |
|
1847 | 1841 | /* 3rd element of header is length of compressed inline data */ |
|
1848 | 1842 | comp_len = getbe32(data + pos + 8); |
|
1849 | 1843 | incr = v1_hdrsize + comp_len; |
|
1850 | 1844 | if (offsets) |
|
1851 | 1845 | offsets[len] = data + pos; |
|
1852 | 1846 | len++; |
|
1853 | 1847 | pos += incr; |
|
1854 | 1848 | } |
|
1855 | 1849 | |
|
1856 | 1850 | if (pos != end) { |
|
1857 | 1851 | if (!PyErr_Occurred()) |
|
1858 | 1852 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); |
|
1859 | 1853 | return -1; |
|
1860 | 1854 | } |
|
1861 | 1855 | |
|
1862 | 1856 | return len; |
|
1863 | 1857 | } |
|
1864 | 1858 | |
|
1865 | 1859 | static int index_init(indexObject *self, PyObject *args) |
|
1866 | 1860 | { |
|
1867 | 1861 | PyObject *data_obj, *inlined_obj; |
|
1868 | 1862 | Py_ssize_t size; |
|
1869 | 1863 | |
|
1870 | 1864 | /* Initialize before argument-checking to avoid index_dealloc() crash. */ |
|
1871 | 1865 | self->raw_length = 0; |
|
1872 | 1866 | self->added = NULL; |
|
1873 | 1867 | self->cache = NULL; |
|
1874 | 1868 | self->data = NULL; |
|
1875 | 1869 | memset(&self->buf, 0, sizeof(self->buf)); |
|
1876 | 1870 | self->headrevs = NULL; |
|
1877 | 1871 | self->filteredrevs = Py_None; |
|
1878 | 1872 | Py_INCREF(Py_None); |
|
1879 | 1873 | self->nt = NULL; |
|
1880 | 1874 | self->offsets = NULL; |
|
1881 | 1875 | |
|
1882 | 1876 | if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj)) |
|
1883 | 1877 | return -1; |
|
1884 | 1878 | if (!PyObject_CheckBuffer(data_obj)) { |
|
1885 | 1879 | PyErr_SetString(PyExc_TypeError, |
|
1886 | 1880 | "data does not support buffer interface"); |
|
1887 | 1881 | return -1; |
|
1888 | 1882 | } |
|
1889 | 1883 | |
|
1890 | 1884 | if (PyObject_GetBuffer(data_obj, &self->buf, PyBUF_SIMPLE) == -1) |
|
1891 | 1885 | return -1; |
|
1892 | 1886 | size = self->buf.len; |
|
1893 | 1887 | |
|
1894 | 1888 | self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj); |
|
1895 | 1889 | self->data = data_obj; |
|
1896 | 1890 | |
|
1897 | 1891 | self->ntlength = self->ntcapacity = 0; |
|
1898 | 1892 | self->ntdepth = self->ntsplits = 0; |
|
1899 | 1893 | self->ntlookups = self->ntmisses = 0; |
|
1900 | 1894 | self->ntrev = -1; |
|
1901 | 1895 | Py_INCREF(self->data); |
|
1902 | 1896 | |
|
1903 | 1897 | if (self->inlined) { |
|
1904 | 1898 | Py_ssize_t len = inline_scan(self, NULL); |
|
1905 | 1899 | if (len == -1) |
|
1906 | 1900 | goto bail; |
|
1907 | 1901 | self->raw_length = len; |
|
1908 | 1902 | self->length = len + 1; |
|
1909 | 1903 | } else { |
|
1910 | 1904 | if (size % v1_hdrsize) { |
|
1911 | 1905 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); |
|
1912 | 1906 | goto bail; |
|
1913 | 1907 | } |
|
1914 | 1908 | self->raw_length = size / v1_hdrsize; |
|
1915 | 1909 | self->length = self->raw_length + 1; |
|
1916 | 1910 | } |
|
1917 | 1911 | |
|
1918 | 1912 | return 0; |
|
1919 | 1913 | bail: |
|
1920 | 1914 | return -1; |
|
1921 | 1915 | } |
|
1922 | 1916 | |
|
1923 | 1917 | static PyObject *index_nodemap(indexObject *self) |
|
1924 | 1918 | { |
|
1925 | 1919 | Py_INCREF(self); |
|
1926 | 1920 | return (PyObject *)self; |
|
1927 | 1921 | } |
|
1928 | 1922 | |
|
1929 | 1923 | static void index_dealloc(indexObject *self) |
|
1930 | 1924 | { |
|
1931 | 1925 | _index_clearcaches(self); |
|
1932 | 1926 | Py_XDECREF(self->filteredrevs); |
|
1933 | 1927 | if (self->buf.buf) { |
|
1934 | 1928 | PyBuffer_Release(&self->buf); |
|
1935 | 1929 | memset(&self->buf, 0, sizeof(self->buf)); |
|
1936 | 1930 | } |
|
1937 | 1931 | Py_XDECREF(self->data); |
|
1938 | 1932 | Py_XDECREF(self->added); |
|
1939 | 1933 | PyObject_Del(self); |
|
1940 | 1934 | } |
|
1941 | 1935 | |
|
1942 | 1936 | static PySequenceMethods index_sequence_methods = { |
|
1943 | 1937 | (lenfunc)index_length, /* sq_length */ |
|
1944 | 1938 | 0, /* sq_concat */ |
|
1945 | 1939 | 0, /* sq_repeat */ |
|
1946 | 1940 | (ssizeargfunc)index_get, /* sq_item */ |
|
1947 | 1941 | 0, /* sq_slice */ |
|
1948 | 1942 | 0, /* sq_ass_item */ |
|
1949 | 1943 | 0, /* sq_ass_slice */ |
|
1950 | 1944 | (objobjproc)index_contains, /* sq_contains */ |
|
1951 | 1945 | }; |
|
1952 | 1946 | |
|
1953 | 1947 | static PyMappingMethods index_mapping_methods = { |
|
1954 | 1948 | (lenfunc)index_length, /* mp_length */ |
|
1955 | 1949 | (binaryfunc)index_getitem, /* mp_subscript */ |
|
1956 | 1950 | (objobjargproc)index_assign_subscript, /* mp_ass_subscript */ |
|
1957 | 1951 | }; |
|
1958 | 1952 | |
|
1959 | 1953 | static PyMethodDef index_methods[] = { |
|
1960 | 1954 | {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS, |
|
1961 | 1955 | "return the gca set of the given revs"}, |
|
1962 | 1956 | {"commonancestorsheads", (PyCFunction)index_commonancestorsheads, |
|
1963 | 1957 | METH_VARARGS, |
|
1964 | 1958 | "return the heads of the common ancestors of the given revs"}, |
|
1965 | 1959 | {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS, |
|
1966 | 1960 | "clear the index caches"}, |
|
1967 | 1961 | {"get", (PyCFunction)index_m_get, METH_VARARGS, |
|
1968 | 1962 | "get an index entry"}, |
|
1969 | 1963 | {"computephasesmapsets", (PyCFunction)compute_phases_map_sets, |
|
1970 | 1964 | METH_VARARGS, "compute phases"}, |
|
1971 | 1965 | {"reachableroots2", (PyCFunction)reachableroots2, METH_VARARGS, |
|
1972 | 1966 | "reachableroots"}, |
|
1973 | 1967 | {"headrevs", (PyCFunction)index_headrevs, METH_VARARGS, |
|
1974 | 1968 | "get head revisions"}, /* Can do filtering since 3.2 */ |
|
1975 | 1969 | {"headrevsfiltered", (PyCFunction)index_headrevs, METH_VARARGS, |
|
1976 | 1970 | "get filtered head revisions"}, /* Can always do filtering */ |
|
1977 | 1971 | {"deltachain", (PyCFunction)index_deltachain, METH_VARARGS, |
|
1978 | 1972 | "determine revisions with deltas to reconstruct fulltext"}, |
|
1979 | 1973 | {"insert", (PyCFunction)index_insert, METH_VARARGS, |
|
1980 | 1974 | "insert an index entry"}, |
|
1981 | 1975 | {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS, |
|
1982 | 1976 | "match a potentially ambiguous node ID"}, |
|
1983 | 1977 | {"stats", (PyCFunction)index_stats, METH_NOARGS, |
|
1984 | 1978 | "stats for the index"}, |
|
1985 | 1979 | {NULL} /* Sentinel */ |
|
1986 | 1980 | }; |
|
1987 | 1981 | |
|
1988 | 1982 | static PyGetSetDef index_getset[] = { |
|
1989 | 1983 | {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL}, |
|
1990 | 1984 | {NULL} /* Sentinel */ |
|
1991 | 1985 | }; |
|
1992 | 1986 | |
|
1993 | 1987 | static PyTypeObject indexType = { |
|
1994 | 1988 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ |
|
1995 | 1989 | "parsers.index", /* tp_name */ |
|
1996 | 1990 | sizeof(indexObject), /* tp_basicsize */ |
|
1997 | 1991 | 0, /* tp_itemsize */ |
|
1998 | 1992 | (destructor)index_dealloc, /* tp_dealloc */ |
|
1999 | 1993 | 0, /* tp_print */ |
|
2000 | 1994 | 0, /* tp_getattr */ |
|
2001 | 1995 | 0, /* tp_setattr */ |
|
2002 | 1996 | 0, /* tp_compare */ |
|
2003 | 1997 | 0, /* tp_repr */ |
|
2004 | 1998 | 0, /* tp_as_number */ |
|
2005 | 1999 | &index_sequence_methods, /* tp_as_sequence */ |
|
2006 | 2000 | &index_mapping_methods, /* tp_as_mapping */ |
|
2007 | 2001 | 0, /* tp_hash */ |
|
2008 | 2002 | 0, /* tp_call */ |
|
2009 | 2003 | 0, /* tp_str */ |
|
2010 | 2004 | 0, /* tp_getattro */ |
|
2011 | 2005 | 0, /* tp_setattro */ |
|
2012 | 2006 | 0, /* tp_as_buffer */ |
|
2013 | 2007 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
2014 | 2008 | "revlog index", /* tp_doc */ |
|
2015 | 2009 | 0, /* tp_traverse */ |
|
2016 | 2010 | 0, /* tp_clear */ |
|
2017 | 2011 | 0, /* tp_richcompare */ |
|
2018 | 2012 | 0, /* tp_weaklistoffset */ |
|
2019 | 2013 | 0, /* tp_iter */ |
|
2020 | 2014 | 0, /* tp_iternext */ |
|
2021 | 2015 | index_methods, /* tp_methods */ |
|
2022 | 2016 | 0, /* tp_members */ |
|
2023 | 2017 | index_getset, /* tp_getset */ |
|
2024 | 2018 | 0, /* tp_base */ |
|
2025 | 2019 | 0, /* tp_dict */ |
|
2026 | 2020 | 0, /* tp_descr_get */ |
|
2027 | 2021 | 0, /* tp_descr_set */ |
|
2028 | 2022 | 0, /* tp_dictoffset */ |
|
2029 | 2023 | (initproc)index_init, /* tp_init */ |
|
2030 | 2024 | 0, /* tp_alloc */ |
|
2031 | 2025 | }; |
|
2032 | 2026 | |
|
2033 | 2027 | /* |
|
2034 | 2028 | * returns a tuple of the form (index, index, cache) with elements as |
|
2035 | 2029 | * follows: |
|
2036 | 2030 | * |
|
2037 | 2031 | * index: an index object that lazily parses RevlogNG records |
|
2038 | 2032 | * cache: if data is inlined, a tuple (0, index_file_content), else None |
|
2039 | 2033 | * index_file_content could be a string, or a buffer |
|
2040 | 2034 | * |
|
2041 | 2035 | * added complications are for backwards compatibility |
|
2042 | 2036 | */ |
|
2043 | 2037 | PyObject *parse_index2(PyObject *self, PyObject *args) |
|
2044 | 2038 | { |
|
2045 | 2039 | PyObject *tuple = NULL, *cache = NULL; |
|
2046 | 2040 | indexObject *idx; |
|
2047 | 2041 | int ret; |
|
2048 | 2042 | |
|
2049 | 2043 | idx = PyObject_New(indexObject, &indexType); |
|
2050 | 2044 | if (idx == NULL) |
|
2051 | 2045 | goto bail; |
|
2052 | 2046 | |
|
2053 | 2047 | ret = index_init(idx, args); |
|
2054 | 2048 | if (ret == -1) |
|
2055 | 2049 | goto bail; |
|
2056 | 2050 | |
|
2057 | 2051 | if (idx->inlined) { |
|
2058 | 2052 | cache = Py_BuildValue("iO", 0, idx->data); |
|
2059 | 2053 | if (cache == NULL) |
|
2060 | 2054 | goto bail; |
|
2061 | 2055 | } else { |
|
2062 | 2056 | cache = Py_None; |
|
2063 | 2057 | Py_INCREF(cache); |
|
2064 | 2058 | } |
|
2065 | 2059 | |
|
2066 | 2060 | tuple = Py_BuildValue("NN", idx, cache); |
|
2067 | 2061 | if (!tuple) |
|
2068 | 2062 | goto bail; |
|
2069 | 2063 | return tuple; |
|
2070 | 2064 | |
|
2071 | 2065 | bail: |
|
2072 | 2066 | Py_XDECREF(idx); |
|
2073 | 2067 | Py_XDECREF(cache); |
|
2074 | 2068 | Py_XDECREF(tuple); |
|
2075 | 2069 | return NULL; |
|
2076 | 2070 | } |
|
2077 | 2071 | |
|
2078 | 2072 | void revlog_module_init(PyObject *mod) |
|
2079 | 2073 | { |
|
2080 | 2074 | indexType.tp_new = PyType_GenericNew; |
|
2081 | 2075 | if (PyType_Ready(&indexType) < 0) |
|
2082 | 2076 | return; |
|
2083 | 2077 | Py_INCREF(&indexType); |
|
2084 | 2078 | PyModule_AddObject(mod, "index", (PyObject *)&indexType); |
|
2085 | 2079 | |
|
2086 | 2080 | nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0, |
|
2087 | 2081 | -1, -1, -1, -1, nullid, 20); |
|
2088 | 2082 | if (nullentry) |
|
2089 | 2083 | PyObject_GC_UnTrack(nullentry); |
|
2090 | 2084 | } |
@@ -1,2274 +1,2274 | |||
|
1 | 1 | # localrepo.py - read/write repository class for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import errno |
|
11 | 11 | import hashlib |
|
12 | 12 | import inspect |
|
13 | 13 | import os |
|
14 | 14 | import random |
|
15 | 15 | import time |
|
16 | 16 | import weakref |
|
17 | 17 | |
|
18 | 18 | from .i18n import _ |
|
19 | 19 | from .node import ( |
|
20 | 20 | hex, |
|
21 | 21 | nullid, |
|
22 | 22 | short, |
|
23 | 23 | ) |
|
24 | 24 | from . import ( |
|
25 | 25 | bookmarks, |
|
26 | 26 | branchmap, |
|
27 | 27 | bundle2, |
|
28 | 28 | changegroup, |
|
29 | 29 | changelog, |
|
30 | 30 | color, |
|
31 | 31 | context, |
|
32 | 32 | dirstate, |
|
33 | 33 | dirstateguard, |
|
34 | 34 | discovery, |
|
35 | 35 | encoding, |
|
36 | 36 | error, |
|
37 | 37 | exchange, |
|
38 | 38 | extensions, |
|
39 | 39 | filelog, |
|
40 | 40 | hook, |
|
41 | 41 | lock as lockmod, |
|
42 | 42 | manifest, |
|
43 | 43 | match as matchmod, |
|
44 | 44 | merge as mergemod, |
|
45 | 45 | mergeutil, |
|
46 | 46 | namespaces, |
|
47 | 47 | obsolete, |
|
48 | 48 | pathutil, |
|
49 | 49 | peer, |
|
50 | 50 | phases, |
|
51 | 51 | pushkey, |
|
52 | 52 | pycompat, |
|
53 | 53 | repository, |
|
54 | 54 | repoview, |
|
55 | 55 | revset, |
|
56 | 56 | revsetlang, |
|
57 | 57 | scmutil, |
|
58 | 58 | sparse, |
|
59 | 59 | store, |
|
60 | 60 | subrepo, |
|
61 | 61 | tags as tagsmod, |
|
62 | 62 | transaction, |
|
63 | 63 | txnutil, |
|
64 | 64 | util, |
|
65 | 65 | vfs as vfsmod, |
|
66 | 66 | ) |
|
67 | 67 | |
|
68 | 68 | release = lockmod.release |
|
69 | 69 | urlerr = util.urlerr |
|
70 | 70 | urlreq = util.urlreq |
|
71 | 71 | |
|
72 | 72 | # set of (path, vfs-location) tuples. vfs-location is: |
|
73 | 73 | # - 'plain for vfs relative paths |
|
74 | 74 | # - '' for svfs relative paths |
|
75 | 75 | _cachedfiles = set() |
|
76 | 76 | |
|
77 | 77 | class _basefilecache(scmutil.filecache): |
|
78 | 78 | """All filecache usage on repo are done for logic that should be unfiltered |
|
79 | 79 | """ |
|
80 | 80 | def __get__(self, repo, type=None): |
|
81 | 81 | if repo is None: |
|
82 | 82 | return self |
|
83 | 83 | return super(_basefilecache, self).__get__(repo.unfiltered(), type) |
|
84 | 84 | def __set__(self, repo, value): |
|
85 | 85 | return super(_basefilecache, self).__set__(repo.unfiltered(), value) |
|
86 | 86 | def __delete__(self, repo): |
|
87 | 87 | return super(_basefilecache, self).__delete__(repo.unfiltered()) |
|
88 | 88 | |
|
89 | 89 | class repofilecache(_basefilecache): |
|
90 | 90 | """filecache for files in .hg but outside of .hg/store""" |
|
91 | 91 | def __init__(self, *paths): |
|
92 | 92 | super(repofilecache, self).__init__(*paths) |
|
93 | 93 | for path in paths: |
|
94 | 94 | _cachedfiles.add((path, 'plain')) |
|
95 | 95 | |
|
96 | 96 | def join(self, obj, fname): |
|
97 | 97 | return obj.vfs.join(fname) |
|
98 | 98 | |
|
99 | 99 | class storecache(_basefilecache): |
|
100 | 100 | """filecache for files in the store""" |
|
101 | 101 | def __init__(self, *paths): |
|
102 | 102 | super(storecache, self).__init__(*paths) |
|
103 | 103 | for path in paths: |
|
104 | 104 | _cachedfiles.add((path, '')) |
|
105 | 105 | |
|
106 | 106 | def join(self, obj, fname): |
|
107 | 107 | return obj.sjoin(fname) |
|
108 | 108 | |
|
109 | 109 | def isfilecached(repo, name): |
|
110 | 110 | """check if a repo has already cached "name" filecache-ed property |
|
111 | 111 | |
|
112 | 112 | This returns (cachedobj-or-None, iscached) tuple. |
|
113 | 113 | """ |
|
114 | 114 | cacheentry = repo.unfiltered()._filecache.get(name, None) |
|
115 | 115 | if not cacheentry: |
|
116 | 116 | return None, False |
|
117 | 117 | return cacheentry.obj, True |
|
118 | 118 | |
|
119 | 119 | class unfilteredpropertycache(util.propertycache): |
|
120 | 120 | """propertycache that apply to unfiltered repo only""" |
|
121 | 121 | |
|
122 | 122 | def __get__(self, repo, type=None): |
|
123 | 123 | unfi = repo.unfiltered() |
|
124 | 124 | if unfi is repo: |
|
125 | 125 | return super(unfilteredpropertycache, self).__get__(unfi) |
|
126 | 126 | return getattr(unfi, self.name) |
|
127 | 127 | |
|
128 | 128 | class filteredpropertycache(util.propertycache): |
|
129 | 129 | """propertycache that must take filtering in account""" |
|
130 | 130 | |
|
131 | 131 | def cachevalue(self, obj, value): |
|
132 | 132 | object.__setattr__(obj, self.name, value) |
|
133 | 133 | |
|
134 | 134 | |
|
135 | 135 | def hasunfilteredcache(repo, name): |
|
136 | 136 | """check if a repo has an unfilteredpropertycache value for <name>""" |
|
137 | 137 | return name in vars(repo.unfiltered()) |
|
138 | 138 | |
|
139 | 139 | def unfilteredmethod(orig): |
|
140 | 140 | """decorate method that always need to be run on unfiltered version""" |
|
141 | 141 | def wrapper(repo, *args, **kwargs): |
|
142 | 142 | return orig(repo.unfiltered(), *args, **kwargs) |
|
143 | 143 | return wrapper |
|
144 | 144 | |
|
145 | 145 | moderncaps = {'lookup', 'branchmap', 'pushkey', 'known', 'getbundle', |
|
146 | 146 | 'unbundle'} |
|
147 | 147 | legacycaps = moderncaps.union({'changegroupsubset'}) |
|
148 | 148 | |
|
149 | 149 | class localpeer(repository.peer): |
|
150 | 150 | '''peer for a local repo; reflects only the most recent API''' |
|
151 | 151 | |
|
152 | 152 | def __init__(self, repo, caps=None): |
|
153 | 153 | super(localpeer, self).__init__() |
|
154 | 154 | |
|
155 | 155 | if caps is None: |
|
156 | 156 | caps = moderncaps.copy() |
|
157 | 157 | self._repo = repo.filtered('served') |
|
158 | 158 | self._ui = repo.ui |
|
159 | 159 | self._caps = repo._restrictcapabilities(caps) |
|
160 | 160 | |
|
161 | 161 | # Begin of _basepeer interface. |
|
162 | 162 | |
|
163 | 163 | @util.propertycache |
|
164 | 164 | def ui(self): |
|
165 | 165 | return self._ui |
|
166 | 166 | |
|
167 | 167 | def url(self): |
|
168 | 168 | return self._repo.url() |
|
169 | 169 | |
|
170 | 170 | def local(self): |
|
171 | 171 | return self._repo |
|
172 | 172 | |
|
173 | 173 | def peer(self): |
|
174 | 174 | return self |
|
175 | 175 | |
|
176 | 176 | def canpush(self): |
|
177 | 177 | return True |
|
178 | 178 | |
|
179 | 179 | def close(self): |
|
180 | 180 | self._repo.close() |
|
181 | 181 | |
|
182 | 182 | # End of _basepeer interface. |
|
183 | 183 | |
|
184 | 184 | # Begin of _basewirecommands interface. |
|
185 | 185 | |
|
186 | 186 | def branchmap(self): |
|
187 | 187 | return self._repo.branchmap() |
|
188 | 188 | |
|
189 | 189 | def capabilities(self): |
|
190 | 190 | return self._caps |
|
191 | 191 | |
|
192 | 192 | def debugwireargs(self, one, two, three=None, four=None, five=None): |
|
193 | 193 | """Used to test argument passing over the wire""" |
|
194 | 194 | return "%s %s %s %s %s" % (one, two, three, four, five) |
|
195 | 195 | |
|
196 | 196 | def getbundle(self, source, heads=None, common=None, bundlecaps=None, |
|
197 | 197 | **kwargs): |
|
198 | 198 | chunks = exchange.getbundlechunks(self._repo, source, heads=heads, |
|
199 | 199 | common=common, bundlecaps=bundlecaps, |
|
200 | 200 | **kwargs) |
|
201 | 201 | cb = util.chunkbuffer(chunks) |
|
202 | 202 | |
|
203 | 203 | if exchange.bundle2requested(bundlecaps): |
|
204 | 204 | # When requesting a bundle2, getbundle returns a stream to make the |
|
205 | 205 | # wire level function happier. We need to build a proper object |
|
206 | 206 | # from it in local peer. |
|
207 | 207 | return bundle2.getunbundler(self.ui, cb) |
|
208 | 208 | else: |
|
209 | 209 | return changegroup.getunbundler('01', cb, None) |
|
210 | 210 | |
|
211 | 211 | def heads(self): |
|
212 | 212 | return self._repo.heads() |
|
213 | 213 | |
|
214 | 214 | def known(self, nodes): |
|
215 | 215 | return self._repo.known(nodes) |
|
216 | 216 | |
|
217 | 217 | def listkeys(self, namespace): |
|
218 | 218 | return self._repo.listkeys(namespace) |
|
219 | 219 | |
|
220 | 220 | def lookup(self, key): |
|
221 | 221 | return self._repo.lookup(key) |
|
222 | 222 | |
|
223 | 223 | def pushkey(self, namespace, key, old, new): |
|
224 | 224 | return self._repo.pushkey(namespace, key, old, new) |
|
225 | 225 | |
|
226 | 226 | def stream_out(self): |
|
227 | 227 | raise error.Abort(_('cannot perform stream clone against local ' |
|
228 | 228 | 'peer')) |
|
229 | 229 | |
|
230 | 230 | def unbundle(self, cg, heads, url): |
|
231 | 231 | """apply a bundle on a repo |
|
232 | 232 | |
|
233 | 233 | This function handles the repo locking itself.""" |
|
234 | 234 | try: |
|
235 | 235 | try: |
|
236 | 236 | cg = exchange.readbundle(self.ui, cg, None) |
|
237 | 237 | ret = exchange.unbundle(self._repo, cg, heads, 'push', url) |
|
238 | 238 | if util.safehasattr(ret, 'getchunks'): |
|
239 | 239 | # This is a bundle20 object, turn it into an unbundler. |
|
240 | 240 | # This little dance should be dropped eventually when the |
|
241 | 241 | # API is finally improved. |
|
242 | 242 | stream = util.chunkbuffer(ret.getchunks()) |
|
243 | 243 | ret = bundle2.getunbundler(self.ui, stream) |
|
244 | 244 | return ret |
|
245 | 245 | except Exception as exc: |
|
246 | 246 | # If the exception contains output salvaged from a bundle2 |
|
247 | 247 | # reply, we need to make sure it is printed before continuing |
|
248 | 248 | # to fail. So we build a bundle2 with such output and consume |
|
249 | 249 | # it directly. |
|
250 | 250 | # |
|
251 | 251 | # This is not very elegant but allows a "simple" solution for |
|
252 | 252 | # issue4594 |
|
253 | 253 | output = getattr(exc, '_bundle2salvagedoutput', ()) |
|
254 | 254 | if output: |
|
255 | 255 | bundler = bundle2.bundle20(self._repo.ui) |
|
256 | 256 | for out in output: |
|
257 | 257 | bundler.addpart(out) |
|
258 | 258 | stream = util.chunkbuffer(bundler.getchunks()) |
|
259 | 259 | b = bundle2.getunbundler(self.ui, stream) |
|
260 | 260 | bundle2.processbundle(self._repo, b) |
|
261 | 261 | raise |
|
262 | 262 | except error.PushRaced as exc: |
|
263 | 263 | raise error.ResponseError(_('push failed:'), str(exc)) |
|
264 | 264 | |
|
265 | 265 | # End of _basewirecommands interface. |
|
266 | 266 | |
|
267 | 267 | # Begin of peer interface. |
|
268 | 268 | |
|
269 | 269 | def iterbatch(self): |
|
270 | 270 | return peer.localiterbatcher(self) |
|
271 | 271 | |
|
272 | 272 | # End of peer interface. |
|
273 | 273 | |
|
274 | 274 | class locallegacypeer(repository.legacypeer, localpeer): |
|
275 | 275 | '''peer extension which implements legacy methods too; used for tests with |
|
276 | 276 | restricted capabilities''' |
|
277 | 277 | |
|
278 | 278 | def __init__(self, repo): |
|
279 | 279 | super(locallegacypeer, self).__init__(repo, caps=legacycaps) |
|
280 | 280 | |
|
281 | 281 | # Begin of baselegacywirecommands interface. |
|
282 | 282 | |
|
283 | 283 | def between(self, pairs): |
|
284 | 284 | return self._repo.between(pairs) |
|
285 | 285 | |
|
286 | 286 | def branches(self, nodes): |
|
287 | 287 | return self._repo.branches(nodes) |
|
288 | 288 | |
|
289 | 289 | def changegroup(self, basenodes, source): |
|
290 | 290 | outgoing = discovery.outgoing(self._repo, missingroots=basenodes, |
|
291 | 291 | missingheads=self._repo.heads()) |
|
292 | 292 | return changegroup.makechangegroup(self._repo, outgoing, '01', source) |
|
293 | 293 | |
|
294 | 294 | def changegroupsubset(self, bases, heads, source): |
|
295 | 295 | outgoing = discovery.outgoing(self._repo, missingroots=bases, |
|
296 | 296 | missingheads=heads) |
|
297 | 297 | return changegroup.makechangegroup(self._repo, outgoing, '01', source) |
|
298 | 298 | |
|
299 | 299 | # End of baselegacywirecommands interface. |
|
300 | 300 | |
|
301 | 301 | # Increment the sub-version when the revlog v2 format changes to lock out old |
|
302 | 302 | # clients. |
|
303 | 303 | REVLOGV2_REQUIREMENT = 'exp-revlogv2.0' |
|
304 | 304 | |
|
305 | 305 | class localrepository(object): |
|
306 | 306 | |
|
307 | 307 | supportedformats = { |
|
308 | 308 | 'revlogv1', |
|
309 | 309 | 'generaldelta', |
|
310 | 310 | 'treemanifest', |
|
311 | 311 | 'manifestv2', |
|
312 | 312 | REVLOGV2_REQUIREMENT, |
|
313 | 313 | } |
|
314 | 314 | _basesupported = supportedformats | { |
|
315 | 315 | 'store', |
|
316 | 316 | 'fncache', |
|
317 | 317 | 'shared', |
|
318 | 318 | 'relshared', |
|
319 | 319 | 'dotencode', |
|
320 | 320 | 'exp-sparse', |
|
321 | 321 | } |
|
322 | 322 | openerreqs = { |
|
323 | 323 | 'revlogv1', |
|
324 | 324 | 'generaldelta', |
|
325 | 325 | 'treemanifest', |
|
326 | 326 | 'manifestv2', |
|
327 | 327 | } |
|
328 | 328 | |
|
329 | 329 | # a list of (ui, featureset) functions. |
|
330 | 330 | # only functions defined in module of enabled extensions are invoked |
|
331 | 331 | featuresetupfuncs = set() |
|
332 | 332 | |
|
333 | 333 | # list of prefix for file which can be written without 'wlock' |
|
334 | 334 | # Extensions should extend this list when needed |
|
335 | 335 | _wlockfreeprefix = { |
|
336 | 336 | # We migh consider requiring 'wlock' for the next |
|
337 | 337 | # two, but pretty much all the existing code assume |
|
338 | 338 | # wlock is not needed so we keep them excluded for |
|
339 | 339 | # now. |
|
340 | 340 | 'hgrc', |
|
341 | 341 | 'requires', |
|
342 | 342 | # XXX cache is a complicatged business someone |
|
343 | 343 | # should investigate this in depth at some point |
|
344 | 344 | 'cache/', |
|
345 | 345 | # XXX shouldn't be dirstate covered by the wlock? |
|
346 | 346 | 'dirstate', |
|
347 | 347 | # XXX bisect was still a bit too messy at the time |
|
348 | 348 | # this changeset was introduced. Someone should fix |
|
349 | 349 | # the remainig bit and drop this line |
|
350 | 350 | 'bisect.state', |
|
351 | 351 | } |
|
352 | 352 | |
|
353 | 353 | def __init__(self, baseui, path, create=False): |
|
354 | 354 | self.requirements = set() |
|
355 | 355 | self.filtername = None |
|
356 | 356 | # wvfs: rooted at the repository root, used to access the working copy |
|
357 | 357 | self.wvfs = vfsmod.vfs(path, expandpath=True, realpath=True) |
|
358 | 358 | # vfs: rooted at .hg, used to access repo files outside of .hg/store |
|
359 | 359 | self.vfs = None |
|
360 | 360 | # svfs: usually rooted at .hg/store, used to access repository history |
|
361 | 361 | # If this is a shared repository, this vfs may point to another |
|
362 | 362 | # repository's .hg/store directory. |
|
363 | 363 | self.svfs = None |
|
364 | 364 | self.root = self.wvfs.base |
|
365 | 365 | self.path = self.wvfs.join(".hg") |
|
366 | 366 | self.origroot = path |
|
367 | 367 | # This is only used by context.workingctx.match in order to |
|
368 | 368 | # detect files in subrepos. |
|
369 | 369 | self.auditor = pathutil.pathauditor( |
|
370 | 370 | self.root, callback=self._checknested) |
|
371 | 371 | # This is only used by context.basectx.match in order to detect |
|
372 | 372 | # files in subrepos. |
|
373 | 373 | self.nofsauditor = pathutil.pathauditor( |
|
374 | 374 | self.root, callback=self._checknested, realfs=False, cached=True) |
|
375 | 375 | self.baseui = baseui |
|
376 | 376 | self.ui = baseui.copy() |
|
377 | 377 | self.ui.copy = baseui.copy # prevent copying repo configuration |
|
378 | 378 | self.vfs = vfsmod.vfs(self.path, cacheaudited=True) |
|
379 | 379 | if (self.ui.configbool('devel', 'all-warnings') or |
|
380 | 380 | self.ui.configbool('devel', 'check-locks')): |
|
381 | 381 | self.vfs.audit = self._getvfsward(self.vfs.audit) |
|
382 | 382 | # A list of callback to shape the phase if no data were found. |
|
383 | 383 | # Callback are in the form: func(repo, roots) --> processed root. |
|
384 | 384 | # This list it to be filled by extension during repo setup |
|
385 | 385 | self._phasedefaults = [] |
|
386 | 386 | try: |
|
387 | 387 | self.ui.readconfig(self.vfs.join("hgrc"), self.root) |
|
388 | 388 | self._loadextensions() |
|
389 | 389 | except IOError: |
|
390 | 390 | pass |
|
391 | 391 | |
|
392 | 392 | if self.featuresetupfuncs: |
|
393 | 393 | self.supported = set(self._basesupported) # use private copy |
|
394 | 394 | extmods = set(m.__name__ for n, m |
|
395 | 395 | in extensions.extensions(self.ui)) |
|
396 | 396 | for setupfunc in self.featuresetupfuncs: |
|
397 | 397 | if setupfunc.__module__ in extmods: |
|
398 | 398 | setupfunc(self.ui, self.supported) |
|
399 | 399 | else: |
|
400 | 400 | self.supported = self._basesupported |
|
401 | 401 | color.setup(self.ui) |
|
402 | 402 | |
|
403 | 403 | # Add compression engines. |
|
404 | 404 | for name in util.compengines: |
|
405 | 405 | engine = util.compengines[name] |
|
406 | 406 | if engine.revlogheader(): |
|
407 | 407 | self.supported.add('exp-compression-%s' % name) |
|
408 | 408 | |
|
409 | 409 | if not self.vfs.isdir(): |
|
410 | 410 | if create: |
|
411 | 411 | self.requirements = newreporequirements(self) |
|
412 | 412 | |
|
413 | 413 | if not self.wvfs.exists(): |
|
414 | 414 | self.wvfs.makedirs() |
|
415 | 415 | self.vfs.makedir(notindexed=True) |
|
416 | 416 | |
|
417 | 417 | if 'store' in self.requirements: |
|
418 | 418 | self.vfs.mkdir("store") |
|
419 | 419 | |
|
420 | 420 | # create an invalid changelog |
|
421 | 421 | self.vfs.append( |
|
422 | 422 | "00changelog.i", |
|
423 | 423 | '\0\0\0\2' # represents revlogv2 |
|
424 | 424 | ' dummy changelog to prevent using the old repo layout' |
|
425 | 425 | ) |
|
426 | 426 | else: |
|
427 | 427 | raise error.RepoError(_("repository %s not found") % path) |
|
428 | 428 | elif create: |
|
429 | 429 | raise error.RepoError(_("repository %s already exists") % path) |
|
430 | 430 | else: |
|
431 | 431 | try: |
|
432 | 432 | self.requirements = scmutil.readrequires( |
|
433 | 433 | self.vfs, self.supported) |
|
434 | 434 | except IOError as inst: |
|
435 | 435 | if inst.errno != errno.ENOENT: |
|
436 | 436 | raise |
|
437 | 437 | |
|
438 | 438 | cachepath = self.vfs.join('cache') |
|
439 | 439 | self.sharedpath = self.path |
|
440 | 440 | try: |
|
441 | 441 | sharedpath = self.vfs.read("sharedpath").rstrip('\n') |
|
442 | 442 | if 'relshared' in self.requirements: |
|
443 | 443 | sharedpath = self.vfs.join(sharedpath) |
|
444 | 444 | vfs = vfsmod.vfs(sharedpath, realpath=True) |
|
445 | 445 | cachepath = vfs.join('cache') |
|
446 | 446 | s = vfs.base |
|
447 | 447 | if not vfs.exists(): |
|
448 | 448 | raise error.RepoError( |
|
449 | 449 | _('.hg/sharedpath points to nonexistent directory %s') % s) |
|
450 | 450 | self.sharedpath = s |
|
451 | 451 | except IOError as inst: |
|
452 | 452 | if inst.errno != errno.ENOENT: |
|
453 | 453 | raise |
|
454 | 454 | |
|
455 | 455 | if 'exp-sparse' in self.requirements and not sparse.enabled: |
|
456 | 456 | raise error.RepoError(_('repository is using sparse feature but ' |
|
457 | 457 | 'sparse is not enabled; enable the ' |
|
458 | 458 | '"sparse" extensions to access')) |
|
459 | 459 | |
|
460 | 460 | self.store = store.store( |
|
461 | 461 | self.requirements, self.sharedpath, |
|
462 | 462 | lambda base: vfsmod.vfs(base, cacheaudited=True)) |
|
463 | 463 | self.spath = self.store.path |
|
464 | 464 | self.svfs = self.store.vfs |
|
465 | 465 | self.sjoin = self.store.join |
|
466 | 466 | self.vfs.createmode = self.store.createmode |
|
467 | 467 | self.cachevfs = vfsmod.vfs(cachepath, cacheaudited=True) |
|
468 | 468 | self.cachevfs.createmode = self.store.createmode |
|
469 | 469 | if (self.ui.configbool('devel', 'all-warnings') or |
|
470 | 470 | self.ui.configbool('devel', 'check-locks')): |
|
471 | 471 | if util.safehasattr(self.svfs, 'vfs'): # this is filtervfs |
|
472 | 472 | self.svfs.vfs.audit = self._getsvfsward(self.svfs.vfs.audit) |
|
473 | 473 | else: # standard vfs |
|
474 | 474 | self.svfs.audit = self._getsvfsward(self.svfs.audit) |
|
475 | 475 | self._applyopenerreqs() |
|
476 | 476 | if create: |
|
477 | 477 | self._writerequirements() |
|
478 | 478 | |
|
479 | 479 | self._dirstatevalidatewarned = False |
|
480 | 480 | |
|
481 | 481 | self._branchcaches = {} |
|
482 | 482 | self._revbranchcache = None |
|
483 | 483 | self.filterpats = {} |
|
484 | 484 | self._datafilters = {} |
|
485 | 485 | self._transref = self._lockref = self._wlockref = None |
|
486 | 486 | |
|
487 | 487 | # A cache for various files under .hg/ that tracks file changes, |
|
488 | 488 | # (used by the filecache decorator) |
|
489 | 489 | # |
|
490 | 490 | # Maps a property name to its util.filecacheentry |
|
491 | 491 | self._filecache = {} |
|
492 | 492 | |
|
493 | 493 | # hold sets of revision to be filtered |
|
494 | 494 | # should be cleared when something might have changed the filter value: |
|
495 | 495 | # - new changesets, |
|
496 | 496 | # - phase change, |
|
497 | 497 | # - new obsolescence marker, |
|
498 | 498 | # - working directory parent change, |
|
499 | 499 | # - bookmark changes |
|
500 | 500 | self.filteredrevcache = {} |
|
501 | 501 | |
|
502 | 502 | # post-dirstate-status hooks |
|
503 | 503 | self._postdsstatus = [] |
|
504 | 504 | |
|
505 | 505 | # generic mapping between names and nodes |
|
506 | 506 | self.names = namespaces.namespaces() |
|
507 | 507 | |
|
508 | 508 | # Key to signature value. |
|
509 | 509 | self._sparsesignaturecache = {} |
|
510 | 510 | # Signature to cached matcher instance. |
|
511 | 511 | self._sparsematchercache = {} |
|
512 | 512 | |
|
513 | 513 | def _getvfsward(self, origfunc): |
|
514 | 514 | """build a ward for self.vfs""" |
|
515 | 515 | rref = weakref.ref(self) |
|
516 | 516 | def checkvfs(path, mode=None): |
|
517 | 517 | ret = origfunc(path, mode=mode) |
|
518 | 518 | repo = rref() |
|
519 | 519 | if (repo is None |
|
520 | 520 | or not util.safehasattr(repo, '_wlockref') |
|
521 | 521 | or not util.safehasattr(repo, '_lockref')): |
|
522 | 522 | return |
|
523 | 523 | if mode in (None, 'r', 'rb'): |
|
524 | 524 | return |
|
525 | 525 | if path.startswith(repo.path): |
|
526 | 526 | # truncate name relative to the repository (.hg) |
|
527 | 527 | path = path[len(repo.path) + 1:] |
|
528 | 528 | if path.startswith('cache/'): |
|
529 | 529 | msg = 'accessing cache with vfs instead of cachevfs: "%s"' |
|
530 | 530 | repo.ui.develwarn(msg % path, stacklevel=2, config="cache-vfs") |
|
531 | 531 | if path.startswith('journal.'): |
|
532 | 532 | # journal is covered by 'lock' |
|
533 | 533 | if repo._currentlock(repo._lockref) is None: |
|
534 | 534 | repo.ui.develwarn('write with no lock: "%s"' % path, |
|
535 | 535 | stacklevel=2, config='check-locks') |
|
536 | 536 | elif repo._currentlock(repo._wlockref) is None: |
|
537 | 537 | # rest of vfs files are covered by 'wlock' |
|
538 | 538 | # |
|
539 | 539 | # exclude special files |
|
540 | 540 | for prefix in self._wlockfreeprefix: |
|
541 | 541 | if path.startswith(prefix): |
|
542 | 542 | return |
|
543 | 543 | repo.ui.develwarn('write with no wlock: "%s"' % path, |
|
544 | 544 | stacklevel=2, config='check-locks') |
|
545 | 545 | return ret |
|
546 | 546 | return checkvfs |
|
547 | 547 | |
|
548 | 548 | def _getsvfsward(self, origfunc): |
|
549 | 549 | """build a ward for self.svfs""" |
|
550 | 550 | rref = weakref.ref(self) |
|
551 | 551 | def checksvfs(path, mode=None): |
|
552 | 552 | ret = origfunc(path, mode=mode) |
|
553 | 553 | repo = rref() |
|
554 | 554 | if repo is None or not util.safehasattr(repo, '_lockref'): |
|
555 | 555 | return |
|
556 | 556 | if mode in (None, 'r', 'rb'): |
|
557 | 557 | return |
|
558 | 558 | if path.startswith(repo.sharedpath): |
|
559 | 559 | # truncate name relative to the repository (.hg) |
|
560 | 560 | path = path[len(repo.sharedpath) + 1:] |
|
561 | 561 | if repo._currentlock(repo._lockref) is None: |
|
562 | 562 | repo.ui.develwarn('write with no lock: "%s"' % path, |
|
563 | 563 | stacklevel=3) |
|
564 | 564 | return ret |
|
565 | 565 | return checksvfs |
|
566 | 566 | |
|
567 | 567 | def close(self): |
|
568 | 568 | self._writecaches() |
|
569 | 569 | |
|
570 | 570 | def _loadextensions(self): |
|
571 | 571 | extensions.loadall(self.ui) |
|
572 | 572 | |
|
573 | 573 | def _writecaches(self): |
|
574 | 574 | if self._revbranchcache: |
|
575 | 575 | self._revbranchcache.write() |
|
576 | 576 | |
|
577 | 577 | def _restrictcapabilities(self, caps): |
|
578 | 578 | if self.ui.configbool('experimental', 'bundle2-advertise'): |
|
579 | 579 | caps = set(caps) |
|
580 | 580 | capsblob = bundle2.encodecaps(bundle2.getrepocaps(self)) |
|
581 | 581 | caps.add('bundle2=' + urlreq.quote(capsblob)) |
|
582 | 582 | return caps |
|
583 | 583 | |
|
584 | 584 | def _applyopenerreqs(self): |
|
585 | 585 | self.svfs.options = dict((r, 1) for r in self.requirements |
|
586 | 586 | if r in self.openerreqs) |
|
587 | 587 | # experimental config: format.chunkcachesize |
|
588 | 588 | chunkcachesize = self.ui.configint('format', 'chunkcachesize') |
|
589 | 589 | if chunkcachesize is not None: |
|
590 | 590 | self.svfs.options['chunkcachesize'] = chunkcachesize |
|
591 | 591 | # experimental config: format.maxchainlen |
|
592 | 592 | maxchainlen = self.ui.configint('format', 'maxchainlen') |
|
593 | 593 | if maxchainlen is not None: |
|
594 | 594 | self.svfs.options['maxchainlen'] = maxchainlen |
|
595 | 595 | # experimental config: format.manifestcachesize |
|
596 | 596 | manifestcachesize = self.ui.configint('format', 'manifestcachesize') |
|
597 | 597 | if manifestcachesize is not None: |
|
598 | 598 | self.svfs.options['manifestcachesize'] = manifestcachesize |
|
599 | 599 | # experimental config: format.aggressivemergedeltas |
|
600 | 600 | aggressivemergedeltas = self.ui.configbool('format', |
|
601 | 601 | 'aggressivemergedeltas') |
|
602 | 602 | self.svfs.options['aggressivemergedeltas'] = aggressivemergedeltas |
|
603 | 603 | self.svfs.options['lazydeltabase'] = not scmutil.gddeltaconfig(self.ui) |
|
604 | 604 | chainspan = self.ui.configbytes('experimental', 'maxdeltachainspan') |
|
605 | 605 | if 0 <= chainspan: |
|
606 | 606 | self.svfs.options['maxdeltachainspan'] = chainspan |
|
607 | 607 | mmapindexthreshold = self.ui.configbytes('experimental', |
|
608 | 608 | 'mmapindexthreshold') |
|
609 | 609 | if mmapindexthreshold is not None: |
|
610 | 610 | self.svfs.options['mmapindexthreshold'] = mmapindexthreshold |
|
611 | 611 | withsparseread = self.ui.configbool('experimental', 'sparse-read') |
|
612 | 612 | srdensitythres = float(self.ui.config('experimental', |
|
613 | 613 | 'sparse-read.density-threshold')) |
|
614 | 614 | srmingapsize = self.ui.configbytes('experimental', |
|
615 | 615 | 'sparse-read.min-gap-size') |
|
616 | 616 | self.svfs.options['with-sparse-read'] = withsparseread |
|
617 | 617 | self.svfs.options['sparse-read-density-threshold'] = srdensitythres |
|
618 | 618 | self.svfs.options['sparse-read-min-gap-size'] = srmingapsize |
|
619 | 619 | |
|
620 | 620 | for r in self.requirements: |
|
621 | 621 | if r.startswith('exp-compression-'): |
|
622 | 622 | self.svfs.options['compengine'] = r[len('exp-compression-'):] |
|
623 | 623 | |
|
624 | 624 | # TODO move "revlogv2" to openerreqs once finalized. |
|
625 | 625 | if REVLOGV2_REQUIREMENT in self.requirements: |
|
626 | 626 | self.svfs.options['revlogv2'] = True |
|
627 | 627 | |
|
628 | 628 | def _writerequirements(self): |
|
629 | 629 | scmutil.writerequires(self.vfs, self.requirements) |
|
630 | 630 | |
|
631 | 631 | def _checknested(self, path): |
|
632 | 632 | """Determine if path is a legal nested repository.""" |
|
633 | 633 | if not path.startswith(self.root): |
|
634 | 634 | return False |
|
635 | 635 | subpath = path[len(self.root) + 1:] |
|
636 | 636 | normsubpath = util.pconvert(subpath) |
|
637 | 637 | |
|
638 | 638 | # XXX: Checking against the current working copy is wrong in |
|
639 | 639 | # the sense that it can reject things like |
|
640 | 640 | # |
|
641 | 641 | # $ hg cat -r 10 sub/x.txt |
|
642 | 642 | # |
|
643 | 643 | # if sub/ is no longer a subrepository in the working copy |
|
644 | 644 | # parent revision. |
|
645 | 645 | # |
|
646 | 646 | # However, it can of course also allow things that would have |
|
647 | 647 | # been rejected before, such as the above cat command if sub/ |
|
648 | 648 | # is a subrepository now, but was a normal directory before. |
|
649 | 649 | # The old path auditor would have rejected by mistake since it |
|
650 | 650 | # panics when it sees sub/.hg/. |
|
651 | 651 | # |
|
652 | 652 | # All in all, checking against the working copy seems sensible |
|
653 | 653 | # since we want to prevent access to nested repositories on |
|
654 | 654 | # the filesystem *now*. |
|
655 | 655 | ctx = self[None] |
|
656 | 656 | parts = util.splitpath(subpath) |
|
657 | 657 | while parts: |
|
658 | 658 | prefix = '/'.join(parts) |
|
659 | 659 | if prefix in ctx.substate: |
|
660 | 660 | if prefix == normsubpath: |
|
661 | 661 | return True |
|
662 | 662 | else: |
|
663 | 663 | sub = ctx.sub(prefix) |
|
664 | 664 | return sub.checknested(subpath[len(prefix) + 1:]) |
|
665 | 665 | else: |
|
666 | 666 | parts.pop() |
|
667 | 667 | return False |
|
668 | 668 | |
|
669 | 669 | def peer(self): |
|
670 | 670 | return localpeer(self) # not cached to avoid reference cycle |
|
671 | 671 | |
|
672 | 672 | def unfiltered(self): |
|
673 | 673 | """Return unfiltered version of the repository |
|
674 | 674 | |
|
675 | 675 | Intended to be overwritten by filtered repo.""" |
|
676 | 676 | return self |
|
677 | 677 | |
|
678 | 678 | def filtered(self, name): |
|
679 | 679 | """Return a filtered version of a repository""" |
|
680 | 680 | cls = repoview.newtype(self.unfiltered().__class__) |
|
681 | 681 | return cls(self, name) |
|
682 | 682 | |
|
683 | 683 | @repofilecache('bookmarks', 'bookmarks.current') |
|
684 | 684 | def _bookmarks(self): |
|
685 | 685 | return bookmarks.bmstore(self) |
|
686 | 686 | |
|
687 | 687 | @property |
|
688 | 688 | def _activebookmark(self): |
|
689 | 689 | return self._bookmarks.active |
|
690 | 690 | |
|
691 |
# _phase |
|
|
692 |
# |
|
|
691 | # _phasesets depend on changelog. what we need is to call | |
|
692 | # _phasecache.invalidate() if '00changelog.i' was changed, but it | |
|
693 | 693 | # can't be easily expressed in filecache mechanism. |
|
694 | 694 | @storecache('phaseroots', '00changelog.i') |
|
695 | 695 | def _phasecache(self): |
|
696 | 696 | return phases.phasecache(self, self._phasedefaults) |
|
697 | 697 | |
|
698 | 698 | @storecache('obsstore') |
|
699 | 699 | def obsstore(self): |
|
700 | 700 | return obsolete.makestore(self.ui, self) |
|
701 | 701 | |
|
702 | 702 | @storecache('00changelog.i') |
|
703 | 703 | def changelog(self): |
|
704 | 704 | return changelog.changelog(self.svfs, |
|
705 | 705 | trypending=txnutil.mayhavepending(self.root)) |
|
706 | 706 | |
|
707 | 707 | def _constructmanifest(self): |
|
708 | 708 | # This is a temporary function while we migrate from manifest to |
|
709 | 709 | # manifestlog. It allows bundlerepo and unionrepo to intercept the |
|
710 | 710 | # manifest creation. |
|
711 | 711 | return manifest.manifestrevlog(self.svfs) |
|
712 | 712 | |
|
713 | 713 | @storecache('00manifest.i') |
|
714 | 714 | def manifestlog(self): |
|
715 | 715 | return manifest.manifestlog(self.svfs, self) |
|
716 | 716 | |
|
717 | 717 | @repofilecache('dirstate') |
|
718 | 718 | def dirstate(self): |
|
719 | 719 | sparsematchfn = lambda: sparse.matcher(self) |
|
720 | 720 | |
|
721 | 721 | return dirstate.dirstate(self.vfs, self.ui, self.root, |
|
722 | 722 | self._dirstatevalidate, sparsematchfn) |
|
723 | 723 | |
|
724 | 724 | def _dirstatevalidate(self, node): |
|
725 | 725 | try: |
|
726 | 726 | self.changelog.rev(node) |
|
727 | 727 | return node |
|
728 | 728 | except error.LookupError: |
|
729 | 729 | if not self._dirstatevalidatewarned: |
|
730 | 730 | self._dirstatevalidatewarned = True |
|
731 | 731 | self.ui.warn(_("warning: ignoring unknown" |
|
732 | 732 | " working parent %s!\n") % short(node)) |
|
733 | 733 | return nullid |
|
734 | 734 | |
|
735 | 735 | def __getitem__(self, changeid): |
|
736 | 736 | if changeid is None: |
|
737 | 737 | return context.workingctx(self) |
|
738 | 738 | if isinstance(changeid, slice): |
|
739 | 739 | # wdirrev isn't contiguous so the slice shouldn't include it |
|
740 | 740 | return [context.changectx(self, i) |
|
741 | 741 | for i in xrange(*changeid.indices(len(self))) |
|
742 | 742 | if i not in self.changelog.filteredrevs] |
|
743 | 743 | try: |
|
744 | 744 | return context.changectx(self, changeid) |
|
745 | 745 | except error.WdirUnsupported: |
|
746 | 746 | return context.workingctx(self) |
|
747 | 747 | |
|
748 | 748 | def __contains__(self, changeid): |
|
749 | 749 | """True if the given changeid exists |
|
750 | 750 | |
|
751 | 751 | error.LookupError is raised if an ambiguous node specified. |
|
752 | 752 | """ |
|
753 | 753 | try: |
|
754 | 754 | self[changeid] |
|
755 | 755 | return True |
|
756 | 756 | except error.RepoLookupError: |
|
757 | 757 | return False |
|
758 | 758 | |
|
759 | 759 | def __nonzero__(self): |
|
760 | 760 | return True |
|
761 | 761 | |
|
762 | 762 | __bool__ = __nonzero__ |
|
763 | 763 | |
|
764 | 764 | def __len__(self): |
|
765 | 765 | return len(self.changelog) |
|
766 | 766 | |
|
767 | 767 | def __iter__(self): |
|
768 | 768 | return iter(self.changelog) |
|
769 | 769 | |
|
770 | 770 | def revs(self, expr, *args): |
|
771 | 771 | '''Find revisions matching a revset. |
|
772 | 772 | |
|
773 | 773 | The revset is specified as a string ``expr`` that may contain |
|
774 | 774 | %-formatting to escape certain types. See ``revsetlang.formatspec``. |
|
775 | 775 | |
|
776 | 776 | Revset aliases from the configuration are not expanded. To expand |
|
777 | 777 | user aliases, consider calling ``scmutil.revrange()`` or |
|
778 | 778 | ``repo.anyrevs([expr], user=True)``. |
|
779 | 779 | |
|
780 | 780 | Returns a revset.abstractsmartset, which is a list-like interface |
|
781 | 781 | that contains integer revisions. |
|
782 | 782 | ''' |
|
783 | 783 | expr = revsetlang.formatspec(expr, *args) |
|
784 | 784 | m = revset.match(None, expr) |
|
785 | 785 | return m(self) |
|
786 | 786 | |
|
787 | 787 | def set(self, expr, *args): |
|
788 | 788 | '''Find revisions matching a revset and emit changectx instances. |
|
789 | 789 | |
|
790 | 790 | This is a convenience wrapper around ``revs()`` that iterates the |
|
791 | 791 | result and is a generator of changectx instances. |
|
792 | 792 | |
|
793 | 793 | Revset aliases from the configuration are not expanded. To expand |
|
794 | 794 | user aliases, consider calling ``scmutil.revrange()``. |
|
795 | 795 | ''' |
|
796 | 796 | for r in self.revs(expr, *args): |
|
797 | 797 | yield self[r] |
|
798 | 798 | |
|
799 | 799 | def anyrevs(self, specs, user=False, localalias=None): |
|
800 | 800 | '''Find revisions matching one of the given revsets. |
|
801 | 801 | |
|
802 | 802 | Revset aliases from the configuration are not expanded by default. To |
|
803 | 803 | expand user aliases, specify ``user=True``. To provide some local |
|
804 | 804 | definitions overriding user aliases, set ``localalias`` to |
|
805 | 805 | ``{name: definitionstring}``. |
|
806 | 806 | ''' |
|
807 | 807 | if user: |
|
808 | 808 | m = revset.matchany(self.ui, specs, repo=self, |
|
809 | 809 | localalias=localalias) |
|
810 | 810 | else: |
|
811 | 811 | m = revset.matchany(None, specs, localalias=localalias) |
|
812 | 812 | return m(self) |
|
813 | 813 | |
|
814 | 814 | def url(self): |
|
815 | 815 | return 'file:' + self.root |
|
816 | 816 | |
|
817 | 817 | def hook(self, name, throw=False, **args): |
|
818 | 818 | """Call a hook, passing this repo instance. |
|
819 | 819 | |
|
820 | 820 | This a convenience method to aid invoking hooks. Extensions likely |
|
821 | 821 | won't call this unless they have registered a custom hook or are |
|
822 | 822 | replacing code that is expected to call a hook. |
|
823 | 823 | """ |
|
824 | 824 | return hook.hook(self.ui, self, name, throw, **args) |
|
825 | 825 | |
|
826 | 826 | @filteredpropertycache |
|
827 | 827 | def _tagscache(self): |
|
828 | 828 | '''Returns a tagscache object that contains various tags related |
|
829 | 829 | caches.''' |
|
830 | 830 | |
|
831 | 831 | # This simplifies its cache management by having one decorated |
|
832 | 832 | # function (this one) and the rest simply fetch things from it. |
|
833 | 833 | class tagscache(object): |
|
834 | 834 | def __init__(self): |
|
835 | 835 | # These two define the set of tags for this repository. tags |
|
836 | 836 | # maps tag name to node; tagtypes maps tag name to 'global' or |
|
837 | 837 | # 'local'. (Global tags are defined by .hgtags across all |
|
838 | 838 | # heads, and local tags are defined in .hg/localtags.) |
|
839 | 839 | # They constitute the in-memory cache of tags. |
|
840 | 840 | self.tags = self.tagtypes = None |
|
841 | 841 | |
|
842 | 842 | self.nodetagscache = self.tagslist = None |
|
843 | 843 | |
|
844 | 844 | cache = tagscache() |
|
845 | 845 | cache.tags, cache.tagtypes = self._findtags() |
|
846 | 846 | |
|
847 | 847 | return cache |
|
848 | 848 | |
|
849 | 849 | def tags(self): |
|
850 | 850 | '''return a mapping of tag to node''' |
|
851 | 851 | t = {} |
|
852 | 852 | if self.changelog.filteredrevs: |
|
853 | 853 | tags, tt = self._findtags() |
|
854 | 854 | else: |
|
855 | 855 | tags = self._tagscache.tags |
|
856 | 856 | for k, v in tags.iteritems(): |
|
857 | 857 | try: |
|
858 | 858 | # ignore tags to unknown nodes |
|
859 | 859 | self.changelog.rev(v) |
|
860 | 860 | t[k] = v |
|
861 | 861 | except (error.LookupError, ValueError): |
|
862 | 862 | pass |
|
863 | 863 | return t |
|
864 | 864 | |
|
865 | 865 | def _findtags(self): |
|
866 | 866 | '''Do the hard work of finding tags. Return a pair of dicts |
|
867 | 867 | (tags, tagtypes) where tags maps tag name to node, and tagtypes |
|
868 | 868 | maps tag name to a string like \'global\' or \'local\'. |
|
869 | 869 | Subclasses or extensions are free to add their own tags, but |
|
870 | 870 | should be aware that the returned dicts will be retained for the |
|
871 | 871 | duration of the localrepo object.''' |
|
872 | 872 | |
|
873 | 873 | # XXX what tagtype should subclasses/extensions use? Currently |
|
874 | 874 | # mq and bookmarks add tags, but do not set the tagtype at all. |
|
875 | 875 | # Should each extension invent its own tag type? Should there |
|
876 | 876 | # be one tagtype for all such "virtual" tags? Or is the status |
|
877 | 877 | # quo fine? |
|
878 | 878 | |
|
879 | 879 | |
|
880 | 880 | # map tag name to (node, hist) |
|
881 | 881 | alltags = tagsmod.findglobaltags(self.ui, self) |
|
882 | 882 | # map tag name to tag type |
|
883 | 883 | tagtypes = dict((tag, 'global') for tag in alltags) |
|
884 | 884 | |
|
885 | 885 | tagsmod.readlocaltags(self.ui, self, alltags, tagtypes) |
|
886 | 886 | |
|
887 | 887 | # Build the return dicts. Have to re-encode tag names because |
|
888 | 888 | # the tags module always uses UTF-8 (in order not to lose info |
|
889 | 889 | # writing to the cache), but the rest of Mercurial wants them in |
|
890 | 890 | # local encoding. |
|
891 | 891 | tags = {} |
|
892 | 892 | for (name, (node, hist)) in alltags.iteritems(): |
|
893 | 893 | if node != nullid: |
|
894 | 894 | tags[encoding.tolocal(name)] = node |
|
895 | 895 | tags['tip'] = self.changelog.tip() |
|
896 | 896 | tagtypes = dict([(encoding.tolocal(name), value) |
|
897 | 897 | for (name, value) in tagtypes.iteritems()]) |
|
898 | 898 | return (tags, tagtypes) |
|
899 | 899 | |
|
900 | 900 | def tagtype(self, tagname): |
|
901 | 901 | ''' |
|
902 | 902 | return the type of the given tag. result can be: |
|
903 | 903 | |
|
904 | 904 | 'local' : a local tag |
|
905 | 905 | 'global' : a global tag |
|
906 | 906 | None : tag does not exist |
|
907 | 907 | ''' |
|
908 | 908 | |
|
909 | 909 | return self._tagscache.tagtypes.get(tagname) |
|
910 | 910 | |
|
911 | 911 | def tagslist(self): |
|
912 | 912 | '''return a list of tags ordered by revision''' |
|
913 | 913 | if not self._tagscache.tagslist: |
|
914 | 914 | l = [] |
|
915 | 915 | for t, n in self.tags().iteritems(): |
|
916 | 916 | l.append((self.changelog.rev(n), t, n)) |
|
917 | 917 | self._tagscache.tagslist = [(t, n) for r, t, n in sorted(l)] |
|
918 | 918 | |
|
919 | 919 | return self._tagscache.tagslist |
|
920 | 920 | |
|
921 | 921 | def nodetags(self, node): |
|
922 | 922 | '''return the tags associated with a node''' |
|
923 | 923 | if not self._tagscache.nodetagscache: |
|
924 | 924 | nodetagscache = {} |
|
925 | 925 | for t, n in self._tagscache.tags.iteritems(): |
|
926 | 926 | nodetagscache.setdefault(n, []).append(t) |
|
927 | 927 | for tags in nodetagscache.itervalues(): |
|
928 | 928 | tags.sort() |
|
929 | 929 | self._tagscache.nodetagscache = nodetagscache |
|
930 | 930 | return self._tagscache.nodetagscache.get(node, []) |
|
931 | 931 | |
|
932 | 932 | def nodebookmarks(self, node): |
|
933 | 933 | """return the list of bookmarks pointing to the specified node""" |
|
934 | 934 | marks = [] |
|
935 | 935 | for bookmark, n in self._bookmarks.iteritems(): |
|
936 | 936 | if n == node: |
|
937 | 937 | marks.append(bookmark) |
|
938 | 938 | return sorted(marks) |
|
939 | 939 | |
|
940 | 940 | def branchmap(self): |
|
941 | 941 | '''returns a dictionary {branch: [branchheads]} with branchheads |
|
942 | 942 | ordered by increasing revision number''' |
|
943 | 943 | branchmap.updatecache(self) |
|
944 | 944 | return self._branchcaches[self.filtername] |
|
945 | 945 | |
|
946 | 946 | @unfilteredmethod |
|
947 | 947 | def revbranchcache(self): |
|
948 | 948 | if not self._revbranchcache: |
|
949 | 949 | self._revbranchcache = branchmap.revbranchcache(self.unfiltered()) |
|
950 | 950 | return self._revbranchcache |
|
951 | 951 | |
|
952 | 952 | def branchtip(self, branch, ignoremissing=False): |
|
953 | 953 | '''return the tip node for a given branch |
|
954 | 954 | |
|
955 | 955 | If ignoremissing is True, then this method will not raise an error. |
|
956 | 956 | This is helpful for callers that only expect None for a missing branch |
|
957 | 957 | (e.g. namespace). |
|
958 | 958 | |
|
959 | 959 | ''' |
|
960 | 960 | try: |
|
961 | 961 | return self.branchmap().branchtip(branch) |
|
962 | 962 | except KeyError: |
|
963 | 963 | if not ignoremissing: |
|
964 | 964 | raise error.RepoLookupError(_("unknown branch '%s'") % branch) |
|
965 | 965 | else: |
|
966 | 966 | pass |
|
967 | 967 | |
|
968 | 968 | def lookup(self, key): |
|
969 | 969 | return self[key].node() |
|
970 | 970 | |
|
971 | 971 | def lookupbranch(self, key, remote=None): |
|
972 | 972 | repo = remote or self |
|
973 | 973 | if key in repo.branchmap(): |
|
974 | 974 | return key |
|
975 | 975 | |
|
976 | 976 | repo = (remote and remote.local()) and remote or self |
|
977 | 977 | return repo[key].branch() |
|
978 | 978 | |
|
979 | 979 | def known(self, nodes): |
|
980 | 980 | cl = self.changelog |
|
981 | 981 | nm = cl.nodemap |
|
982 | 982 | filtered = cl.filteredrevs |
|
983 | 983 | result = [] |
|
984 | 984 | for n in nodes: |
|
985 | 985 | r = nm.get(n) |
|
986 | 986 | resp = not (r is None or r in filtered) |
|
987 | 987 | result.append(resp) |
|
988 | 988 | return result |
|
989 | 989 | |
|
990 | 990 | def local(self): |
|
991 | 991 | return self |
|
992 | 992 | |
|
993 | 993 | def publishing(self): |
|
994 | 994 | # it's safe (and desirable) to trust the publish flag unconditionally |
|
995 | 995 | # so that we don't finalize changes shared between users via ssh or nfs |
|
996 | 996 | return self.ui.configbool('phases', 'publish', untrusted=True) |
|
997 | 997 | |
|
998 | 998 | def cancopy(self): |
|
999 | 999 | # so statichttprepo's override of local() works |
|
1000 | 1000 | if not self.local(): |
|
1001 | 1001 | return False |
|
1002 | 1002 | if not self.publishing(): |
|
1003 | 1003 | return True |
|
1004 | 1004 | # if publishing we can't copy if there is filtered content |
|
1005 | 1005 | return not self.filtered('visible').changelog.filteredrevs |
|
1006 | 1006 | |
|
1007 | 1007 | def shared(self): |
|
1008 | 1008 | '''the type of shared repository (None if not shared)''' |
|
1009 | 1009 | if self.sharedpath != self.path: |
|
1010 | 1010 | return 'store' |
|
1011 | 1011 | return None |
|
1012 | 1012 | |
|
1013 | 1013 | def wjoin(self, f, *insidef): |
|
1014 | 1014 | return self.vfs.reljoin(self.root, f, *insidef) |
|
1015 | 1015 | |
|
1016 | 1016 | def file(self, f): |
|
1017 | 1017 | if f[0] == '/': |
|
1018 | 1018 | f = f[1:] |
|
1019 | 1019 | return filelog.filelog(self.svfs, f) |
|
1020 | 1020 | |
|
1021 | 1021 | def changectx(self, changeid): |
|
1022 | 1022 | return self[changeid] |
|
1023 | 1023 | |
|
1024 | 1024 | def setparents(self, p1, p2=nullid): |
|
1025 | 1025 | with self.dirstate.parentchange(): |
|
1026 | 1026 | copies = self.dirstate.setparents(p1, p2) |
|
1027 | 1027 | pctx = self[p1] |
|
1028 | 1028 | if copies: |
|
1029 | 1029 | # Adjust copy records, the dirstate cannot do it, it |
|
1030 | 1030 | # requires access to parents manifests. Preserve them |
|
1031 | 1031 | # only for entries added to first parent. |
|
1032 | 1032 | for f in copies: |
|
1033 | 1033 | if f not in pctx and copies[f] in pctx: |
|
1034 | 1034 | self.dirstate.copy(copies[f], f) |
|
1035 | 1035 | if p2 == nullid: |
|
1036 | 1036 | for f, s in sorted(self.dirstate.copies().items()): |
|
1037 | 1037 | if f not in pctx and s not in pctx: |
|
1038 | 1038 | self.dirstate.copy(None, f) |
|
1039 | 1039 | |
|
1040 | 1040 | def filectx(self, path, changeid=None, fileid=None): |
|
1041 | 1041 | """changeid can be a changeset revision, node, or tag. |
|
1042 | 1042 | fileid can be a file revision or node.""" |
|
1043 | 1043 | return context.filectx(self, path, changeid, fileid) |
|
1044 | 1044 | |
|
1045 | 1045 | def getcwd(self): |
|
1046 | 1046 | return self.dirstate.getcwd() |
|
1047 | 1047 | |
|
1048 | 1048 | def pathto(self, f, cwd=None): |
|
1049 | 1049 | return self.dirstate.pathto(f, cwd) |
|
1050 | 1050 | |
|
1051 | 1051 | def _loadfilter(self, filter): |
|
1052 | 1052 | if filter not in self.filterpats: |
|
1053 | 1053 | l = [] |
|
1054 | 1054 | for pat, cmd in self.ui.configitems(filter): |
|
1055 | 1055 | if cmd == '!': |
|
1056 | 1056 | continue |
|
1057 | 1057 | mf = matchmod.match(self.root, '', [pat]) |
|
1058 | 1058 | fn = None |
|
1059 | 1059 | params = cmd |
|
1060 | 1060 | for name, filterfn in self._datafilters.iteritems(): |
|
1061 | 1061 | if cmd.startswith(name): |
|
1062 | 1062 | fn = filterfn |
|
1063 | 1063 | params = cmd[len(name):].lstrip() |
|
1064 | 1064 | break |
|
1065 | 1065 | if not fn: |
|
1066 | 1066 | fn = lambda s, c, **kwargs: util.filter(s, c) |
|
1067 | 1067 | # Wrap old filters not supporting keyword arguments |
|
1068 | 1068 | if not inspect.getargspec(fn)[2]: |
|
1069 | 1069 | oldfn = fn |
|
1070 | 1070 | fn = lambda s, c, **kwargs: oldfn(s, c) |
|
1071 | 1071 | l.append((mf, fn, params)) |
|
1072 | 1072 | self.filterpats[filter] = l |
|
1073 | 1073 | return self.filterpats[filter] |
|
1074 | 1074 | |
|
1075 | 1075 | def _filter(self, filterpats, filename, data): |
|
1076 | 1076 | for mf, fn, cmd in filterpats: |
|
1077 | 1077 | if mf(filename): |
|
1078 | 1078 | self.ui.debug("filtering %s through %s\n" % (filename, cmd)) |
|
1079 | 1079 | data = fn(data, cmd, ui=self.ui, repo=self, filename=filename) |
|
1080 | 1080 | break |
|
1081 | 1081 | |
|
1082 | 1082 | return data |
|
1083 | 1083 | |
|
1084 | 1084 | @unfilteredpropertycache |
|
1085 | 1085 | def _encodefilterpats(self): |
|
1086 | 1086 | return self._loadfilter('encode') |
|
1087 | 1087 | |
|
1088 | 1088 | @unfilteredpropertycache |
|
1089 | 1089 | def _decodefilterpats(self): |
|
1090 | 1090 | return self._loadfilter('decode') |
|
1091 | 1091 | |
|
1092 | 1092 | def adddatafilter(self, name, filter): |
|
1093 | 1093 | self._datafilters[name] = filter |
|
1094 | 1094 | |
|
1095 | 1095 | def wread(self, filename): |
|
1096 | 1096 | if self.wvfs.islink(filename): |
|
1097 | 1097 | data = self.wvfs.readlink(filename) |
|
1098 | 1098 | else: |
|
1099 | 1099 | data = self.wvfs.read(filename) |
|
1100 | 1100 | return self._filter(self._encodefilterpats, filename, data) |
|
1101 | 1101 | |
|
1102 | 1102 | def wwrite(self, filename, data, flags, backgroundclose=False): |
|
1103 | 1103 | """write ``data`` into ``filename`` in the working directory |
|
1104 | 1104 | |
|
1105 | 1105 | This returns length of written (maybe decoded) data. |
|
1106 | 1106 | """ |
|
1107 | 1107 | data = self._filter(self._decodefilterpats, filename, data) |
|
1108 | 1108 | if 'l' in flags: |
|
1109 | 1109 | self.wvfs.symlink(data, filename) |
|
1110 | 1110 | else: |
|
1111 | 1111 | self.wvfs.write(filename, data, backgroundclose=backgroundclose) |
|
1112 | 1112 | if 'x' in flags: |
|
1113 | 1113 | self.wvfs.setflags(filename, False, True) |
|
1114 | 1114 | return len(data) |
|
1115 | 1115 | |
|
1116 | 1116 | def wwritedata(self, filename, data): |
|
1117 | 1117 | return self._filter(self._decodefilterpats, filename, data) |
|
1118 | 1118 | |
|
1119 | 1119 | def currenttransaction(self): |
|
1120 | 1120 | """return the current transaction or None if non exists""" |
|
1121 | 1121 | if self._transref: |
|
1122 | 1122 | tr = self._transref() |
|
1123 | 1123 | else: |
|
1124 | 1124 | tr = None |
|
1125 | 1125 | |
|
1126 | 1126 | if tr and tr.running(): |
|
1127 | 1127 | return tr |
|
1128 | 1128 | return None |
|
1129 | 1129 | |
|
1130 | 1130 | def transaction(self, desc, report=None): |
|
1131 | 1131 | if (self.ui.configbool('devel', 'all-warnings') |
|
1132 | 1132 | or self.ui.configbool('devel', 'check-locks')): |
|
1133 | 1133 | if self._currentlock(self._lockref) is None: |
|
1134 | 1134 | raise error.ProgrammingError('transaction requires locking') |
|
1135 | 1135 | tr = self.currenttransaction() |
|
1136 | 1136 | if tr is not None: |
|
1137 | 1137 | scmutil.registersummarycallback(self, tr, desc) |
|
1138 | 1138 | return tr.nest() |
|
1139 | 1139 | |
|
1140 | 1140 | # abort here if the journal already exists |
|
1141 | 1141 | if self.svfs.exists("journal"): |
|
1142 | 1142 | raise error.RepoError( |
|
1143 | 1143 | _("abandoned transaction found"), |
|
1144 | 1144 | hint=_("run 'hg recover' to clean up transaction")) |
|
1145 | 1145 | |
|
1146 | 1146 | idbase = "%.40f#%f" % (random.random(), time.time()) |
|
1147 | 1147 | ha = hex(hashlib.sha1(idbase).digest()) |
|
1148 | 1148 | txnid = 'TXN:' + ha |
|
1149 | 1149 | self.hook('pretxnopen', throw=True, txnname=desc, txnid=txnid) |
|
1150 | 1150 | |
|
1151 | 1151 | self._writejournal(desc) |
|
1152 | 1152 | renames = [(vfs, x, undoname(x)) for vfs, x in self._journalfiles()] |
|
1153 | 1153 | if report: |
|
1154 | 1154 | rp = report |
|
1155 | 1155 | else: |
|
1156 | 1156 | rp = self.ui.warn |
|
1157 | 1157 | vfsmap = {'plain': self.vfs} # root of .hg/ |
|
1158 | 1158 | # we must avoid cyclic reference between repo and transaction. |
|
1159 | 1159 | reporef = weakref.ref(self) |
|
1160 | 1160 | # Code to track tag movement |
|
1161 | 1161 | # |
|
1162 | 1162 | # Since tags are all handled as file content, it is actually quite hard |
|
1163 | 1163 | # to track these movement from a code perspective. So we fallback to a |
|
1164 | 1164 | # tracking at the repository level. One could envision to track changes |
|
1165 | 1165 | # to the '.hgtags' file through changegroup apply but that fails to |
|
1166 | 1166 | # cope with case where transaction expose new heads without changegroup |
|
1167 | 1167 | # being involved (eg: phase movement). |
|
1168 | 1168 | # |
|
1169 | 1169 | # For now, We gate the feature behind a flag since this likely comes |
|
1170 | 1170 | # with performance impacts. The current code run more often than needed |
|
1171 | 1171 | # and do not use caches as much as it could. The current focus is on |
|
1172 | 1172 | # the behavior of the feature so we disable it by default. The flag |
|
1173 | 1173 | # will be removed when we are happy with the performance impact. |
|
1174 | 1174 | # |
|
1175 | 1175 | # Once this feature is no longer experimental move the following |
|
1176 | 1176 | # documentation to the appropriate help section: |
|
1177 | 1177 | # |
|
1178 | 1178 | # The ``HG_TAG_MOVED`` variable will be set if the transaction touched |
|
1179 | 1179 | # tags (new or changed or deleted tags). In addition the details of |
|
1180 | 1180 | # these changes are made available in a file at: |
|
1181 | 1181 | # ``REPOROOT/.hg/changes/tags.changes``. |
|
1182 | 1182 | # Make sure you check for HG_TAG_MOVED before reading that file as it |
|
1183 | 1183 | # might exist from a previous transaction even if no tag were touched |
|
1184 | 1184 | # in this one. Changes are recorded in a line base format:: |
|
1185 | 1185 | # |
|
1186 | 1186 | # <action> <hex-node> <tag-name>\n |
|
1187 | 1187 | # |
|
1188 | 1188 | # Actions are defined as follow: |
|
1189 | 1189 | # "-R": tag is removed, |
|
1190 | 1190 | # "+A": tag is added, |
|
1191 | 1191 | # "-M": tag is moved (old value), |
|
1192 | 1192 | # "+M": tag is moved (new value), |
|
1193 | 1193 | tracktags = lambda x: None |
|
1194 | 1194 | # experimental config: experimental.hook-track-tags |
|
1195 | 1195 | shouldtracktags = self.ui.configbool('experimental', 'hook-track-tags') |
|
1196 | 1196 | if desc != 'strip' and shouldtracktags: |
|
1197 | 1197 | oldheads = self.changelog.headrevs() |
|
1198 | 1198 | def tracktags(tr2): |
|
1199 | 1199 | repo = reporef() |
|
1200 | 1200 | oldfnodes = tagsmod.fnoderevs(repo.ui, repo, oldheads) |
|
1201 | 1201 | newheads = repo.changelog.headrevs() |
|
1202 | 1202 | newfnodes = tagsmod.fnoderevs(repo.ui, repo, newheads) |
|
1203 | 1203 | # notes: we compare lists here. |
|
1204 | 1204 | # As we do it only once buiding set would not be cheaper |
|
1205 | 1205 | changes = tagsmod.difftags(repo.ui, repo, oldfnodes, newfnodes) |
|
1206 | 1206 | if changes: |
|
1207 | 1207 | tr2.hookargs['tag_moved'] = '1' |
|
1208 | 1208 | with repo.vfs('changes/tags.changes', 'w', |
|
1209 | 1209 | atomictemp=True) as changesfile: |
|
1210 | 1210 | # note: we do not register the file to the transaction |
|
1211 | 1211 | # because we needs it to still exist on the transaction |
|
1212 | 1212 | # is close (for txnclose hooks) |
|
1213 | 1213 | tagsmod.writediff(changesfile, changes) |
|
1214 | 1214 | def validate(tr2): |
|
1215 | 1215 | """will run pre-closing hooks""" |
|
1216 | 1216 | # XXX the transaction API is a bit lacking here so we take a hacky |
|
1217 | 1217 | # path for now |
|
1218 | 1218 | # |
|
1219 | 1219 | # We cannot add this as a "pending" hooks since the 'tr.hookargs' |
|
1220 | 1220 | # dict is copied before these run. In addition we needs the data |
|
1221 | 1221 | # available to in memory hooks too. |
|
1222 | 1222 | # |
|
1223 | 1223 | # Moreover, we also need to make sure this runs before txnclose |
|
1224 | 1224 | # hooks and there is no "pending" mechanism that would execute |
|
1225 | 1225 | # logic only if hooks are about to run. |
|
1226 | 1226 | # |
|
1227 | 1227 | # Fixing this limitation of the transaction is also needed to track |
|
1228 | 1228 | # other families of changes (bookmarks, phases, obsolescence). |
|
1229 | 1229 | # |
|
1230 | 1230 | # This will have to be fixed before we remove the experimental |
|
1231 | 1231 | # gating. |
|
1232 | 1232 | tracktags(tr2) |
|
1233 | 1233 | repo = reporef() |
|
1234 | 1234 | if repo.ui.configbool('experimental', 'single-head-per-branch'): |
|
1235 | 1235 | scmutil.enforcesinglehead(repo, tr2, desc) |
|
1236 | 1236 | if hook.hashook(repo.ui, 'pretxnclose-bookmark'): |
|
1237 | 1237 | for name, (old, new) in sorted(tr.changes['bookmarks'].items()): |
|
1238 | 1238 | args = tr.hookargs.copy() |
|
1239 | 1239 | args.update(bookmarks.preparehookargs(name, old, new)) |
|
1240 | 1240 | repo.hook('pretxnclose-bookmark', throw=True, |
|
1241 | 1241 | txnname=desc, |
|
1242 | 1242 | **pycompat.strkwargs(args)) |
|
1243 | 1243 | if hook.hashook(repo.ui, 'pretxnclose-phase'): |
|
1244 | 1244 | cl = repo.unfiltered().changelog |
|
1245 | 1245 | for rev, (old, new) in tr.changes['phases'].items(): |
|
1246 | 1246 | args = tr.hookargs.copy() |
|
1247 | 1247 | node = hex(cl.node(rev)) |
|
1248 | 1248 | args.update(phases.preparehookargs(node, old, new)) |
|
1249 | 1249 | repo.hook('pretxnclose-phase', throw=True, txnname=desc, |
|
1250 | 1250 | **pycompat.strkwargs(args)) |
|
1251 | 1251 | |
|
1252 | 1252 | repo.hook('pretxnclose', throw=True, |
|
1253 | 1253 | txnname=desc, **pycompat.strkwargs(tr.hookargs)) |
|
1254 | 1254 | def releasefn(tr, success): |
|
1255 | 1255 | repo = reporef() |
|
1256 | 1256 | if success: |
|
1257 | 1257 | # this should be explicitly invoked here, because |
|
1258 | 1258 | # in-memory changes aren't written out at closing |
|
1259 | 1259 | # transaction, if tr.addfilegenerator (via |
|
1260 | 1260 | # dirstate.write or so) isn't invoked while |
|
1261 | 1261 | # transaction running |
|
1262 | 1262 | repo.dirstate.write(None) |
|
1263 | 1263 | else: |
|
1264 | 1264 | # discard all changes (including ones already written |
|
1265 | 1265 | # out) in this transaction |
|
1266 | 1266 | repo.dirstate.restorebackup(None, 'journal.dirstate') |
|
1267 | 1267 | |
|
1268 | 1268 | repo.invalidate(clearfilecache=True) |
|
1269 | 1269 | |
|
1270 | 1270 | tr = transaction.transaction(rp, self.svfs, vfsmap, |
|
1271 | 1271 | "journal", |
|
1272 | 1272 | "undo", |
|
1273 | 1273 | aftertrans(renames), |
|
1274 | 1274 | self.store.createmode, |
|
1275 | 1275 | validator=validate, |
|
1276 | 1276 | releasefn=releasefn, |
|
1277 | 1277 | checkambigfiles=_cachedfiles) |
|
1278 | 1278 | tr.changes['revs'] = xrange(0, 0) |
|
1279 | 1279 | tr.changes['obsmarkers'] = set() |
|
1280 | 1280 | tr.changes['phases'] = {} |
|
1281 | 1281 | tr.changes['bookmarks'] = {} |
|
1282 | 1282 | |
|
1283 | 1283 | tr.hookargs['txnid'] = txnid |
|
1284 | 1284 | # note: writing the fncache only during finalize mean that the file is |
|
1285 | 1285 | # outdated when running hooks. As fncache is used for streaming clone, |
|
1286 | 1286 | # this is not expected to break anything that happen during the hooks. |
|
1287 | 1287 | tr.addfinalize('flush-fncache', self.store.write) |
|
1288 | 1288 | def txnclosehook(tr2): |
|
1289 | 1289 | """To be run if transaction is successful, will schedule a hook run |
|
1290 | 1290 | """ |
|
1291 | 1291 | # Don't reference tr2 in hook() so we don't hold a reference. |
|
1292 | 1292 | # This reduces memory consumption when there are multiple |
|
1293 | 1293 | # transactions per lock. This can likely go away if issue5045 |
|
1294 | 1294 | # fixes the function accumulation. |
|
1295 | 1295 | hookargs = tr2.hookargs |
|
1296 | 1296 | |
|
1297 | 1297 | def hookfunc(): |
|
1298 | 1298 | repo = reporef() |
|
1299 | 1299 | if hook.hashook(repo.ui, 'txnclose-bookmark'): |
|
1300 | 1300 | bmchanges = sorted(tr.changes['bookmarks'].items()) |
|
1301 | 1301 | for name, (old, new) in bmchanges: |
|
1302 | 1302 | args = tr.hookargs.copy() |
|
1303 | 1303 | args.update(bookmarks.preparehookargs(name, old, new)) |
|
1304 | 1304 | repo.hook('txnclose-bookmark', throw=False, |
|
1305 | 1305 | txnname=desc, **pycompat.strkwargs(args)) |
|
1306 | 1306 | |
|
1307 | 1307 | if hook.hashook(repo.ui, 'txnclose-phase'): |
|
1308 | 1308 | cl = repo.unfiltered().changelog |
|
1309 | 1309 | phasemv = sorted(tr.changes['phases'].items()) |
|
1310 | 1310 | for rev, (old, new) in phasemv: |
|
1311 | 1311 | args = tr.hookargs.copy() |
|
1312 | 1312 | node = hex(cl.node(rev)) |
|
1313 | 1313 | args.update(phases.preparehookargs(node, old, new)) |
|
1314 | 1314 | repo.hook('txnclose-phase', throw=False, txnname=desc, |
|
1315 | 1315 | **pycompat.strkwargs(args)) |
|
1316 | 1316 | |
|
1317 | 1317 | repo.hook('txnclose', throw=False, txnname=desc, |
|
1318 | 1318 | **pycompat.strkwargs(hookargs)) |
|
1319 | 1319 | reporef()._afterlock(hookfunc) |
|
1320 | 1320 | tr.addfinalize('txnclose-hook', txnclosehook) |
|
1321 | 1321 | tr.addpostclose('warms-cache', self._buildcacheupdater(tr)) |
|
1322 | 1322 | def txnaborthook(tr2): |
|
1323 | 1323 | """To be run if transaction is aborted |
|
1324 | 1324 | """ |
|
1325 | 1325 | reporef().hook('txnabort', throw=False, txnname=desc, |
|
1326 | 1326 | **tr2.hookargs) |
|
1327 | 1327 | tr.addabort('txnabort-hook', txnaborthook) |
|
1328 | 1328 | # avoid eager cache invalidation. in-memory data should be identical |
|
1329 | 1329 | # to stored data if transaction has no error. |
|
1330 | 1330 | tr.addpostclose('refresh-filecachestats', self._refreshfilecachestats) |
|
1331 | 1331 | self._transref = weakref.ref(tr) |
|
1332 | 1332 | scmutil.registersummarycallback(self, tr, desc) |
|
1333 | 1333 | return tr |
|
1334 | 1334 | |
|
1335 | 1335 | def _journalfiles(self): |
|
1336 | 1336 | return ((self.svfs, 'journal'), |
|
1337 | 1337 | (self.vfs, 'journal.dirstate'), |
|
1338 | 1338 | (self.vfs, 'journal.branch'), |
|
1339 | 1339 | (self.vfs, 'journal.desc'), |
|
1340 | 1340 | (self.vfs, 'journal.bookmarks'), |
|
1341 | 1341 | (self.svfs, 'journal.phaseroots')) |
|
1342 | 1342 | |
|
1343 | 1343 | def undofiles(self): |
|
1344 | 1344 | return [(vfs, undoname(x)) for vfs, x in self._journalfiles()] |
|
1345 | 1345 | |
|
1346 | 1346 | @unfilteredmethod |
|
1347 | 1347 | def _writejournal(self, desc): |
|
1348 | 1348 | self.dirstate.savebackup(None, 'journal.dirstate') |
|
1349 | 1349 | self.vfs.write("journal.branch", |
|
1350 | 1350 | encoding.fromlocal(self.dirstate.branch())) |
|
1351 | 1351 | self.vfs.write("journal.desc", |
|
1352 | 1352 | "%d\n%s\n" % (len(self), desc)) |
|
1353 | 1353 | self.vfs.write("journal.bookmarks", |
|
1354 | 1354 | self.vfs.tryread("bookmarks")) |
|
1355 | 1355 | self.svfs.write("journal.phaseroots", |
|
1356 | 1356 | self.svfs.tryread("phaseroots")) |
|
1357 | 1357 | |
|
1358 | 1358 | def recover(self): |
|
1359 | 1359 | with self.lock(): |
|
1360 | 1360 | if self.svfs.exists("journal"): |
|
1361 | 1361 | self.ui.status(_("rolling back interrupted transaction\n")) |
|
1362 | 1362 | vfsmap = {'': self.svfs, |
|
1363 | 1363 | 'plain': self.vfs,} |
|
1364 | 1364 | transaction.rollback(self.svfs, vfsmap, "journal", |
|
1365 | 1365 | self.ui.warn, |
|
1366 | 1366 | checkambigfiles=_cachedfiles) |
|
1367 | 1367 | self.invalidate() |
|
1368 | 1368 | return True |
|
1369 | 1369 | else: |
|
1370 | 1370 | self.ui.warn(_("no interrupted transaction available\n")) |
|
1371 | 1371 | return False |
|
1372 | 1372 | |
|
1373 | 1373 | def rollback(self, dryrun=False, force=False): |
|
1374 | 1374 | wlock = lock = dsguard = None |
|
1375 | 1375 | try: |
|
1376 | 1376 | wlock = self.wlock() |
|
1377 | 1377 | lock = self.lock() |
|
1378 | 1378 | if self.svfs.exists("undo"): |
|
1379 | 1379 | dsguard = dirstateguard.dirstateguard(self, 'rollback') |
|
1380 | 1380 | |
|
1381 | 1381 | return self._rollback(dryrun, force, dsguard) |
|
1382 | 1382 | else: |
|
1383 | 1383 | self.ui.warn(_("no rollback information available\n")) |
|
1384 | 1384 | return 1 |
|
1385 | 1385 | finally: |
|
1386 | 1386 | release(dsguard, lock, wlock) |
|
1387 | 1387 | |
|
1388 | 1388 | @unfilteredmethod # Until we get smarter cache management |
|
1389 | 1389 | def _rollback(self, dryrun, force, dsguard): |
|
1390 | 1390 | ui = self.ui |
|
1391 | 1391 | try: |
|
1392 | 1392 | args = self.vfs.read('undo.desc').splitlines() |
|
1393 | 1393 | (oldlen, desc, detail) = (int(args[0]), args[1], None) |
|
1394 | 1394 | if len(args) >= 3: |
|
1395 | 1395 | detail = args[2] |
|
1396 | 1396 | oldtip = oldlen - 1 |
|
1397 | 1397 | |
|
1398 | 1398 | if detail and ui.verbose: |
|
1399 | 1399 | msg = (_('repository tip rolled back to revision %d' |
|
1400 | 1400 | ' (undo %s: %s)\n') |
|
1401 | 1401 | % (oldtip, desc, detail)) |
|
1402 | 1402 | else: |
|
1403 | 1403 | msg = (_('repository tip rolled back to revision %d' |
|
1404 | 1404 | ' (undo %s)\n') |
|
1405 | 1405 | % (oldtip, desc)) |
|
1406 | 1406 | except IOError: |
|
1407 | 1407 | msg = _('rolling back unknown transaction\n') |
|
1408 | 1408 | desc = None |
|
1409 | 1409 | |
|
1410 | 1410 | if not force and self['.'] != self['tip'] and desc == 'commit': |
|
1411 | 1411 | raise error.Abort( |
|
1412 | 1412 | _('rollback of last commit while not checked out ' |
|
1413 | 1413 | 'may lose data'), hint=_('use -f to force')) |
|
1414 | 1414 | |
|
1415 | 1415 | ui.status(msg) |
|
1416 | 1416 | if dryrun: |
|
1417 | 1417 | return 0 |
|
1418 | 1418 | |
|
1419 | 1419 | parents = self.dirstate.parents() |
|
1420 | 1420 | self.destroying() |
|
1421 | 1421 | vfsmap = {'plain': self.vfs, '': self.svfs} |
|
1422 | 1422 | transaction.rollback(self.svfs, vfsmap, 'undo', ui.warn, |
|
1423 | 1423 | checkambigfiles=_cachedfiles) |
|
1424 | 1424 | if self.vfs.exists('undo.bookmarks'): |
|
1425 | 1425 | self.vfs.rename('undo.bookmarks', 'bookmarks', checkambig=True) |
|
1426 | 1426 | if self.svfs.exists('undo.phaseroots'): |
|
1427 | 1427 | self.svfs.rename('undo.phaseroots', 'phaseroots', checkambig=True) |
|
1428 | 1428 | self.invalidate() |
|
1429 | 1429 | |
|
1430 | 1430 | parentgone = (parents[0] not in self.changelog.nodemap or |
|
1431 | 1431 | parents[1] not in self.changelog.nodemap) |
|
1432 | 1432 | if parentgone: |
|
1433 | 1433 | # prevent dirstateguard from overwriting already restored one |
|
1434 | 1434 | dsguard.close() |
|
1435 | 1435 | |
|
1436 | 1436 | self.dirstate.restorebackup(None, 'undo.dirstate') |
|
1437 | 1437 | try: |
|
1438 | 1438 | branch = self.vfs.read('undo.branch') |
|
1439 | 1439 | self.dirstate.setbranch(encoding.tolocal(branch)) |
|
1440 | 1440 | except IOError: |
|
1441 | 1441 | ui.warn(_('named branch could not be reset: ' |
|
1442 | 1442 | 'current branch is still \'%s\'\n') |
|
1443 | 1443 | % self.dirstate.branch()) |
|
1444 | 1444 | |
|
1445 | 1445 | parents = tuple([p.rev() for p in self[None].parents()]) |
|
1446 | 1446 | if len(parents) > 1: |
|
1447 | 1447 | ui.status(_('working directory now based on ' |
|
1448 | 1448 | 'revisions %d and %d\n') % parents) |
|
1449 | 1449 | else: |
|
1450 | 1450 | ui.status(_('working directory now based on ' |
|
1451 | 1451 | 'revision %d\n') % parents) |
|
1452 | 1452 | mergemod.mergestate.clean(self, self['.'].node()) |
|
1453 | 1453 | |
|
1454 | 1454 | # TODO: if we know which new heads may result from this rollback, pass |
|
1455 | 1455 | # them to destroy(), which will prevent the branchhead cache from being |
|
1456 | 1456 | # invalidated. |
|
1457 | 1457 | self.destroyed() |
|
1458 | 1458 | return 0 |
|
1459 | 1459 | |
|
1460 | 1460 | def _buildcacheupdater(self, newtransaction): |
|
1461 | 1461 | """called during transaction to build the callback updating cache |
|
1462 | 1462 | |
|
1463 | 1463 | Lives on the repository to help extension who might want to augment |
|
1464 | 1464 | this logic. For this purpose, the created transaction is passed to the |
|
1465 | 1465 | method. |
|
1466 | 1466 | """ |
|
1467 | 1467 | # we must avoid cyclic reference between repo and transaction. |
|
1468 | 1468 | reporef = weakref.ref(self) |
|
1469 | 1469 | def updater(tr): |
|
1470 | 1470 | repo = reporef() |
|
1471 | 1471 | repo.updatecaches(tr) |
|
1472 | 1472 | return updater |
|
1473 | 1473 | |
|
1474 | 1474 | @unfilteredmethod |
|
1475 | 1475 | def updatecaches(self, tr=None): |
|
1476 | 1476 | """warm appropriate caches |
|
1477 | 1477 | |
|
1478 | 1478 | If this function is called after a transaction closed. The transaction |
|
1479 | 1479 | will be available in the 'tr' argument. This can be used to selectively |
|
1480 | 1480 | update caches relevant to the changes in that transaction. |
|
1481 | 1481 | """ |
|
1482 | 1482 | if tr is not None and tr.hookargs.get('source') == 'strip': |
|
1483 | 1483 | # During strip, many caches are invalid but |
|
1484 | 1484 | # later call to `destroyed` will refresh them. |
|
1485 | 1485 | return |
|
1486 | 1486 | |
|
1487 | 1487 | if tr is None or tr.changes['revs']: |
|
1488 | 1488 | # updating the unfiltered branchmap should refresh all the others, |
|
1489 | 1489 | self.ui.debug('updating the branch cache\n') |
|
1490 | 1490 | branchmap.updatecache(self.filtered('served')) |
|
1491 | 1491 | |
|
1492 | 1492 | def invalidatecaches(self): |
|
1493 | 1493 | |
|
1494 | 1494 | if '_tagscache' in vars(self): |
|
1495 | 1495 | # can't use delattr on proxy |
|
1496 | 1496 | del self.__dict__['_tagscache'] |
|
1497 | 1497 | |
|
1498 | 1498 | self.unfiltered()._branchcaches.clear() |
|
1499 | 1499 | self.invalidatevolatilesets() |
|
1500 | 1500 | self._sparsesignaturecache.clear() |
|
1501 | 1501 | |
|
1502 | 1502 | def invalidatevolatilesets(self): |
|
1503 | 1503 | self.filteredrevcache.clear() |
|
1504 | 1504 | obsolete.clearobscaches(self) |
|
1505 | 1505 | |
|
1506 | 1506 | def invalidatedirstate(self): |
|
1507 | 1507 | '''Invalidates the dirstate, causing the next call to dirstate |
|
1508 | 1508 | to check if it was modified since the last time it was read, |
|
1509 | 1509 | rereading it if it has. |
|
1510 | 1510 | |
|
1511 | 1511 | This is different to dirstate.invalidate() that it doesn't always |
|
1512 | 1512 | rereads the dirstate. Use dirstate.invalidate() if you want to |
|
1513 | 1513 | explicitly read the dirstate again (i.e. restoring it to a previous |
|
1514 | 1514 | known good state).''' |
|
1515 | 1515 | if hasunfilteredcache(self, 'dirstate'): |
|
1516 | 1516 | for k in self.dirstate._filecache: |
|
1517 | 1517 | try: |
|
1518 | 1518 | delattr(self.dirstate, k) |
|
1519 | 1519 | except AttributeError: |
|
1520 | 1520 | pass |
|
1521 | 1521 | delattr(self.unfiltered(), 'dirstate') |
|
1522 | 1522 | |
|
1523 | 1523 | def invalidate(self, clearfilecache=False): |
|
1524 | 1524 | '''Invalidates both store and non-store parts other than dirstate |
|
1525 | 1525 | |
|
1526 | 1526 | If a transaction is running, invalidation of store is omitted, |
|
1527 | 1527 | because discarding in-memory changes might cause inconsistency |
|
1528 | 1528 | (e.g. incomplete fncache causes unintentional failure, but |
|
1529 | 1529 | redundant one doesn't). |
|
1530 | 1530 | ''' |
|
1531 | 1531 | unfiltered = self.unfiltered() # all file caches are stored unfiltered |
|
1532 | 1532 | for k in list(self._filecache.keys()): |
|
1533 | 1533 | # dirstate is invalidated separately in invalidatedirstate() |
|
1534 | 1534 | if k == 'dirstate': |
|
1535 | 1535 | continue |
|
1536 | 1536 | if (k == 'changelog' and |
|
1537 | 1537 | self.currenttransaction() and |
|
1538 | 1538 | self.changelog._delayed): |
|
1539 | 1539 | # The changelog object may store unwritten revisions. We don't |
|
1540 | 1540 | # want to lose them. |
|
1541 | 1541 | # TODO: Solve the problem instead of working around it. |
|
1542 | 1542 | continue |
|
1543 | 1543 | |
|
1544 | 1544 | if clearfilecache: |
|
1545 | 1545 | del self._filecache[k] |
|
1546 | 1546 | try: |
|
1547 | 1547 | delattr(unfiltered, k) |
|
1548 | 1548 | except AttributeError: |
|
1549 | 1549 | pass |
|
1550 | 1550 | self.invalidatecaches() |
|
1551 | 1551 | if not self.currenttransaction(): |
|
1552 | 1552 | # TODO: Changing contents of store outside transaction |
|
1553 | 1553 | # causes inconsistency. We should make in-memory store |
|
1554 | 1554 | # changes detectable, and abort if changed. |
|
1555 | 1555 | self.store.invalidatecaches() |
|
1556 | 1556 | |
|
1557 | 1557 | def invalidateall(self): |
|
1558 | 1558 | '''Fully invalidates both store and non-store parts, causing the |
|
1559 | 1559 | subsequent operation to reread any outside changes.''' |
|
1560 | 1560 | # extension should hook this to invalidate its caches |
|
1561 | 1561 | self.invalidate() |
|
1562 | 1562 | self.invalidatedirstate() |
|
1563 | 1563 | |
|
1564 | 1564 | @unfilteredmethod |
|
1565 | 1565 | def _refreshfilecachestats(self, tr): |
|
1566 | 1566 | """Reload stats of cached files so that they are flagged as valid""" |
|
1567 | 1567 | for k, ce in self._filecache.items(): |
|
1568 | 1568 | if k == 'dirstate' or k not in self.__dict__: |
|
1569 | 1569 | continue |
|
1570 | 1570 | ce.refresh() |
|
1571 | 1571 | |
|
1572 | 1572 | def _lock(self, vfs, lockname, wait, releasefn, acquirefn, desc, |
|
1573 | 1573 | inheritchecker=None, parentenvvar=None): |
|
1574 | 1574 | parentlock = None |
|
1575 | 1575 | # the contents of parentenvvar are used by the underlying lock to |
|
1576 | 1576 | # determine whether it can be inherited |
|
1577 | 1577 | if parentenvvar is not None: |
|
1578 | 1578 | parentlock = encoding.environ.get(parentenvvar) |
|
1579 | 1579 | |
|
1580 | 1580 | timeout = 0 |
|
1581 | 1581 | warntimeout = 0 |
|
1582 | 1582 | if wait: |
|
1583 | 1583 | timeout = self.ui.configint("ui", "timeout") |
|
1584 | 1584 | warntimeout = self.ui.configint("ui", "timeout.warn") |
|
1585 | 1585 | |
|
1586 | 1586 | l = lockmod.trylock(self.ui, vfs, lockname, timeout, warntimeout, |
|
1587 | 1587 | releasefn=releasefn, |
|
1588 | 1588 | acquirefn=acquirefn, desc=desc, |
|
1589 | 1589 | inheritchecker=inheritchecker, |
|
1590 | 1590 | parentlock=parentlock) |
|
1591 | 1591 | return l |
|
1592 | 1592 | |
|
1593 | 1593 | def _afterlock(self, callback): |
|
1594 | 1594 | """add a callback to be run when the repository is fully unlocked |
|
1595 | 1595 | |
|
1596 | 1596 | The callback will be executed when the outermost lock is released |
|
1597 | 1597 | (with wlock being higher level than 'lock').""" |
|
1598 | 1598 | for ref in (self._wlockref, self._lockref): |
|
1599 | 1599 | l = ref and ref() |
|
1600 | 1600 | if l and l.held: |
|
1601 | 1601 | l.postrelease.append(callback) |
|
1602 | 1602 | break |
|
1603 | 1603 | else: # no lock have been found. |
|
1604 | 1604 | callback() |
|
1605 | 1605 | |
|
1606 | 1606 | def lock(self, wait=True): |
|
1607 | 1607 | '''Lock the repository store (.hg/store) and return a weak reference |
|
1608 | 1608 | to the lock. Use this before modifying the store (e.g. committing or |
|
1609 | 1609 | stripping). If you are opening a transaction, get a lock as well.) |
|
1610 | 1610 | |
|
1611 | 1611 | If both 'lock' and 'wlock' must be acquired, ensure you always acquires |
|
1612 | 1612 | 'wlock' first to avoid a dead-lock hazard.''' |
|
1613 | 1613 | l = self._currentlock(self._lockref) |
|
1614 | 1614 | if l is not None: |
|
1615 | 1615 | l.lock() |
|
1616 | 1616 | return l |
|
1617 | 1617 | |
|
1618 | 1618 | l = self._lock(self.svfs, "lock", wait, None, |
|
1619 | 1619 | self.invalidate, _('repository %s') % self.origroot) |
|
1620 | 1620 | self._lockref = weakref.ref(l) |
|
1621 | 1621 | return l |
|
1622 | 1622 | |
|
1623 | 1623 | def _wlockchecktransaction(self): |
|
1624 | 1624 | if self.currenttransaction() is not None: |
|
1625 | 1625 | raise error.LockInheritanceContractViolation( |
|
1626 | 1626 | 'wlock cannot be inherited in the middle of a transaction') |
|
1627 | 1627 | |
|
1628 | 1628 | def wlock(self, wait=True): |
|
1629 | 1629 | '''Lock the non-store parts of the repository (everything under |
|
1630 | 1630 | .hg except .hg/store) and return a weak reference to the lock. |
|
1631 | 1631 | |
|
1632 | 1632 | Use this before modifying files in .hg. |
|
1633 | 1633 | |
|
1634 | 1634 | If both 'lock' and 'wlock' must be acquired, ensure you always acquires |
|
1635 | 1635 | 'wlock' first to avoid a dead-lock hazard.''' |
|
1636 | 1636 | l = self._wlockref and self._wlockref() |
|
1637 | 1637 | if l is not None and l.held: |
|
1638 | 1638 | l.lock() |
|
1639 | 1639 | return l |
|
1640 | 1640 | |
|
1641 | 1641 | # We do not need to check for non-waiting lock acquisition. Such |
|
1642 | 1642 | # acquisition would not cause dead-lock as they would just fail. |
|
1643 | 1643 | if wait and (self.ui.configbool('devel', 'all-warnings') |
|
1644 | 1644 | or self.ui.configbool('devel', 'check-locks')): |
|
1645 | 1645 | if self._currentlock(self._lockref) is not None: |
|
1646 | 1646 | self.ui.develwarn('"wlock" acquired after "lock"') |
|
1647 | 1647 | |
|
1648 | 1648 | def unlock(): |
|
1649 | 1649 | if self.dirstate.pendingparentchange(): |
|
1650 | 1650 | self.dirstate.invalidate() |
|
1651 | 1651 | else: |
|
1652 | 1652 | self.dirstate.write(None) |
|
1653 | 1653 | |
|
1654 | 1654 | self._filecache['dirstate'].refresh() |
|
1655 | 1655 | |
|
1656 | 1656 | l = self._lock(self.vfs, "wlock", wait, unlock, |
|
1657 | 1657 | self.invalidatedirstate, _('working directory of %s') % |
|
1658 | 1658 | self.origroot, |
|
1659 | 1659 | inheritchecker=self._wlockchecktransaction, |
|
1660 | 1660 | parentenvvar='HG_WLOCK_LOCKER') |
|
1661 | 1661 | self._wlockref = weakref.ref(l) |
|
1662 | 1662 | return l |
|
1663 | 1663 | |
|
1664 | 1664 | def _currentlock(self, lockref): |
|
1665 | 1665 | """Returns the lock if it's held, or None if it's not.""" |
|
1666 | 1666 | if lockref is None: |
|
1667 | 1667 | return None |
|
1668 | 1668 | l = lockref() |
|
1669 | 1669 | if l is None or not l.held: |
|
1670 | 1670 | return None |
|
1671 | 1671 | return l |
|
1672 | 1672 | |
|
1673 | 1673 | def currentwlock(self): |
|
1674 | 1674 | """Returns the wlock if it's held, or None if it's not.""" |
|
1675 | 1675 | return self._currentlock(self._wlockref) |
|
1676 | 1676 | |
|
1677 | 1677 | def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist): |
|
1678 | 1678 | """ |
|
1679 | 1679 | commit an individual file as part of a larger transaction |
|
1680 | 1680 | """ |
|
1681 | 1681 | |
|
1682 | 1682 | fname = fctx.path() |
|
1683 | 1683 | fparent1 = manifest1.get(fname, nullid) |
|
1684 | 1684 | fparent2 = manifest2.get(fname, nullid) |
|
1685 | 1685 | if isinstance(fctx, context.filectx): |
|
1686 | 1686 | node = fctx.filenode() |
|
1687 | 1687 | if node in [fparent1, fparent2]: |
|
1688 | 1688 | self.ui.debug('reusing %s filelog entry\n' % fname) |
|
1689 | 1689 | if manifest1.flags(fname) != fctx.flags(): |
|
1690 | 1690 | changelist.append(fname) |
|
1691 | 1691 | return node |
|
1692 | 1692 | |
|
1693 | 1693 | flog = self.file(fname) |
|
1694 | 1694 | meta = {} |
|
1695 | 1695 | copy = fctx.renamed() |
|
1696 | 1696 | if copy and copy[0] != fname: |
|
1697 | 1697 | # Mark the new revision of this file as a copy of another |
|
1698 | 1698 | # file. This copy data will effectively act as a parent |
|
1699 | 1699 | # of this new revision. If this is a merge, the first |
|
1700 | 1700 | # parent will be the nullid (meaning "look up the copy data") |
|
1701 | 1701 | # and the second one will be the other parent. For example: |
|
1702 | 1702 | # |
|
1703 | 1703 | # 0 --- 1 --- 3 rev1 changes file foo |
|
1704 | 1704 | # \ / rev2 renames foo to bar and changes it |
|
1705 | 1705 | # \- 2 -/ rev3 should have bar with all changes and |
|
1706 | 1706 | # should record that bar descends from |
|
1707 | 1707 | # bar in rev2 and foo in rev1 |
|
1708 | 1708 | # |
|
1709 | 1709 | # this allows this merge to succeed: |
|
1710 | 1710 | # |
|
1711 | 1711 | # 0 --- 1 --- 3 rev4 reverts the content change from rev2 |
|
1712 | 1712 | # \ / merging rev3 and rev4 should use bar@rev2 |
|
1713 | 1713 | # \- 2 --- 4 as the merge base |
|
1714 | 1714 | # |
|
1715 | 1715 | |
|
1716 | 1716 | cfname = copy[0] |
|
1717 | 1717 | crev = manifest1.get(cfname) |
|
1718 | 1718 | newfparent = fparent2 |
|
1719 | 1719 | |
|
1720 | 1720 | if manifest2: # branch merge |
|
1721 | 1721 | if fparent2 == nullid or crev is None: # copied on remote side |
|
1722 | 1722 | if cfname in manifest2: |
|
1723 | 1723 | crev = manifest2[cfname] |
|
1724 | 1724 | newfparent = fparent1 |
|
1725 | 1725 | |
|
1726 | 1726 | # Here, we used to search backwards through history to try to find |
|
1727 | 1727 | # where the file copy came from if the source of a copy was not in |
|
1728 | 1728 | # the parent directory. However, this doesn't actually make sense to |
|
1729 | 1729 | # do (what does a copy from something not in your working copy even |
|
1730 | 1730 | # mean?) and it causes bugs (eg, issue4476). Instead, we will warn |
|
1731 | 1731 | # the user that copy information was dropped, so if they didn't |
|
1732 | 1732 | # expect this outcome it can be fixed, but this is the correct |
|
1733 | 1733 | # behavior in this circumstance. |
|
1734 | 1734 | |
|
1735 | 1735 | if crev: |
|
1736 | 1736 | self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev))) |
|
1737 | 1737 | meta["copy"] = cfname |
|
1738 | 1738 | meta["copyrev"] = hex(crev) |
|
1739 | 1739 | fparent1, fparent2 = nullid, newfparent |
|
1740 | 1740 | else: |
|
1741 | 1741 | self.ui.warn(_("warning: can't find ancestor for '%s' " |
|
1742 | 1742 | "copied from '%s'!\n") % (fname, cfname)) |
|
1743 | 1743 | |
|
1744 | 1744 | elif fparent1 == nullid: |
|
1745 | 1745 | fparent1, fparent2 = fparent2, nullid |
|
1746 | 1746 | elif fparent2 != nullid: |
|
1747 | 1747 | # is one parent an ancestor of the other? |
|
1748 | 1748 | fparentancestors = flog.commonancestorsheads(fparent1, fparent2) |
|
1749 | 1749 | if fparent1 in fparentancestors: |
|
1750 | 1750 | fparent1, fparent2 = fparent2, nullid |
|
1751 | 1751 | elif fparent2 in fparentancestors: |
|
1752 | 1752 | fparent2 = nullid |
|
1753 | 1753 | |
|
1754 | 1754 | # is the file changed? |
|
1755 | 1755 | text = fctx.data() |
|
1756 | 1756 | if fparent2 != nullid or flog.cmp(fparent1, text) or meta: |
|
1757 | 1757 | changelist.append(fname) |
|
1758 | 1758 | return flog.add(text, meta, tr, linkrev, fparent1, fparent2) |
|
1759 | 1759 | # are just the flags changed during merge? |
|
1760 | 1760 | elif fname in manifest1 and manifest1.flags(fname) != fctx.flags(): |
|
1761 | 1761 | changelist.append(fname) |
|
1762 | 1762 | |
|
1763 | 1763 | return fparent1 |
|
1764 | 1764 | |
|
1765 | 1765 | def checkcommitpatterns(self, wctx, vdirs, match, status, fail): |
|
1766 | 1766 | """check for commit arguments that aren't committable""" |
|
1767 | 1767 | if match.isexact() or match.prefix(): |
|
1768 | 1768 | matched = set(status.modified + status.added + status.removed) |
|
1769 | 1769 | |
|
1770 | 1770 | for f in match.files(): |
|
1771 | 1771 | f = self.dirstate.normalize(f) |
|
1772 | 1772 | if f == '.' or f in matched or f in wctx.substate: |
|
1773 | 1773 | continue |
|
1774 | 1774 | if f in status.deleted: |
|
1775 | 1775 | fail(f, _('file not found!')) |
|
1776 | 1776 | if f in vdirs: # visited directory |
|
1777 | 1777 | d = f + '/' |
|
1778 | 1778 | for mf in matched: |
|
1779 | 1779 | if mf.startswith(d): |
|
1780 | 1780 | break |
|
1781 | 1781 | else: |
|
1782 | 1782 | fail(f, _("no match under directory!")) |
|
1783 | 1783 | elif f not in self.dirstate: |
|
1784 | 1784 | fail(f, _("file not tracked!")) |
|
1785 | 1785 | |
|
1786 | 1786 | @unfilteredmethod |
|
1787 | 1787 | def commit(self, text="", user=None, date=None, match=None, force=False, |
|
1788 | 1788 | editor=False, extra=None): |
|
1789 | 1789 | """Add a new revision to current repository. |
|
1790 | 1790 | |
|
1791 | 1791 | Revision information is gathered from the working directory, |
|
1792 | 1792 | match can be used to filter the committed files. If editor is |
|
1793 | 1793 | supplied, it is called to get a commit message. |
|
1794 | 1794 | """ |
|
1795 | 1795 | if extra is None: |
|
1796 | 1796 | extra = {} |
|
1797 | 1797 | |
|
1798 | 1798 | def fail(f, msg): |
|
1799 | 1799 | raise error.Abort('%s: %s' % (f, msg)) |
|
1800 | 1800 | |
|
1801 | 1801 | if not match: |
|
1802 | 1802 | match = matchmod.always(self.root, '') |
|
1803 | 1803 | |
|
1804 | 1804 | if not force: |
|
1805 | 1805 | vdirs = [] |
|
1806 | 1806 | match.explicitdir = vdirs.append |
|
1807 | 1807 | match.bad = fail |
|
1808 | 1808 | |
|
1809 | 1809 | wlock = lock = tr = None |
|
1810 | 1810 | try: |
|
1811 | 1811 | wlock = self.wlock() |
|
1812 | 1812 | lock = self.lock() # for recent changelog (see issue4368) |
|
1813 | 1813 | |
|
1814 | 1814 | wctx = self[None] |
|
1815 | 1815 | merge = len(wctx.parents()) > 1 |
|
1816 | 1816 | |
|
1817 | 1817 | if not force and merge and not match.always(): |
|
1818 | 1818 | raise error.Abort(_('cannot partially commit a merge ' |
|
1819 | 1819 | '(do not specify files or patterns)')) |
|
1820 | 1820 | |
|
1821 | 1821 | status = self.status(match=match, clean=force) |
|
1822 | 1822 | if force: |
|
1823 | 1823 | status.modified.extend(status.clean) # mq may commit clean files |
|
1824 | 1824 | |
|
1825 | 1825 | # check subrepos |
|
1826 | 1826 | subs, commitsubs, newstate = subrepo.precommit( |
|
1827 | 1827 | self.ui, wctx, status, match, force=force) |
|
1828 | 1828 | |
|
1829 | 1829 | # make sure all explicit patterns are matched |
|
1830 | 1830 | if not force: |
|
1831 | 1831 | self.checkcommitpatterns(wctx, vdirs, match, status, fail) |
|
1832 | 1832 | |
|
1833 | 1833 | cctx = context.workingcommitctx(self, status, |
|
1834 | 1834 | text, user, date, extra) |
|
1835 | 1835 | |
|
1836 | 1836 | # internal config: ui.allowemptycommit |
|
1837 | 1837 | allowemptycommit = (wctx.branch() != wctx.p1().branch() |
|
1838 | 1838 | or extra.get('close') or merge or cctx.files() |
|
1839 | 1839 | or self.ui.configbool('ui', 'allowemptycommit')) |
|
1840 | 1840 | if not allowemptycommit: |
|
1841 | 1841 | return None |
|
1842 | 1842 | |
|
1843 | 1843 | if merge and cctx.deleted(): |
|
1844 | 1844 | raise error.Abort(_("cannot commit merge with missing files")) |
|
1845 | 1845 | |
|
1846 | 1846 | ms = mergemod.mergestate.read(self) |
|
1847 | 1847 | mergeutil.checkunresolved(ms) |
|
1848 | 1848 | |
|
1849 | 1849 | if editor: |
|
1850 | 1850 | cctx._text = editor(self, cctx, subs) |
|
1851 | 1851 | edited = (text != cctx._text) |
|
1852 | 1852 | |
|
1853 | 1853 | # Save commit message in case this transaction gets rolled back |
|
1854 | 1854 | # (e.g. by a pretxncommit hook). Leave the content alone on |
|
1855 | 1855 | # the assumption that the user will use the same editor again. |
|
1856 | 1856 | msgfn = self.savecommitmessage(cctx._text) |
|
1857 | 1857 | |
|
1858 | 1858 | # commit subs and write new state |
|
1859 | 1859 | if subs: |
|
1860 | 1860 | for s in sorted(commitsubs): |
|
1861 | 1861 | sub = wctx.sub(s) |
|
1862 | 1862 | self.ui.status(_('committing subrepository %s\n') % |
|
1863 | 1863 | subrepo.subrelpath(sub)) |
|
1864 | 1864 | sr = sub.commit(cctx._text, user, date) |
|
1865 | 1865 | newstate[s] = (newstate[s][0], sr) |
|
1866 | 1866 | subrepo.writestate(self, newstate) |
|
1867 | 1867 | |
|
1868 | 1868 | p1, p2 = self.dirstate.parents() |
|
1869 | 1869 | hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '') |
|
1870 | 1870 | try: |
|
1871 | 1871 | self.hook("precommit", throw=True, parent1=hookp1, |
|
1872 | 1872 | parent2=hookp2) |
|
1873 | 1873 | tr = self.transaction('commit') |
|
1874 | 1874 | ret = self.commitctx(cctx, True) |
|
1875 | 1875 | except: # re-raises |
|
1876 | 1876 | if edited: |
|
1877 | 1877 | self.ui.write( |
|
1878 | 1878 | _('note: commit message saved in %s\n') % msgfn) |
|
1879 | 1879 | raise |
|
1880 | 1880 | # update bookmarks, dirstate and mergestate |
|
1881 | 1881 | bookmarks.update(self, [p1, p2], ret) |
|
1882 | 1882 | cctx.markcommitted(ret) |
|
1883 | 1883 | ms.reset() |
|
1884 | 1884 | tr.close() |
|
1885 | 1885 | |
|
1886 | 1886 | finally: |
|
1887 | 1887 | lockmod.release(tr, lock, wlock) |
|
1888 | 1888 | |
|
1889 | 1889 | def commithook(node=hex(ret), parent1=hookp1, parent2=hookp2): |
|
1890 | 1890 | # hack for command that use a temporary commit (eg: histedit) |
|
1891 | 1891 | # temporary commit got stripped before hook release |
|
1892 | 1892 | if self.changelog.hasnode(ret): |
|
1893 | 1893 | self.hook("commit", node=node, parent1=parent1, |
|
1894 | 1894 | parent2=parent2) |
|
1895 | 1895 | self._afterlock(commithook) |
|
1896 | 1896 | return ret |
|
1897 | 1897 | |
|
1898 | 1898 | @unfilteredmethod |
|
1899 | 1899 | def commitctx(self, ctx, error=False): |
|
1900 | 1900 | """Add a new revision to current repository. |
|
1901 | 1901 | Revision information is passed via the context argument. |
|
1902 | 1902 | """ |
|
1903 | 1903 | |
|
1904 | 1904 | tr = None |
|
1905 | 1905 | p1, p2 = ctx.p1(), ctx.p2() |
|
1906 | 1906 | user = ctx.user() |
|
1907 | 1907 | |
|
1908 | 1908 | lock = self.lock() |
|
1909 | 1909 | try: |
|
1910 | 1910 | tr = self.transaction("commit") |
|
1911 | 1911 | trp = weakref.proxy(tr) |
|
1912 | 1912 | |
|
1913 | 1913 | if ctx.manifestnode(): |
|
1914 | 1914 | # reuse an existing manifest revision |
|
1915 | 1915 | mn = ctx.manifestnode() |
|
1916 | 1916 | files = ctx.files() |
|
1917 | 1917 | elif ctx.files(): |
|
1918 | 1918 | m1ctx = p1.manifestctx() |
|
1919 | 1919 | m2ctx = p2.manifestctx() |
|
1920 | 1920 | mctx = m1ctx.copy() |
|
1921 | 1921 | |
|
1922 | 1922 | m = mctx.read() |
|
1923 | 1923 | m1 = m1ctx.read() |
|
1924 | 1924 | m2 = m2ctx.read() |
|
1925 | 1925 | |
|
1926 | 1926 | # check in files |
|
1927 | 1927 | added = [] |
|
1928 | 1928 | changed = [] |
|
1929 | 1929 | removed = list(ctx.removed()) |
|
1930 | 1930 | linkrev = len(self) |
|
1931 | 1931 | self.ui.note(_("committing files:\n")) |
|
1932 | 1932 | for f in sorted(ctx.modified() + ctx.added()): |
|
1933 | 1933 | self.ui.note(f + "\n") |
|
1934 | 1934 | try: |
|
1935 | 1935 | fctx = ctx[f] |
|
1936 | 1936 | if fctx is None: |
|
1937 | 1937 | removed.append(f) |
|
1938 | 1938 | else: |
|
1939 | 1939 | added.append(f) |
|
1940 | 1940 | m[f] = self._filecommit(fctx, m1, m2, linkrev, |
|
1941 | 1941 | trp, changed) |
|
1942 | 1942 | m.setflag(f, fctx.flags()) |
|
1943 | 1943 | except OSError as inst: |
|
1944 | 1944 | self.ui.warn(_("trouble committing %s!\n") % f) |
|
1945 | 1945 | raise |
|
1946 | 1946 | except IOError as inst: |
|
1947 | 1947 | errcode = getattr(inst, 'errno', errno.ENOENT) |
|
1948 | 1948 | if error or errcode and errcode != errno.ENOENT: |
|
1949 | 1949 | self.ui.warn(_("trouble committing %s!\n") % f) |
|
1950 | 1950 | raise |
|
1951 | 1951 | |
|
1952 | 1952 | # update manifest |
|
1953 | 1953 | self.ui.note(_("committing manifest\n")) |
|
1954 | 1954 | removed = [f for f in sorted(removed) if f in m1 or f in m2] |
|
1955 | 1955 | drop = [f for f in removed if f in m] |
|
1956 | 1956 | for f in drop: |
|
1957 | 1957 | del m[f] |
|
1958 | 1958 | mn = mctx.write(trp, linkrev, |
|
1959 | 1959 | p1.manifestnode(), p2.manifestnode(), |
|
1960 | 1960 | added, drop) |
|
1961 | 1961 | files = changed + removed |
|
1962 | 1962 | else: |
|
1963 | 1963 | mn = p1.manifestnode() |
|
1964 | 1964 | files = [] |
|
1965 | 1965 | |
|
1966 | 1966 | # update changelog |
|
1967 | 1967 | self.ui.note(_("committing changelog\n")) |
|
1968 | 1968 | self.changelog.delayupdate(tr) |
|
1969 | 1969 | n = self.changelog.add(mn, files, ctx.description(), |
|
1970 | 1970 | trp, p1.node(), p2.node(), |
|
1971 | 1971 | user, ctx.date(), ctx.extra().copy()) |
|
1972 | 1972 | xp1, xp2 = p1.hex(), p2 and p2.hex() or '' |
|
1973 | 1973 | self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1, |
|
1974 | 1974 | parent2=xp2) |
|
1975 | 1975 | # set the new commit is proper phase |
|
1976 | 1976 | targetphase = subrepo.newcommitphase(self.ui, ctx) |
|
1977 | 1977 | if targetphase: |
|
1978 | 1978 | # retract boundary do not alter parent changeset. |
|
1979 | 1979 | # if a parent have higher the resulting phase will |
|
1980 | 1980 | # be compliant anyway |
|
1981 | 1981 | # |
|
1982 | 1982 | # if minimal phase was 0 we don't need to retract anything |
|
1983 | 1983 | phases.registernew(self, tr, targetphase, [n]) |
|
1984 | 1984 | tr.close() |
|
1985 | 1985 | return n |
|
1986 | 1986 | finally: |
|
1987 | 1987 | if tr: |
|
1988 | 1988 | tr.release() |
|
1989 | 1989 | lock.release() |
|
1990 | 1990 | |
|
1991 | 1991 | @unfilteredmethod |
|
1992 | 1992 | def destroying(self): |
|
1993 | 1993 | '''Inform the repository that nodes are about to be destroyed. |
|
1994 | 1994 | Intended for use by strip and rollback, so there's a common |
|
1995 | 1995 | place for anything that has to be done before destroying history. |
|
1996 | 1996 | |
|
1997 | 1997 | This is mostly useful for saving state that is in memory and waiting |
|
1998 | 1998 | to be flushed when the current lock is released. Because a call to |
|
1999 | 1999 | destroyed is imminent, the repo will be invalidated causing those |
|
2000 | 2000 | changes to stay in memory (waiting for the next unlock), or vanish |
|
2001 | 2001 | completely. |
|
2002 | 2002 | ''' |
|
2003 | 2003 | # When using the same lock to commit and strip, the phasecache is left |
|
2004 | 2004 | # dirty after committing. Then when we strip, the repo is invalidated, |
|
2005 | 2005 | # causing those changes to disappear. |
|
2006 | 2006 | if '_phasecache' in vars(self): |
|
2007 | 2007 | self._phasecache.write() |
|
2008 | 2008 | |
|
2009 | 2009 | @unfilteredmethod |
|
2010 | 2010 | def destroyed(self): |
|
2011 | 2011 | '''Inform the repository that nodes have been destroyed. |
|
2012 | 2012 | Intended for use by strip and rollback, so there's a common |
|
2013 | 2013 | place for anything that has to be done after destroying history. |
|
2014 | 2014 | ''' |
|
2015 | 2015 | # When one tries to: |
|
2016 | 2016 | # 1) destroy nodes thus calling this method (e.g. strip) |
|
2017 | 2017 | # 2) use phasecache somewhere (e.g. commit) |
|
2018 | 2018 | # |
|
2019 | 2019 | # then 2) will fail because the phasecache contains nodes that were |
|
2020 | 2020 | # removed. We can either remove phasecache from the filecache, |
|
2021 | 2021 | # causing it to reload next time it is accessed, or simply filter |
|
2022 | 2022 | # the removed nodes now and write the updated cache. |
|
2023 | 2023 | self._phasecache.filterunknown(self) |
|
2024 | 2024 | self._phasecache.write() |
|
2025 | 2025 | |
|
2026 | 2026 | # refresh all repository caches |
|
2027 | 2027 | self.updatecaches() |
|
2028 | 2028 | |
|
2029 | 2029 | # Ensure the persistent tag cache is updated. Doing it now |
|
2030 | 2030 | # means that the tag cache only has to worry about destroyed |
|
2031 | 2031 | # heads immediately after a strip/rollback. That in turn |
|
2032 | 2032 | # guarantees that "cachetip == currenttip" (comparing both rev |
|
2033 | 2033 | # and node) always means no nodes have been added or destroyed. |
|
2034 | 2034 | |
|
2035 | 2035 | # XXX this is suboptimal when qrefresh'ing: we strip the current |
|
2036 | 2036 | # head, refresh the tag cache, then immediately add a new head. |
|
2037 | 2037 | # But I think doing it this way is necessary for the "instant |
|
2038 | 2038 | # tag cache retrieval" case to work. |
|
2039 | 2039 | self.invalidate() |
|
2040 | 2040 | |
|
2041 | 2041 | def walk(self, match, node=None): |
|
2042 | 2042 | ''' |
|
2043 | 2043 | walk recursively through the directory tree or a given |
|
2044 | 2044 | changeset, finding all files matched by the match |
|
2045 | 2045 | function |
|
2046 | 2046 | ''' |
|
2047 | 2047 | self.ui.deprecwarn('use repo[node].walk instead of repo.walk', '4.3') |
|
2048 | 2048 | return self[node].walk(match) |
|
2049 | 2049 | |
|
2050 | 2050 | def status(self, node1='.', node2=None, match=None, |
|
2051 | 2051 | ignored=False, clean=False, unknown=False, |
|
2052 | 2052 | listsubrepos=False): |
|
2053 | 2053 | '''a convenience method that calls node1.status(node2)''' |
|
2054 | 2054 | return self[node1].status(node2, match, ignored, clean, unknown, |
|
2055 | 2055 | listsubrepos) |
|
2056 | 2056 | |
|
2057 | 2057 | def addpostdsstatus(self, ps): |
|
2058 | 2058 | """Add a callback to run within the wlock, at the point at which status |
|
2059 | 2059 | fixups happen. |
|
2060 | 2060 | |
|
2061 | 2061 | On status completion, callback(wctx, status) will be called with the |
|
2062 | 2062 | wlock held, unless the dirstate has changed from underneath or the wlock |
|
2063 | 2063 | couldn't be grabbed. |
|
2064 | 2064 | |
|
2065 | 2065 | Callbacks should not capture and use a cached copy of the dirstate -- |
|
2066 | 2066 | it might change in the meanwhile. Instead, they should access the |
|
2067 | 2067 | dirstate via wctx.repo().dirstate. |
|
2068 | 2068 | |
|
2069 | 2069 | This list is emptied out after each status run -- extensions should |
|
2070 | 2070 | make sure it adds to this list each time dirstate.status is called. |
|
2071 | 2071 | Extensions should also make sure they don't call this for statuses |
|
2072 | 2072 | that don't involve the dirstate. |
|
2073 | 2073 | """ |
|
2074 | 2074 | |
|
2075 | 2075 | # The list is located here for uniqueness reasons -- it is actually |
|
2076 | 2076 | # managed by the workingctx, but that isn't unique per-repo. |
|
2077 | 2077 | self._postdsstatus.append(ps) |
|
2078 | 2078 | |
|
2079 | 2079 | def postdsstatus(self): |
|
2080 | 2080 | """Used by workingctx to get the list of post-dirstate-status hooks.""" |
|
2081 | 2081 | return self._postdsstatus |
|
2082 | 2082 | |
|
2083 | 2083 | def clearpostdsstatus(self): |
|
2084 | 2084 | """Used by workingctx to clear post-dirstate-status hooks.""" |
|
2085 | 2085 | del self._postdsstatus[:] |
|
2086 | 2086 | |
|
2087 | 2087 | def heads(self, start=None): |
|
2088 | 2088 | if start is None: |
|
2089 | 2089 | cl = self.changelog |
|
2090 | 2090 | headrevs = reversed(cl.headrevs()) |
|
2091 | 2091 | return [cl.node(rev) for rev in headrevs] |
|
2092 | 2092 | |
|
2093 | 2093 | heads = self.changelog.heads(start) |
|
2094 | 2094 | # sort the output in rev descending order |
|
2095 | 2095 | return sorted(heads, key=self.changelog.rev, reverse=True) |
|
2096 | 2096 | |
|
2097 | 2097 | def branchheads(self, branch=None, start=None, closed=False): |
|
2098 | 2098 | '''return a (possibly filtered) list of heads for the given branch |
|
2099 | 2099 | |
|
2100 | 2100 | Heads are returned in topological order, from newest to oldest. |
|
2101 | 2101 | If branch is None, use the dirstate branch. |
|
2102 | 2102 | If start is not None, return only heads reachable from start. |
|
2103 | 2103 | If closed is True, return heads that are marked as closed as well. |
|
2104 | 2104 | ''' |
|
2105 | 2105 | if branch is None: |
|
2106 | 2106 | branch = self[None].branch() |
|
2107 | 2107 | branches = self.branchmap() |
|
2108 | 2108 | if branch not in branches: |
|
2109 | 2109 | return [] |
|
2110 | 2110 | # the cache returns heads ordered lowest to highest |
|
2111 | 2111 | bheads = list(reversed(branches.branchheads(branch, closed=closed))) |
|
2112 | 2112 | if start is not None: |
|
2113 | 2113 | # filter out the heads that cannot be reached from startrev |
|
2114 | 2114 | fbheads = set(self.changelog.nodesbetween([start], bheads)[2]) |
|
2115 | 2115 | bheads = [h for h in bheads if h in fbheads] |
|
2116 | 2116 | return bheads |
|
2117 | 2117 | |
|
2118 | 2118 | def branches(self, nodes): |
|
2119 | 2119 | if not nodes: |
|
2120 | 2120 | nodes = [self.changelog.tip()] |
|
2121 | 2121 | b = [] |
|
2122 | 2122 | for n in nodes: |
|
2123 | 2123 | t = n |
|
2124 | 2124 | while True: |
|
2125 | 2125 | p = self.changelog.parents(n) |
|
2126 | 2126 | if p[1] != nullid or p[0] == nullid: |
|
2127 | 2127 | b.append((t, n, p[0], p[1])) |
|
2128 | 2128 | break |
|
2129 | 2129 | n = p[0] |
|
2130 | 2130 | return b |
|
2131 | 2131 | |
|
2132 | 2132 | def between(self, pairs): |
|
2133 | 2133 | r = [] |
|
2134 | 2134 | |
|
2135 | 2135 | for top, bottom in pairs: |
|
2136 | 2136 | n, l, i = top, [], 0 |
|
2137 | 2137 | f = 1 |
|
2138 | 2138 | |
|
2139 | 2139 | while n != bottom and n != nullid: |
|
2140 | 2140 | p = self.changelog.parents(n)[0] |
|
2141 | 2141 | if i == f: |
|
2142 | 2142 | l.append(n) |
|
2143 | 2143 | f = f * 2 |
|
2144 | 2144 | n = p |
|
2145 | 2145 | i += 1 |
|
2146 | 2146 | |
|
2147 | 2147 | r.append(l) |
|
2148 | 2148 | |
|
2149 | 2149 | return r |
|
2150 | 2150 | |
|
2151 | 2151 | def checkpush(self, pushop): |
|
2152 | 2152 | """Extensions can override this function if additional checks have |
|
2153 | 2153 | to be performed before pushing, or call it if they override push |
|
2154 | 2154 | command. |
|
2155 | 2155 | """ |
|
2156 | 2156 | |
|
2157 | 2157 | @unfilteredpropertycache |
|
2158 | 2158 | def prepushoutgoinghooks(self): |
|
2159 | 2159 | """Return util.hooks consists of a pushop with repo, remote, outgoing |
|
2160 | 2160 | methods, which are called before pushing changesets. |
|
2161 | 2161 | """ |
|
2162 | 2162 | return util.hooks() |
|
2163 | 2163 | |
|
2164 | 2164 | def pushkey(self, namespace, key, old, new): |
|
2165 | 2165 | try: |
|
2166 | 2166 | tr = self.currenttransaction() |
|
2167 | 2167 | hookargs = {} |
|
2168 | 2168 | if tr is not None: |
|
2169 | 2169 | hookargs.update(tr.hookargs) |
|
2170 | 2170 | hookargs['namespace'] = namespace |
|
2171 | 2171 | hookargs['key'] = key |
|
2172 | 2172 | hookargs['old'] = old |
|
2173 | 2173 | hookargs['new'] = new |
|
2174 | 2174 | self.hook('prepushkey', throw=True, **hookargs) |
|
2175 | 2175 | except error.HookAbort as exc: |
|
2176 | 2176 | self.ui.write_err(_("pushkey-abort: %s\n") % exc) |
|
2177 | 2177 | if exc.hint: |
|
2178 | 2178 | self.ui.write_err(_("(%s)\n") % exc.hint) |
|
2179 | 2179 | return False |
|
2180 | 2180 | self.ui.debug('pushing key for "%s:%s"\n' % (namespace, key)) |
|
2181 | 2181 | ret = pushkey.push(self, namespace, key, old, new) |
|
2182 | 2182 | def runhook(): |
|
2183 | 2183 | self.hook('pushkey', namespace=namespace, key=key, old=old, new=new, |
|
2184 | 2184 | ret=ret) |
|
2185 | 2185 | self._afterlock(runhook) |
|
2186 | 2186 | return ret |
|
2187 | 2187 | |
|
2188 | 2188 | def listkeys(self, namespace): |
|
2189 | 2189 | self.hook('prelistkeys', throw=True, namespace=namespace) |
|
2190 | 2190 | self.ui.debug('listing keys for "%s"\n' % namespace) |
|
2191 | 2191 | values = pushkey.list(self, namespace) |
|
2192 | 2192 | self.hook('listkeys', namespace=namespace, values=values) |
|
2193 | 2193 | return values |
|
2194 | 2194 | |
|
2195 | 2195 | def debugwireargs(self, one, two, three=None, four=None, five=None): |
|
2196 | 2196 | '''used to test argument passing over the wire''' |
|
2197 | 2197 | return "%s %s %s %s %s" % (one, two, three, four, five) |
|
2198 | 2198 | |
|
2199 | 2199 | def savecommitmessage(self, text): |
|
2200 | 2200 | fp = self.vfs('last-message.txt', 'wb') |
|
2201 | 2201 | try: |
|
2202 | 2202 | fp.write(text) |
|
2203 | 2203 | finally: |
|
2204 | 2204 | fp.close() |
|
2205 | 2205 | return self.pathto(fp.name[len(self.root) + 1:]) |
|
2206 | 2206 | |
|
2207 | 2207 | # used to avoid circular references so destructors work |
|
2208 | 2208 | def aftertrans(files): |
|
2209 | 2209 | renamefiles = [tuple(t) for t in files] |
|
2210 | 2210 | def a(): |
|
2211 | 2211 | for vfs, src, dest in renamefiles: |
|
2212 | 2212 | # if src and dest refer to a same file, vfs.rename is a no-op, |
|
2213 | 2213 | # leaving both src and dest on disk. delete dest to make sure |
|
2214 | 2214 | # the rename couldn't be such a no-op. |
|
2215 | 2215 | vfs.tryunlink(dest) |
|
2216 | 2216 | try: |
|
2217 | 2217 | vfs.rename(src, dest) |
|
2218 | 2218 | except OSError: # journal file does not yet exist |
|
2219 | 2219 | pass |
|
2220 | 2220 | return a |
|
2221 | 2221 | |
|
2222 | 2222 | def undoname(fn): |
|
2223 | 2223 | base, name = os.path.split(fn) |
|
2224 | 2224 | assert name.startswith('journal') |
|
2225 | 2225 | return os.path.join(base, name.replace('journal', 'undo', 1)) |
|
2226 | 2226 | |
|
2227 | 2227 | def instance(ui, path, create): |
|
2228 | 2228 | return localrepository(ui, util.urllocalpath(path), create) |
|
2229 | 2229 | |
|
2230 | 2230 | def islocal(path): |
|
2231 | 2231 | return True |
|
2232 | 2232 | |
|
2233 | 2233 | def newreporequirements(repo): |
|
2234 | 2234 | """Determine the set of requirements for a new local repository. |
|
2235 | 2235 | |
|
2236 | 2236 | Extensions can wrap this function to specify custom requirements for |
|
2237 | 2237 | new repositories. |
|
2238 | 2238 | """ |
|
2239 | 2239 | ui = repo.ui |
|
2240 | 2240 | requirements = {'revlogv1'} |
|
2241 | 2241 | if ui.configbool('format', 'usestore'): |
|
2242 | 2242 | requirements.add('store') |
|
2243 | 2243 | if ui.configbool('format', 'usefncache'): |
|
2244 | 2244 | requirements.add('fncache') |
|
2245 | 2245 | if ui.configbool('format', 'dotencode'): |
|
2246 | 2246 | requirements.add('dotencode') |
|
2247 | 2247 | |
|
2248 | 2248 | compengine = ui.config('experimental', 'format.compression') |
|
2249 | 2249 | if compengine not in util.compengines: |
|
2250 | 2250 | raise error.Abort(_('compression engine %s defined by ' |
|
2251 | 2251 | 'experimental.format.compression not available') % |
|
2252 | 2252 | compengine, |
|
2253 | 2253 | hint=_('run "hg debuginstall" to list available ' |
|
2254 | 2254 | 'compression engines')) |
|
2255 | 2255 | |
|
2256 | 2256 | # zlib is the historical default and doesn't need an explicit requirement. |
|
2257 | 2257 | if compengine != 'zlib': |
|
2258 | 2258 | requirements.add('exp-compression-%s' % compengine) |
|
2259 | 2259 | |
|
2260 | 2260 | if scmutil.gdinitconfig(ui): |
|
2261 | 2261 | requirements.add('generaldelta') |
|
2262 | 2262 | if ui.configbool('experimental', 'treemanifest'): |
|
2263 | 2263 | requirements.add('treemanifest') |
|
2264 | 2264 | if ui.configbool('experimental', 'manifestv2'): |
|
2265 | 2265 | requirements.add('manifestv2') |
|
2266 | 2266 | |
|
2267 | 2267 | revlogv2 = ui.config('experimental', 'revlogv2') |
|
2268 | 2268 | if revlogv2 == 'enable-unstable-format-and-corrupt-my-data': |
|
2269 | 2269 | requirements.remove('revlogv1') |
|
2270 | 2270 | # generaldelta is implied by revlogv2. |
|
2271 | 2271 | requirements.discard('generaldelta') |
|
2272 | 2272 | requirements.add(REVLOGV2_REQUIREMENT) |
|
2273 | 2273 | |
|
2274 | 2274 | return requirements |
@@ -1,666 +1,674 | |||
|
1 | 1 | """ Mercurial phases support code |
|
2 | 2 | |
|
3 | 3 | --- |
|
4 | 4 | |
|
5 | 5 | Copyright 2011 Pierre-Yves David <pierre-yves.david@ens-lyon.org> |
|
6 | 6 | Logilab SA <contact@logilab.fr> |
|
7 | 7 | Augie Fackler <durin42@gmail.com> |
|
8 | 8 | |
|
9 | 9 | This software may be used and distributed according to the terms |
|
10 | 10 | of the GNU General Public License version 2 or any later version. |
|
11 | 11 | |
|
12 | 12 | --- |
|
13 | 13 | |
|
14 | 14 | This module implements most phase logic in mercurial. |
|
15 | 15 | |
|
16 | 16 | |
|
17 | 17 | Basic Concept |
|
18 | 18 | ============= |
|
19 | 19 | |
|
20 | 20 | A 'changeset phase' is an indicator that tells us how a changeset is |
|
21 | 21 | manipulated and communicated. The details of each phase is described |
|
22 | 22 | below, here we describe the properties they have in common. |
|
23 | 23 | |
|
24 | 24 | Like bookmarks, phases are not stored in history and thus are not |
|
25 | 25 | permanent and leave no audit trail. |
|
26 | 26 | |
|
27 | 27 | First, no changeset can be in two phases at once. Phases are ordered, |
|
28 | 28 | so they can be considered from lowest to highest. The default, lowest |
|
29 | 29 | phase is 'public' - this is the normal phase of existing changesets. A |
|
30 | 30 | child changeset can not be in a lower phase than its parents. |
|
31 | 31 | |
|
32 | 32 | These phases share a hierarchy of traits: |
|
33 | 33 | |
|
34 | 34 | immutable shared |
|
35 | 35 | public: X X |
|
36 | 36 | draft: X |
|
37 | 37 | secret: |
|
38 | 38 | |
|
39 | 39 | Local commits are draft by default. |
|
40 | 40 | |
|
41 | 41 | Phase Movement and Exchange |
|
42 | 42 | =========================== |
|
43 | 43 | |
|
44 | 44 | Phase data is exchanged by pushkey on pull and push. Some servers have |
|
45 | 45 | a publish option set, we call such a server a "publishing server". |
|
46 | 46 | Pushing a draft changeset to a publishing server changes the phase to |
|
47 | 47 | public. |
|
48 | 48 | |
|
49 | 49 | A small list of fact/rules define the exchange of phase: |
|
50 | 50 | |
|
51 | 51 | * old client never changes server states |
|
52 | 52 | * pull never changes server states |
|
53 | 53 | * publish and old server changesets are seen as public by client |
|
54 | 54 | * any secret changeset seen in another repository is lowered to at |
|
55 | 55 | least draft |
|
56 | 56 | |
|
57 | 57 | Here is the final table summing up the 49 possible use cases of phase |
|
58 | 58 | exchange: |
|
59 | 59 | |
|
60 | 60 | server |
|
61 | 61 | old publish non-publish |
|
62 | 62 | N X N D P N D P |
|
63 | 63 | old client |
|
64 | 64 | pull |
|
65 | 65 | N - X/X - X/D X/P - X/D X/P |
|
66 | 66 | X - X/X - X/D X/P - X/D X/P |
|
67 | 67 | push |
|
68 | 68 | X X/X X/X X/P X/P X/P X/D X/D X/P |
|
69 | 69 | new client |
|
70 | 70 | pull |
|
71 | 71 | N - P/X - P/D P/P - D/D P/P |
|
72 | 72 | D - P/X - P/D P/P - D/D P/P |
|
73 | 73 | P - P/X - P/D P/P - P/D P/P |
|
74 | 74 | push |
|
75 | 75 | D P/X P/X P/P P/P P/P D/D D/D P/P |
|
76 | 76 | P P/X P/X P/P P/P P/P P/P P/P P/P |
|
77 | 77 | |
|
78 | 78 | Legend: |
|
79 | 79 | |
|
80 | 80 | A/B = final state on client / state on server |
|
81 | 81 | |
|
82 | 82 | * N = new/not present, |
|
83 | 83 | * P = public, |
|
84 | 84 | * D = draft, |
|
85 | 85 | * X = not tracked (i.e., the old client or server has no internal |
|
86 | 86 | way of recording the phase.) |
|
87 | 87 | |
|
88 | 88 | passive = only pushes |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | A cell here can be read like this: |
|
92 | 92 | |
|
93 | 93 | "When a new client pushes a draft changeset (D) to a publishing |
|
94 | 94 | server where it's not present (N), it's marked public on both |
|
95 | 95 | sides (P/P)." |
|
96 | 96 | |
|
97 | 97 | Note: old client behave as a publishing server with draft only content |
|
98 | 98 | - other people see it as public |
|
99 | 99 | - content is pushed as draft |
|
100 | 100 | |
|
101 | 101 | """ |
|
102 | 102 | |
|
103 | 103 | from __future__ import absolute_import |
|
104 | 104 | |
|
105 | 105 | import errno |
|
106 | 106 | import struct |
|
107 | 107 | |
|
108 | 108 | from .i18n import _ |
|
109 | 109 | from .node import ( |
|
110 | 110 | bin, |
|
111 | 111 | hex, |
|
112 | 112 | nullid, |
|
113 | 113 | nullrev, |
|
114 | 114 | short, |
|
115 | 115 | ) |
|
116 | 116 | from . import ( |
|
117 | 117 | error, |
|
118 | pycompat, | |
|
118 | 119 | smartset, |
|
119 | 120 | txnutil, |
|
120 | 121 | util, |
|
121 | 122 | ) |
|
122 | 123 | |
|
123 | 124 | _fphasesentry = struct.Struct('>i20s') |
|
124 | 125 | |
|
125 | 126 | allphases = public, draft, secret = range(3) |
|
126 | 127 | trackedphases = allphases[1:] |
|
127 | 128 | phasenames = ['public', 'draft', 'secret'] |
|
128 | 129 | |
|
129 | 130 | def _readroots(repo, phasedefaults=None): |
|
130 | 131 | """Read phase roots from disk |
|
131 | 132 | |
|
132 | 133 | phasedefaults is a list of fn(repo, roots) callable, which are |
|
133 | 134 | executed if the phase roots file does not exist. When phases are |
|
134 | 135 | being initialized on an existing repository, this could be used to |
|
135 | 136 | set selected changesets phase to something else than public. |
|
136 | 137 | |
|
137 | 138 | Return (roots, dirty) where dirty is true if roots differ from |
|
138 | 139 | what is being stored. |
|
139 | 140 | """ |
|
140 | 141 | repo = repo.unfiltered() |
|
141 | 142 | dirty = False |
|
142 | 143 | roots = [set() for i in allphases] |
|
143 | 144 | try: |
|
144 | 145 | f, pending = txnutil.trypending(repo.root, repo.svfs, 'phaseroots') |
|
145 | 146 | try: |
|
146 | 147 | for line in f: |
|
147 | 148 | phase, nh = line.split() |
|
148 | 149 | roots[int(phase)].add(bin(nh)) |
|
149 | 150 | finally: |
|
150 | 151 | f.close() |
|
151 | 152 | except IOError as inst: |
|
152 | 153 | if inst.errno != errno.ENOENT: |
|
153 | 154 | raise |
|
154 | 155 | if phasedefaults: |
|
155 | 156 | for f in phasedefaults: |
|
156 | 157 | roots = f(repo, roots) |
|
157 | 158 | dirty = True |
|
158 | 159 | return roots, dirty |
|
159 | 160 | |
|
160 | 161 | def binaryencode(phasemapping): |
|
161 | 162 | """encode a 'phase -> nodes' mapping into a binary stream |
|
162 | 163 | |
|
163 | 164 | Since phases are integer the mapping is actually a python list: |
|
164 | 165 | [[PUBLIC_HEADS], [DRAFTS_HEADS], [SECRET_HEADS]] |
|
165 | 166 | """ |
|
166 | 167 | binarydata = [] |
|
167 | 168 | for phase, nodes in enumerate(phasemapping): |
|
168 | 169 | for head in nodes: |
|
169 | 170 | binarydata.append(_fphasesentry.pack(phase, head)) |
|
170 | 171 | return ''.join(binarydata) |
|
171 | 172 | |
|
172 | 173 | def binarydecode(stream): |
|
173 | 174 | """decode a binary stream into a 'phase -> nodes' mapping |
|
174 | 175 | |
|
175 | 176 | Since phases are integer the mapping is actually a python list.""" |
|
176 | 177 | headsbyphase = [[] for i in allphases] |
|
177 | 178 | entrysize = _fphasesentry.size |
|
178 | 179 | while True: |
|
179 | 180 | entry = stream.read(entrysize) |
|
180 | 181 | if len(entry) < entrysize: |
|
181 | 182 | if entry: |
|
182 | 183 | raise error.Abort(_('bad phase-heads stream')) |
|
183 | 184 | break |
|
184 | 185 | phase, node = _fphasesentry.unpack(entry) |
|
185 | 186 | headsbyphase[phase].append(node) |
|
186 | 187 | return headsbyphase |
|
187 | 188 | |
|
188 | 189 | def _trackphasechange(data, rev, old, new): |
|
189 | 190 | """add a phase move the <data> dictionnary |
|
190 | 191 | |
|
191 | 192 | If data is None, nothing happens. |
|
192 | 193 | """ |
|
193 | 194 | if data is None: |
|
194 | 195 | return |
|
195 | 196 | existing = data.get(rev) |
|
196 | 197 | if existing is not None: |
|
197 | 198 | old = existing[0] |
|
198 | 199 | data[rev] = (old, new) |
|
199 | 200 | |
|
200 | 201 | class phasecache(object): |
|
201 | 202 | def __init__(self, repo, phasedefaults, _load=True): |
|
202 | 203 | if _load: |
|
203 | 204 | # Cheap trick to allow shallow-copy without copy module |
|
204 | 205 | self.phaseroots, self.dirty = _readroots(repo, phasedefaults) |
|
205 |
self._phaserev |
|
|
206 | self._phasemaxrev = nullrev | |
|
206 | 207 | self._phasesets = None |
|
207 | 208 | self.filterunknown(repo) |
|
208 | 209 | self.opener = repo.svfs |
|
209 | 210 | |
|
210 | 211 | def getrevset(self, repo, phases): |
|
211 | 212 | """return a smartset for the given phases""" |
|
212 | 213 | self.loadphaserevs(repo) # ensure phase's sets are loaded |
|
213 | ||
|
214 | if self._phasesets and all(self._phasesets[p] is not None | |
|
215 | for p in phases): | |
|
216 | # fast path - use _phasesets | |
|
217 | revs = self._phasesets[phases[0]] | |
|
218 |
|
|
|
219 | revs = revs.copy() # only copy when needed | |
|
220 | for p in phases[1:]: | |
|
221 |
|
|
|
214 | phases = set(phases) | |
|
215 | if public not in phases: | |
|
216 | # fast path: _phasesets contains the interesting sets, | |
|
217 | # might only need a union and post-filtering. | |
|
218 | if len(phases) == 1: | |
|
219 | [p] = phases | |
|
220 | revs = self._phasesets[p] | |
|
221 | else: | |
|
222 | revs = set.union(*[self._phasesets[p] for p in phases]) | |
|
222 | 223 | if repo.changelog.filteredrevs: |
|
223 | 224 | revs = revs - repo.changelog.filteredrevs |
|
224 | 225 | return smartset.baseset(revs) |
|
225 | 226 | else: |
|
226 | # slow path - enumerate all revisions | |
|
227 |
|
|
|
228 | revs = (r for r in repo if phase(repo, r) in phases) | |
|
229 | return smartset.generatorset(revs, iterasc=True) | |
|
227 | phases = set(allphases).difference(phases) | |
|
228 | if not phases: | |
|
229 | return smartset.fullreposet(repo) | |
|
230 | if len(phases) == 1: | |
|
231 | [p] = phases | |
|
232 | revs = self._phasesets[p] | |
|
233 | else: | |
|
234 | revs = set.union(*[self._phasesets[p] for p in phases]) | |
|
235 | if not revs: | |
|
236 | return smartset.fullreposet(repo) | |
|
237 | return smartset.fullreposet(repo).filter(lambda r: r not in revs) | |
|
230 | 238 | |
|
231 | 239 | def copy(self): |
|
232 | 240 | # Shallow copy meant to ensure isolation in |
|
233 | 241 | # advance/retractboundary(), nothing more. |
|
234 | 242 | ph = self.__class__(None, None, _load=False) |
|
235 | 243 | ph.phaseroots = self.phaseroots[:] |
|
236 | 244 | ph.dirty = self.dirty |
|
237 | 245 | ph.opener = self.opener |
|
238 |
ph._phaserev |
|
|
246 | ph._phasemaxrev = self._phasemaxrev | |
|
239 | 247 | ph._phasesets = self._phasesets |
|
240 | 248 | return ph |
|
241 | 249 | |
|
242 | 250 | def replace(self, phcache): |
|
243 | 251 | """replace all values in 'self' with content of phcache""" |
|
244 |
for a in ('phaseroots', 'dirty', 'opener', '_phaserev |
|
|
252 | for a in ('phaseroots', 'dirty', 'opener', '_phasemaxrev', | |
|
253 | '_phasesets'): | |
|
245 | 254 | setattr(self, a, getattr(phcache, a)) |
|
246 | 255 | |
|
247 | 256 | def _getphaserevsnative(self, repo): |
|
248 | 257 | repo = repo.unfiltered() |
|
249 | 258 | nativeroots = [] |
|
250 | 259 | for phase in trackedphases: |
|
251 | 260 | nativeroots.append(map(repo.changelog.rev, self.phaseroots[phase])) |
|
252 | 261 | return repo.changelog.computephases(nativeroots) |
|
253 | 262 | |
|
254 | 263 | def _computephaserevspure(self, repo): |
|
255 | 264 | repo = repo.unfiltered() |
|
256 |
|
|
|
257 |
self._phase |
|
|
258 | self._populatephaseroots(repo) | |
|
259 | for phase in trackedphases: | |
|
260 | roots = list(map(repo.changelog.rev, self.phaseroots[phase])) | |
|
265 | cl = repo.changelog | |
|
266 | self._phasesets = [set() for phase in allphases] | |
|
267 | roots = pycompat.maplist(cl.rev, self.phaseroots[secret]) | |
|
261 | 268 |
|
|
262 | for rev in roots: | |
|
263 | revs[rev] = phase | |
|
264 |
|
|
|
265 | revs[rev] = phase | |
|
269 | ps = set(cl.descendants(roots)) | |
|
270 | for root in roots: | |
|
271 | ps.add(root) | |
|
272 | self._phasesets[secret] = ps | |
|
273 | roots = pycompat.maplist(cl.rev, self.phaseroots[draft]) | |
|
274 | if roots: | |
|
275 | ps = set(cl.descendants(roots)) | |
|
276 | for root in roots: | |
|
277 | ps.add(root) | |
|
278 | ps.difference_update(self._phasesets[secret]) | |
|
279 | self._phasesets[draft] = ps | |
|
280 | self._phasemaxrev = len(cl) | |
|
266 | 281 | |
|
267 | 282 | def loadphaserevs(self, repo): |
|
268 | 283 | """ensure phase information is loaded in the object""" |
|
269 |
if self._phase |
|
|
284 | if self._phasesets is None: | |
|
270 | 285 | try: |
|
271 | 286 | res = self._getphaserevsnative(repo) |
|
272 |
self._phaserev |
|
|
287 | self._phasemaxrev, self._phasesets = res | |
|
273 | 288 | except AttributeError: |
|
274 | 289 | self._computephaserevspure(repo) |
|
275 | 290 | |
|
276 | 291 | def invalidate(self): |
|
277 |
self._phaserev |
|
|
292 | self._phasemaxrev = nullrev | |
|
278 | 293 | self._phasesets = None |
|
279 | 294 | |
|
280 | def _populatephaseroots(self, repo): | |
|
281 | """Fills the _phaserevs cache with phases for the roots. | |
|
282 | """ | |
|
283 | cl = repo.changelog | |
|
284 | phaserevs = self._phaserevs | |
|
285 | for phase in trackedphases: | |
|
286 | roots = map(cl.rev, self.phaseroots[phase]) | |
|
287 | for root in roots: | |
|
288 | phaserevs[root] = phase | |
|
289 | ||
|
290 | 295 | def phase(self, repo, rev): |
|
291 |
# We need a repo argument here to be able to build _phase |
|
|
296 | # We need a repo argument here to be able to build _phasesets | |
|
292 | 297 | # if necessary. The repository instance is not stored in |
|
293 | 298 | # phasecache to avoid reference cycles. The changelog instance |
|
294 | 299 | # is not stored because it is a filecache() property and can |
|
295 | 300 | # be replaced without us being notified. |
|
296 | 301 | if rev == nullrev: |
|
297 | 302 | return public |
|
298 | 303 | if rev < nullrev: |
|
299 | 304 | raise ValueError(_('cannot lookup negative revision')) |
|
300 | if self._phaserevs is None or rev >= len(self._phaserevs): | |
|
305 | if rev >= self._phasemaxrev: | |
|
301 | 306 | self.invalidate() |
|
302 | 307 | self.loadphaserevs(repo) |
|
303 | return self._phaserevs[rev] | |
|
308 | for phase in trackedphases: | |
|
309 | if rev in self._phasesets[phase]: | |
|
310 | return phase | |
|
311 | return public | |
|
304 | 312 | |
|
305 | 313 | def write(self): |
|
306 | 314 | if not self.dirty: |
|
307 | 315 | return |
|
308 | 316 | f = self.opener('phaseroots', 'w', atomictemp=True, checkambig=True) |
|
309 | 317 | try: |
|
310 | 318 | self._write(f) |
|
311 | 319 | finally: |
|
312 | 320 | f.close() |
|
313 | 321 | |
|
314 | 322 | def _write(self, fp): |
|
315 | 323 | for phase, roots in enumerate(self.phaseroots): |
|
316 | 324 | for h in roots: |
|
317 | 325 | fp.write('%i %s\n' % (phase, hex(h))) |
|
318 | 326 | self.dirty = False |
|
319 | 327 | |
|
320 | 328 | def _updateroots(self, phase, newroots, tr): |
|
321 | 329 | self.phaseroots[phase] = newroots |
|
322 | 330 | self.invalidate() |
|
323 | 331 | self.dirty = True |
|
324 | 332 | |
|
325 | 333 | tr.addfilegenerator('phase', ('phaseroots',), self._write) |
|
326 | 334 | tr.hookargs['phases_moved'] = '1' |
|
327 | 335 | |
|
328 | 336 | def registernew(self, repo, tr, targetphase, nodes): |
|
329 | 337 | repo = repo.unfiltered() |
|
330 | 338 | self._retractboundary(repo, tr, targetphase, nodes) |
|
331 | 339 | if tr is not None and 'phases' in tr.changes: |
|
332 | 340 | phasetracking = tr.changes['phases'] |
|
333 | 341 | torev = repo.changelog.rev |
|
334 | 342 | phase = self.phase |
|
335 | 343 | for n in nodes: |
|
336 | 344 | rev = torev(n) |
|
337 | 345 | revphase = phase(repo, rev) |
|
338 | 346 | _trackphasechange(phasetracking, rev, None, revphase) |
|
339 | 347 | repo.invalidatevolatilesets() |
|
340 | 348 | |
|
341 | 349 | def advanceboundary(self, repo, tr, targetphase, nodes): |
|
342 | 350 | """Set all 'nodes' to phase 'targetphase' |
|
343 | 351 | |
|
344 | 352 | Nodes with a phase lower than 'targetphase' are not affected. |
|
345 | 353 | """ |
|
346 | 354 | # Be careful to preserve shallow-copied values: do not update |
|
347 | 355 | # phaseroots values, replace them. |
|
348 | 356 | if tr is None: |
|
349 | 357 | phasetracking = None |
|
350 | 358 | else: |
|
351 | 359 | phasetracking = tr.changes.get('phases') |
|
352 | 360 | |
|
353 | 361 | repo = repo.unfiltered() |
|
354 | 362 | |
|
355 | 363 | delroots = [] # set of root deleted by this path |
|
356 | 364 | for phase in xrange(targetphase + 1, len(allphases)): |
|
357 | 365 | # filter nodes that are not in a compatible phase already |
|
358 | 366 | nodes = [n for n in nodes |
|
359 | 367 | if self.phase(repo, repo[n].rev()) >= phase] |
|
360 | 368 | if not nodes: |
|
361 | 369 | break # no roots to move anymore |
|
362 | 370 | |
|
363 | 371 | olds = self.phaseroots[phase] |
|
364 | 372 | |
|
365 | 373 | affected = repo.revs('%ln::%ln', olds, nodes) |
|
366 | 374 | for r in affected: |
|
367 | 375 | _trackphasechange(phasetracking, r, self.phase(repo, r), |
|
368 | 376 | targetphase) |
|
369 | 377 | |
|
370 | 378 | roots = set(ctx.node() for ctx in repo.set( |
|
371 | 379 | 'roots((%ln::) - %ld)', olds, affected)) |
|
372 | 380 | if olds != roots: |
|
373 | 381 | self._updateroots(phase, roots, tr) |
|
374 | 382 | # some roots may need to be declared for lower phases |
|
375 | 383 | delroots.extend(olds - roots) |
|
376 | 384 | # declare deleted root in the target phase |
|
377 | 385 | if targetphase != 0: |
|
378 | 386 | self._retractboundary(repo, tr, targetphase, delroots) |
|
379 | 387 | repo.invalidatevolatilesets() |
|
380 | 388 | |
|
381 | 389 | def retractboundary(self, repo, tr, targetphase, nodes): |
|
382 | 390 | oldroots = self.phaseroots[:targetphase + 1] |
|
383 | 391 | if tr is None: |
|
384 | 392 | phasetracking = None |
|
385 | 393 | else: |
|
386 | 394 | phasetracking = tr.changes.get('phases') |
|
387 | 395 | repo = repo.unfiltered() |
|
388 | 396 | if (self._retractboundary(repo, tr, targetphase, nodes) |
|
389 | 397 | and phasetracking is not None): |
|
390 | 398 | |
|
391 | 399 | # find the affected revisions |
|
392 | 400 | new = self.phaseroots[targetphase] |
|
393 | 401 | old = oldroots[targetphase] |
|
394 | 402 | affected = set(repo.revs('(%ln::) - (%ln::)', new, old)) |
|
395 | 403 | |
|
396 | 404 | # find the phase of the affected revision |
|
397 | 405 | for phase in xrange(targetphase, -1, -1): |
|
398 | 406 | if phase: |
|
399 | 407 | roots = oldroots[phase] |
|
400 | 408 | revs = set(repo.revs('%ln::%ld', roots, affected)) |
|
401 | 409 | affected -= revs |
|
402 | 410 | else: # public phase |
|
403 | 411 | revs = affected |
|
404 | 412 | for r in revs: |
|
405 | 413 | _trackphasechange(phasetracking, r, phase, targetphase) |
|
406 | 414 | repo.invalidatevolatilesets() |
|
407 | 415 | |
|
408 | 416 | def _retractboundary(self, repo, tr, targetphase, nodes): |
|
409 | 417 | # Be careful to preserve shallow-copied values: do not update |
|
410 | 418 | # phaseroots values, replace them. |
|
411 | 419 | |
|
412 | 420 | repo = repo.unfiltered() |
|
413 | 421 | currentroots = self.phaseroots[targetphase] |
|
414 | 422 | finalroots = oldroots = set(currentroots) |
|
415 | 423 | newroots = [n for n in nodes |
|
416 | 424 | if self.phase(repo, repo[n].rev()) < targetphase] |
|
417 | 425 | if newroots: |
|
418 | 426 | |
|
419 | 427 | if nullid in newroots: |
|
420 | 428 | raise error.Abort(_('cannot change null revision phase')) |
|
421 | 429 | currentroots = currentroots.copy() |
|
422 | 430 | currentroots.update(newroots) |
|
423 | 431 | |
|
424 | 432 | # Only compute new roots for revs above the roots that are being |
|
425 | 433 | # retracted. |
|
426 | 434 | minnewroot = min(repo[n].rev() for n in newroots) |
|
427 | 435 | aboveroots = [n for n in currentroots |
|
428 | 436 | if repo[n].rev() >= minnewroot] |
|
429 | 437 | updatedroots = repo.set('roots(%ln::)', aboveroots) |
|
430 | 438 | |
|
431 | 439 | finalroots = set(n for n in currentroots if repo[n].rev() < |
|
432 | 440 | minnewroot) |
|
433 | 441 | finalroots.update(ctx.node() for ctx in updatedroots) |
|
434 | 442 | if finalroots != oldroots: |
|
435 | 443 | self._updateroots(targetphase, finalroots, tr) |
|
436 | 444 | return True |
|
437 | 445 | return False |
|
438 | 446 | |
|
439 | 447 | def filterunknown(self, repo): |
|
440 | 448 | """remove unknown nodes from the phase boundary |
|
441 | 449 | |
|
442 | 450 | Nothing is lost as unknown nodes only hold data for their descendants. |
|
443 | 451 | """ |
|
444 | 452 | filtered = False |
|
445 | 453 | nodemap = repo.changelog.nodemap # to filter unknown nodes |
|
446 | 454 | for phase, nodes in enumerate(self.phaseroots): |
|
447 | 455 | missing = sorted(node for node in nodes if node not in nodemap) |
|
448 | 456 | if missing: |
|
449 | 457 | for mnode in missing: |
|
450 | 458 | repo.ui.debug( |
|
451 | 459 | 'removing unknown node %s from %i-phase boundary\n' |
|
452 | 460 | % (short(mnode), phase)) |
|
453 | 461 | nodes.symmetric_difference_update(missing) |
|
454 | 462 | filtered = True |
|
455 | 463 | if filtered: |
|
456 | 464 | self.dirty = True |
|
457 | 465 | # filterunknown is called by repo.destroyed, we may have no changes in |
|
458 |
# root but |
|
|
466 | # root but _phasesets contents is certainly invalid (or at least we | |
|
459 | 467 | # have not proper way to check that). related to issue 3858. |
|
460 | 468 | # |
|
461 |
# The other caller is __init__ that have no _phase |
|
|
469 | # The other caller is __init__ that have no _phasesets initialized | |
|
462 | 470 | # anyway. If this change we should consider adding a dedicated |
|
463 | 471 | # "destroyed" function to phasecache or a proper cache key mechanism |
|
464 | 472 | # (see branchmap one) |
|
465 | 473 | self.invalidate() |
|
466 | 474 | |
|
467 | 475 | def advanceboundary(repo, tr, targetphase, nodes): |
|
468 | 476 | """Add nodes to a phase changing other nodes phases if necessary. |
|
469 | 477 | |
|
470 | 478 | This function move boundary *forward* this means that all nodes |
|
471 | 479 | are set in the target phase or kept in a *lower* phase. |
|
472 | 480 | |
|
473 | 481 | Simplify boundary to contains phase roots only.""" |
|
474 | 482 | phcache = repo._phasecache.copy() |
|
475 | 483 | phcache.advanceboundary(repo, tr, targetphase, nodes) |
|
476 | 484 | repo._phasecache.replace(phcache) |
|
477 | 485 | |
|
478 | 486 | def retractboundary(repo, tr, targetphase, nodes): |
|
479 | 487 | """Set nodes back to a phase changing other nodes phases if |
|
480 | 488 | necessary. |
|
481 | 489 | |
|
482 | 490 | This function move boundary *backward* this means that all nodes |
|
483 | 491 | are set in the target phase or kept in a *higher* phase. |
|
484 | 492 | |
|
485 | 493 | Simplify boundary to contains phase roots only.""" |
|
486 | 494 | phcache = repo._phasecache.copy() |
|
487 | 495 | phcache.retractboundary(repo, tr, targetphase, nodes) |
|
488 | 496 | repo._phasecache.replace(phcache) |
|
489 | 497 | |
|
490 | 498 | def registernew(repo, tr, targetphase, nodes): |
|
491 | 499 | """register a new revision and its phase |
|
492 | 500 | |
|
493 | 501 | Code adding revisions to the repository should use this function to |
|
494 | 502 | set new changeset in their target phase (or higher). |
|
495 | 503 | """ |
|
496 | 504 | phcache = repo._phasecache.copy() |
|
497 | 505 | phcache.registernew(repo, tr, targetphase, nodes) |
|
498 | 506 | repo._phasecache.replace(phcache) |
|
499 | 507 | |
|
500 | 508 | def listphases(repo): |
|
501 | 509 | """List phases root for serialization over pushkey""" |
|
502 | 510 | # Use ordered dictionary so behavior is deterministic. |
|
503 | 511 | keys = util.sortdict() |
|
504 | 512 | value = '%i' % draft |
|
505 | 513 | cl = repo.unfiltered().changelog |
|
506 | 514 | for root in repo._phasecache.phaseroots[draft]: |
|
507 | 515 | if repo._phasecache.phase(repo, cl.rev(root)) <= draft: |
|
508 | 516 | keys[hex(root)] = value |
|
509 | 517 | |
|
510 | 518 | if repo.publishing(): |
|
511 | 519 | # Add an extra data to let remote know we are a publishing |
|
512 | 520 | # repo. Publishing repo can't just pretend they are old repo. |
|
513 | 521 | # When pushing to a publishing repo, the client still need to |
|
514 | 522 | # push phase boundary |
|
515 | 523 | # |
|
516 | 524 | # Push do not only push changeset. It also push phase data. |
|
517 | 525 | # New phase data may apply to common changeset which won't be |
|
518 | 526 | # push (as they are common). Here is a very simple example: |
|
519 | 527 | # |
|
520 | 528 | # 1) repo A push changeset X as draft to repo B |
|
521 | 529 | # 2) repo B make changeset X public |
|
522 | 530 | # 3) repo B push to repo A. X is not pushed but the data that |
|
523 | 531 | # X as now public should |
|
524 | 532 | # |
|
525 | 533 | # The server can't handle it on it's own as it has no idea of |
|
526 | 534 | # client phase data. |
|
527 | 535 | keys['publishing'] = 'True' |
|
528 | 536 | return keys |
|
529 | 537 | |
|
530 | 538 | def pushphase(repo, nhex, oldphasestr, newphasestr): |
|
531 | 539 | """List phases root for serialization over pushkey""" |
|
532 | 540 | repo = repo.unfiltered() |
|
533 | 541 | with repo.lock(): |
|
534 | 542 | currentphase = repo[nhex].phase() |
|
535 | 543 | newphase = abs(int(newphasestr)) # let's avoid negative index surprise |
|
536 | 544 | oldphase = abs(int(oldphasestr)) # let's avoid negative index surprise |
|
537 | 545 | if currentphase == oldphase and newphase < oldphase: |
|
538 | 546 | with repo.transaction('pushkey-phase') as tr: |
|
539 | 547 | advanceboundary(repo, tr, newphase, [bin(nhex)]) |
|
540 | 548 | return True |
|
541 | 549 | elif currentphase == newphase: |
|
542 | 550 | # raced, but got correct result |
|
543 | 551 | return True |
|
544 | 552 | else: |
|
545 | 553 | return False |
|
546 | 554 | |
|
547 | 555 | def subsetphaseheads(repo, subset): |
|
548 | 556 | """Finds the phase heads for a subset of a history |
|
549 | 557 | |
|
550 | 558 | Returns a list indexed by phase number where each item is a list of phase |
|
551 | 559 | head nodes. |
|
552 | 560 | """ |
|
553 | 561 | cl = repo.changelog |
|
554 | 562 | |
|
555 | 563 | headsbyphase = [[] for i in allphases] |
|
556 | 564 | # No need to keep track of secret phase; any heads in the subset that |
|
557 | 565 | # are not mentioned are implicitly secret. |
|
558 | 566 | for phase in allphases[:-1]: |
|
559 | 567 | revset = "heads(%%ln & %s())" % phasenames[phase] |
|
560 | 568 | headsbyphase[phase] = [cl.node(r) for r in repo.revs(revset, subset)] |
|
561 | 569 | return headsbyphase |
|
562 | 570 | |
|
563 | 571 | def updatephases(repo, trgetter, headsbyphase): |
|
564 | 572 | """Updates the repo with the given phase heads""" |
|
565 | 573 | # Now advance phase boundaries of all but secret phase |
|
566 | 574 | # |
|
567 | 575 | # run the update (and fetch transaction) only if there are actually things |
|
568 | 576 | # to update. This avoid creating empty transaction during no-op operation. |
|
569 | 577 | |
|
570 | 578 | for phase in allphases[:-1]: |
|
571 | 579 | revset = '%%ln - %s()' % phasenames[phase] |
|
572 | 580 | heads = [c.node() for c in repo.set(revset, headsbyphase[phase])] |
|
573 | 581 | if heads: |
|
574 | 582 | advanceboundary(repo, trgetter(), phase, heads) |
|
575 | 583 | |
|
576 | 584 | def analyzeremotephases(repo, subset, roots): |
|
577 | 585 | """Compute phases heads and root in a subset of node from root dict |
|
578 | 586 | |
|
579 | 587 | * subset is heads of the subset |
|
580 | 588 | * roots is {<nodeid> => phase} mapping. key and value are string. |
|
581 | 589 | |
|
582 | 590 | Accept unknown element input |
|
583 | 591 | """ |
|
584 | 592 | repo = repo.unfiltered() |
|
585 | 593 | # build list from dictionary |
|
586 | 594 | draftroots = [] |
|
587 | 595 | nodemap = repo.changelog.nodemap # to filter unknown nodes |
|
588 | 596 | for nhex, phase in roots.iteritems(): |
|
589 | 597 | if nhex == 'publishing': # ignore data related to publish option |
|
590 | 598 | continue |
|
591 | 599 | node = bin(nhex) |
|
592 | 600 | phase = int(phase) |
|
593 | 601 | if phase == public: |
|
594 | 602 | if node != nullid: |
|
595 | 603 | repo.ui.warn(_('ignoring inconsistent public root' |
|
596 | 604 | ' from remote: %s\n') % nhex) |
|
597 | 605 | elif phase == draft: |
|
598 | 606 | if node in nodemap: |
|
599 | 607 | draftroots.append(node) |
|
600 | 608 | else: |
|
601 | 609 | repo.ui.warn(_('ignoring unexpected root from remote: %i %s\n') |
|
602 | 610 | % (phase, nhex)) |
|
603 | 611 | # compute heads |
|
604 | 612 | publicheads = newheads(repo, subset, draftroots) |
|
605 | 613 | return publicheads, draftroots |
|
606 | 614 | |
|
607 | 615 | class remotephasessummary(object): |
|
608 | 616 | """summarize phase information on the remote side |
|
609 | 617 | |
|
610 | 618 | :publishing: True is the remote is publishing |
|
611 | 619 | :publicheads: list of remote public phase heads (nodes) |
|
612 | 620 | :draftheads: list of remote draft phase heads (nodes) |
|
613 | 621 | :draftroots: list of remote draft phase root (nodes) |
|
614 | 622 | """ |
|
615 | 623 | |
|
616 | 624 | def __init__(self, repo, remotesubset, remoteroots): |
|
617 | 625 | unfi = repo.unfiltered() |
|
618 | 626 | self._allremoteroots = remoteroots |
|
619 | 627 | |
|
620 | 628 | self.publishing = remoteroots.get('publishing', False) |
|
621 | 629 | |
|
622 | 630 | ana = analyzeremotephases(repo, remotesubset, remoteroots) |
|
623 | 631 | self.publicheads, self.draftroots = ana |
|
624 | 632 | # Get the list of all "heads" revs draft on remote |
|
625 | 633 | dheads = unfi.set('heads(%ln::%ln)', self.draftroots, remotesubset) |
|
626 | 634 | self.draftheads = [c.node() for c in dheads] |
|
627 | 635 | |
|
628 | 636 | def newheads(repo, heads, roots): |
|
629 | 637 | """compute new head of a subset minus another |
|
630 | 638 | |
|
631 | 639 | * `heads`: define the first subset |
|
632 | 640 | * `roots`: define the second we subtract from the first""" |
|
633 | 641 | repo = repo.unfiltered() |
|
634 | 642 | revset = repo.set('heads((%ln + parents(%ln)) - (%ln::%ln))', |
|
635 | 643 | heads, roots, roots, heads) |
|
636 | 644 | return [c.node() for c in revset] |
|
637 | 645 | |
|
638 | 646 | |
|
639 | 647 | def newcommitphase(ui): |
|
640 | 648 | """helper to get the target phase of new commit |
|
641 | 649 | |
|
642 | 650 | Handle all possible values for the phases.new-commit options. |
|
643 | 651 | |
|
644 | 652 | """ |
|
645 | 653 | v = ui.config('phases', 'new-commit') |
|
646 | 654 | try: |
|
647 | 655 | return phasenames.index(v) |
|
648 | 656 | except ValueError: |
|
649 | 657 | try: |
|
650 | 658 | return int(v) |
|
651 | 659 | except ValueError: |
|
652 | 660 | msg = _("phases.new-commit: not a valid phase name ('%s')") |
|
653 | 661 | raise error.ConfigError(msg % v) |
|
654 | 662 | |
|
655 | 663 | def hassecret(repo): |
|
656 | 664 | """utility function that check if a repo have any secret changeset.""" |
|
657 | 665 | return bool(repo._phasecache.phaseroots[2]) |
|
658 | 666 | |
|
659 | 667 | def preparehookargs(node, old, new): |
|
660 | 668 | if old is None: |
|
661 | 669 | old = '' |
|
662 | 670 | else: |
|
663 | 671 | old = phasenames[old] |
|
664 | 672 | return {'node': node, |
|
665 | 673 | 'oldphase': old, |
|
666 | 674 | 'phase': phasenames[new]} |
@@ -1,116 +1,116 | |||
|
1 | 1 | # policy.py - module policy logic for Mercurial. |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2015 Gregory Szorc <gregory.szorc@gmail.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import os |
|
11 | 11 | import sys |
|
12 | 12 | |
|
13 | 13 | # Rules for how modules can be loaded. Values are: |
|
14 | 14 | # |
|
15 | 15 | # c - require C extensions |
|
16 | 16 | # allow - allow pure Python implementation when C loading fails |
|
17 | 17 | # cffi - required cffi versions (implemented within pure module) |
|
18 | 18 | # cffi-allow - allow pure Python implementation if cffi version is missing |
|
19 | 19 | # py - only load pure Python modules |
|
20 | 20 | # |
|
21 | 21 | # By default, fall back to the pure modules so the in-place build can |
|
22 | 22 | # run without recompiling the C extensions. This will be overridden by |
|
23 | 23 | # __modulepolicy__ generated by setup.py. |
|
24 | 24 | policy = b'allow' |
|
25 | 25 | _packageprefs = { |
|
26 | 26 | # policy: (versioned package, pure package) |
|
27 | 27 | b'c': (r'cext', None), |
|
28 | 28 | b'allow': (r'cext', r'pure'), |
|
29 | 29 | b'cffi': (r'cffi', None), |
|
30 | 30 | b'cffi-allow': (r'cffi', r'pure'), |
|
31 | 31 | b'py': (None, r'pure'), |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | try: |
|
35 | 35 | from . import __modulepolicy__ |
|
36 | 36 | policy = __modulepolicy__.modulepolicy |
|
37 | 37 | except ImportError: |
|
38 | 38 | pass |
|
39 | 39 | |
|
40 | 40 | # PyPy doesn't load C extensions. |
|
41 | 41 | # |
|
42 | 42 | # The canonical way to do this is to test platform.python_implementation(). |
|
43 | 43 | # But we don't import platform and don't bloat for it here. |
|
44 | 44 | if r'__pypy__' in sys.builtin_module_names: |
|
45 | 45 | policy = b'cffi' |
|
46 | 46 | |
|
47 | 47 | # Our C extensions aren't yet compatible with Python 3. So use pure Python |
|
48 | 48 | # on Python 3 for now. |
|
49 | 49 | if sys.version_info[0] >= 3: |
|
50 | 50 | policy = b'py' |
|
51 | 51 | |
|
52 | 52 | # Environment variable can always force settings. |
|
53 | 53 | if sys.version_info[0] >= 3: |
|
54 | 54 | if r'HGMODULEPOLICY' in os.environ: |
|
55 | 55 | policy = os.environ[r'HGMODULEPOLICY'].encode(r'utf-8') |
|
56 | 56 | else: |
|
57 | 57 | policy = os.environ.get(r'HGMODULEPOLICY', policy) |
|
58 | 58 | |
|
59 | 59 | def _importfrom(pkgname, modname): |
|
60 | 60 | # from .<pkgname> import <modname> (where . is looked through this module) |
|
61 | 61 | fakelocals = {} |
|
62 | 62 | pkg = __import__(pkgname, globals(), fakelocals, [modname], level=1) |
|
63 | 63 | try: |
|
64 | 64 | fakelocals[modname] = mod = getattr(pkg, modname) |
|
65 | 65 | except AttributeError: |
|
66 | 66 | raise ImportError(r'cannot import name %s' % modname) |
|
67 | 67 | # force import; fakelocals[modname] may be replaced with the real module |
|
68 | 68 | getattr(mod, r'__doc__', None) |
|
69 | 69 | return fakelocals[modname] |
|
70 | 70 | |
|
71 | 71 | # keep in sync with "version" in C modules |
|
72 | 72 | _cextversions = { |
|
73 | 73 | (r'cext', r'base85'): 1, |
|
74 | 74 | (r'cext', r'bdiff'): 1, |
|
75 | 75 | (r'cext', r'diffhelpers'): 1, |
|
76 | 76 | (r'cext', r'mpatch'): 1, |
|
77 | 77 | (r'cext', r'osutil'): 1, |
|
78 |
(r'cext', r'parsers'): |
|
|
78 | (r'cext', r'parsers'): 4, | |
|
79 | 79 | } |
|
80 | 80 | |
|
81 | 81 | # map import request to other package or module |
|
82 | 82 | _modredirects = { |
|
83 | 83 | (r'cext', r'charencode'): (r'cext', r'parsers'), |
|
84 | 84 | (r'cffi', r'base85'): (r'pure', r'base85'), |
|
85 | 85 | (r'cffi', r'charencode'): (r'pure', r'charencode'), |
|
86 | 86 | (r'cffi', r'diffhelpers'): (r'pure', r'diffhelpers'), |
|
87 | 87 | (r'cffi', r'parsers'): (r'pure', r'parsers'), |
|
88 | 88 | } |
|
89 | 89 | |
|
90 | 90 | def _checkmod(pkgname, modname, mod): |
|
91 | 91 | expected = _cextversions.get((pkgname, modname)) |
|
92 | 92 | actual = getattr(mod, r'version', None) |
|
93 | 93 | if actual != expected: |
|
94 | 94 | raise ImportError(r'cannot import module %s.%s ' |
|
95 | 95 | r'(expected version: %d, actual: %r)' |
|
96 | 96 | % (pkgname, modname, expected, actual)) |
|
97 | 97 | |
|
98 | 98 | def importmod(modname): |
|
99 | 99 | """Import module according to policy and check API version""" |
|
100 | 100 | try: |
|
101 | 101 | verpkg, purepkg = _packageprefs[policy] |
|
102 | 102 | except KeyError: |
|
103 | 103 | raise ImportError(r'invalid HGMODULEPOLICY %r' % policy) |
|
104 | 104 | assert verpkg or purepkg |
|
105 | 105 | if verpkg: |
|
106 | 106 | pn, mn = _modredirects.get((verpkg, modname), (verpkg, modname)) |
|
107 | 107 | try: |
|
108 | 108 | mod = _importfrom(pn, mn) |
|
109 | 109 | if pn == verpkg: |
|
110 | 110 | _checkmod(pn, mn, mod) |
|
111 | 111 | return mod |
|
112 | 112 | except ImportError: |
|
113 | 113 | if not purepkg: |
|
114 | 114 | raise |
|
115 | 115 | pn, mn = _modredirects.get((purepkg, modname), (purepkg, modname)) |
|
116 | 116 | return _importfrom(pn, mn) |
General Comments 0
You need to be logged in to leave comments.
Login now