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