Show More
@@ -1,1954 +1,1959 b'' | |||
|
1 | 1 | /* |
|
2 | 2 | parsers.c - efficient content parsing |
|
3 | 3 | |
|
4 | 4 | Copyright 2008 Matt Mackall <mpm@selenic.com> and others |
|
5 | 5 | |
|
6 | 6 | This software may be used and distributed according to the terms of |
|
7 | 7 | the GNU General Public License, incorporated herein by reference. |
|
8 | 8 | */ |
|
9 | 9 | |
|
10 | 10 | #include <Python.h> |
|
11 | 11 | #include <ctype.h> |
|
12 | 12 | #include <stddef.h> |
|
13 | 13 | #include <string.h> |
|
14 | 14 | |
|
15 | 15 | #include "util.h" |
|
16 | 16 | |
|
17 | 17 | static int8_t hextable[256] = { |
|
18 | 18 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
19 | 19 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
20 | 20 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
21 | 21 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0-9 */ |
|
22 | 22 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* A-F */ |
|
23 | 23 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
24 | 24 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a-f */ |
|
25 | 25 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
26 | 26 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
27 | 27 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
28 | 28 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
29 | 29 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
30 | 30 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
31 | 31 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
32 | 32 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
33 | 33 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
|
34 | 34 | }; |
|
35 | 35 | |
|
36 | 36 | static inline int hexdigit(const char *p, Py_ssize_t off) |
|
37 | 37 | { |
|
38 | 38 | int8_t val = hextable[(unsigned char)p[off]]; |
|
39 | 39 | |
|
40 | 40 | if (val >= 0) { |
|
41 | 41 | return val; |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | PyErr_SetString(PyExc_ValueError, "input contains non-hex character"); |
|
45 | 45 | return 0; |
|
46 | 46 | } |
|
47 | 47 | |
|
48 | 48 | /* |
|
49 | 49 | * Turn a hex-encoded string into binary. |
|
50 | 50 | */ |
|
51 | 51 | static PyObject *unhexlify(const char *str, int len) |
|
52 | 52 | { |
|
53 | 53 | PyObject *ret; |
|
54 | 54 | char *d; |
|
55 | 55 | int i; |
|
56 | 56 | |
|
57 | 57 | ret = PyBytes_FromStringAndSize(NULL, len / 2); |
|
58 | 58 | |
|
59 | 59 | if (!ret) |
|
60 | 60 | return NULL; |
|
61 | 61 | |
|
62 | 62 | d = PyBytes_AsString(ret); |
|
63 | 63 | |
|
64 | 64 | for (i = 0; i < len;) { |
|
65 | 65 | int hi = hexdigit(str, i++); |
|
66 | 66 | int lo = hexdigit(str, i++); |
|
67 | 67 | *d++ = (hi << 4) | lo; |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | return ret; |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | /* |
|
74 | 74 | * This code assumes that a manifest is stitched together with newline |
|
75 | 75 | * ('\n') characters. |
|
76 | 76 | */ |
|
77 | 77 | static PyObject *parse_manifest(PyObject *self, PyObject *args) |
|
78 | 78 | { |
|
79 | 79 | PyObject *mfdict, *fdict; |
|
80 | 80 | char *str, *cur, *start, *zero; |
|
81 | 81 | int len; |
|
82 | 82 | |
|
83 | 83 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest", |
|
84 | 84 | &PyDict_Type, &mfdict, |
|
85 | 85 | &PyDict_Type, &fdict, |
|
86 | 86 | &str, &len)) |
|
87 | 87 | goto quit; |
|
88 | 88 | |
|
89 | 89 | for (start = cur = str, zero = NULL; cur < str + len; cur++) { |
|
90 | 90 | PyObject *file = NULL, *node = NULL; |
|
91 | 91 | PyObject *flags = NULL; |
|
92 | 92 | ptrdiff_t nlen; |
|
93 | 93 | |
|
94 | 94 | if (!*cur) { |
|
95 | 95 | zero = cur; |
|
96 | 96 | continue; |
|
97 | 97 | } |
|
98 | 98 | else if (*cur != '\n') |
|
99 | 99 | continue; |
|
100 | 100 | |
|
101 | 101 | if (!zero) { |
|
102 | 102 | PyErr_SetString(PyExc_ValueError, |
|
103 | 103 | "manifest entry has no separator"); |
|
104 | 104 | goto quit; |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | file = PyBytes_FromStringAndSize(start, zero - start); |
|
108 | 108 | |
|
109 | 109 | if (!file) |
|
110 | 110 | goto bail; |
|
111 | 111 | |
|
112 | 112 | nlen = cur - zero - 1; |
|
113 | 113 | |
|
114 | 114 | node = unhexlify(zero + 1, nlen > 40 ? 40 : (int)nlen); |
|
115 | 115 | if (!node) |
|
116 | 116 | goto bail; |
|
117 | 117 | |
|
118 | 118 | if (nlen > 40) { |
|
119 | 119 | flags = PyBytes_FromStringAndSize(zero + 41, |
|
120 | 120 | nlen - 40); |
|
121 | 121 | if (!flags) |
|
122 | 122 | goto bail; |
|
123 | 123 | |
|
124 | 124 | if (PyDict_SetItem(fdict, file, flags) == -1) |
|
125 | 125 | goto bail; |
|
126 | 126 | } |
|
127 | 127 | |
|
128 | 128 | if (PyDict_SetItem(mfdict, file, node) == -1) |
|
129 | 129 | goto bail; |
|
130 | 130 | |
|
131 | 131 | start = cur + 1; |
|
132 | 132 | zero = NULL; |
|
133 | 133 | |
|
134 | 134 | Py_XDECREF(flags); |
|
135 | 135 | Py_XDECREF(node); |
|
136 | 136 | Py_XDECREF(file); |
|
137 | 137 | continue; |
|
138 | 138 | bail: |
|
139 | 139 | Py_XDECREF(flags); |
|
140 | 140 | Py_XDECREF(node); |
|
141 | 141 | Py_XDECREF(file); |
|
142 | 142 | goto quit; |
|
143 | 143 | } |
|
144 | 144 | |
|
145 | 145 | if (len > 0 && *(cur - 1) != '\n') { |
|
146 | 146 | PyErr_SetString(PyExc_ValueError, |
|
147 | 147 | "manifest contains trailing garbage"); |
|
148 | 148 | goto quit; |
|
149 | 149 | } |
|
150 | 150 | |
|
151 | 151 | Py_INCREF(Py_None); |
|
152 | 152 | return Py_None; |
|
153 | 153 | quit: |
|
154 | 154 | return NULL; |
|
155 | 155 | } |
|
156 | 156 | |
|
157 | 157 | static PyObject *parse_dirstate(PyObject *self, PyObject *args) |
|
158 | 158 | { |
|
159 | 159 | PyObject *dmap, *cmap, *parents = NULL, *ret = NULL; |
|
160 | 160 | PyObject *fname = NULL, *cname = NULL, *entry = NULL; |
|
161 | 161 | char state, *str, *cur, *end, *cpos; |
|
162 | 162 | int mode, size, mtime; |
|
163 | 163 | unsigned int flen; |
|
164 | 164 | int len; |
|
165 | 165 | |
|
166 | 166 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate", |
|
167 | 167 | &PyDict_Type, &dmap, |
|
168 | 168 | &PyDict_Type, &cmap, |
|
169 | 169 | &str, &len)) |
|
170 | 170 | goto quit; |
|
171 | 171 | |
|
172 | 172 | /* read parents */ |
|
173 | 173 | if (len < 40) |
|
174 | 174 | goto quit; |
|
175 | 175 | |
|
176 | 176 | parents = Py_BuildValue("s#s#", str, 20, str + 20, 20); |
|
177 | 177 | if (!parents) |
|
178 | 178 | goto quit; |
|
179 | 179 | |
|
180 | 180 | /* read filenames */ |
|
181 | 181 | cur = str + 40; |
|
182 | 182 | end = str + len; |
|
183 | 183 | |
|
184 | 184 | while (cur < end - 17) { |
|
185 | 185 | /* unpack header */ |
|
186 | 186 | state = *cur; |
|
187 | 187 | mode = getbe32(cur + 1); |
|
188 | 188 | size = getbe32(cur + 5); |
|
189 | 189 | mtime = getbe32(cur + 9); |
|
190 | 190 | flen = getbe32(cur + 13); |
|
191 | 191 | cur += 17; |
|
192 | 192 | if (cur + flen > end || cur + flen < cur) { |
|
193 | 193 | PyErr_SetString(PyExc_ValueError, "overflow in dirstate"); |
|
194 | 194 | goto quit; |
|
195 | 195 | } |
|
196 | 196 | |
|
197 | 197 | entry = Py_BuildValue("ciii", state, mode, size, mtime); |
|
198 | 198 | if (!entry) |
|
199 | 199 | goto quit; |
|
200 | 200 | PyObject_GC_UnTrack(entry); /* don't waste time with this */ |
|
201 | 201 | |
|
202 | 202 | cpos = memchr(cur, 0, flen); |
|
203 | 203 | if (cpos) { |
|
204 | 204 | fname = PyBytes_FromStringAndSize(cur, cpos - cur); |
|
205 | 205 | cname = PyBytes_FromStringAndSize(cpos + 1, |
|
206 | 206 | flen - (cpos - cur) - 1); |
|
207 | 207 | if (!fname || !cname || |
|
208 | 208 | PyDict_SetItem(cmap, fname, cname) == -1 || |
|
209 | 209 | PyDict_SetItem(dmap, fname, entry) == -1) |
|
210 | 210 | goto quit; |
|
211 | 211 | Py_DECREF(cname); |
|
212 | 212 | } else { |
|
213 | 213 | fname = PyBytes_FromStringAndSize(cur, flen); |
|
214 | 214 | if (!fname || |
|
215 | 215 | PyDict_SetItem(dmap, fname, entry) == -1) |
|
216 | 216 | goto quit; |
|
217 | 217 | } |
|
218 | 218 | cur += flen; |
|
219 | 219 | Py_DECREF(fname); |
|
220 | 220 | Py_DECREF(entry); |
|
221 | 221 | fname = cname = entry = NULL; |
|
222 | 222 | } |
|
223 | 223 | |
|
224 | 224 | ret = parents; |
|
225 | 225 | Py_INCREF(ret); |
|
226 | 226 | quit: |
|
227 | 227 | Py_XDECREF(fname); |
|
228 | 228 | Py_XDECREF(cname); |
|
229 | 229 | Py_XDECREF(entry); |
|
230 | 230 | Py_XDECREF(parents); |
|
231 | 231 | return ret; |
|
232 | 232 | } |
|
233 | 233 | |
|
234 | 234 | static inline int getintat(PyObject *tuple, int off, uint32_t *v) |
|
235 | 235 | { |
|
236 | 236 | PyObject *o = PyTuple_GET_ITEM(tuple, off); |
|
237 | 237 | long val; |
|
238 | 238 | |
|
239 | 239 | if (PyInt_Check(o)) |
|
240 | 240 | val = PyInt_AS_LONG(o); |
|
241 | 241 | else if (PyLong_Check(o)) { |
|
242 | 242 | val = PyLong_AsLong(o); |
|
243 | 243 | if (val == -1 && PyErr_Occurred()) |
|
244 | 244 | return -1; |
|
245 | 245 | } else { |
|
246 | 246 | PyErr_SetString(PyExc_TypeError, "expected an int or long"); |
|
247 | 247 | return -1; |
|
248 | 248 | } |
|
249 | 249 | if (LONG_MAX > INT_MAX && (val > INT_MAX || val < INT_MIN)) { |
|
250 | 250 | PyErr_SetString(PyExc_OverflowError, |
|
251 | 251 | "Python value to large to convert to uint32_t"); |
|
252 | 252 | return -1; |
|
253 | 253 | } |
|
254 | 254 | *v = (uint32_t)val; |
|
255 | 255 | return 0; |
|
256 | 256 | } |
|
257 | 257 | |
|
258 | 258 | static PyObject *dirstate_unset; |
|
259 | 259 | |
|
260 | 260 | /* |
|
261 | 261 | * Efficiently pack a dirstate object into its on-disk format. |
|
262 | 262 | */ |
|
263 | 263 | static PyObject *pack_dirstate(PyObject *self, PyObject *args) |
|
264 | 264 | { |
|
265 | 265 | PyObject *packobj = NULL; |
|
266 | 266 | PyObject *map, *copymap, *pl; |
|
267 | 267 | Py_ssize_t nbytes, pos, l; |
|
268 | 268 | PyObject *k, *v, *pn; |
|
269 | 269 | char *p, *s; |
|
270 | 270 | double now; |
|
271 | 271 | |
|
272 | 272 | if (!PyArg_ParseTuple(args, "O!O!Od:pack_dirstate", |
|
273 | 273 | &PyDict_Type, &map, &PyDict_Type, ©map, |
|
274 | 274 | &pl, &now)) |
|
275 | 275 | return NULL; |
|
276 | 276 | |
|
277 | 277 | if (!PySequence_Check(pl) || PySequence_Size(pl) != 2) { |
|
278 | 278 | PyErr_SetString(PyExc_TypeError, "expected 2-element sequence"); |
|
279 | 279 | return NULL; |
|
280 | 280 | } |
|
281 | 281 | |
|
282 | 282 | /* Figure out how much we need to allocate. */ |
|
283 | 283 | for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) { |
|
284 | 284 | PyObject *c; |
|
285 | 285 | if (!PyString_Check(k)) { |
|
286 | 286 | PyErr_SetString(PyExc_TypeError, "expected string key"); |
|
287 | 287 | goto bail; |
|
288 | 288 | } |
|
289 | 289 | nbytes += PyString_GET_SIZE(k) + 17; |
|
290 | 290 | c = PyDict_GetItem(copymap, k); |
|
291 | 291 | if (c) { |
|
292 | 292 | if (!PyString_Check(c)) { |
|
293 | 293 | PyErr_SetString(PyExc_TypeError, |
|
294 | 294 | "expected string key"); |
|
295 | 295 | goto bail; |
|
296 | 296 | } |
|
297 | 297 | nbytes += PyString_GET_SIZE(c) + 1; |
|
298 | 298 | } |
|
299 | 299 | } |
|
300 | 300 | |
|
301 | 301 | packobj = PyString_FromStringAndSize(NULL, nbytes); |
|
302 | 302 | if (packobj == NULL) |
|
303 | 303 | goto bail; |
|
304 | 304 | |
|
305 | 305 | p = PyString_AS_STRING(packobj); |
|
306 | 306 | |
|
307 | 307 | pn = PySequence_ITEM(pl, 0); |
|
308 | 308 | if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { |
|
309 | 309 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); |
|
310 | 310 | goto bail; |
|
311 | 311 | } |
|
312 | 312 | memcpy(p, s, l); |
|
313 | 313 | p += 20; |
|
314 | 314 | pn = PySequence_ITEM(pl, 1); |
|
315 | 315 | if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { |
|
316 | 316 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); |
|
317 | 317 | goto bail; |
|
318 | 318 | } |
|
319 | 319 | memcpy(p, s, l); |
|
320 | 320 | p += 20; |
|
321 | 321 | |
|
322 | 322 | for (pos = 0; PyDict_Next(map, &pos, &k, &v); ) { |
|
323 | 323 | uint32_t mode, size, mtime; |
|
324 | 324 | Py_ssize_t len, l; |
|
325 | 325 | PyObject *o; |
|
326 | 326 | char *s, *t; |
|
327 | 327 | |
|
328 | 328 | if (!PyTuple_Check(v) || PyTuple_GET_SIZE(v) != 4) { |
|
329 | 329 | PyErr_SetString(PyExc_TypeError, "expected a 4-tuple"); |
|
330 | 330 | goto bail; |
|
331 | 331 | } |
|
332 | 332 | o = PyTuple_GET_ITEM(v, 0); |
|
333 | 333 | if (PyString_AsStringAndSize(o, &s, &l) == -1 || l != 1) { |
|
334 | 334 | PyErr_SetString(PyExc_TypeError, "expected one byte"); |
|
335 | 335 | goto bail; |
|
336 | 336 | } |
|
337 | 337 | *p++ = *s; |
|
338 | 338 | if (getintat(v, 1, &mode) == -1) |
|
339 | 339 | goto bail; |
|
340 | 340 | if (getintat(v, 2, &size) == -1) |
|
341 | 341 | goto bail; |
|
342 | 342 | if (getintat(v, 3, &mtime) == -1) |
|
343 | 343 | goto bail; |
|
344 | 344 | if (*s == 'n' && mtime == (uint32_t)now) { |
|
345 | 345 | /* See pure/parsers.py:pack_dirstate for why we do |
|
346 | 346 | * this. */ |
|
347 | 347 | if (PyDict_SetItem(map, k, dirstate_unset) == -1) |
|
348 | 348 | goto bail; |
|
349 | 349 | mtime = -1; |
|
350 | 350 | } |
|
351 | 351 | putbe32(mode, p); |
|
352 | 352 | putbe32(size, p + 4); |
|
353 | 353 | putbe32(mtime, p + 8); |
|
354 | 354 | t = p + 12; |
|
355 | 355 | p += 16; |
|
356 | 356 | len = PyString_GET_SIZE(k); |
|
357 | 357 | memcpy(p, PyString_AS_STRING(k), len); |
|
358 | 358 | p += len; |
|
359 | 359 | o = PyDict_GetItem(copymap, k); |
|
360 | 360 | if (o) { |
|
361 | 361 | *p++ = '\0'; |
|
362 | 362 | l = PyString_GET_SIZE(o); |
|
363 | 363 | memcpy(p, PyString_AS_STRING(o), l); |
|
364 | 364 | p += l; |
|
365 | 365 | len += l + 1; |
|
366 | 366 | } |
|
367 | 367 | putbe32((uint32_t)len, t); |
|
368 | 368 | } |
|
369 | 369 | |
|
370 | 370 | pos = p - PyString_AS_STRING(packobj); |
|
371 | 371 | if (pos != nbytes) { |
|
372 | 372 | PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld", |
|
373 | 373 | (long)pos, (long)nbytes); |
|
374 | 374 | goto bail; |
|
375 | 375 | } |
|
376 | 376 | |
|
377 | 377 | return packobj; |
|
378 | 378 | bail: |
|
379 | 379 | Py_XDECREF(packobj); |
|
380 | 380 | return NULL; |
|
381 | 381 | } |
|
382 | 382 | |
|
383 | 383 | /* |
|
384 | 384 | * A base-16 trie for fast node->rev mapping. |
|
385 | 385 | * |
|
386 | 386 | * Positive value is index of the next node in the trie |
|
387 | 387 | * Negative value is a leaf: -(rev + 1) |
|
388 | 388 | * Zero is empty |
|
389 | 389 | */ |
|
390 | 390 | typedef struct { |
|
391 | 391 | int children[16]; |
|
392 | 392 | } nodetree; |
|
393 | 393 | |
|
394 | 394 | /* |
|
395 | 395 | * This class has two behaviours. |
|
396 | 396 | * |
|
397 | 397 | * When used in a list-like way (with integer keys), we decode an |
|
398 | 398 | * entry in a RevlogNG index file on demand. Our last entry is a |
|
399 | 399 | * sentinel, always a nullid. We have limited support for |
|
400 | 400 | * integer-keyed insert and delete, only at elements right before the |
|
401 | 401 | * sentinel. |
|
402 | 402 | * |
|
403 | 403 | * With string keys, we lazily perform a reverse mapping from node to |
|
404 | 404 | * rev, using a base-16 trie. |
|
405 | 405 | */ |
|
406 | 406 | typedef struct { |
|
407 | 407 | PyObject_HEAD |
|
408 | 408 | /* Type-specific fields go here. */ |
|
409 | 409 | PyObject *data; /* raw bytes of index */ |
|
410 | 410 | PyObject **cache; /* cached tuples */ |
|
411 | 411 | const char **offsets; /* populated on demand */ |
|
412 | 412 | Py_ssize_t raw_length; /* original number of elements */ |
|
413 | 413 | Py_ssize_t length; /* current number of elements */ |
|
414 | 414 | PyObject *added; /* populated on demand */ |
|
415 | 415 | PyObject *headrevs; /* cache, invalidated on changes */ |
|
416 | 416 | nodetree *nt; /* base-16 trie */ |
|
417 | 417 | int ntlength; /* # nodes in use */ |
|
418 | 418 | int ntcapacity; /* # nodes allocated */ |
|
419 | 419 | int ntdepth; /* maximum depth of tree */ |
|
420 | 420 | int ntsplits; /* # splits performed */ |
|
421 | 421 | int ntrev; /* last rev scanned */ |
|
422 | 422 | int ntlookups; /* # lookups */ |
|
423 | 423 | int ntmisses; /* # lookups that miss the cache */ |
|
424 | 424 | int inlined; |
|
425 | 425 | } indexObject; |
|
426 | 426 | |
|
427 | 427 | static Py_ssize_t index_length(const indexObject *self) |
|
428 | 428 | { |
|
429 | 429 | if (self->added == NULL) |
|
430 | 430 | return self->length; |
|
431 | 431 | return self->length + PyList_GET_SIZE(self->added); |
|
432 | 432 | } |
|
433 | 433 | |
|
434 | 434 | static PyObject *nullentry; |
|
435 | 435 | static const char nullid[20]; |
|
436 | 436 | |
|
437 | 437 | static long inline_scan(indexObject *self, const char **offsets); |
|
438 | 438 | |
|
439 | 439 | #if LONG_MAX == 0x7fffffffL |
|
440 | 440 | static char *tuple_format = "Kiiiiiis#"; |
|
441 | 441 | #else |
|
442 | 442 | static char *tuple_format = "kiiiiiis#"; |
|
443 | 443 | #endif |
|
444 | 444 | |
|
445 | 445 | /* A RevlogNG v1 index entry is 64 bytes long. */ |
|
446 | 446 | static const long v1_hdrsize = 64; |
|
447 | 447 | |
|
448 | 448 | /* |
|
449 | 449 | * Return a pointer to the beginning of a RevlogNG record. |
|
450 | 450 | */ |
|
451 | 451 | static const char *index_deref(indexObject *self, Py_ssize_t pos) |
|
452 | 452 | { |
|
453 | 453 | if (self->inlined && pos > 0) { |
|
454 | 454 | if (self->offsets == NULL) { |
|
455 | 455 | self->offsets = malloc(self->raw_length * |
|
456 | 456 | sizeof(*self->offsets)); |
|
457 | 457 | if (self->offsets == NULL) |
|
458 | 458 | return (const char *)PyErr_NoMemory(); |
|
459 | 459 | inline_scan(self, self->offsets); |
|
460 | 460 | } |
|
461 | 461 | return self->offsets[pos]; |
|
462 | 462 | } |
|
463 | 463 | |
|
464 | 464 | return PyString_AS_STRING(self->data) + pos * v1_hdrsize; |
|
465 | 465 | } |
|
466 | 466 | |
|
467 | 467 | /* |
|
468 | 468 | * RevlogNG format (all in big endian, data may be inlined): |
|
469 | 469 | * 6 bytes: offset |
|
470 | 470 | * 2 bytes: flags |
|
471 | 471 | * 4 bytes: compressed length |
|
472 | 472 | * 4 bytes: uncompressed length |
|
473 | 473 | * 4 bytes: base revision |
|
474 | 474 | * 4 bytes: link revision |
|
475 | 475 | * 4 bytes: parent 1 revision |
|
476 | 476 | * 4 bytes: parent 2 revision |
|
477 | 477 | * 32 bytes: nodeid (only 20 bytes used) |
|
478 | 478 | */ |
|
479 | 479 | static PyObject *index_get(indexObject *self, Py_ssize_t pos) |
|
480 | 480 | { |
|
481 | 481 | uint64_t offset_flags; |
|
482 | 482 | int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2; |
|
483 | 483 | const char *c_node_id; |
|
484 | 484 | const char *data; |
|
485 | 485 | Py_ssize_t length = index_length(self); |
|
486 | 486 | PyObject *entry; |
|
487 | 487 | |
|
488 | 488 | if (pos < 0) |
|
489 | 489 | pos += length; |
|
490 | 490 | |
|
491 | 491 | if (pos < 0 || pos >= length) { |
|
492 | 492 | PyErr_SetString(PyExc_IndexError, "revlog index out of range"); |
|
493 | 493 | return NULL; |
|
494 | 494 | } |
|
495 | 495 | |
|
496 | 496 | if (pos == length - 1) { |
|
497 | 497 | Py_INCREF(nullentry); |
|
498 | 498 | return nullentry; |
|
499 | 499 | } |
|
500 | 500 | |
|
501 | 501 | if (pos >= self->length - 1) { |
|
502 | 502 | PyObject *obj; |
|
503 | 503 | obj = PyList_GET_ITEM(self->added, pos - self->length + 1); |
|
504 | 504 | Py_INCREF(obj); |
|
505 | 505 | return obj; |
|
506 | 506 | } |
|
507 | 507 | |
|
508 | 508 | if (self->cache) { |
|
509 | 509 | if (self->cache[pos]) { |
|
510 | 510 | Py_INCREF(self->cache[pos]); |
|
511 | 511 | return self->cache[pos]; |
|
512 | 512 | } |
|
513 | 513 | } else { |
|
514 | 514 | self->cache = calloc(self->raw_length, sizeof(PyObject *)); |
|
515 | 515 | if (self->cache == NULL) |
|
516 | 516 | return PyErr_NoMemory(); |
|
517 | 517 | } |
|
518 | 518 | |
|
519 | 519 | data = index_deref(self, pos); |
|
520 | 520 | if (data == NULL) |
|
521 | 521 | return NULL; |
|
522 | 522 | |
|
523 | 523 | offset_flags = getbe32(data + 4); |
|
524 | 524 | if (pos == 0) /* mask out version number for the first entry */ |
|
525 | 525 | offset_flags &= 0xFFFF; |
|
526 | 526 | else { |
|
527 | 527 | uint32_t offset_high = getbe32(data); |
|
528 | 528 | offset_flags |= ((uint64_t)offset_high) << 32; |
|
529 | 529 | } |
|
530 | 530 | |
|
531 | 531 | comp_len = getbe32(data + 8); |
|
532 | 532 | uncomp_len = getbe32(data + 12); |
|
533 | 533 | base_rev = getbe32(data + 16); |
|
534 | 534 | link_rev = getbe32(data + 20); |
|
535 | 535 | parent_1 = getbe32(data + 24); |
|
536 | 536 | parent_2 = getbe32(data + 28); |
|
537 | 537 | c_node_id = data + 32; |
|
538 | 538 | |
|
539 | 539 | entry = Py_BuildValue(tuple_format, offset_flags, comp_len, |
|
540 | 540 | uncomp_len, base_rev, link_rev, |
|
541 | 541 | parent_1, parent_2, c_node_id, 20); |
|
542 | 542 | |
|
543 | 543 | if (entry) { |
|
544 | 544 | PyObject_GC_UnTrack(entry); |
|
545 | 545 | Py_INCREF(entry); |
|
546 | 546 | } |
|
547 | 547 | |
|
548 | 548 | self->cache[pos] = entry; |
|
549 | 549 | |
|
550 | 550 | return entry; |
|
551 | 551 | } |
|
552 | 552 | |
|
553 | 553 | /* |
|
554 | 554 | * Return the 20-byte SHA of the node corresponding to the given rev. |
|
555 | 555 | */ |
|
556 | 556 | static const char *index_node(indexObject *self, Py_ssize_t pos) |
|
557 | 557 | { |
|
558 | 558 | Py_ssize_t length = index_length(self); |
|
559 | 559 | const char *data; |
|
560 | 560 | |
|
561 | 561 | if (pos == length - 1 || pos == INT_MAX) |
|
562 | 562 | return nullid; |
|
563 | 563 | |
|
564 | 564 | if (pos >= length) |
|
565 | 565 | return NULL; |
|
566 | 566 | |
|
567 | 567 | if (pos >= self->length - 1) { |
|
568 | 568 | PyObject *tuple, *str; |
|
569 | 569 | tuple = PyList_GET_ITEM(self->added, pos - self->length + 1); |
|
570 | 570 | str = PyTuple_GetItem(tuple, 7); |
|
571 | 571 | return str ? PyString_AS_STRING(str) : NULL; |
|
572 | 572 | } |
|
573 | 573 | |
|
574 | 574 | data = index_deref(self, pos); |
|
575 | 575 | return data ? data + 32 : NULL; |
|
576 | 576 | } |
|
577 | 577 | |
|
578 | 578 | static int nt_insert(indexObject *self, const char *node, int rev); |
|
579 | 579 | |
|
580 | 580 | static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen) |
|
581 | 581 | { |
|
582 | 582 | if (PyString_AsStringAndSize(obj, node, nodelen) == -1) |
|
583 | 583 | return -1; |
|
584 | 584 | if (*nodelen == 20) |
|
585 | 585 | return 0; |
|
586 | 586 | PyErr_SetString(PyExc_ValueError, "20-byte hash required"); |
|
587 | 587 | return -1; |
|
588 | 588 | } |
|
589 | 589 | |
|
590 | 590 | static PyObject *index_insert(indexObject *self, PyObject *args) |
|
591 | 591 | { |
|
592 | 592 | PyObject *obj; |
|
593 | 593 | char *node; |
|
594 | 594 | long offset; |
|
595 | 595 | Py_ssize_t len, nodelen; |
|
596 | 596 | |
|
597 | 597 | if (!PyArg_ParseTuple(args, "lO", &offset, &obj)) |
|
598 | 598 | return NULL; |
|
599 | 599 | |
|
600 | 600 | if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) { |
|
601 | 601 | PyErr_SetString(PyExc_TypeError, "8-tuple required"); |
|
602 | 602 | return NULL; |
|
603 | 603 | } |
|
604 | 604 | |
|
605 | 605 | if (node_check(PyTuple_GET_ITEM(obj, 7), &node, &nodelen) == -1) |
|
606 | 606 | return NULL; |
|
607 | 607 | |
|
608 | 608 | len = index_length(self); |
|
609 | 609 | |
|
610 | 610 | if (offset < 0) |
|
611 | 611 | offset += len; |
|
612 | 612 | |
|
613 | 613 | if (offset != len - 1) { |
|
614 | 614 | PyErr_SetString(PyExc_IndexError, |
|
615 | 615 | "insert only supported at index -1"); |
|
616 | 616 | return NULL; |
|
617 | 617 | } |
|
618 | 618 | |
|
619 | 619 | if (offset > INT_MAX) { |
|
620 | 620 | PyErr_SetString(PyExc_ValueError, |
|
621 | 621 | "currently only 2**31 revs supported"); |
|
622 | 622 | return NULL; |
|
623 | 623 | } |
|
624 | 624 | |
|
625 | 625 | if (self->added == NULL) { |
|
626 | 626 | self->added = PyList_New(0); |
|
627 | 627 | if (self->added == NULL) |
|
628 | 628 | return NULL; |
|
629 | 629 | } |
|
630 | 630 | |
|
631 | 631 | if (PyList_Append(self->added, obj) == -1) |
|
632 | 632 | return NULL; |
|
633 | 633 | |
|
634 | 634 | if (self->nt) |
|
635 | 635 | nt_insert(self, node, (int)offset); |
|
636 | 636 | |
|
637 | 637 | Py_CLEAR(self->headrevs); |
|
638 | 638 | Py_RETURN_NONE; |
|
639 | 639 | } |
|
640 | 640 | |
|
641 | 641 | static void _index_clearcaches(indexObject *self) |
|
642 | 642 | { |
|
643 | 643 | if (self->cache) { |
|
644 | 644 | Py_ssize_t i; |
|
645 | 645 | |
|
646 | 646 | for (i = 0; i < self->raw_length; i++) |
|
647 | 647 | Py_CLEAR(self->cache[i]); |
|
648 | 648 | free(self->cache); |
|
649 | 649 | self->cache = NULL; |
|
650 | 650 | } |
|
651 | 651 | if (self->offsets) { |
|
652 | 652 | free(self->offsets); |
|
653 | 653 | self->offsets = NULL; |
|
654 | 654 | } |
|
655 | 655 | if (self->nt) { |
|
656 | 656 | free(self->nt); |
|
657 | 657 | self->nt = NULL; |
|
658 | 658 | } |
|
659 | 659 | Py_CLEAR(self->headrevs); |
|
660 | 660 | } |
|
661 | 661 | |
|
662 | 662 | static PyObject *index_clearcaches(indexObject *self) |
|
663 | 663 | { |
|
664 | 664 | _index_clearcaches(self); |
|
665 | 665 | self->ntlength = self->ntcapacity = 0; |
|
666 | 666 | self->ntdepth = self->ntsplits = 0; |
|
667 | 667 | self->ntrev = -1; |
|
668 | 668 | self->ntlookups = self->ntmisses = 0; |
|
669 | 669 | Py_RETURN_NONE; |
|
670 | 670 | } |
|
671 | 671 | |
|
672 | 672 | static PyObject *index_stats(indexObject *self) |
|
673 | 673 | { |
|
674 | 674 | PyObject *obj = PyDict_New(); |
|
675 | 675 | |
|
676 | 676 | if (obj == NULL) |
|
677 | 677 | return NULL; |
|
678 | 678 | |
|
679 | 679 | #define istat(__n, __d) \ |
|
680 | 680 | if (PyDict_SetItemString(obj, __d, PyInt_FromSsize_t(self->__n)) == -1) \ |
|
681 | 681 | goto bail; |
|
682 | 682 | |
|
683 | 683 | if (self->added) { |
|
684 | 684 | Py_ssize_t len = PyList_GET_SIZE(self->added); |
|
685 | 685 | if (PyDict_SetItemString(obj, "index entries added", |
|
686 | 686 | PyInt_FromSsize_t(len)) == -1) |
|
687 | 687 | goto bail; |
|
688 | 688 | } |
|
689 | 689 | |
|
690 | 690 | if (self->raw_length != self->length - 1) |
|
691 | 691 | istat(raw_length, "revs on disk"); |
|
692 | 692 | istat(length, "revs in memory"); |
|
693 | 693 | istat(ntcapacity, "node trie capacity"); |
|
694 | 694 | istat(ntdepth, "node trie depth"); |
|
695 | 695 | istat(ntlength, "node trie count"); |
|
696 | 696 | istat(ntlookups, "node trie lookups"); |
|
697 | 697 | istat(ntmisses, "node trie misses"); |
|
698 | 698 | istat(ntrev, "node trie last rev scanned"); |
|
699 | 699 | istat(ntsplits, "node trie splits"); |
|
700 | 700 | |
|
701 | 701 | #undef istat |
|
702 | 702 | |
|
703 | 703 | return obj; |
|
704 | 704 | |
|
705 | 705 | bail: |
|
706 | 706 | Py_XDECREF(obj); |
|
707 | 707 | return NULL; |
|
708 | 708 | } |
|
709 | 709 | |
|
710 | 710 | /* |
|
711 | 711 | * When we cache a list, we want to be sure the caller can't mutate |
|
712 | 712 | * the cached copy. |
|
713 | 713 | */ |
|
714 | 714 | static PyObject *list_copy(PyObject *list) |
|
715 | 715 | { |
|
716 | 716 | Py_ssize_t len = PyList_GET_SIZE(list); |
|
717 | 717 | PyObject *newlist = PyList_New(len); |
|
718 | 718 | Py_ssize_t i; |
|
719 | 719 | |
|
720 | 720 | if (newlist == NULL) |
|
721 | 721 | return NULL; |
|
722 | 722 | |
|
723 | 723 | for (i = 0; i < len; i++) { |
|
724 | 724 | PyObject *obj = PyList_GET_ITEM(list, i); |
|
725 | 725 | Py_INCREF(obj); |
|
726 | 726 | PyList_SET_ITEM(newlist, i, obj); |
|
727 | 727 | } |
|
728 | 728 | |
|
729 | 729 | return newlist; |
|
730 | 730 | } |
|
731 | 731 | |
|
732 | 732 | static PyObject *index_headrevs(indexObject *self) |
|
733 | 733 | { |
|
734 | 734 | Py_ssize_t i, len, addlen; |
|
735 | 735 | char *nothead = NULL; |
|
736 | 736 | PyObject *heads; |
|
737 | 737 | |
|
738 | 738 | if (self->headrevs) |
|
739 | 739 | return list_copy(self->headrevs); |
|
740 | 740 | |
|
741 | 741 | len = index_length(self) - 1; |
|
742 | 742 | heads = PyList_New(0); |
|
743 | 743 | if (heads == NULL) |
|
744 | 744 | goto bail; |
|
745 | 745 | if (len == 0) { |
|
746 | 746 | PyObject *nullid = PyInt_FromLong(-1); |
|
747 | 747 | if (nullid == NULL || PyList_Append(heads, nullid) == -1) { |
|
748 | 748 | Py_XDECREF(nullid); |
|
749 | 749 | goto bail; |
|
750 | 750 | } |
|
751 | 751 | goto done; |
|
752 | 752 | } |
|
753 | 753 | |
|
754 | 754 | nothead = calloc(len, 1); |
|
755 | 755 | if (nothead == NULL) |
|
756 | 756 | goto bail; |
|
757 | 757 | |
|
758 | 758 | for (i = 0; i < self->raw_length; i++) { |
|
759 | 759 | const char *data = index_deref(self, i); |
|
760 | 760 | int parent_1 = getbe32(data + 24); |
|
761 | 761 | int parent_2 = getbe32(data + 28); |
|
762 | 762 | if (parent_1 >= 0) |
|
763 | 763 | nothead[parent_1] = 1; |
|
764 | 764 | if (parent_2 >= 0) |
|
765 | 765 | nothead[parent_2] = 1; |
|
766 | 766 | } |
|
767 | 767 | |
|
768 | 768 | addlen = self->added ? PyList_GET_SIZE(self->added) : 0; |
|
769 | 769 | |
|
770 | 770 | for (i = 0; i < addlen; i++) { |
|
771 | 771 | PyObject *rev = PyList_GET_ITEM(self->added, i); |
|
772 | 772 | PyObject *p1 = PyTuple_GET_ITEM(rev, 5); |
|
773 | 773 | PyObject *p2 = PyTuple_GET_ITEM(rev, 6); |
|
774 | 774 | long parent_1, parent_2; |
|
775 | 775 | |
|
776 | 776 | if (!PyInt_Check(p1) || !PyInt_Check(p2)) { |
|
777 | 777 | PyErr_SetString(PyExc_TypeError, |
|
778 | 778 | "revlog parents are invalid"); |
|
779 | 779 | goto bail; |
|
780 | 780 | } |
|
781 | 781 | parent_1 = PyInt_AS_LONG(p1); |
|
782 | 782 | parent_2 = PyInt_AS_LONG(p2); |
|
783 | 783 | if (parent_1 >= 0) |
|
784 | 784 | nothead[parent_1] = 1; |
|
785 | 785 | if (parent_2 >= 0) |
|
786 | 786 | nothead[parent_2] = 1; |
|
787 | 787 | } |
|
788 | 788 | |
|
789 | 789 | for (i = 0; i < len; i++) { |
|
790 | 790 | PyObject *head; |
|
791 | 791 | |
|
792 | 792 | if (nothead[i]) |
|
793 | 793 | continue; |
|
794 | 794 | head = PyInt_FromLong(i); |
|
795 | 795 | if (head == NULL || PyList_Append(heads, head) == -1) { |
|
796 | 796 | Py_XDECREF(head); |
|
797 | 797 | goto bail; |
|
798 | 798 | } |
|
799 | 799 | } |
|
800 | 800 | |
|
801 | 801 | done: |
|
802 | 802 | self->headrevs = heads; |
|
803 | 803 | free(nothead); |
|
804 | 804 | return list_copy(self->headrevs); |
|
805 | 805 | bail: |
|
806 | 806 | Py_XDECREF(heads); |
|
807 | 807 | free(nothead); |
|
808 | 808 | return NULL; |
|
809 | 809 | } |
|
810 | 810 | |
|
811 | 811 | static inline int nt_level(const char *node, Py_ssize_t level) |
|
812 | 812 | { |
|
813 | 813 | int v = node[level>>1]; |
|
814 | 814 | if (!(level & 1)) |
|
815 | 815 | v >>= 4; |
|
816 | 816 | return v & 0xf; |
|
817 | 817 | } |
|
818 | 818 | |
|
819 | 819 | /* |
|
820 | 820 | * Return values: |
|
821 | 821 | * |
|
822 | 822 | * -4: match is ambiguous (multiple candidates) |
|
823 | 823 | * -2: not found |
|
824 | 824 | * rest: valid rev |
|
825 | 825 | */ |
|
826 | 826 | static int nt_find(indexObject *self, const char *node, Py_ssize_t nodelen, |
|
827 | 827 | int hex) |
|
828 | 828 | { |
|
829 | 829 | int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level; |
|
830 | 830 | int level, maxlevel, off; |
|
831 | 831 | |
|
832 | 832 | if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0) |
|
833 | 833 | return -1; |
|
834 | 834 | |
|
835 | 835 | if (self->nt == NULL) |
|
836 | 836 | return -2; |
|
837 | 837 | |
|
838 | 838 | if (hex) |
|
839 | 839 | maxlevel = nodelen > 40 ? 40 : (int)nodelen; |
|
840 | 840 | else |
|
841 | 841 | maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2); |
|
842 | 842 | |
|
843 | 843 | for (level = off = 0; level < maxlevel; level++) { |
|
844 | 844 | int k = getnybble(node, level); |
|
845 | 845 | nodetree *n = &self->nt[off]; |
|
846 | 846 | int v = n->children[k]; |
|
847 | 847 | |
|
848 | 848 | if (v < 0) { |
|
849 | 849 | const char *n; |
|
850 | 850 | Py_ssize_t i; |
|
851 | 851 | |
|
852 | 852 | v = -v - 1; |
|
853 | 853 | n = index_node(self, v); |
|
854 | 854 | if (n == NULL) |
|
855 | 855 | return -2; |
|
856 | 856 | for (i = level; i < maxlevel; i++) |
|
857 | 857 | if (getnybble(node, i) != nt_level(n, i)) |
|
858 | 858 | return -2; |
|
859 | 859 | return v; |
|
860 | 860 | } |
|
861 | 861 | if (v == 0) |
|
862 | 862 | return -2; |
|
863 | 863 | off = v; |
|
864 | 864 | } |
|
865 | 865 | /* multiple matches against an ambiguous prefix */ |
|
866 | 866 | return -4; |
|
867 | 867 | } |
|
868 | 868 | |
|
869 | 869 | static int nt_new(indexObject *self) |
|
870 | 870 | { |
|
871 | 871 | if (self->ntlength == self->ntcapacity) { |
|
872 | 872 | self->ntcapacity *= 2; |
|
873 | 873 | self->nt = realloc(self->nt, |
|
874 | 874 | self->ntcapacity * sizeof(nodetree)); |
|
875 | 875 | if (self->nt == NULL) { |
|
876 | 876 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
|
877 | 877 | return -1; |
|
878 | 878 | } |
|
879 | 879 | memset(&self->nt[self->ntlength], 0, |
|
880 | 880 | sizeof(nodetree) * (self->ntcapacity - self->ntlength)); |
|
881 | 881 | } |
|
882 | 882 | return self->ntlength++; |
|
883 | 883 | } |
|
884 | 884 | |
|
885 | 885 | static int nt_insert(indexObject *self, const char *node, int rev) |
|
886 | 886 | { |
|
887 | 887 | int level = 0; |
|
888 | 888 | int off = 0; |
|
889 | 889 | |
|
890 | 890 | while (level < 40) { |
|
891 | 891 | int k = nt_level(node, level); |
|
892 | 892 | nodetree *n; |
|
893 | 893 | int v; |
|
894 | 894 | |
|
895 | 895 | n = &self->nt[off]; |
|
896 | 896 | v = n->children[k]; |
|
897 | 897 | |
|
898 | 898 | if (v == 0) { |
|
899 | 899 | n->children[k] = -rev - 1; |
|
900 | 900 | return 0; |
|
901 | 901 | } |
|
902 | 902 | if (v < 0) { |
|
903 | 903 | const char *oldnode = index_node(self, -v - 1); |
|
904 | 904 | int noff; |
|
905 | 905 | |
|
906 | 906 | if (!oldnode || !memcmp(oldnode, node, 20)) { |
|
907 | 907 | n->children[k] = -rev - 1; |
|
908 | 908 | return 0; |
|
909 | 909 | } |
|
910 | 910 | noff = nt_new(self); |
|
911 | 911 | if (noff == -1) |
|
912 | 912 | return -1; |
|
913 | 913 | /* self->nt may have been changed by realloc */ |
|
914 | 914 | self->nt[off].children[k] = noff; |
|
915 | 915 | off = noff; |
|
916 | 916 | n = &self->nt[off]; |
|
917 | 917 | n->children[nt_level(oldnode, ++level)] = v; |
|
918 | 918 | if (level > self->ntdepth) |
|
919 | 919 | self->ntdepth = level; |
|
920 | 920 | self->ntsplits += 1; |
|
921 | 921 | } else { |
|
922 | 922 | level += 1; |
|
923 | 923 | off = v; |
|
924 | 924 | } |
|
925 | 925 | } |
|
926 | 926 | |
|
927 | 927 | return -1; |
|
928 | 928 | } |
|
929 | 929 | |
|
930 | 930 | static int nt_init(indexObject *self) |
|
931 | 931 | { |
|
932 | 932 | if (self->nt == NULL) { |
|
933 | 933 | self->ntcapacity = self->raw_length < 4 |
|
934 | 934 | ? 4 : self->raw_length / 2; |
|
935 | 935 | self->nt = calloc(self->ntcapacity, sizeof(nodetree)); |
|
936 | 936 | if (self->nt == NULL) { |
|
937 | 937 | PyErr_NoMemory(); |
|
938 | 938 | return -1; |
|
939 | 939 | } |
|
940 | 940 | self->ntlength = 1; |
|
941 | 941 | self->ntrev = (int)index_length(self) - 1; |
|
942 | 942 | self->ntlookups = 1; |
|
943 | 943 | self->ntmisses = 0; |
|
944 | 944 | if (nt_insert(self, nullid, INT_MAX) == -1) |
|
945 | 945 | return -1; |
|
946 | 946 | } |
|
947 | 947 | return 0; |
|
948 | 948 | } |
|
949 | 949 | |
|
950 | 950 | /* |
|
951 | 951 | * Return values: |
|
952 | 952 | * |
|
953 | 953 | * -3: error (exception set) |
|
954 | 954 | * -2: not found (no exception set) |
|
955 | 955 | * rest: valid rev |
|
956 | 956 | */ |
|
957 | 957 | static int index_find_node(indexObject *self, |
|
958 | 958 | const char *node, Py_ssize_t nodelen) |
|
959 | 959 | { |
|
960 | 960 | int rev; |
|
961 | 961 | |
|
962 | 962 | self->ntlookups++; |
|
963 | 963 | rev = nt_find(self, node, nodelen, 0); |
|
964 | 964 | if (rev >= -1) |
|
965 | 965 | return rev; |
|
966 | 966 | |
|
967 | 967 | if (nt_init(self) == -1) |
|
968 | 968 | return -3; |
|
969 | 969 | |
|
970 | 970 | /* |
|
971 | 971 | * For the first handful of lookups, we scan the entire index, |
|
972 | 972 | * and cache only the matching nodes. This optimizes for cases |
|
973 | 973 | * like "hg tip", where only a few nodes are accessed. |
|
974 | 974 | * |
|
975 | 975 | * After that, we cache every node we visit, using a single |
|
976 | 976 | * scan amortized over multiple lookups. This gives the best |
|
977 | 977 | * bulk performance, e.g. for "hg log". |
|
978 | 978 | */ |
|
979 | 979 | if (self->ntmisses++ < 4) { |
|
980 | 980 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
981 | 981 | const char *n = index_node(self, rev); |
|
982 | 982 | if (n == NULL) |
|
983 | 983 | return -2; |
|
984 | 984 | if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) { |
|
985 | 985 | if (nt_insert(self, n, rev) == -1) |
|
986 | 986 | return -3; |
|
987 | 987 | break; |
|
988 | 988 | } |
|
989 | 989 | } |
|
990 | 990 | } else { |
|
991 | 991 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
992 | 992 | const char *n = index_node(self, rev); |
|
993 | 993 | if (n == NULL) { |
|
994 | 994 | self->ntrev = rev + 1; |
|
995 | 995 | return -2; |
|
996 | 996 | } |
|
997 | 997 | if (nt_insert(self, n, rev) == -1) { |
|
998 | 998 | self->ntrev = rev + 1; |
|
999 | 999 | return -3; |
|
1000 | 1000 | } |
|
1001 | 1001 | if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) { |
|
1002 | 1002 | break; |
|
1003 | 1003 | } |
|
1004 | 1004 | } |
|
1005 | 1005 | self->ntrev = rev; |
|
1006 | 1006 | } |
|
1007 | 1007 | |
|
1008 | 1008 | if (rev >= 0) |
|
1009 | 1009 | return rev; |
|
1010 | 1010 | return -2; |
|
1011 | 1011 | } |
|
1012 | 1012 | |
|
1013 | 1013 | static PyObject *raise_revlog_error(void) |
|
1014 | 1014 | { |
|
1015 | 1015 | static PyObject *errclass; |
|
1016 | 1016 | PyObject *mod = NULL, *errobj; |
|
1017 | 1017 | |
|
1018 | 1018 | if (errclass == NULL) { |
|
1019 | 1019 | PyObject *dict; |
|
1020 | 1020 | |
|
1021 | 1021 | mod = PyImport_ImportModule("mercurial.error"); |
|
1022 | 1022 | if (mod == NULL) |
|
1023 | 1023 | goto classfail; |
|
1024 | 1024 | |
|
1025 | 1025 | dict = PyModule_GetDict(mod); |
|
1026 | 1026 | if (dict == NULL) |
|
1027 | 1027 | goto classfail; |
|
1028 | 1028 | |
|
1029 | 1029 | errclass = PyDict_GetItemString(dict, "RevlogError"); |
|
1030 | 1030 | if (errclass == NULL) { |
|
1031 | 1031 | PyErr_SetString(PyExc_SystemError, |
|
1032 | 1032 | "could not find RevlogError"); |
|
1033 | 1033 | goto classfail; |
|
1034 | 1034 | } |
|
1035 | 1035 | Py_INCREF(errclass); |
|
1036 | 1036 | } |
|
1037 | 1037 | |
|
1038 | 1038 | errobj = PyObject_CallFunction(errclass, NULL); |
|
1039 | 1039 | if (errobj == NULL) |
|
1040 | 1040 | return NULL; |
|
1041 | 1041 | PyErr_SetObject(errclass, errobj); |
|
1042 | 1042 | return errobj; |
|
1043 | 1043 | |
|
1044 | 1044 | classfail: |
|
1045 | 1045 | Py_XDECREF(mod); |
|
1046 | 1046 | return NULL; |
|
1047 | 1047 | } |
|
1048 | 1048 | |
|
1049 | 1049 | static PyObject *index_getitem(indexObject *self, PyObject *value) |
|
1050 | 1050 | { |
|
1051 | 1051 | char *node; |
|
1052 | 1052 | Py_ssize_t nodelen; |
|
1053 | 1053 | int rev; |
|
1054 | 1054 | |
|
1055 | 1055 | if (PyInt_Check(value)) |
|
1056 | 1056 | return index_get(self, PyInt_AS_LONG(value)); |
|
1057 | 1057 | |
|
1058 | 1058 | if (node_check(value, &node, &nodelen) == -1) |
|
1059 | 1059 | return NULL; |
|
1060 | 1060 | rev = index_find_node(self, node, nodelen); |
|
1061 | 1061 | if (rev >= -1) |
|
1062 | 1062 | return PyInt_FromLong(rev); |
|
1063 | 1063 | if (rev == -2) |
|
1064 | 1064 | raise_revlog_error(); |
|
1065 | 1065 | return NULL; |
|
1066 | 1066 | } |
|
1067 | 1067 | |
|
1068 | 1068 | static int nt_partialmatch(indexObject *self, const char *node, |
|
1069 | 1069 | Py_ssize_t nodelen) |
|
1070 | 1070 | { |
|
1071 | 1071 | int rev; |
|
1072 | 1072 | |
|
1073 | 1073 | if (nt_init(self) == -1) |
|
1074 | 1074 | return -3; |
|
1075 | 1075 | |
|
1076 | 1076 | if (self->ntrev > 0) { |
|
1077 | 1077 | /* ensure that the radix tree is fully populated */ |
|
1078 | 1078 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
1079 | 1079 | const char *n = index_node(self, rev); |
|
1080 | 1080 | if (n == NULL) |
|
1081 | 1081 | return -2; |
|
1082 | 1082 | if (nt_insert(self, n, rev) == -1) |
|
1083 | 1083 | return -3; |
|
1084 | 1084 | } |
|
1085 | 1085 | self->ntrev = rev; |
|
1086 | 1086 | } |
|
1087 | 1087 | |
|
1088 | 1088 | return nt_find(self, node, nodelen, 1); |
|
1089 | 1089 | } |
|
1090 | 1090 | |
|
1091 | 1091 | static PyObject *index_partialmatch(indexObject *self, PyObject *args) |
|
1092 | 1092 | { |
|
1093 | 1093 | const char *fullnode; |
|
1094 | 1094 | int nodelen; |
|
1095 | 1095 | char *node; |
|
1096 | 1096 | int rev, i; |
|
1097 | 1097 | |
|
1098 | 1098 | if (!PyArg_ParseTuple(args, "s#", &node, &nodelen)) |
|
1099 | 1099 | return NULL; |
|
1100 | 1100 | |
|
1101 | 1101 | if (nodelen < 4) { |
|
1102 | 1102 | PyErr_SetString(PyExc_ValueError, "key too short"); |
|
1103 | 1103 | return NULL; |
|
1104 | 1104 | } |
|
1105 | 1105 | |
|
1106 | 1106 | if (nodelen > 40) { |
|
1107 | 1107 | PyErr_SetString(PyExc_ValueError, "key too long"); |
|
1108 | 1108 | return NULL; |
|
1109 | 1109 | } |
|
1110 | 1110 | |
|
1111 | 1111 | for (i = 0; i < nodelen; i++) |
|
1112 | 1112 | hexdigit(node, i); |
|
1113 | 1113 | if (PyErr_Occurred()) { |
|
1114 | 1114 | /* input contains non-hex characters */ |
|
1115 | 1115 | PyErr_Clear(); |
|
1116 | 1116 | Py_RETURN_NONE; |
|
1117 | 1117 | } |
|
1118 | 1118 | |
|
1119 | 1119 | rev = nt_partialmatch(self, node, nodelen); |
|
1120 | 1120 | |
|
1121 | 1121 | switch (rev) { |
|
1122 | 1122 | case -4: |
|
1123 | 1123 | raise_revlog_error(); |
|
1124 | 1124 | case -3: |
|
1125 | 1125 | return NULL; |
|
1126 | 1126 | case -2: |
|
1127 | 1127 | Py_RETURN_NONE; |
|
1128 | 1128 | case -1: |
|
1129 | 1129 | return PyString_FromStringAndSize(nullid, 20); |
|
1130 | 1130 | } |
|
1131 | 1131 | |
|
1132 | 1132 | fullnode = index_node(self, rev); |
|
1133 | 1133 | if (fullnode == NULL) { |
|
1134 | 1134 | PyErr_Format(PyExc_IndexError, |
|
1135 | 1135 | "could not access rev %d", rev); |
|
1136 | 1136 | return NULL; |
|
1137 | 1137 | } |
|
1138 | 1138 | return PyString_FromStringAndSize(fullnode, 20); |
|
1139 | 1139 | } |
|
1140 | 1140 | |
|
1141 | 1141 | static PyObject *index_m_get(indexObject *self, PyObject *args) |
|
1142 | 1142 | { |
|
1143 | 1143 | Py_ssize_t nodelen; |
|
1144 | 1144 | PyObject *val; |
|
1145 | 1145 | char *node; |
|
1146 | 1146 | int rev; |
|
1147 | 1147 | |
|
1148 | 1148 | if (!PyArg_ParseTuple(args, "O", &val)) |
|
1149 | 1149 | return NULL; |
|
1150 | 1150 | if (node_check(val, &node, &nodelen) == -1) |
|
1151 | 1151 | return NULL; |
|
1152 | 1152 | rev = index_find_node(self, node, nodelen); |
|
1153 | 1153 | if (rev == -3) |
|
1154 | 1154 | return NULL; |
|
1155 | 1155 | if (rev == -2) |
|
1156 | 1156 | Py_RETURN_NONE; |
|
1157 | 1157 | return PyInt_FromLong(rev); |
|
1158 | 1158 | } |
|
1159 | 1159 | |
|
1160 | 1160 | static int index_contains(indexObject *self, PyObject *value) |
|
1161 | 1161 | { |
|
1162 | 1162 | char *node; |
|
1163 | 1163 | Py_ssize_t nodelen; |
|
1164 | 1164 | |
|
1165 | 1165 | if (PyInt_Check(value)) { |
|
1166 | 1166 | long rev = PyInt_AS_LONG(value); |
|
1167 | 1167 | return rev >= -1 && rev < index_length(self); |
|
1168 | 1168 | } |
|
1169 | 1169 | |
|
1170 | 1170 | if (node_check(value, &node, &nodelen) == -1) |
|
1171 | 1171 | return -1; |
|
1172 | 1172 | |
|
1173 | 1173 | switch (index_find_node(self, node, nodelen)) { |
|
1174 | 1174 | case -3: |
|
1175 | 1175 | return -1; |
|
1176 | 1176 | case -2: |
|
1177 | 1177 | return 0; |
|
1178 | 1178 | default: |
|
1179 | 1179 | return 1; |
|
1180 | 1180 | } |
|
1181 | 1181 | } |
|
1182 | 1182 | |
|
1183 | 1183 | static inline void index_get_parents(indexObject *self, int rev, int *ps) |
|
1184 | 1184 | { |
|
1185 | 1185 | if (rev >= self->length - 1) { |
|
1186 | 1186 | PyObject *tuple = PyList_GET_ITEM(self->added, |
|
1187 | 1187 | rev - self->length + 1); |
|
1188 | 1188 | ps[0] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 5)); |
|
1189 | 1189 | ps[1] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 6)); |
|
1190 | 1190 | } else { |
|
1191 | 1191 | const char *data = index_deref(self, rev); |
|
1192 | 1192 | ps[0] = getbe32(data + 24); |
|
1193 | 1193 | ps[1] = getbe32(data + 28); |
|
1194 | 1194 | } |
|
1195 | 1195 | } |
|
1196 | 1196 | |
|
1197 | 1197 | typedef uint64_t bitmask; |
|
1198 | 1198 | |
|
1199 | 1199 | /* |
|
1200 | 1200 | * Given a disjoint set of revs, return all candidates for the |
|
1201 | 1201 | * greatest common ancestor. In revset notation, this is the set |
|
1202 | 1202 | * "heads(::a and ::b and ...)" |
|
1203 | 1203 | */ |
|
1204 | 1204 | static PyObject *find_gca_candidates(indexObject *self, const int *revs, |
|
1205 | 1205 | int revcount) |
|
1206 | 1206 | { |
|
1207 | 1207 | const bitmask allseen = (1ull << revcount) - 1; |
|
1208 | 1208 | const bitmask poison = 1ull << revcount; |
|
1209 | 1209 | PyObject *gca = PyList_New(0); |
|
1210 | 1210 | int i, v, interesting, left; |
|
1211 | 1211 | int maxrev = -1; |
|
1212 | 1212 | long sp; |
|
1213 | 1213 | bitmask *seen; |
|
1214 | 1214 | |
|
1215 | if (gca == NULL) | |
|
1216 | return PyErr_NoMemory(); | |
|
1217 | ||
|
1215 | 1218 | for (i = 0; i < revcount; i++) { |
|
1216 | 1219 | if (revs[i] > maxrev) |
|
1217 | 1220 | maxrev = revs[i]; |
|
1218 | 1221 | } |
|
1219 | 1222 | |
|
1220 | 1223 | seen = calloc(sizeof(*seen), maxrev + 1); |
|
1221 | if (seen == NULL) | |
|
1224 | if (seen == NULL) { | |
|
1225 | Py_DECREF(gca); | |
|
1222 | 1226 | return PyErr_NoMemory(); |
|
1227 | } | |
|
1223 | 1228 | |
|
1224 | 1229 | for (i = 0; i < revcount; i++) |
|
1225 | 1230 | seen[revs[i]] = 1ull << i; |
|
1226 | 1231 | |
|
1227 | 1232 | interesting = left = revcount; |
|
1228 | 1233 | |
|
1229 | 1234 | for (v = maxrev; v >= 0 && interesting; v--) { |
|
1230 | 1235 | long sv = seen[v]; |
|
1231 | 1236 | int parents[2]; |
|
1232 | 1237 | |
|
1233 | 1238 | if (!sv) |
|
1234 | 1239 | continue; |
|
1235 | 1240 | |
|
1236 | 1241 | if (sv < poison) { |
|
1237 | 1242 | interesting -= 1; |
|
1238 | 1243 | if (sv == allseen) { |
|
1239 | 1244 | PyObject *obj = PyInt_FromLong(v); |
|
1240 | 1245 | if (obj == NULL) |
|
1241 | 1246 | goto bail; |
|
1242 | 1247 | if (PyList_Append(gca, obj) == -1) { |
|
1243 | 1248 | Py_DECREF(obj); |
|
1244 | 1249 | goto bail; |
|
1245 | 1250 | } |
|
1246 | 1251 | sv |= poison; |
|
1247 | 1252 | for (i = 0; i < revcount; i++) { |
|
1248 | 1253 | if (revs[i] == v) { |
|
1249 | 1254 | if (--left <= 1) |
|
1250 | 1255 | goto done; |
|
1251 | 1256 | break; |
|
1252 | 1257 | } |
|
1253 | 1258 | } |
|
1254 | 1259 | } |
|
1255 | 1260 | } |
|
1256 | 1261 | index_get_parents(self, v, parents); |
|
1257 | 1262 | |
|
1258 | 1263 | for (i = 0; i < 2; i++) { |
|
1259 | 1264 | int p = parents[i]; |
|
1260 | 1265 | if (p == -1) |
|
1261 | 1266 | continue; |
|
1262 | 1267 | sp = seen[p]; |
|
1263 | 1268 | if (sv < poison) { |
|
1264 | 1269 | if (sp == 0) { |
|
1265 | 1270 | seen[p] = sv; |
|
1266 | 1271 | interesting++; |
|
1267 | 1272 | } |
|
1268 | 1273 | else if (sp != sv) |
|
1269 | 1274 | seen[p] |= sv; |
|
1270 | 1275 | } else { |
|
1271 | 1276 | if (sp && sp < poison) |
|
1272 | 1277 | interesting--; |
|
1273 | 1278 | seen[p] = sv; |
|
1274 | 1279 | } |
|
1275 | 1280 | } |
|
1276 | 1281 | } |
|
1277 | 1282 | |
|
1278 | 1283 | done: |
|
1279 | 1284 | free(seen); |
|
1280 | 1285 | return gca; |
|
1281 | 1286 | bail: |
|
1282 | 1287 | free(seen); |
|
1283 | 1288 | Py_XDECREF(gca); |
|
1284 | 1289 | return NULL; |
|
1285 | 1290 | } |
|
1286 | 1291 | |
|
1287 | 1292 | /* |
|
1288 | 1293 | * Given a disjoint set of revs, return the subset with the longest |
|
1289 | 1294 | * path to the root. |
|
1290 | 1295 | */ |
|
1291 | 1296 | static PyObject *find_deepest(indexObject *self, PyObject *revs) |
|
1292 | 1297 | { |
|
1293 | 1298 | const Py_ssize_t revcount = PyList_GET_SIZE(revs); |
|
1294 | 1299 | static const Py_ssize_t capacity = 24; |
|
1295 | 1300 | int *depth, *interesting = NULL; |
|
1296 | 1301 | int i, j, v, ninteresting; |
|
1297 | 1302 | PyObject *dict = NULL, *keys; |
|
1298 | 1303 | long *seen = NULL; |
|
1299 | 1304 | int maxrev = -1; |
|
1300 | 1305 | long final; |
|
1301 | 1306 | |
|
1302 | 1307 | if (revcount > capacity) { |
|
1303 | 1308 | PyErr_Format(PyExc_OverflowError, |
|
1304 | 1309 | "bitset size (%ld) > capacity (%ld)", |
|
1305 | 1310 | (long)revcount, (long)capacity); |
|
1306 | 1311 | return NULL; |
|
1307 | 1312 | } |
|
1308 | 1313 | |
|
1309 | 1314 | for (i = 0; i < revcount; i++) { |
|
1310 | 1315 | int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i)); |
|
1311 | 1316 | if (n > maxrev) |
|
1312 | 1317 | maxrev = n; |
|
1313 | 1318 | } |
|
1314 | 1319 | |
|
1315 | 1320 | depth = calloc(sizeof(*depth), maxrev + 1); |
|
1316 | 1321 | if (depth == NULL) |
|
1317 | 1322 | return PyErr_NoMemory(); |
|
1318 | 1323 | |
|
1319 | 1324 | seen = calloc(sizeof(*seen), maxrev + 1); |
|
1320 | 1325 | if (seen == NULL) { |
|
1321 | 1326 | PyErr_NoMemory(); |
|
1322 | 1327 | goto bail; |
|
1323 | 1328 | } |
|
1324 | 1329 | |
|
1325 | 1330 | interesting = calloc(sizeof(*interesting), 2 << revcount); |
|
1326 | 1331 | if (interesting == NULL) { |
|
1327 | 1332 | PyErr_NoMemory(); |
|
1328 | 1333 | goto bail; |
|
1329 | 1334 | } |
|
1330 | 1335 | |
|
1331 | 1336 | if (PyList_Sort(revs) == -1) |
|
1332 | 1337 | goto bail; |
|
1333 | 1338 | |
|
1334 | 1339 | for (i = 0; i < revcount; i++) { |
|
1335 | 1340 | int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i)); |
|
1336 | 1341 | long b = 1l << i; |
|
1337 | 1342 | depth[n] = 1; |
|
1338 | 1343 | seen[n] = b; |
|
1339 | 1344 | interesting[b] = 1; |
|
1340 | 1345 | } |
|
1341 | 1346 | |
|
1342 | 1347 | ninteresting = (int)revcount; |
|
1343 | 1348 | |
|
1344 | 1349 | for (v = maxrev; v >= 0 && ninteresting > 1; v--) { |
|
1345 | 1350 | int dv = depth[v]; |
|
1346 | 1351 | int parents[2]; |
|
1347 | 1352 | long sv; |
|
1348 | 1353 | |
|
1349 | 1354 | if (dv == 0) |
|
1350 | 1355 | continue; |
|
1351 | 1356 | |
|
1352 | 1357 | sv = seen[v]; |
|
1353 | 1358 | index_get_parents(self, v, parents); |
|
1354 | 1359 | |
|
1355 | 1360 | for (i = 0; i < 2; i++) { |
|
1356 | 1361 | int p = parents[i]; |
|
1357 | 1362 | long nsp, sp; |
|
1358 | 1363 | int dp; |
|
1359 | 1364 | |
|
1360 | 1365 | if (p == -1) |
|
1361 | 1366 | continue; |
|
1362 | 1367 | |
|
1363 | 1368 | dp = depth[p]; |
|
1364 | 1369 | nsp = sp = seen[p]; |
|
1365 | 1370 | if (dp <= dv) { |
|
1366 | 1371 | depth[p] = dv + 1; |
|
1367 | 1372 | if (sp != sv) { |
|
1368 | 1373 | interesting[sv] += 1; |
|
1369 | 1374 | nsp = seen[p] = sv; |
|
1370 | 1375 | if (sp) { |
|
1371 | 1376 | interesting[sp] -= 1; |
|
1372 | 1377 | if (interesting[sp] == 0) |
|
1373 | 1378 | ninteresting -= 1; |
|
1374 | 1379 | } |
|
1375 | 1380 | } |
|
1376 | 1381 | } |
|
1377 | 1382 | else if (dv == dp - 1) { |
|
1378 | 1383 | nsp = sp | sv; |
|
1379 | 1384 | if (nsp == sp) |
|
1380 | 1385 | continue; |
|
1381 | 1386 | seen[p] = nsp; |
|
1382 | 1387 | interesting[sp] -= 1; |
|
1383 | 1388 | if (interesting[sp] == 0 && interesting[nsp] > 0) |
|
1384 | 1389 | ninteresting -= 1; |
|
1385 | 1390 | interesting[nsp] += 1; |
|
1386 | 1391 | } |
|
1387 | 1392 | } |
|
1388 | 1393 | interesting[sv] -= 1; |
|
1389 | 1394 | if (interesting[sv] == 0) |
|
1390 | 1395 | ninteresting -= 1; |
|
1391 | 1396 | } |
|
1392 | 1397 | |
|
1393 | 1398 | final = 0; |
|
1394 | 1399 | j = ninteresting; |
|
1395 | 1400 | for (i = 0; i < (int)(2 << revcount) && j > 0; i++) { |
|
1396 | 1401 | if (interesting[i] == 0) |
|
1397 | 1402 | continue; |
|
1398 | 1403 | final |= i; |
|
1399 | 1404 | j -= 1; |
|
1400 | 1405 | } |
|
1401 | 1406 | if (final == 0) |
|
1402 | 1407 | return PyList_New(0); |
|
1403 | 1408 | |
|
1404 | 1409 | dict = PyDict_New(); |
|
1405 | 1410 | if (dict == NULL) |
|
1406 | 1411 | goto bail; |
|
1407 | 1412 | |
|
1408 | 1413 | for (i = 0; i < revcount; i++) { |
|
1409 | 1414 | PyObject *key; |
|
1410 | 1415 | |
|
1411 | 1416 | if ((final & (1 << i)) == 0) |
|
1412 | 1417 | continue; |
|
1413 | 1418 | |
|
1414 | 1419 | key = PyList_GET_ITEM(revs, i); |
|
1415 | 1420 | Py_INCREF(key); |
|
1416 | 1421 | Py_INCREF(Py_None); |
|
1417 | 1422 | if (PyDict_SetItem(dict, key, Py_None) == -1) { |
|
1418 | 1423 | Py_DECREF(key); |
|
1419 | 1424 | Py_DECREF(Py_None); |
|
1420 | 1425 | goto bail; |
|
1421 | 1426 | } |
|
1422 | 1427 | } |
|
1423 | 1428 | |
|
1424 | 1429 | keys = PyDict_Keys(dict); |
|
1425 | 1430 | |
|
1426 | 1431 | free(depth); |
|
1427 | 1432 | free(seen); |
|
1428 | 1433 | free(interesting); |
|
1429 | 1434 | Py_DECREF(dict); |
|
1430 | 1435 | |
|
1431 | 1436 | return keys; |
|
1432 | 1437 | bail: |
|
1433 | 1438 | free(depth); |
|
1434 | 1439 | free(seen); |
|
1435 | 1440 | free(interesting); |
|
1436 | 1441 | Py_XDECREF(dict); |
|
1437 | 1442 | |
|
1438 | 1443 | return NULL; |
|
1439 | 1444 | } |
|
1440 | 1445 | |
|
1441 | 1446 | /* |
|
1442 | 1447 | * Given a (possibly overlapping) set of revs, return the greatest |
|
1443 | 1448 | * common ancestors: those with the longest path to the root. |
|
1444 | 1449 | */ |
|
1445 | 1450 | static PyObject *index_ancestors(indexObject *self, PyObject *args) |
|
1446 | 1451 | { |
|
1447 | 1452 | PyObject *ret = NULL, *gca = NULL; |
|
1448 | 1453 | Py_ssize_t argcount, i, len; |
|
1449 | 1454 | bitmask repeat = 0; |
|
1450 | 1455 | int revcount = 0; |
|
1451 | 1456 | int *revs; |
|
1452 | 1457 | |
|
1453 | 1458 | argcount = PySequence_Length(args); |
|
1454 | 1459 | revs = malloc(argcount * sizeof(*revs)); |
|
1455 | 1460 | if (argcount > 0 && revs == NULL) |
|
1456 | 1461 | return PyErr_NoMemory(); |
|
1457 | 1462 | len = index_length(self) - 1; |
|
1458 | 1463 | |
|
1459 | 1464 | for (i = 0; i < argcount; i++) { |
|
1460 | 1465 | static const int capacity = 24; |
|
1461 | 1466 | PyObject *obj = PySequence_GetItem(args, i); |
|
1462 | 1467 | bitmask x; |
|
1463 | 1468 | long val; |
|
1464 | 1469 | |
|
1465 | 1470 | if (!PyInt_Check(obj)) { |
|
1466 | 1471 | PyErr_SetString(PyExc_TypeError, |
|
1467 | 1472 | "arguments must all be ints"); |
|
1468 | 1473 | goto bail; |
|
1469 | 1474 | } |
|
1470 | 1475 | val = PyInt_AsLong(obj); |
|
1471 | 1476 | if (val == -1) { |
|
1472 | 1477 | ret = PyList_New(0); |
|
1473 | 1478 | goto done; |
|
1474 | 1479 | } |
|
1475 | 1480 | if (val < 0 || val >= len) { |
|
1476 | 1481 | PyErr_SetString(PyExc_IndexError, |
|
1477 | 1482 | "index out of range"); |
|
1478 | 1483 | goto bail; |
|
1479 | 1484 | } |
|
1480 | 1485 | /* this cheesy bloom filter lets us avoid some more |
|
1481 | 1486 | * expensive duplicate checks in the common set-is-disjoint |
|
1482 | 1487 | * case */ |
|
1483 | 1488 | x = 1ull << (val & 0x3f); |
|
1484 | 1489 | if (repeat & x) { |
|
1485 | 1490 | int k; |
|
1486 | 1491 | for (k = 0; k < revcount; k++) { |
|
1487 | 1492 | if (val == revs[k]) |
|
1488 | 1493 | goto duplicate; |
|
1489 | 1494 | } |
|
1490 | 1495 | } |
|
1491 | 1496 | else repeat |= x; |
|
1492 | 1497 | if (revcount >= capacity) { |
|
1493 | 1498 | PyErr_Format(PyExc_OverflowError, |
|
1494 | 1499 | "bitset size (%d) > capacity (%d)", |
|
1495 | 1500 | revcount, capacity); |
|
1496 | 1501 | goto bail; |
|
1497 | 1502 | } |
|
1498 | 1503 | revs[revcount++] = (int)val; |
|
1499 | 1504 | duplicate:; |
|
1500 | 1505 | } |
|
1501 | 1506 | |
|
1502 | 1507 | if (revcount == 0) { |
|
1503 | 1508 | ret = PyList_New(0); |
|
1504 | 1509 | goto done; |
|
1505 | 1510 | } |
|
1506 | 1511 | if (revcount == 1) { |
|
1507 | 1512 | PyObject *obj; |
|
1508 | 1513 | ret = PyList_New(1); |
|
1509 | 1514 | if (ret == NULL) |
|
1510 | 1515 | goto bail; |
|
1511 | 1516 | obj = PyInt_FromLong(revs[0]); |
|
1512 | 1517 | if (obj == NULL) |
|
1513 | 1518 | goto bail; |
|
1514 | 1519 | PyList_SET_ITEM(ret, 0, obj); |
|
1515 | 1520 | goto done; |
|
1516 | 1521 | } |
|
1517 | 1522 | |
|
1518 | 1523 | gca = find_gca_candidates(self, revs, revcount); |
|
1519 | 1524 | if (gca == NULL) |
|
1520 | 1525 | goto bail; |
|
1521 | 1526 | |
|
1522 | 1527 | if (PyList_GET_SIZE(gca) <= 1) { |
|
1523 | 1528 | ret = gca; |
|
1524 | 1529 | Py_INCREF(gca); |
|
1525 | 1530 | } |
|
1526 | 1531 | else if (PyList_GET_SIZE(gca) == 1) { |
|
1527 | 1532 | ret = PyList_GET_ITEM(gca, 0); |
|
1528 | 1533 | Py_INCREF(ret); |
|
1529 | 1534 | } |
|
1530 | 1535 | else ret = find_deepest(self, gca); |
|
1531 | 1536 | |
|
1532 | 1537 | done: |
|
1533 | 1538 | free(revs); |
|
1534 | 1539 | Py_XDECREF(gca); |
|
1535 | 1540 | |
|
1536 | 1541 | return ret; |
|
1537 | 1542 | |
|
1538 | 1543 | bail: |
|
1539 | 1544 | free(revs); |
|
1540 | 1545 | Py_XDECREF(gca); |
|
1541 | 1546 | Py_XDECREF(ret); |
|
1542 | 1547 | return NULL; |
|
1543 | 1548 | } |
|
1544 | 1549 | |
|
1545 | 1550 | /* |
|
1546 | 1551 | * Invalidate any trie entries introduced by added revs. |
|
1547 | 1552 | */ |
|
1548 | 1553 | static void nt_invalidate_added(indexObject *self, Py_ssize_t start) |
|
1549 | 1554 | { |
|
1550 | 1555 | Py_ssize_t i, len = PyList_GET_SIZE(self->added); |
|
1551 | 1556 | |
|
1552 | 1557 | for (i = start; i < len; i++) { |
|
1553 | 1558 | PyObject *tuple = PyList_GET_ITEM(self->added, i); |
|
1554 | 1559 | PyObject *node = PyTuple_GET_ITEM(tuple, 7); |
|
1555 | 1560 | |
|
1556 | 1561 | nt_insert(self, PyString_AS_STRING(node), -1); |
|
1557 | 1562 | } |
|
1558 | 1563 | |
|
1559 | 1564 | if (start == 0) |
|
1560 | 1565 | Py_CLEAR(self->added); |
|
1561 | 1566 | } |
|
1562 | 1567 | |
|
1563 | 1568 | /* |
|
1564 | 1569 | * Delete a numeric range of revs, which must be at the end of the |
|
1565 | 1570 | * range, but exclude the sentinel nullid entry. |
|
1566 | 1571 | */ |
|
1567 | 1572 | static int index_slice_del(indexObject *self, PyObject *item) |
|
1568 | 1573 | { |
|
1569 | 1574 | Py_ssize_t start, stop, step, slicelength; |
|
1570 | 1575 | Py_ssize_t length = index_length(self); |
|
1571 | 1576 | int ret = 0; |
|
1572 | 1577 | |
|
1573 | 1578 | if (PySlice_GetIndicesEx((PySliceObject*)item, length, |
|
1574 | 1579 | &start, &stop, &step, &slicelength) < 0) |
|
1575 | 1580 | return -1; |
|
1576 | 1581 | |
|
1577 | 1582 | if (slicelength <= 0) |
|
1578 | 1583 | return 0; |
|
1579 | 1584 | |
|
1580 | 1585 | if ((step < 0 && start < stop) || (step > 0 && start > stop)) |
|
1581 | 1586 | stop = start; |
|
1582 | 1587 | |
|
1583 | 1588 | if (step < 0) { |
|
1584 | 1589 | stop = start + 1; |
|
1585 | 1590 | start = stop + step*(slicelength - 1) - 1; |
|
1586 | 1591 | step = -step; |
|
1587 | 1592 | } |
|
1588 | 1593 | |
|
1589 | 1594 | if (step != 1) { |
|
1590 | 1595 | PyErr_SetString(PyExc_ValueError, |
|
1591 | 1596 | "revlog index delete requires step size of 1"); |
|
1592 | 1597 | return -1; |
|
1593 | 1598 | } |
|
1594 | 1599 | |
|
1595 | 1600 | if (stop != length - 1) { |
|
1596 | 1601 | PyErr_SetString(PyExc_IndexError, |
|
1597 | 1602 | "revlog index deletion indices are invalid"); |
|
1598 | 1603 | return -1; |
|
1599 | 1604 | } |
|
1600 | 1605 | |
|
1601 | 1606 | if (start < self->length - 1) { |
|
1602 | 1607 | if (self->nt) { |
|
1603 | 1608 | Py_ssize_t i; |
|
1604 | 1609 | |
|
1605 | 1610 | for (i = start + 1; i < self->length - 1; i++) { |
|
1606 | 1611 | const char *node = index_node(self, i); |
|
1607 | 1612 | |
|
1608 | 1613 | if (node) |
|
1609 | 1614 | nt_insert(self, node, -1); |
|
1610 | 1615 | } |
|
1611 | 1616 | if (self->added) |
|
1612 | 1617 | nt_invalidate_added(self, 0); |
|
1613 | 1618 | if (self->ntrev > start) |
|
1614 | 1619 | self->ntrev = (int)start; |
|
1615 | 1620 | } |
|
1616 | 1621 | self->length = start + 1; |
|
1617 | 1622 | if (start < self->raw_length) { |
|
1618 | 1623 | if (self->cache) { |
|
1619 | 1624 | Py_ssize_t i; |
|
1620 | 1625 | for (i = start; i < self->raw_length; i++) |
|
1621 | 1626 | Py_CLEAR(self->cache[i]); |
|
1622 | 1627 | } |
|
1623 | 1628 | self->raw_length = start; |
|
1624 | 1629 | } |
|
1625 | 1630 | goto done; |
|
1626 | 1631 | } |
|
1627 | 1632 | |
|
1628 | 1633 | if (self->nt) { |
|
1629 | 1634 | nt_invalidate_added(self, start - self->length + 1); |
|
1630 | 1635 | if (self->ntrev > start) |
|
1631 | 1636 | self->ntrev = (int)start; |
|
1632 | 1637 | } |
|
1633 | 1638 | if (self->added) |
|
1634 | 1639 | ret = PyList_SetSlice(self->added, start - self->length + 1, |
|
1635 | 1640 | PyList_GET_SIZE(self->added), NULL); |
|
1636 | 1641 | done: |
|
1637 | 1642 | Py_CLEAR(self->headrevs); |
|
1638 | 1643 | return ret; |
|
1639 | 1644 | } |
|
1640 | 1645 | |
|
1641 | 1646 | /* |
|
1642 | 1647 | * Supported ops: |
|
1643 | 1648 | * |
|
1644 | 1649 | * slice deletion |
|
1645 | 1650 | * string assignment (extend node->rev mapping) |
|
1646 | 1651 | * string deletion (shrink node->rev mapping) |
|
1647 | 1652 | */ |
|
1648 | 1653 | static int index_assign_subscript(indexObject *self, PyObject *item, |
|
1649 | 1654 | PyObject *value) |
|
1650 | 1655 | { |
|
1651 | 1656 | char *node; |
|
1652 | 1657 | Py_ssize_t nodelen; |
|
1653 | 1658 | long rev; |
|
1654 | 1659 | |
|
1655 | 1660 | if (PySlice_Check(item) && value == NULL) |
|
1656 | 1661 | return index_slice_del(self, item); |
|
1657 | 1662 | |
|
1658 | 1663 | if (node_check(item, &node, &nodelen) == -1) |
|
1659 | 1664 | return -1; |
|
1660 | 1665 | |
|
1661 | 1666 | if (value == NULL) |
|
1662 | 1667 | return self->nt ? nt_insert(self, node, -1) : 0; |
|
1663 | 1668 | rev = PyInt_AsLong(value); |
|
1664 | 1669 | if (rev > INT_MAX || rev < 0) { |
|
1665 | 1670 | if (!PyErr_Occurred()) |
|
1666 | 1671 | PyErr_SetString(PyExc_ValueError, "rev out of range"); |
|
1667 | 1672 | return -1; |
|
1668 | 1673 | } |
|
1669 | 1674 | return nt_insert(self, node, (int)rev); |
|
1670 | 1675 | } |
|
1671 | 1676 | |
|
1672 | 1677 | /* |
|
1673 | 1678 | * Find all RevlogNG entries in an index that has inline data. Update |
|
1674 | 1679 | * the optional "offsets" table with those entries. |
|
1675 | 1680 | */ |
|
1676 | 1681 | static long inline_scan(indexObject *self, const char **offsets) |
|
1677 | 1682 | { |
|
1678 | 1683 | const char *data = PyString_AS_STRING(self->data); |
|
1679 | 1684 | const char *end = data + PyString_GET_SIZE(self->data); |
|
1680 | 1685 | long incr = v1_hdrsize; |
|
1681 | 1686 | Py_ssize_t len = 0; |
|
1682 | 1687 | |
|
1683 | 1688 | while (data + v1_hdrsize <= end) { |
|
1684 | 1689 | uint32_t comp_len; |
|
1685 | 1690 | const char *old_data; |
|
1686 | 1691 | /* 3rd element of header is length of compressed inline data */ |
|
1687 | 1692 | comp_len = getbe32(data + 8); |
|
1688 | 1693 | incr = v1_hdrsize + comp_len; |
|
1689 | 1694 | if (incr < v1_hdrsize) |
|
1690 | 1695 | break; |
|
1691 | 1696 | if (offsets) |
|
1692 | 1697 | offsets[len] = data; |
|
1693 | 1698 | len++; |
|
1694 | 1699 | old_data = data; |
|
1695 | 1700 | data += incr; |
|
1696 | 1701 | if (data <= old_data) |
|
1697 | 1702 | break; |
|
1698 | 1703 | } |
|
1699 | 1704 | |
|
1700 | 1705 | if (data != end && data + v1_hdrsize != end) { |
|
1701 | 1706 | if (!PyErr_Occurred()) |
|
1702 | 1707 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); |
|
1703 | 1708 | return -1; |
|
1704 | 1709 | } |
|
1705 | 1710 | |
|
1706 | 1711 | return len; |
|
1707 | 1712 | } |
|
1708 | 1713 | |
|
1709 | 1714 | static int index_init(indexObject *self, PyObject *args) |
|
1710 | 1715 | { |
|
1711 | 1716 | PyObject *data_obj, *inlined_obj; |
|
1712 | 1717 | Py_ssize_t size; |
|
1713 | 1718 | |
|
1714 | 1719 | if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj)) |
|
1715 | 1720 | return -1; |
|
1716 | 1721 | if (!PyString_Check(data_obj)) { |
|
1717 | 1722 | PyErr_SetString(PyExc_TypeError, "data is not a string"); |
|
1718 | 1723 | return -1; |
|
1719 | 1724 | } |
|
1720 | 1725 | size = PyString_GET_SIZE(data_obj); |
|
1721 | 1726 | |
|
1722 | 1727 | self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj); |
|
1723 | 1728 | self->data = data_obj; |
|
1724 | 1729 | self->cache = NULL; |
|
1725 | 1730 | |
|
1726 | 1731 | self->added = NULL; |
|
1727 | 1732 | self->headrevs = NULL; |
|
1728 | 1733 | self->offsets = NULL; |
|
1729 | 1734 | self->nt = NULL; |
|
1730 | 1735 | self->ntlength = self->ntcapacity = 0; |
|
1731 | 1736 | self->ntdepth = self->ntsplits = 0; |
|
1732 | 1737 | self->ntlookups = self->ntmisses = 0; |
|
1733 | 1738 | self->ntrev = -1; |
|
1734 | 1739 | Py_INCREF(self->data); |
|
1735 | 1740 | |
|
1736 | 1741 | if (self->inlined) { |
|
1737 | 1742 | long len = inline_scan(self, NULL); |
|
1738 | 1743 | if (len == -1) |
|
1739 | 1744 | goto bail; |
|
1740 | 1745 | self->raw_length = len; |
|
1741 | 1746 | self->length = len + 1; |
|
1742 | 1747 | } else { |
|
1743 | 1748 | if (size % v1_hdrsize) { |
|
1744 | 1749 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); |
|
1745 | 1750 | goto bail; |
|
1746 | 1751 | } |
|
1747 | 1752 | self->raw_length = size / v1_hdrsize; |
|
1748 | 1753 | self->length = self->raw_length + 1; |
|
1749 | 1754 | } |
|
1750 | 1755 | |
|
1751 | 1756 | return 0; |
|
1752 | 1757 | bail: |
|
1753 | 1758 | return -1; |
|
1754 | 1759 | } |
|
1755 | 1760 | |
|
1756 | 1761 | static PyObject *index_nodemap(indexObject *self) |
|
1757 | 1762 | { |
|
1758 | 1763 | Py_INCREF(self); |
|
1759 | 1764 | return (PyObject *)self; |
|
1760 | 1765 | } |
|
1761 | 1766 | |
|
1762 | 1767 | static void index_dealloc(indexObject *self) |
|
1763 | 1768 | { |
|
1764 | 1769 | _index_clearcaches(self); |
|
1765 | 1770 | Py_DECREF(self->data); |
|
1766 | 1771 | Py_XDECREF(self->added); |
|
1767 | 1772 | PyObject_Del(self); |
|
1768 | 1773 | } |
|
1769 | 1774 | |
|
1770 | 1775 | static PySequenceMethods index_sequence_methods = { |
|
1771 | 1776 | (lenfunc)index_length, /* sq_length */ |
|
1772 | 1777 | 0, /* sq_concat */ |
|
1773 | 1778 | 0, /* sq_repeat */ |
|
1774 | 1779 | (ssizeargfunc)index_get, /* sq_item */ |
|
1775 | 1780 | 0, /* sq_slice */ |
|
1776 | 1781 | 0, /* sq_ass_item */ |
|
1777 | 1782 | 0, /* sq_ass_slice */ |
|
1778 | 1783 | (objobjproc)index_contains, /* sq_contains */ |
|
1779 | 1784 | }; |
|
1780 | 1785 | |
|
1781 | 1786 | static PyMappingMethods index_mapping_methods = { |
|
1782 | 1787 | (lenfunc)index_length, /* mp_length */ |
|
1783 | 1788 | (binaryfunc)index_getitem, /* mp_subscript */ |
|
1784 | 1789 | (objobjargproc)index_assign_subscript, /* mp_ass_subscript */ |
|
1785 | 1790 | }; |
|
1786 | 1791 | |
|
1787 | 1792 | static PyMethodDef index_methods[] = { |
|
1788 | 1793 | {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS, |
|
1789 | 1794 | "return the gca set of the given revs"}, |
|
1790 | 1795 | {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS, |
|
1791 | 1796 | "clear the index caches"}, |
|
1792 | 1797 | {"get", (PyCFunction)index_m_get, METH_VARARGS, |
|
1793 | 1798 | "get an index entry"}, |
|
1794 | 1799 | {"headrevs", (PyCFunction)index_headrevs, METH_NOARGS, |
|
1795 | 1800 | "get head revisions"}, |
|
1796 | 1801 | {"insert", (PyCFunction)index_insert, METH_VARARGS, |
|
1797 | 1802 | "insert an index entry"}, |
|
1798 | 1803 | {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS, |
|
1799 | 1804 | "match a potentially ambiguous node ID"}, |
|
1800 | 1805 | {"stats", (PyCFunction)index_stats, METH_NOARGS, |
|
1801 | 1806 | "stats for the index"}, |
|
1802 | 1807 | {NULL} /* Sentinel */ |
|
1803 | 1808 | }; |
|
1804 | 1809 | |
|
1805 | 1810 | static PyGetSetDef index_getset[] = { |
|
1806 | 1811 | {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL}, |
|
1807 | 1812 | {NULL} /* Sentinel */ |
|
1808 | 1813 | }; |
|
1809 | 1814 | |
|
1810 | 1815 | static PyTypeObject indexType = { |
|
1811 | 1816 | PyObject_HEAD_INIT(NULL) |
|
1812 | 1817 | 0, /* ob_size */ |
|
1813 | 1818 | "parsers.index", /* tp_name */ |
|
1814 | 1819 | sizeof(indexObject), /* tp_basicsize */ |
|
1815 | 1820 | 0, /* tp_itemsize */ |
|
1816 | 1821 | (destructor)index_dealloc, /* tp_dealloc */ |
|
1817 | 1822 | 0, /* tp_print */ |
|
1818 | 1823 | 0, /* tp_getattr */ |
|
1819 | 1824 | 0, /* tp_setattr */ |
|
1820 | 1825 | 0, /* tp_compare */ |
|
1821 | 1826 | 0, /* tp_repr */ |
|
1822 | 1827 | 0, /* tp_as_number */ |
|
1823 | 1828 | &index_sequence_methods, /* tp_as_sequence */ |
|
1824 | 1829 | &index_mapping_methods, /* tp_as_mapping */ |
|
1825 | 1830 | 0, /* tp_hash */ |
|
1826 | 1831 | 0, /* tp_call */ |
|
1827 | 1832 | 0, /* tp_str */ |
|
1828 | 1833 | 0, /* tp_getattro */ |
|
1829 | 1834 | 0, /* tp_setattro */ |
|
1830 | 1835 | 0, /* tp_as_buffer */ |
|
1831 | 1836 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
1832 | 1837 | "revlog index", /* tp_doc */ |
|
1833 | 1838 | 0, /* tp_traverse */ |
|
1834 | 1839 | 0, /* tp_clear */ |
|
1835 | 1840 | 0, /* tp_richcompare */ |
|
1836 | 1841 | 0, /* tp_weaklistoffset */ |
|
1837 | 1842 | 0, /* tp_iter */ |
|
1838 | 1843 | 0, /* tp_iternext */ |
|
1839 | 1844 | index_methods, /* tp_methods */ |
|
1840 | 1845 | 0, /* tp_members */ |
|
1841 | 1846 | index_getset, /* tp_getset */ |
|
1842 | 1847 | 0, /* tp_base */ |
|
1843 | 1848 | 0, /* tp_dict */ |
|
1844 | 1849 | 0, /* tp_descr_get */ |
|
1845 | 1850 | 0, /* tp_descr_set */ |
|
1846 | 1851 | 0, /* tp_dictoffset */ |
|
1847 | 1852 | (initproc)index_init, /* tp_init */ |
|
1848 | 1853 | 0, /* tp_alloc */ |
|
1849 | 1854 | }; |
|
1850 | 1855 | |
|
1851 | 1856 | /* |
|
1852 | 1857 | * returns a tuple of the form (index, index, cache) with elements as |
|
1853 | 1858 | * follows: |
|
1854 | 1859 | * |
|
1855 | 1860 | * index: an index object that lazily parses RevlogNG records |
|
1856 | 1861 | * cache: if data is inlined, a tuple (index_file_content, 0), else None |
|
1857 | 1862 | * |
|
1858 | 1863 | * added complications are for backwards compatibility |
|
1859 | 1864 | */ |
|
1860 | 1865 | static PyObject *parse_index2(PyObject *self, PyObject *args) |
|
1861 | 1866 | { |
|
1862 | 1867 | PyObject *tuple = NULL, *cache = NULL; |
|
1863 | 1868 | indexObject *idx; |
|
1864 | 1869 | int ret; |
|
1865 | 1870 | |
|
1866 | 1871 | idx = PyObject_New(indexObject, &indexType); |
|
1867 | 1872 | if (idx == NULL) |
|
1868 | 1873 | goto bail; |
|
1869 | 1874 | |
|
1870 | 1875 | ret = index_init(idx, args); |
|
1871 | 1876 | if (ret == -1) |
|
1872 | 1877 | goto bail; |
|
1873 | 1878 | |
|
1874 | 1879 | if (idx->inlined) { |
|
1875 | 1880 | cache = Py_BuildValue("iO", 0, idx->data); |
|
1876 | 1881 | if (cache == NULL) |
|
1877 | 1882 | goto bail; |
|
1878 | 1883 | } else { |
|
1879 | 1884 | cache = Py_None; |
|
1880 | 1885 | Py_INCREF(cache); |
|
1881 | 1886 | } |
|
1882 | 1887 | |
|
1883 | 1888 | tuple = Py_BuildValue("NN", idx, cache); |
|
1884 | 1889 | if (!tuple) |
|
1885 | 1890 | goto bail; |
|
1886 | 1891 | return tuple; |
|
1887 | 1892 | |
|
1888 | 1893 | bail: |
|
1889 | 1894 | Py_XDECREF(idx); |
|
1890 | 1895 | Py_XDECREF(cache); |
|
1891 | 1896 | Py_XDECREF(tuple); |
|
1892 | 1897 | return NULL; |
|
1893 | 1898 | } |
|
1894 | 1899 | |
|
1895 | 1900 | static char parsers_doc[] = "Efficient content parsing."; |
|
1896 | 1901 | |
|
1897 | 1902 | PyObject *encodedir(PyObject *self, PyObject *args); |
|
1898 | 1903 | PyObject *pathencode(PyObject *self, PyObject *args); |
|
1899 | 1904 | PyObject *lowerencode(PyObject *self, PyObject *args); |
|
1900 | 1905 | |
|
1901 | 1906 | static PyMethodDef methods[] = { |
|
1902 | 1907 | {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"}, |
|
1903 | 1908 | {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"}, |
|
1904 | 1909 | {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"}, |
|
1905 | 1910 | {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"}, |
|
1906 | 1911 | {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"}, |
|
1907 | 1912 | {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"}, |
|
1908 | 1913 | {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"}, |
|
1909 | 1914 | {NULL, NULL} |
|
1910 | 1915 | }; |
|
1911 | 1916 | |
|
1912 | 1917 | void dirs_module_init(PyObject *mod); |
|
1913 | 1918 | |
|
1914 | 1919 | static void module_init(PyObject *mod) |
|
1915 | 1920 | { |
|
1916 | 1921 | dirs_module_init(mod); |
|
1917 | 1922 | |
|
1918 | 1923 | indexType.tp_new = PyType_GenericNew; |
|
1919 | 1924 | if (PyType_Ready(&indexType) < 0) |
|
1920 | 1925 | return; |
|
1921 | 1926 | Py_INCREF(&indexType); |
|
1922 | 1927 | |
|
1923 | 1928 | PyModule_AddObject(mod, "index", (PyObject *)&indexType); |
|
1924 | 1929 | |
|
1925 | 1930 | nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0, |
|
1926 | 1931 | -1, -1, -1, -1, nullid, 20); |
|
1927 | 1932 | if (nullentry) |
|
1928 | 1933 | PyObject_GC_UnTrack(nullentry); |
|
1929 | 1934 | |
|
1930 | 1935 | dirstate_unset = Py_BuildValue("ciii", 'n', 0, -1, -1); |
|
1931 | 1936 | } |
|
1932 | 1937 | |
|
1933 | 1938 | #ifdef IS_PY3K |
|
1934 | 1939 | static struct PyModuleDef parsers_module = { |
|
1935 | 1940 | PyModuleDef_HEAD_INIT, |
|
1936 | 1941 | "parsers", |
|
1937 | 1942 | parsers_doc, |
|
1938 | 1943 | -1, |
|
1939 | 1944 | methods |
|
1940 | 1945 | }; |
|
1941 | 1946 | |
|
1942 | 1947 | PyMODINIT_FUNC PyInit_parsers(void) |
|
1943 | 1948 | { |
|
1944 | 1949 | PyObject *mod = PyModule_Create(&parsers_module); |
|
1945 | 1950 | module_init(mod); |
|
1946 | 1951 | return mod; |
|
1947 | 1952 | } |
|
1948 | 1953 | #else |
|
1949 | 1954 | PyMODINIT_FUNC initparsers(void) |
|
1950 | 1955 | { |
|
1951 | 1956 | PyObject *mod = Py_InitModule3("parsers", methods, parsers_doc); |
|
1952 | 1957 | module_init(mod); |
|
1953 | 1958 | } |
|
1954 | 1959 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now