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