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