Show More
@@ -1,1014 +1,1011 b'' | |||||
1 | /* |
|
1 | /* | |
2 | * manifest.c - manifest type that does on-demand parsing. |
|
2 | * manifest.c - manifest type that does on-demand parsing. | |
3 | * |
|
3 | * | |
4 | * Copyright 2015, Google Inc. |
|
4 | * Copyright 2015, Google Inc. | |
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 | #include <Python.h> |
|
9 | #include <Python.h> | |
10 |
|
10 | |||
11 | #include <assert.h> |
|
11 | #include <assert.h> | |
12 | #include <stdlib.h> |
|
12 | #include <stdlib.h> | |
13 | #include <string.h> |
|
13 | #include <string.h> | |
14 |
|
14 | |||
15 | #include "charencode.h" |
|
15 | #include "charencode.h" | |
16 | #include "util.h" |
|
16 | #include "util.h" | |
17 |
|
17 | |||
18 | #define DEFAULT_LINES 100000 |
|
18 | #define DEFAULT_LINES 100000 | |
19 |
|
19 | |||
20 | typedef struct { |
|
20 | typedef struct { | |
21 | char *start; |
|
21 | char *start; | |
22 | Py_ssize_t len; /* length of line including terminal newline */ |
|
22 | Py_ssize_t len; /* length of line including terminal newline */ | |
23 | char hash_suffix; |
|
23 | char hash_suffix; | |
24 | bool from_malloc; |
|
24 | bool from_malloc; | |
25 | bool deleted; |
|
25 | bool deleted; | |
26 | } line; |
|
26 | } line; | |
27 |
|
27 | |||
28 | typedef struct { |
|
28 | typedef struct { | |
29 | PyObject_HEAD |
|
29 | PyObject_HEAD | |
30 | PyObject *pydata; |
|
30 | PyObject *pydata; | |
31 | line *lines; |
|
31 | line *lines; | |
32 | int numlines; /* number of line entries */ |
|
32 | int numlines; /* number of line entries */ | |
33 | int livelines; /* number of non-deleted lines */ |
|
33 | int livelines; /* number of non-deleted lines */ | |
34 | int maxlines; /* allocated number of lines */ |
|
34 | int maxlines; /* allocated number of lines */ | |
35 | bool dirty; |
|
35 | bool dirty; | |
36 | } lazymanifest; |
|
36 | } lazymanifest; | |
37 |
|
37 | |||
38 | #define MANIFEST_OOM -1 |
|
38 | #define MANIFEST_OOM -1 | |
39 | #define MANIFEST_NOT_SORTED -2 |
|
39 | #define MANIFEST_NOT_SORTED -2 | |
40 | #define MANIFEST_MALFORMED -3 |
|
40 | #define MANIFEST_MALFORMED -3 | |
41 | #define MANIFEST_BOGUS_FILENAME -4 |
|
41 | #define MANIFEST_BOGUS_FILENAME -4 | |
42 | #define MANIFEST_TOO_SHORT_LINE -5 |
|
42 | #define MANIFEST_TOO_SHORT_LINE -5 | |
43 |
|
43 | |||
44 | /* get the length of the path for a line */ |
|
44 | /* get the length of the path for a line */ | |
45 | static Py_ssize_t pathlen(line *l) |
|
45 | static Py_ssize_t pathlen(line *l) | |
46 | { |
|
46 | { | |
47 | const char *end = memchr(l->start, '\0', l->len); |
|
47 | const char *end = memchr(l->start, '\0', l->len); | |
48 | return (end) ? (Py_ssize_t)(end - l->start) : l->len; |
|
48 | return (end) ? (Py_ssize_t)(end - l->start) : l->len; | |
49 | } |
|
49 | } | |
50 |
|
50 | |||
51 | /* get the node value of a single line */ |
|
51 | /* get the node value of a single line */ | |
52 | static PyObject *nodeof(line *l, char *flag) |
|
52 | static PyObject *nodeof(line *l, char *flag) | |
53 | { |
|
53 | { | |
54 | char *s = l->start; |
|
54 | char *s = l->start; | |
55 | Py_ssize_t llen = pathlen(l); |
|
55 | Py_ssize_t llen = pathlen(l); | |
56 | Py_ssize_t hlen = l->len - llen - 2; |
|
56 | Py_ssize_t hlen = l->len - llen - 2; | |
57 | Py_ssize_t hlen_raw; |
|
57 | Py_ssize_t hlen_raw; | |
58 | PyObject *hash; |
|
58 | PyObject *hash; | |
59 | if (llen + 1 + 40 + 1 > l->len) { /* path '\0' hash '\n' */ |
|
59 | if (llen + 1 + 40 + 1 > l->len) { /* path '\0' hash '\n' */ | |
60 | PyErr_SetString(PyExc_ValueError, "manifest line too short"); |
|
60 | PyErr_SetString(PyExc_ValueError, "manifest line too short"); | |
61 | return NULL; |
|
61 | return NULL; | |
62 | } |
|
62 | } | |
63 | /* Detect flags after the hash first. */ |
|
63 | /* Detect flags after the hash first. */ | |
64 | switch (s[llen + hlen]) { |
|
64 | switch (s[llen + hlen]) { | |
65 | case 'l': |
|
65 | case 'l': | |
66 | case 't': |
|
66 | case 't': | |
67 | case 'x': |
|
67 | case 'x': | |
68 | *flag = s[llen + hlen]; |
|
68 | *flag = s[llen + hlen]; | |
69 | --hlen; |
|
69 | --hlen; | |
70 | break; |
|
70 | break; | |
71 | default: |
|
71 | default: | |
72 | *flag = '\0'; |
|
72 | *flag = '\0'; | |
73 | break; |
|
73 | break; | |
74 | } |
|
74 | } | |
75 |
|
75 | |||
76 | switch (hlen) { |
|
76 | switch (hlen) { | |
77 | case 40: /* sha1 */ |
|
77 | case 40: /* sha1 */ | |
78 | hlen_raw = 20; |
|
78 | hlen_raw = 20; | |
79 | break; |
|
79 | break; | |
80 | case 64: /* new hash */ |
|
80 | case 64: /* new hash */ | |
81 | hlen_raw = 32; |
|
81 | hlen_raw = 32; | |
82 | break; |
|
82 | break; | |
83 | default: |
|
83 | default: | |
84 | PyErr_SetString(PyExc_ValueError, "invalid node length in manifest"); |
|
84 | PyErr_SetString(PyExc_ValueError, "invalid node length in manifest"); | |
85 | return NULL; |
|
85 | return NULL; | |
86 | } |
|
86 | } | |
87 | hash = unhexlify(s + llen + 1, hlen_raw * 2); |
|
87 | hash = unhexlify(s + llen + 1, hlen_raw * 2); | |
88 | if (!hash) { |
|
88 | if (!hash) { | |
89 | return NULL; |
|
89 | return NULL; | |
90 | } |
|
90 | } | |
91 | if (l->hash_suffix != '\0') { |
|
91 | if (l->hash_suffix != '\0') { | |
92 | char newhash[33]; |
|
92 | char newhash[33]; | |
93 | memcpy(newhash, PyBytes_AsString(hash), hlen_raw); |
|
93 | memcpy(newhash, PyBytes_AsString(hash), hlen_raw); | |
94 | Py_DECREF(hash); |
|
94 | Py_DECREF(hash); | |
95 | newhash[hlen_raw] = l->hash_suffix; |
|
95 | newhash[hlen_raw] = l->hash_suffix; | |
96 | hash = PyBytes_FromStringAndSize(newhash, hlen_raw+1); |
|
96 | hash = PyBytes_FromStringAndSize(newhash, hlen_raw+1); | |
97 | } |
|
97 | } | |
98 | return hash; |
|
98 | return hash; | |
99 | } |
|
99 | } | |
100 |
|
100 | |||
101 | /* get the node hash and flags of a line as a tuple */ |
|
101 | /* get the node hash and flags of a line as a tuple */ | |
102 | static PyObject *hashflags(line *l) |
|
102 | static PyObject *hashflags(line *l) | |
103 | { |
|
103 | { | |
104 | char flag; |
|
104 | char flag; | |
105 | PyObject *hash = nodeof(l, &flag); |
|
105 | PyObject *hash = nodeof(l, &flag); | |
106 | ssize_t hlen; |
|
|||
107 | Py_ssize_t hplen, flen; |
|
|||
108 | PyObject *flags; |
|
106 | PyObject *flags; | |
109 | PyObject *tup; |
|
107 | PyObject *tup; | |
110 |
|
108 | |||
111 | if (!hash) |
|
109 | if (!hash) | |
112 | return NULL; |
|
110 | return NULL; | |
113 | flags = PyBytes_FromStringAndSize(&flag, flag ? 1 : 0); |
|
111 | flags = PyBytes_FromStringAndSize(&flag, flag ? 1 : 0); | |
114 | if (!flags) { |
|
112 | if (!flags) { | |
115 | Py_DECREF(hash); |
|
113 | Py_DECREF(hash); | |
116 | return NULL; |
|
114 | return NULL; | |
117 | } |
|
115 | } | |
118 | tup = PyTuple_Pack(2, hash, flags); |
|
116 | tup = PyTuple_Pack(2, hash, flags); | |
119 | Py_DECREF(flags); |
|
117 | Py_DECREF(flags); | |
120 | Py_DECREF(hash); |
|
118 | Py_DECREF(hash); | |
121 | return tup; |
|
119 | return tup; | |
122 | } |
|
120 | } | |
123 |
|
121 | |||
124 | /* if we're about to run out of space in the line index, add more */ |
|
122 | /* if we're about to run out of space in the line index, add more */ | |
125 | static bool realloc_if_full(lazymanifest *self) |
|
123 | static bool realloc_if_full(lazymanifest *self) | |
126 | { |
|
124 | { | |
127 | if (self->numlines == self->maxlines) { |
|
125 | if (self->numlines == self->maxlines) { | |
128 | self->maxlines *= 2; |
|
126 | self->maxlines *= 2; | |
129 | self->lines = realloc(self->lines, self->maxlines * sizeof(line)); |
|
127 | self->lines = realloc(self->lines, self->maxlines * sizeof(line)); | |
130 | } |
|
128 | } | |
131 | return !!self->lines; |
|
129 | return !!self->lines; | |
132 | } |
|
130 | } | |
133 |
|
131 | |||
134 | /* |
|
132 | /* | |
135 | * Find the line boundaries in the manifest that 'data' points to and store |
|
133 | * Find the line boundaries in the manifest that 'data' points to and store | |
136 | * information about each line in 'self'. |
|
134 | * information about each line in 'self'. | |
137 | */ |
|
135 | */ | |
138 | static int find_lines(lazymanifest *self, char *data, Py_ssize_t len) |
|
136 | static int find_lines(lazymanifest *self, char *data, Py_ssize_t len) | |
139 | { |
|
137 | { | |
140 | char *prev = NULL; |
|
138 | char *prev = NULL; | |
141 | while (len > 0) { |
|
139 | while (len > 0) { | |
142 | line *l; |
|
140 | line *l; | |
143 | char *next; |
|
141 | char *next; | |
144 | if (*data == '\0') { |
|
142 | if (*data == '\0') { | |
145 | /* It's implausible there's no filename, don't |
|
143 | /* It's implausible there's no filename, don't | |
146 | * even bother looking for the newline. */ |
|
144 | * even bother looking for the newline. */ | |
147 | return MANIFEST_BOGUS_FILENAME; |
|
145 | return MANIFEST_BOGUS_FILENAME; | |
148 | } |
|
146 | } | |
149 | next = memchr(data, '\n', len); |
|
147 | next = memchr(data, '\n', len); | |
150 | if (!next) { |
|
148 | if (!next) { | |
151 | return MANIFEST_MALFORMED; |
|
149 | return MANIFEST_MALFORMED; | |
152 | } |
|
150 | } | |
153 | if ((next - data) < 42) { |
|
151 | if ((next - data) < 42) { | |
154 | /* We should have at least 42 bytes in a line: |
|
152 | /* We should have at least 42 bytes in a line: | |
155 | 1 byte filename |
|
153 | 1 byte filename | |
156 | 1 NUL |
|
154 | 1 NUL | |
157 | 40 bytes of hash |
|
155 | 40 bytes of hash | |
158 | so we can give up here. |
|
156 | so we can give up here. | |
159 | */ |
|
157 | */ | |
160 | return MANIFEST_TOO_SHORT_LINE; |
|
158 | return MANIFEST_TOO_SHORT_LINE; | |
161 | } |
|
159 | } | |
162 | next++; /* advance past newline */ |
|
160 | next++; /* advance past newline */ | |
163 | if (prev && strcmp(prev, data) > -1) { |
|
161 | if (prev && strcmp(prev, data) > -1) { | |
164 | /* This data isn't sorted, so we have to abort. */ |
|
162 | /* This data isn't sorted, so we have to abort. */ | |
165 | return MANIFEST_NOT_SORTED; |
|
163 | return MANIFEST_NOT_SORTED; | |
166 | } |
|
164 | } | |
167 | if (!realloc_if_full(self)) { |
|
165 | if (!realloc_if_full(self)) { | |
168 | return MANIFEST_OOM; /* no memory */ |
|
166 | return MANIFEST_OOM; /* no memory */ | |
169 | } |
|
167 | } | |
170 | l = self->lines + ((self->numlines)++); |
|
168 | l = self->lines + ((self->numlines)++); | |
171 | l->start = data; |
|
169 | l->start = data; | |
172 | l->len = next - data; |
|
170 | l->len = next - data; | |
173 | l->hash_suffix = '\0'; |
|
171 | l->hash_suffix = '\0'; | |
174 | l->from_malloc = false; |
|
172 | l->from_malloc = false; | |
175 | l->deleted = false; |
|
173 | l->deleted = false; | |
176 | len = len - l->len; |
|
174 | len = len - l->len; | |
177 | prev = data; |
|
175 | prev = data; | |
178 | data = next; |
|
176 | data = next; | |
179 | } |
|
177 | } | |
180 | self->livelines = self->numlines; |
|
178 | self->livelines = self->numlines; | |
181 | return 0; |
|
179 | return 0; | |
182 | } |
|
180 | } | |
183 |
|
181 | |||
184 | static void lazymanifest_init_early(lazymanifest *self) |
|
182 | static void lazymanifest_init_early(lazymanifest *self) | |
185 | { |
|
183 | { | |
186 | self->pydata = NULL; |
|
184 | self->pydata = NULL; | |
187 | self->lines = NULL; |
|
185 | self->lines = NULL; | |
188 | self->numlines = 0; |
|
186 | self->numlines = 0; | |
189 | self->maxlines = 0; |
|
187 | self->maxlines = 0; | |
190 | } |
|
188 | } | |
191 |
|
189 | |||
192 | static int lazymanifest_init(lazymanifest *self, PyObject *args) |
|
190 | static int lazymanifest_init(lazymanifest *self, PyObject *args) | |
193 | { |
|
191 | { | |
194 | char *data; |
|
192 | char *data; | |
195 | Py_ssize_t len; |
|
193 | Py_ssize_t len; | |
196 | int err, ret; |
|
194 | int err, ret; | |
197 | PyObject *pydata; |
|
195 | PyObject *pydata; | |
198 |
|
196 | |||
199 | lazymanifest_init_early(self); |
|
197 | lazymanifest_init_early(self); | |
200 | if (!PyArg_ParseTuple(args, "S", &pydata)) { |
|
198 | if (!PyArg_ParseTuple(args, "S", &pydata)) { | |
201 | return -1; |
|
199 | return -1; | |
202 | } |
|
200 | } | |
203 | err = PyBytes_AsStringAndSize(pydata, &data, &len); |
|
201 | err = PyBytes_AsStringAndSize(pydata, &data, &len); | |
204 |
|
202 | |||
205 | self->dirty = false; |
|
203 | self->dirty = false; | |
206 | if (err == -1) |
|
204 | if (err == -1) | |
207 | return -1; |
|
205 | return -1; | |
208 | self->pydata = pydata; |
|
206 | self->pydata = pydata; | |
209 | Py_INCREF(self->pydata); |
|
207 | Py_INCREF(self->pydata); | |
210 | Py_BEGIN_ALLOW_THREADS |
|
208 | Py_BEGIN_ALLOW_THREADS | |
211 | self->lines = malloc(DEFAULT_LINES * sizeof(line)); |
|
209 | self->lines = malloc(DEFAULT_LINES * sizeof(line)); | |
212 | self->maxlines = DEFAULT_LINES; |
|
210 | self->maxlines = DEFAULT_LINES; | |
213 | self->numlines = 0; |
|
211 | self->numlines = 0; | |
214 | if (!self->lines) |
|
212 | if (!self->lines) | |
215 | ret = MANIFEST_OOM; |
|
213 | ret = MANIFEST_OOM; | |
216 | else |
|
214 | else | |
217 | ret = find_lines(self, data, len); |
|
215 | ret = find_lines(self, data, len); | |
218 | Py_END_ALLOW_THREADS |
|
216 | Py_END_ALLOW_THREADS | |
219 | switch (ret) { |
|
217 | switch (ret) { | |
220 | case 0: |
|
218 | case 0: | |
221 | break; |
|
219 | break; | |
222 | case MANIFEST_OOM: |
|
220 | case MANIFEST_OOM: | |
223 | PyErr_NoMemory(); |
|
221 | PyErr_NoMemory(); | |
224 | break; |
|
222 | break; | |
225 | case MANIFEST_NOT_SORTED: |
|
223 | case MANIFEST_NOT_SORTED: | |
226 | PyErr_Format(PyExc_ValueError, |
|
224 | PyErr_Format(PyExc_ValueError, | |
227 | "Manifest lines not in sorted order."); |
|
225 | "Manifest lines not in sorted order."); | |
228 | break; |
|
226 | break; | |
229 | case MANIFEST_MALFORMED: |
|
227 | case MANIFEST_MALFORMED: | |
230 | PyErr_Format(PyExc_ValueError, |
|
228 | PyErr_Format(PyExc_ValueError, | |
231 | "Manifest did not end in a newline."); |
|
229 | "Manifest did not end in a newline."); | |
232 | break; |
|
230 | break; | |
233 | case MANIFEST_BOGUS_FILENAME: |
|
231 | case MANIFEST_BOGUS_FILENAME: | |
234 | PyErr_Format( |
|
232 | PyErr_Format( | |
235 | PyExc_ValueError, |
|
233 | PyExc_ValueError, | |
236 | "Manifest had an entry with a zero-length filename."); |
|
234 | "Manifest had an entry with a zero-length filename."); | |
237 | break; |
|
235 | break; | |
238 | case MANIFEST_TOO_SHORT_LINE: |
|
236 | case MANIFEST_TOO_SHORT_LINE: | |
239 | PyErr_Format( |
|
237 | PyErr_Format( | |
240 | PyExc_ValueError, |
|
238 | PyExc_ValueError, | |
241 | "Manifest had implausibly-short line."); |
|
239 | "Manifest had implausibly-short line."); | |
242 | break; |
|
240 | break; | |
243 | default: |
|
241 | default: | |
244 | PyErr_Format(PyExc_ValueError, |
|
242 | PyErr_Format(PyExc_ValueError, | |
245 | "Unknown problem parsing manifest."); |
|
243 | "Unknown problem parsing manifest."); | |
246 | } |
|
244 | } | |
247 | return ret == 0 ? 0 : -1; |
|
245 | return ret == 0 ? 0 : -1; | |
248 | } |
|
246 | } | |
249 |
|
247 | |||
250 | static void lazymanifest_dealloc(lazymanifest *self) |
|
248 | static void lazymanifest_dealloc(lazymanifest *self) | |
251 | { |
|
249 | { | |
252 | /* free any extra lines we had to allocate */ |
|
250 | /* free any extra lines we had to allocate */ | |
253 | int i; |
|
251 | int i; | |
254 | for (i = 0; self->lines && (i < self->numlines); i++) { |
|
252 | for (i = 0; self->lines && (i < self->numlines); i++) { | |
255 | if (self->lines[i].from_malloc) { |
|
253 | if (self->lines[i].from_malloc) { | |
256 | free(self->lines[i].start); |
|
254 | free(self->lines[i].start); | |
257 | } |
|
255 | } | |
258 | } |
|
256 | } | |
259 | free(self->lines); |
|
257 | free(self->lines); | |
260 | self->lines = NULL; |
|
258 | self->lines = NULL; | |
261 | if (self->pydata) { |
|
259 | if (self->pydata) { | |
262 | Py_DECREF(self->pydata); |
|
260 | Py_DECREF(self->pydata); | |
263 | self->pydata = NULL; |
|
261 | self->pydata = NULL; | |
264 | } |
|
262 | } | |
265 | PyObject_Del(self); |
|
263 | PyObject_Del(self); | |
266 | } |
|
264 | } | |
267 |
|
265 | |||
268 | /* iteration support */ |
|
266 | /* iteration support */ | |
269 |
|
267 | |||
270 | typedef struct { |
|
268 | typedef struct { | |
271 | PyObject_HEAD lazymanifest *m; |
|
269 | PyObject_HEAD lazymanifest *m; | |
272 | Py_ssize_t pos; |
|
270 | Py_ssize_t pos; | |
273 | } lmIter; |
|
271 | } lmIter; | |
274 |
|
272 | |||
275 | static void lmiter_dealloc(PyObject *o) |
|
273 | static void lmiter_dealloc(PyObject *o) | |
276 | { |
|
274 | { | |
277 | lmIter *self = (lmIter *)o; |
|
275 | lmIter *self = (lmIter *)o; | |
278 | Py_DECREF(self->m); |
|
276 | Py_DECREF(self->m); | |
279 | PyObject_Del(self); |
|
277 | PyObject_Del(self); | |
280 | } |
|
278 | } | |
281 |
|
279 | |||
282 | static line *lmiter_nextline(lmIter *self) |
|
280 | static line *lmiter_nextline(lmIter *self) | |
283 | { |
|
281 | { | |
284 | do { |
|
282 | do { | |
285 | self->pos++; |
|
283 | self->pos++; | |
286 | if (self->pos >= self->m->numlines) { |
|
284 | if (self->pos >= self->m->numlines) { | |
287 | return NULL; |
|
285 | return NULL; | |
288 | } |
|
286 | } | |
289 | /* skip over deleted manifest entries */ |
|
287 | /* skip over deleted manifest entries */ | |
290 | } while (self->m->lines[self->pos].deleted); |
|
288 | } while (self->m->lines[self->pos].deleted); | |
291 | return self->m->lines + self->pos; |
|
289 | return self->m->lines + self->pos; | |
292 | } |
|
290 | } | |
293 |
|
291 | |||
294 | static PyObject *lmiter_iterentriesnext(PyObject *o) |
|
292 | static PyObject *lmiter_iterentriesnext(PyObject *o) | |
295 | { |
|
293 | { | |
296 | Py_ssize_t pl; |
|
294 | Py_ssize_t pl; | |
297 | line *l; |
|
295 | line *l; | |
298 | char flag; |
|
296 | char flag; | |
299 | Py_ssize_t consumed; |
|
|||
300 | PyObject *ret = NULL, *path = NULL, *hash = NULL, *flags = NULL; |
|
297 | PyObject *ret = NULL, *path = NULL, *hash = NULL, *flags = NULL; | |
301 | l = lmiter_nextline((lmIter *)o); |
|
298 | l = lmiter_nextline((lmIter *)o); | |
302 | if (!l) { |
|
299 | if (!l) { | |
303 | goto done; |
|
300 | goto done; | |
304 | } |
|
301 | } | |
305 | pl = pathlen(l); |
|
302 | pl = pathlen(l); | |
306 | path = PyBytes_FromStringAndSize(l->start, pl); |
|
303 | path = PyBytes_FromStringAndSize(l->start, pl); | |
307 | hash = nodeof(l, &flag); |
|
304 | hash = nodeof(l, &flag); | |
308 | if (!path || !hash) { |
|
305 | if (!path || !hash) { | |
309 | goto done; |
|
306 | goto done; | |
310 | } |
|
307 | } | |
311 | flags = PyBytes_FromStringAndSize(&flag, flag ? 1 : 0); |
|
308 | flags = PyBytes_FromStringAndSize(&flag, flag ? 1 : 0); | |
312 | if (!flags) { |
|
309 | if (!flags) { | |
313 | goto done; |
|
310 | goto done; | |
314 | } |
|
311 | } | |
315 | ret = PyTuple_Pack(3, path, hash, flags); |
|
312 | ret = PyTuple_Pack(3, path, hash, flags); | |
316 | done: |
|
313 | done: | |
317 | Py_XDECREF(path); |
|
314 | Py_XDECREF(path); | |
318 | Py_XDECREF(hash); |
|
315 | Py_XDECREF(hash); | |
319 | Py_XDECREF(flags); |
|
316 | Py_XDECREF(flags); | |
320 | return ret; |
|
317 | return ret; | |
321 | } |
|
318 | } | |
322 |
|
319 | |||
323 | #ifdef IS_PY3K |
|
320 | #ifdef IS_PY3K | |
324 | #define LAZYMANIFESTENTRIESITERATOR_TPFLAGS Py_TPFLAGS_DEFAULT |
|
321 | #define LAZYMANIFESTENTRIESITERATOR_TPFLAGS Py_TPFLAGS_DEFAULT | |
325 | #else |
|
322 | #else | |
326 | #define LAZYMANIFESTENTRIESITERATOR_TPFLAGS Py_TPFLAGS_DEFAULT \ |
|
323 | #define LAZYMANIFESTENTRIESITERATOR_TPFLAGS Py_TPFLAGS_DEFAULT \ | |
327 | | Py_TPFLAGS_HAVE_ITER |
|
324 | | Py_TPFLAGS_HAVE_ITER | |
328 | #endif |
|
325 | #endif | |
329 |
|
326 | |||
330 | static PyTypeObject lazymanifestEntriesIterator = { |
|
327 | static PyTypeObject lazymanifestEntriesIterator = { | |
331 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ |
|
328 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ | |
332 | "parsers.lazymanifest.entriesiterator", /*tp_name */ |
|
329 | "parsers.lazymanifest.entriesiterator", /*tp_name */ | |
333 | sizeof(lmIter), /*tp_basicsize */ |
|
330 | sizeof(lmIter), /*tp_basicsize */ | |
334 | 0, /*tp_itemsize */ |
|
331 | 0, /*tp_itemsize */ | |
335 | lmiter_dealloc, /*tp_dealloc */ |
|
332 | lmiter_dealloc, /*tp_dealloc */ | |
336 | 0, /*tp_print */ |
|
333 | 0, /*tp_print */ | |
337 | 0, /*tp_getattr */ |
|
334 | 0, /*tp_getattr */ | |
338 | 0, /*tp_setattr */ |
|
335 | 0, /*tp_setattr */ | |
339 | 0, /*tp_compare */ |
|
336 | 0, /*tp_compare */ | |
340 | 0, /*tp_repr */ |
|
337 | 0, /*tp_repr */ | |
341 | 0, /*tp_as_number */ |
|
338 | 0, /*tp_as_number */ | |
342 | 0, /*tp_as_sequence */ |
|
339 | 0, /*tp_as_sequence */ | |
343 | 0, /*tp_as_mapping */ |
|
340 | 0, /*tp_as_mapping */ | |
344 | 0, /*tp_hash */ |
|
341 | 0, /*tp_hash */ | |
345 | 0, /*tp_call */ |
|
342 | 0, /*tp_call */ | |
346 | 0, /*tp_str */ |
|
343 | 0, /*tp_str */ | |
347 | 0, /*tp_getattro */ |
|
344 | 0, /*tp_getattro */ | |
348 | 0, /*tp_setattro */ |
|
345 | 0, /*tp_setattro */ | |
349 | 0, /*tp_as_buffer */ |
|
346 | 0, /*tp_as_buffer */ | |
350 | LAZYMANIFESTENTRIESITERATOR_TPFLAGS, /* tp_flags */ |
|
347 | LAZYMANIFESTENTRIESITERATOR_TPFLAGS, /* tp_flags */ | |
351 | "Iterator for 3-tuples in a lazymanifest.", /* tp_doc */ |
|
348 | "Iterator for 3-tuples in a lazymanifest.", /* tp_doc */ | |
352 | 0, /* tp_traverse */ |
|
349 | 0, /* tp_traverse */ | |
353 | 0, /* tp_clear */ |
|
350 | 0, /* tp_clear */ | |
354 | 0, /* tp_richcompare */ |
|
351 | 0, /* tp_richcompare */ | |
355 | 0, /* tp_weaklistoffset */ |
|
352 | 0, /* tp_weaklistoffset */ | |
356 | PyObject_SelfIter, /* tp_iter: __iter__() method */ |
|
353 | PyObject_SelfIter, /* tp_iter: __iter__() method */ | |
357 | lmiter_iterentriesnext, /* tp_iternext: next() method */ |
|
354 | lmiter_iterentriesnext, /* tp_iternext: next() method */ | |
358 | }; |
|
355 | }; | |
359 |
|
356 | |||
360 | static PyObject *lmiter_iterkeysnext(PyObject *o) |
|
357 | static PyObject *lmiter_iterkeysnext(PyObject *o) | |
361 | { |
|
358 | { | |
362 | Py_ssize_t pl; |
|
359 | Py_ssize_t pl; | |
363 | line *l = lmiter_nextline((lmIter *)o); |
|
360 | line *l = lmiter_nextline((lmIter *)o); | |
364 | if (!l) { |
|
361 | if (!l) { | |
365 | return NULL; |
|
362 | return NULL; | |
366 | } |
|
363 | } | |
367 | pl = pathlen(l); |
|
364 | pl = pathlen(l); | |
368 | return PyBytes_FromStringAndSize(l->start, pl); |
|
365 | return PyBytes_FromStringAndSize(l->start, pl); | |
369 | } |
|
366 | } | |
370 |
|
367 | |||
371 | #ifdef IS_PY3K |
|
368 | #ifdef IS_PY3K | |
372 | #define LAZYMANIFESTKEYSITERATOR_TPFLAGS Py_TPFLAGS_DEFAULT |
|
369 | #define LAZYMANIFESTKEYSITERATOR_TPFLAGS Py_TPFLAGS_DEFAULT | |
373 | #else |
|
370 | #else | |
374 | #define LAZYMANIFESTKEYSITERATOR_TPFLAGS Py_TPFLAGS_DEFAULT \ |
|
371 | #define LAZYMANIFESTKEYSITERATOR_TPFLAGS Py_TPFLAGS_DEFAULT \ | |
375 | | Py_TPFLAGS_HAVE_ITER |
|
372 | | Py_TPFLAGS_HAVE_ITER | |
376 | #endif |
|
373 | #endif | |
377 |
|
374 | |||
378 | static PyTypeObject lazymanifestKeysIterator = { |
|
375 | static PyTypeObject lazymanifestKeysIterator = { | |
379 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ |
|
376 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ | |
380 | "parsers.lazymanifest.keysiterator", /*tp_name */ |
|
377 | "parsers.lazymanifest.keysiterator", /*tp_name */ | |
381 | sizeof(lmIter), /*tp_basicsize */ |
|
378 | sizeof(lmIter), /*tp_basicsize */ | |
382 | 0, /*tp_itemsize */ |
|
379 | 0, /*tp_itemsize */ | |
383 | lmiter_dealloc, /*tp_dealloc */ |
|
380 | lmiter_dealloc, /*tp_dealloc */ | |
384 | 0, /*tp_print */ |
|
381 | 0, /*tp_print */ | |
385 | 0, /*tp_getattr */ |
|
382 | 0, /*tp_getattr */ | |
386 | 0, /*tp_setattr */ |
|
383 | 0, /*tp_setattr */ | |
387 | 0, /*tp_compare */ |
|
384 | 0, /*tp_compare */ | |
388 | 0, /*tp_repr */ |
|
385 | 0, /*tp_repr */ | |
389 | 0, /*tp_as_number */ |
|
386 | 0, /*tp_as_number */ | |
390 | 0, /*tp_as_sequence */ |
|
387 | 0, /*tp_as_sequence */ | |
391 | 0, /*tp_as_mapping */ |
|
388 | 0, /*tp_as_mapping */ | |
392 | 0, /*tp_hash */ |
|
389 | 0, /*tp_hash */ | |
393 | 0, /*tp_call */ |
|
390 | 0, /*tp_call */ | |
394 | 0, /*tp_str */ |
|
391 | 0, /*tp_str */ | |
395 | 0, /*tp_getattro */ |
|
392 | 0, /*tp_getattro */ | |
396 | 0, /*tp_setattro */ |
|
393 | 0, /*tp_setattro */ | |
397 | 0, /*tp_as_buffer */ |
|
394 | 0, /*tp_as_buffer */ | |
398 | LAZYMANIFESTKEYSITERATOR_TPFLAGS, /* tp_flags */ |
|
395 | LAZYMANIFESTKEYSITERATOR_TPFLAGS, /* tp_flags */ | |
399 | "Keys iterator for a lazymanifest.", /* tp_doc */ |
|
396 | "Keys iterator for a lazymanifest.", /* tp_doc */ | |
400 | 0, /* tp_traverse */ |
|
397 | 0, /* tp_traverse */ | |
401 | 0, /* tp_clear */ |
|
398 | 0, /* tp_clear */ | |
402 | 0, /* tp_richcompare */ |
|
399 | 0, /* tp_richcompare */ | |
403 | 0, /* tp_weaklistoffset */ |
|
400 | 0, /* tp_weaklistoffset */ | |
404 | PyObject_SelfIter, /* tp_iter: __iter__() method */ |
|
401 | PyObject_SelfIter, /* tp_iter: __iter__() method */ | |
405 | lmiter_iterkeysnext, /* tp_iternext: next() method */ |
|
402 | lmiter_iterkeysnext, /* tp_iternext: next() method */ | |
406 | }; |
|
403 | }; | |
407 |
|
404 | |||
408 | static lazymanifest *lazymanifest_copy(lazymanifest *self); |
|
405 | static lazymanifest *lazymanifest_copy(lazymanifest *self); | |
409 |
|
406 | |||
410 | static PyObject *lazymanifest_getentriesiter(lazymanifest *self) |
|
407 | static PyObject *lazymanifest_getentriesiter(lazymanifest *self) | |
411 | { |
|
408 | { | |
412 | lmIter *i = NULL; |
|
409 | lmIter *i = NULL; | |
413 | lazymanifest *t = lazymanifest_copy(self); |
|
410 | lazymanifest *t = lazymanifest_copy(self); | |
414 | if (!t) { |
|
411 | if (!t) { | |
415 | PyErr_NoMemory(); |
|
412 | PyErr_NoMemory(); | |
416 | return NULL; |
|
413 | return NULL; | |
417 | } |
|
414 | } | |
418 | i = PyObject_New(lmIter, &lazymanifestEntriesIterator); |
|
415 | i = PyObject_New(lmIter, &lazymanifestEntriesIterator); | |
419 | if (i) { |
|
416 | if (i) { | |
420 | i->m = t; |
|
417 | i->m = t; | |
421 | i->pos = -1; |
|
418 | i->pos = -1; | |
422 | } else { |
|
419 | } else { | |
423 | Py_DECREF(t); |
|
420 | Py_DECREF(t); | |
424 | PyErr_NoMemory(); |
|
421 | PyErr_NoMemory(); | |
425 | } |
|
422 | } | |
426 | return (PyObject *)i; |
|
423 | return (PyObject *)i; | |
427 | } |
|
424 | } | |
428 |
|
425 | |||
429 | static PyObject *lazymanifest_getkeysiter(lazymanifest *self) |
|
426 | static PyObject *lazymanifest_getkeysiter(lazymanifest *self) | |
430 | { |
|
427 | { | |
431 | lmIter *i = NULL; |
|
428 | lmIter *i = NULL; | |
432 | lazymanifest *t = lazymanifest_copy(self); |
|
429 | lazymanifest *t = lazymanifest_copy(self); | |
433 | if (!t) { |
|
430 | if (!t) { | |
434 | PyErr_NoMemory(); |
|
431 | PyErr_NoMemory(); | |
435 | return NULL; |
|
432 | return NULL; | |
436 | } |
|
433 | } | |
437 | i = PyObject_New(lmIter, &lazymanifestKeysIterator); |
|
434 | i = PyObject_New(lmIter, &lazymanifestKeysIterator); | |
438 | if (i) { |
|
435 | if (i) { | |
439 | i->m = t; |
|
436 | i->m = t; | |
440 | i->pos = -1; |
|
437 | i->pos = -1; | |
441 | } else { |
|
438 | } else { | |
442 | Py_DECREF(t); |
|
439 | Py_DECREF(t); | |
443 | PyErr_NoMemory(); |
|
440 | PyErr_NoMemory(); | |
444 | } |
|
441 | } | |
445 | return (PyObject *)i; |
|
442 | return (PyObject *)i; | |
446 | } |
|
443 | } | |
447 |
|
444 | |||
448 | /* __getitem__ and __setitem__ support */ |
|
445 | /* __getitem__ and __setitem__ support */ | |
449 |
|
446 | |||
450 | static Py_ssize_t lazymanifest_size(lazymanifest *self) |
|
447 | static Py_ssize_t lazymanifest_size(lazymanifest *self) | |
451 | { |
|
448 | { | |
452 | return self->livelines; |
|
449 | return self->livelines; | |
453 | } |
|
450 | } | |
454 |
|
451 | |||
455 | static int linecmp(const void *left, const void *right) |
|
452 | static int linecmp(const void *left, const void *right) | |
456 | { |
|
453 | { | |
457 | return strcmp(((const line *)left)->start, |
|
454 | return strcmp(((const line *)left)->start, | |
458 | ((const line *)right)->start); |
|
455 | ((const line *)right)->start); | |
459 | } |
|
456 | } | |
460 |
|
457 | |||
461 | static PyObject *lazymanifest_getitem(lazymanifest *self, PyObject *key) |
|
458 | static PyObject *lazymanifest_getitem(lazymanifest *self, PyObject *key) | |
462 | { |
|
459 | { | |
463 | line needle; |
|
460 | line needle; | |
464 | line *hit; |
|
461 | line *hit; | |
465 | if (!PyBytes_Check(key)) { |
|
462 | if (!PyBytes_Check(key)) { | |
466 | PyErr_Format(PyExc_TypeError, |
|
463 | PyErr_Format(PyExc_TypeError, | |
467 | "getitem: manifest keys must be a string."); |
|
464 | "getitem: manifest keys must be a string."); | |
468 | return NULL; |
|
465 | return NULL; | |
469 | } |
|
466 | } | |
470 | needle.start = PyBytes_AsString(key); |
|
467 | needle.start = PyBytes_AsString(key); | |
471 | hit = bsearch(&needle, self->lines, self->numlines, sizeof(line), |
|
468 | hit = bsearch(&needle, self->lines, self->numlines, sizeof(line), | |
472 | &linecmp); |
|
469 | &linecmp); | |
473 | if (!hit || hit->deleted) { |
|
470 | if (!hit || hit->deleted) { | |
474 | PyErr_Format(PyExc_KeyError, "No such manifest entry."); |
|
471 | PyErr_Format(PyExc_KeyError, "No such manifest entry."); | |
475 | return NULL; |
|
472 | return NULL; | |
476 | } |
|
473 | } | |
477 | return hashflags(hit); |
|
474 | return hashflags(hit); | |
478 | } |
|
475 | } | |
479 |
|
476 | |||
480 | static int lazymanifest_delitem(lazymanifest *self, PyObject *key) |
|
477 | static int lazymanifest_delitem(lazymanifest *self, PyObject *key) | |
481 | { |
|
478 | { | |
482 | line needle; |
|
479 | line needle; | |
483 | line *hit; |
|
480 | line *hit; | |
484 | if (!PyBytes_Check(key)) { |
|
481 | if (!PyBytes_Check(key)) { | |
485 | PyErr_Format(PyExc_TypeError, |
|
482 | PyErr_Format(PyExc_TypeError, | |
486 | "delitem: manifest keys must be a string."); |
|
483 | "delitem: manifest keys must be a string."); | |
487 | return -1; |
|
484 | return -1; | |
488 | } |
|
485 | } | |
489 | needle.start = PyBytes_AsString(key); |
|
486 | needle.start = PyBytes_AsString(key); | |
490 | hit = bsearch(&needle, self->lines, self->numlines, sizeof(line), |
|
487 | hit = bsearch(&needle, self->lines, self->numlines, sizeof(line), | |
491 | &linecmp); |
|
488 | &linecmp); | |
492 | if (!hit || hit->deleted) { |
|
489 | if (!hit || hit->deleted) { | |
493 | PyErr_Format(PyExc_KeyError, |
|
490 | PyErr_Format(PyExc_KeyError, | |
494 | "Tried to delete nonexistent manifest entry."); |
|
491 | "Tried to delete nonexistent manifest entry."); | |
495 | return -1; |
|
492 | return -1; | |
496 | } |
|
493 | } | |
497 | self->dirty = true; |
|
494 | self->dirty = true; | |
498 | hit->deleted = true; |
|
495 | hit->deleted = true; | |
499 | self->livelines--; |
|
496 | self->livelines--; | |
500 | return 0; |
|
497 | return 0; | |
501 | } |
|
498 | } | |
502 |
|
499 | |||
503 | /* Do a binary search for the insertion point for new, creating the |
|
500 | /* Do a binary search for the insertion point for new, creating the | |
504 | * new entry if needed. */ |
|
501 | * new entry if needed. */ | |
505 | static int internalsetitem(lazymanifest *self, line *new) |
|
502 | static int internalsetitem(lazymanifest *self, line *new) | |
506 | { |
|
503 | { | |
507 | int start = 0, end = self->numlines; |
|
504 | int start = 0, end = self->numlines; | |
508 | while (start < end) { |
|
505 | while (start < end) { | |
509 | int pos = start + (end - start) / 2; |
|
506 | int pos = start + (end - start) / 2; | |
510 | int c = linecmp(new, self->lines + pos); |
|
507 | int c = linecmp(new, self->lines + pos); | |
511 | if (c < 0) |
|
508 | if (c < 0) | |
512 | end = pos; |
|
509 | end = pos; | |
513 | else if (c > 0) |
|
510 | else if (c > 0) | |
514 | start = pos + 1; |
|
511 | start = pos + 1; | |
515 | else { |
|
512 | else { | |
516 | if (self->lines[pos].deleted) |
|
513 | if (self->lines[pos].deleted) | |
517 | self->livelines++; |
|
514 | self->livelines++; | |
518 | if (self->lines[pos].from_malloc) |
|
515 | if (self->lines[pos].from_malloc) | |
519 | free(self->lines[pos].start); |
|
516 | free(self->lines[pos].start); | |
520 | start = pos; |
|
517 | start = pos; | |
521 | goto finish; |
|
518 | goto finish; | |
522 | } |
|
519 | } | |
523 | } |
|
520 | } | |
524 | /* being here means we need to do an insert */ |
|
521 | /* being here means we need to do an insert */ | |
525 | if (!realloc_if_full(self)) { |
|
522 | if (!realloc_if_full(self)) { | |
526 | PyErr_NoMemory(); |
|
523 | PyErr_NoMemory(); | |
527 | return -1; |
|
524 | return -1; | |
528 | } |
|
525 | } | |
529 | memmove(self->lines + start + 1, self->lines + start, |
|
526 | memmove(self->lines + start + 1, self->lines + start, | |
530 | (self->numlines - start) * sizeof(line)); |
|
527 | (self->numlines - start) * sizeof(line)); | |
531 | self->numlines++; |
|
528 | self->numlines++; | |
532 | self->livelines++; |
|
529 | self->livelines++; | |
533 | finish: |
|
530 | finish: | |
534 | self->lines[start] = *new; |
|
531 | self->lines[start] = *new; | |
535 | self->dirty = true; |
|
532 | self->dirty = true; | |
536 | return 0; |
|
533 | return 0; | |
537 | } |
|
534 | } | |
538 |
|
535 | |||
539 | static int lazymanifest_setitem( |
|
536 | static int lazymanifest_setitem( | |
540 | lazymanifest *self, PyObject *key, PyObject *value) |
|
537 | lazymanifest *self, PyObject *key, PyObject *value) | |
541 | { |
|
538 | { | |
542 | char *path; |
|
539 | char *path; | |
543 | Py_ssize_t plen; |
|
540 | Py_ssize_t plen; | |
544 | PyObject *pyhash; |
|
541 | PyObject *pyhash; | |
545 | Py_ssize_t hlen; |
|
542 | Py_ssize_t hlen; | |
546 | char *hash; |
|
543 | char *hash; | |
547 | PyObject *pyflags; |
|
544 | PyObject *pyflags; | |
548 | char *flags; |
|
545 | char *flags; | |
549 | Py_ssize_t flen; |
|
546 | Py_ssize_t flen; | |
550 | Py_ssize_t dlen; |
|
547 | Py_ssize_t dlen; | |
551 | char *dest; |
|
548 | char *dest; | |
552 | int i; |
|
549 | int i; | |
553 | line new; |
|
550 | line new; | |
554 | if (!PyBytes_Check(key)) { |
|
551 | if (!PyBytes_Check(key)) { | |
555 | PyErr_Format(PyExc_TypeError, |
|
552 | PyErr_Format(PyExc_TypeError, | |
556 | "setitem: manifest keys must be a string."); |
|
553 | "setitem: manifest keys must be a string."); | |
557 | return -1; |
|
554 | return -1; | |
558 | } |
|
555 | } | |
559 | if (!value) { |
|
556 | if (!value) { | |
560 | return lazymanifest_delitem(self, key); |
|
557 | return lazymanifest_delitem(self, key); | |
561 | } |
|
558 | } | |
562 | if (!PyTuple_Check(value) || PyTuple_Size(value) != 2) { |
|
559 | if (!PyTuple_Check(value) || PyTuple_Size(value) != 2) { | |
563 | PyErr_Format(PyExc_TypeError, |
|
560 | PyErr_Format(PyExc_TypeError, | |
564 | "Manifest values must be a tuple of (node, flags)."); |
|
561 | "Manifest values must be a tuple of (node, flags)."); | |
565 | return -1; |
|
562 | return -1; | |
566 | } |
|
563 | } | |
567 | if (PyBytes_AsStringAndSize(key, &path, &plen) == -1) { |
|
564 | if (PyBytes_AsStringAndSize(key, &path, &plen) == -1) { | |
568 | return -1; |
|
565 | return -1; | |
569 | } |
|
566 | } | |
570 |
|
567 | |||
571 | pyhash = PyTuple_GetItem(value, 0); |
|
568 | pyhash = PyTuple_GetItem(value, 0); | |
572 | if (!PyBytes_Check(pyhash)) { |
|
569 | if (!PyBytes_Check(pyhash)) { | |
573 | PyErr_Format(PyExc_TypeError, |
|
570 | PyErr_Format(PyExc_TypeError, | |
574 | "node must be a 20 or 32 bytes string"); |
|
571 | "node must be a 20 or 32 bytes string"); | |
575 | return -1; |
|
572 | return -1; | |
576 | } |
|
573 | } | |
577 | hlen = PyBytes_Size(pyhash); |
|
574 | hlen = PyBytes_Size(pyhash); | |
578 | if (hlen != 20 && hlen != 32) { |
|
575 | if (hlen != 20 && hlen != 32) { | |
579 | PyErr_Format(PyExc_TypeError, |
|
576 | PyErr_Format(PyExc_TypeError, | |
580 | "node must be a 20 or 32 bytes string"); |
|
577 | "node must be a 20 or 32 bytes string"); | |
581 | return -1; |
|
578 | return -1; | |
582 | } |
|
579 | } | |
583 | hash = PyBytes_AsString(pyhash); |
|
580 | hash = PyBytes_AsString(pyhash); | |
584 |
|
581 | |||
585 | pyflags = PyTuple_GetItem(value, 1); |
|
582 | pyflags = PyTuple_GetItem(value, 1); | |
586 | if (!PyBytes_Check(pyflags) || PyBytes_Size(pyflags) > 1) { |
|
583 | if (!PyBytes_Check(pyflags) || PyBytes_Size(pyflags) > 1) { | |
587 | PyErr_Format(PyExc_TypeError, |
|
584 | PyErr_Format(PyExc_TypeError, | |
588 | "flags must a 0 or 1 bytes string"); |
|
585 | "flags must a 0 or 1 bytes string"); | |
589 | return -1; |
|
586 | return -1; | |
590 | } |
|
587 | } | |
591 | if (PyBytes_AsStringAndSize(pyflags, &flags, &flen) == -1) { |
|
588 | if (PyBytes_AsStringAndSize(pyflags, &flags, &flen) == -1) { | |
592 | return -1; |
|
589 | return -1; | |
593 | } |
|
590 | } | |
594 | if (flen == 1) { |
|
591 | if (flen == 1) { | |
595 | switch (*flags) { |
|
592 | switch (*flags) { | |
596 | case 'l': |
|
593 | case 'l': | |
597 | case 't': |
|
594 | case 't': | |
598 | case 'x': |
|
595 | case 'x': | |
599 | break; |
|
596 | break; | |
600 | default: |
|
597 | default: | |
601 | PyErr_Format(PyExc_TypeError, "invalid manifest flag"); |
|
598 | PyErr_Format(PyExc_TypeError, "invalid manifest flag"); | |
602 | return -1; |
|
599 | return -1; | |
603 | } |
|
600 | } | |
604 | } |
|
601 | } | |
605 | /* one null byte and one newline */ |
|
602 | /* one null byte and one newline */ | |
606 | dlen = plen + hlen * 2 + 1 + flen + 1; |
|
603 | dlen = plen + hlen * 2 + 1 + flen + 1; | |
607 | dest = malloc(dlen); |
|
604 | dest = malloc(dlen); | |
608 | if (!dest) { |
|
605 | if (!dest) { | |
609 | PyErr_NoMemory(); |
|
606 | PyErr_NoMemory(); | |
610 | return -1; |
|
607 | return -1; | |
611 | } |
|
608 | } | |
612 | memcpy(dest, path, plen + 1); |
|
609 | memcpy(dest, path, plen + 1); | |
613 | for (i = 0; i < hlen; i++) { |
|
610 | for (i = 0; i < hlen; i++) { | |
614 | /* Cast to unsigned, so it will not get sign-extended when promoted |
|
611 | /* Cast to unsigned, so it will not get sign-extended when promoted | |
615 | * to int (as is done when passing to a variadic function) |
|
612 | * to int (as is done when passing to a variadic function) | |
616 | */ |
|
613 | */ | |
617 | sprintf(dest + plen + 1 + (i * 2), "%02x", (unsigned char)hash[i]); |
|
614 | sprintf(dest + plen + 1 + (i * 2), "%02x", (unsigned char)hash[i]); | |
618 | } |
|
615 | } | |
619 | memcpy(dest + plen + 2 * hlen + 1, flags, flen); |
|
616 | memcpy(dest + plen + 2 * hlen + 1, flags, flen); | |
620 | dest[plen + 2 * hlen + 1 + flen] = '\n'; |
|
617 | dest[plen + 2 * hlen + 1 + flen] = '\n'; | |
621 | new.start = dest; |
|
618 | new.start = dest; | |
622 | new.len = dlen; |
|
619 | new.len = dlen; | |
623 | new.hash_suffix = '\0'; |
|
620 | new.hash_suffix = '\0'; | |
624 | if (hlen > 20) { |
|
621 | if (hlen > 20) { | |
625 | new.hash_suffix = hash[20]; |
|
622 | new.hash_suffix = hash[20]; | |
626 | } |
|
623 | } | |
627 | new.from_malloc = true; /* is `start` a pointer we allocated? */ |
|
624 | new.from_malloc = true; /* is `start` a pointer we allocated? */ | |
628 | new.deleted = false; /* is this entry deleted? */ |
|
625 | new.deleted = false; /* is this entry deleted? */ | |
629 | if (internalsetitem(self, &new)) { |
|
626 | if (internalsetitem(self, &new)) { | |
630 | return -1; |
|
627 | return -1; | |
631 | } |
|
628 | } | |
632 | return 0; |
|
629 | return 0; | |
633 | } |
|
630 | } | |
634 |
|
631 | |||
635 | static PyMappingMethods lazymanifest_mapping_methods = { |
|
632 | static PyMappingMethods lazymanifest_mapping_methods = { | |
636 | (lenfunc)lazymanifest_size, /* mp_length */ |
|
633 | (lenfunc)lazymanifest_size, /* mp_length */ | |
637 | (binaryfunc)lazymanifest_getitem, /* mp_subscript */ |
|
634 | (binaryfunc)lazymanifest_getitem, /* mp_subscript */ | |
638 | (objobjargproc)lazymanifest_setitem, /* mp_ass_subscript */ |
|
635 | (objobjargproc)lazymanifest_setitem, /* mp_ass_subscript */ | |
639 | }; |
|
636 | }; | |
640 |
|
637 | |||
641 | /* sequence methods (important or __contains__ builds an iterator) */ |
|
638 | /* sequence methods (important or __contains__ builds an iterator) */ | |
642 |
|
639 | |||
643 | static int lazymanifest_contains(lazymanifest *self, PyObject *key) |
|
640 | static int lazymanifest_contains(lazymanifest *self, PyObject *key) | |
644 | { |
|
641 | { | |
645 | line needle; |
|
642 | line needle; | |
646 | line *hit; |
|
643 | line *hit; | |
647 | if (!PyBytes_Check(key)) { |
|
644 | if (!PyBytes_Check(key)) { | |
648 | /* Our keys are always strings, so if the contains |
|
645 | /* Our keys are always strings, so if the contains | |
649 | * check is for a non-string, just return false. */ |
|
646 | * check is for a non-string, just return false. */ | |
650 | return 0; |
|
647 | return 0; | |
651 | } |
|
648 | } | |
652 | needle.start = PyBytes_AsString(key); |
|
649 | needle.start = PyBytes_AsString(key); | |
653 | hit = bsearch(&needle, self->lines, self->numlines, sizeof(line), |
|
650 | hit = bsearch(&needle, self->lines, self->numlines, sizeof(line), | |
654 | &linecmp); |
|
651 | &linecmp); | |
655 | if (!hit || hit->deleted) { |
|
652 | if (!hit || hit->deleted) { | |
656 | return 0; |
|
653 | return 0; | |
657 | } |
|
654 | } | |
658 | return 1; |
|
655 | return 1; | |
659 | } |
|
656 | } | |
660 |
|
657 | |||
661 | static PySequenceMethods lazymanifest_seq_meths = { |
|
658 | static PySequenceMethods lazymanifest_seq_meths = { | |
662 | (lenfunc)lazymanifest_size, /* sq_length */ |
|
659 | (lenfunc)lazymanifest_size, /* sq_length */ | |
663 | 0, /* sq_concat */ |
|
660 | 0, /* sq_concat */ | |
664 | 0, /* sq_repeat */ |
|
661 | 0, /* sq_repeat */ | |
665 | 0, /* sq_item */ |
|
662 | 0, /* sq_item */ | |
666 | 0, /* sq_slice */ |
|
663 | 0, /* sq_slice */ | |
667 | 0, /* sq_ass_item */ |
|
664 | 0, /* sq_ass_item */ | |
668 | 0, /* sq_ass_slice */ |
|
665 | 0, /* sq_ass_slice */ | |
669 | (objobjproc)lazymanifest_contains, /* sq_contains */ |
|
666 | (objobjproc)lazymanifest_contains, /* sq_contains */ | |
670 | 0, /* sq_inplace_concat */ |
|
667 | 0, /* sq_inplace_concat */ | |
671 | 0, /* sq_inplace_repeat */ |
|
668 | 0, /* sq_inplace_repeat */ | |
672 | }; |
|
669 | }; | |
673 |
|
670 | |||
674 |
|
671 | |||
675 | /* Other methods (copy, diff, etc) */ |
|
672 | /* Other methods (copy, diff, etc) */ | |
676 | static PyTypeObject lazymanifestType; |
|
673 | static PyTypeObject lazymanifestType; | |
677 |
|
674 | |||
678 | /* If the manifest has changes, build the new manifest text and reindex it. */ |
|
675 | /* If the manifest has changes, build the new manifest text and reindex it. */ | |
679 | static int compact(lazymanifest *self) |
|
676 | static int compact(lazymanifest *self) | |
680 | { |
|
677 | { | |
681 | int i; |
|
678 | int i; | |
682 | ssize_t need = 0; |
|
679 | ssize_t need = 0; | |
683 | char *data; |
|
680 | char *data; | |
684 | line *src, *dst; |
|
681 | line *src, *dst; | |
685 | PyObject *pydata; |
|
682 | PyObject *pydata; | |
686 | if (!self->dirty) |
|
683 | if (!self->dirty) | |
687 | return 0; |
|
684 | return 0; | |
688 | for (i = 0; i < self->numlines; i++) { |
|
685 | for (i = 0; i < self->numlines; i++) { | |
689 | if (!self->lines[i].deleted) { |
|
686 | if (!self->lines[i].deleted) { | |
690 | need += self->lines[i].len; |
|
687 | need += self->lines[i].len; | |
691 | } |
|
688 | } | |
692 | } |
|
689 | } | |
693 | pydata = PyBytes_FromStringAndSize(NULL, need); |
|
690 | pydata = PyBytes_FromStringAndSize(NULL, need); | |
694 | if (!pydata) |
|
691 | if (!pydata) | |
695 | return -1; |
|
692 | return -1; | |
696 | data = PyBytes_AsString(pydata); |
|
693 | data = PyBytes_AsString(pydata); | |
697 | if (!data) { |
|
694 | if (!data) { | |
698 | return -1; |
|
695 | return -1; | |
699 | } |
|
696 | } | |
700 | src = self->lines; |
|
697 | src = self->lines; | |
701 | dst = self->lines; |
|
698 | dst = self->lines; | |
702 | for (i = 0; i < self->numlines; i++, src++) { |
|
699 | for (i = 0; i < self->numlines; i++, src++) { | |
703 | char *tofree = NULL; |
|
700 | char *tofree = NULL; | |
704 | if (src->from_malloc) { |
|
701 | if (src->from_malloc) { | |
705 | tofree = src->start; |
|
702 | tofree = src->start; | |
706 | } |
|
703 | } | |
707 | if (!src->deleted) { |
|
704 | if (!src->deleted) { | |
708 | memcpy(data, src->start, src->len); |
|
705 | memcpy(data, src->start, src->len); | |
709 | *dst = *src; |
|
706 | *dst = *src; | |
710 | dst->start = data; |
|
707 | dst->start = data; | |
711 | dst->from_malloc = false; |
|
708 | dst->from_malloc = false; | |
712 | data += dst->len; |
|
709 | data += dst->len; | |
713 | dst++; |
|
710 | dst++; | |
714 | } |
|
711 | } | |
715 | free(tofree); |
|
712 | free(tofree); | |
716 | } |
|
713 | } | |
717 | Py_DECREF(self->pydata); |
|
714 | Py_DECREF(self->pydata); | |
718 | self->pydata = pydata; |
|
715 | self->pydata = pydata; | |
719 | self->numlines = self->livelines; |
|
716 | self->numlines = self->livelines; | |
720 | self->dirty = false; |
|
717 | self->dirty = false; | |
721 | return 0; |
|
718 | return 0; | |
722 | } |
|
719 | } | |
723 |
|
720 | |||
724 | static PyObject *lazymanifest_text(lazymanifest *self) |
|
721 | static PyObject *lazymanifest_text(lazymanifest *self) | |
725 | { |
|
722 | { | |
726 | if (compact(self) != 0) { |
|
723 | if (compact(self) != 0) { | |
727 | PyErr_NoMemory(); |
|
724 | PyErr_NoMemory(); | |
728 | return NULL; |
|
725 | return NULL; | |
729 | } |
|
726 | } | |
730 | Py_INCREF(self->pydata); |
|
727 | Py_INCREF(self->pydata); | |
731 | return self->pydata; |
|
728 | return self->pydata; | |
732 | } |
|
729 | } | |
733 |
|
730 | |||
734 | static lazymanifest *lazymanifest_copy(lazymanifest *self) |
|
731 | static lazymanifest *lazymanifest_copy(lazymanifest *self) | |
735 | { |
|
732 | { | |
736 | lazymanifest *copy = NULL; |
|
733 | lazymanifest *copy = NULL; | |
737 | if (compact(self) != 0) { |
|
734 | if (compact(self) != 0) { | |
738 | goto nomem; |
|
735 | goto nomem; | |
739 | } |
|
736 | } | |
740 | copy = PyObject_New(lazymanifest, &lazymanifestType); |
|
737 | copy = PyObject_New(lazymanifest, &lazymanifestType); | |
741 | if (!copy) { |
|
738 | if (!copy) { | |
742 | goto nomem; |
|
739 | goto nomem; | |
743 | } |
|
740 | } | |
744 | lazymanifest_init_early(copy); |
|
741 | lazymanifest_init_early(copy); | |
745 | copy->numlines = self->numlines; |
|
742 | copy->numlines = self->numlines; | |
746 | copy->livelines = self->livelines; |
|
743 | copy->livelines = self->livelines; | |
747 | copy->dirty = false; |
|
744 | copy->dirty = false; | |
748 | copy->lines = malloc(self->maxlines *sizeof(line)); |
|
745 | copy->lines = malloc(self->maxlines *sizeof(line)); | |
749 | if (!copy->lines) { |
|
746 | if (!copy->lines) { | |
750 | goto nomem; |
|
747 | goto nomem; | |
751 | } |
|
748 | } | |
752 | memcpy(copy->lines, self->lines, self->numlines * sizeof(line)); |
|
749 | memcpy(copy->lines, self->lines, self->numlines * sizeof(line)); | |
753 | copy->maxlines = self->maxlines; |
|
750 | copy->maxlines = self->maxlines; | |
754 | copy->pydata = self->pydata; |
|
751 | copy->pydata = self->pydata; | |
755 | Py_INCREF(copy->pydata); |
|
752 | Py_INCREF(copy->pydata); | |
756 | return copy; |
|
753 | return copy; | |
757 | nomem: |
|
754 | nomem: | |
758 | PyErr_NoMemory(); |
|
755 | PyErr_NoMemory(); | |
759 | Py_XDECREF(copy); |
|
756 | Py_XDECREF(copy); | |
760 | return NULL; |
|
757 | return NULL; | |
761 | } |
|
758 | } | |
762 |
|
759 | |||
763 | static lazymanifest *lazymanifest_filtercopy( |
|
760 | static lazymanifest *lazymanifest_filtercopy( | |
764 | lazymanifest *self, PyObject *matchfn) |
|
761 | lazymanifest *self, PyObject *matchfn) | |
765 | { |
|
762 | { | |
766 | lazymanifest *copy = NULL; |
|
763 | lazymanifest *copy = NULL; | |
767 | int i; |
|
764 | int i; | |
768 | if (!PyCallable_Check(matchfn)) { |
|
765 | if (!PyCallable_Check(matchfn)) { | |
769 | PyErr_SetString(PyExc_TypeError, "matchfn must be callable"); |
|
766 | PyErr_SetString(PyExc_TypeError, "matchfn must be callable"); | |
770 | return NULL; |
|
767 | return NULL; | |
771 | } |
|
768 | } | |
772 | /* compact ourselves first to avoid double-frees later when we |
|
769 | /* compact ourselves first to avoid double-frees later when we | |
773 | * compact tmp so that it doesn't have random pointers to our |
|
770 | * compact tmp so that it doesn't have random pointers to our | |
774 | * underlying from_malloc-data (self->pydata is safe) */ |
|
771 | * underlying from_malloc-data (self->pydata is safe) */ | |
775 | if (compact(self) != 0) { |
|
772 | if (compact(self) != 0) { | |
776 | goto nomem; |
|
773 | goto nomem; | |
777 | } |
|
774 | } | |
778 | copy = PyObject_New(lazymanifest, &lazymanifestType); |
|
775 | copy = PyObject_New(lazymanifest, &lazymanifestType); | |
779 | if (!copy) { |
|
776 | if (!copy) { | |
780 | goto nomem; |
|
777 | goto nomem; | |
781 | } |
|
778 | } | |
782 | lazymanifest_init_early(copy); |
|
779 | lazymanifest_init_early(copy); | |
783 | copy->dirty = true; |
|
780 | copy->dirty = true; | |
784 | copy->lines = malloc(self->maxlines * sizeof(line)); |
|
781 | copy->lines = malloc(self->maxlines * sizeof(line)); | |
785 | if (!copy->lines) { |
|
782 | if (!copy->lines) { | |
786 | goto nomem; |
|
783 | goto nomem; | |
787 | } |
|
784 | } | |
788 | copy->maxlines = self->maxlines; |
|
785 | copy->maxlines = self->maxlines; | |
789 | copy->numlines = 0; |
|
786 | copy->numlines = 0; | |
790 | copy->pydata = self->pydata; |
|
787 | copy->pydata = self->pydata; | |
791 | Py_INCREF(copy->pydata); |
|
788 | Py_INCREF(copy->pydata); | |
792 | for (i = 0; i < self->numlines; i++) { |
|
789 | for (i = 0; i < self->numlines; i++) { | |
793 | PyObject *arglist = NULL, *result = NULL; |
|
790 | PyObject *arglist = NULL, *result = NULL; | |
794 | arglist = Py_BuildValue(PY23("(s)", "(y)"), |
|
791 | arglist = Py_BuildValue(PY23("(s)", "(y)"), | |
795 | self->lines[i].start); |
|
792 | self->lines[i].start); | |
796 | if (!arglist) { |
|
793 | if (!arglist) { | |
797 | goto bail; |
|
794 | goto bail; | |
798 | } |
|
795 | } | |
799 | result = PyObject_CallObject(matchfn, arglist); |
|
796 | result = PyObject_CallObject(matchfn, arglist); | |
800 | Py_DECREF(arglist); |
|
797 | Py_DECREF(arglist); | |
801 | /* if the callback raised an exception, just let it |
|
798 | /* if the callback raised an exception, just let it | |
802 | * through and give up */ |
|
799 | * through and give up */ | |
803 | if (!result) { |
|
800 | if (!result) { | |
804 | goto bail; |
|
801 | goto bail; | |
805 | } |
|
802 | } | |
806 | if (PyObject_IsTrue(result)) { |
|
803 | if (PyObject_IsTrue(result)) { | |
807 | assert(!(self->lines[i].from_malloc)); |
|
804 | assert(!(self->lines[i].from_malloc)); | |
808 | copy->lines[copy->numlines++] = self->lines[i]; |
|
805 | copy->lines[copy->numlines++] = self->lines[i]; | |
809 | } |
|
806 | } | |
810 | Py_DECREF(result); |
|
807 | Py_DECREF(result); | |
811 | } |
|
808 | } | |
812 | copy->livelines = copy->numlines; |
|
809 | copy->livelines = copy->numlines; | |
813 | return copy; |
|
810 | return copy; | |
814 | nomem: |
|
811 | nomem: | |
815 | PyErr_NoMemory(); |
|
812 | PyErr_NoMemory(); | |
816 | bail: |
|
813 | bail: | |
817 | Py_XDECREF(copy); |
|
814 | Py_XDECREF(copy); | |
818 | return NULL; |
|
815 | return NULL; | |
819 | } |
|
816 | } | |
820 |
|
817 | |||
821 | static PyObject *lazymanifest_diff(lazymanifest *self, PyObject *args) |
|
818 | static PyObject *lazymanifest_diff(lazymanifest *self, PyObject *args) | |
822 | { |
|
819 | { | |
823 | lazymanifest *other; |
|
820 | lazymanifest *other; | |
824 | PyObject *pyclean = NULL; |
|
821 | PyObject *pyclean = NULL; | |
825 | bool listclean; |
|
822 | bool listclean; | |
826 | PyObject *emptyTup = NULL, *ret = NULL; |
|
823 | PyObject *emptyTup = NULL, *ret = NULL; | |
827 | PyObject *es; |
|
824 | PyObject *es; | |
828 | int sneedle = 0, oneedle = 0; |
|
825 | int sneedle = 0, oneedle = 0; | |
829 | if (!PyArg_ParseTuple(args, "O!|O", &lazymanifestType, &other, &pyclean)) { |
|
826 | if (!PyArg_ParseTuple(args, "O!|O", &lazymanifestType, &other, &pyclean)) { | |
830 | return NULL; |
|
827 | return NULL; | |
831 | } |
|
828 | } | |
832 | listclean = (!pyclean) ? false : PyObject_IsTrue(pyclean); |
|
829 | listclean = (!pyclean) ? false : PyObject_IsTrue(pyclean); | |
833 | es = PyBytes_FromString(""); |
|
830 | es = PyBytes_FromString(""); | |
834 | if (!es) { |
|
831 | if (!es) { | |
835 | goto nomem; |
|
832 | goto nomem; | |
836 | } |
|
833 | } | |
837 | emptyTup = PyTuple_Pack(2, Py_None, es); |
|
834 | emptyTup = PyTuple_Pack(2, Py_None, es); | |
838 | Py_DECREF(es); |
|
835 | Py_DECREF(es); | |
839 | if (!emptyTup) { |
|
836 | if (!emptyTup) { | |
840 | goto nomem; |
|
837 | goto nomem; | |
841 | } |
|
838 | } | |
842 | ret = PyDict_New(); |
|
839 | ret = PyDict_New(); | |
843 | if (!ret) { |
|
840 | if (!ret) { | |
844 | goto nomem; |
|
841 | goto nomem; | |
845 | } |
|
842 | } | |
846 | while (sneedle != self->numlines || oneedle != other->numlines) { |
|
843 | while (sneedle != self->numlines || oneedle != other->numlines) { | |
847 | line *left = self->lines + sneedle; |
|
844 | line *left = self->lines + sneedle; | |
848 | line *right = other->lines + oneedle; |
|
845 | line *right = other->lines + oneedle; | |
849 | int result; |
|
846 | int result; | |
850 | PyObject *key; |
|
847 | PyObject *key; | |
851 | PyObject *outer; |
|
848 | PyObject *outer; | |
852 | /* If we're looking at a deleted entry and it's not |
|
849 | /* If we're looking at a deleted entry and it's not | |
853 | * the end of the manifest, just skip it. */ |
|
850 | * the end of the manifest, just skip it. */ | |
854 | if (sneedle < self->numlines && left->deleted) { |
|
851 | if (sneedle < self->numlines && left->deleted) { | |
855 | sneedle++; |
|
852 | sneedle++; | |
856 | continue; |
|
853 | continue; | |
857 | } |
|
854 | } | |
858 | if (oneedle < other->numlines && right->deleted) { |
|
855 | if (oneedle < other->numlines && right->deleted) { | |
859 | oneedle++; |
|
856 | oneedle++; | |
860 | continue; |
|
857 | continue; | |
861 | } |
|
858 | } | |
862 | /* if we're at the end of either manifest, then we |
|
859 | /* if we're at the end of either manifest, then we | |
863 | * know the remaining items are adds so we can skip |
|
860 | * know the remaining items are adds so we can skip | |
864 | * the strcmp. */ |
|
861 | * the strcmp. */ | |
865 | if (sneedle == self->numlines) { |
|
862 | if (sneedle == self->numlines) { | |
866 | result = 1; |
|
863 | result = 1; | |
867 | } else if (oneedle == other->numlines) { |
|
864 | } else if (oneedle == other->numlines) { | |
868 | result = -1; |
|
865 | result = -1; | |
869 | } else { |
|
866 | } else { | |
870 | result = linecmp(left, right); |
|
867 | result = linecmp(left, right); | |
871 | } |
|
868 | } | |
872 | key = result <= 0 ? |
|
869 | key = result <= 0 ? | |
873 | PyBytes_FromString(left->start) : |
|
870 | PyBytes_FromString(left->start) : | |
874 | PyBytes_FromString(right->start); |
|
871 | PyBytes_FromString(right->start); | |
875 | if (!key) |
|
872 | if (!key) | |
876 | goto nomem; |
|
873 | goto nomem; | |
877 | if (result < 0) { |
|
874 | if (result < 0) { | |
878 | PyObject *l = hashflags(left); |
|
875 | PyObject *l = hashflags(left); | |
879 | if (!l) { |
|
876 | if (!l) { | |
880 | goto nomem; |
|
877 | goto nomem; | |
881 | } |
|
878 | } | |
882 | outer = PyTuple_Pack(2, l, emptyTup); |
|
879 | outer = PyTuple_Pack(2, l, emptyTup); | |
883 | Py_DECREF(l); |
|
880 | Py_DECREF(l); | |
884 | if (!outer) { |
|
881 | if (!outer) { | |
885 | goto nomem; |
|
882 | goto nomem; | |
886 | } |
|
883 | } | |
887 | PyDict_SetItem(ret, key, outer); |
|
884 | PyDict_SetItem(ret, key, outer); | |
888 | Py_DECREF(outer); |
|
885 | Py_DECREF(outer); | |
889 | sneedle++; |
|
886 | sneedle++; | |
890 | } else if (result > 0) { |
|
887 | } else if (result > 0) { | |
891 | PyObject *r = hashflags(right); |
|
888 | PyObject *r = hashflags(right); | |
892 | if (!r) { |
|
889 | if (!r) { | |
893 | goto nomem; |
|
890 | goto nomem; | |
894 | } |
|
891 | } | |
895 | outer = PyTuple_Pack(2, emptyTup, r); |
|
892 | outer = PyTuple_Pack(2, emptyTup, r); | |
896 | Py_DECREF(r); |
|
893 | Py_DECREF(r); | |
897 | if (!outer) { |
|
894 | if (!outer) { | |
898 | goto nomem; |
|
895 | goto nomem; | |
899 | } |
|
896 | } | |
900 | PyDict_SetItem(ret, key, outer); |
|
897 | PyDict_SetItem(ret, key, outer); | |
901 | Py_DECREF(outer); |
|
898 | Py_DECREF(outer); | |
902 | oneedle++; |
|
899 | oneedle++; | |
903 | } else { |
|
900 | } else { | |
904 | /* file exists in both manifests */ |
|
901 | /* file exists in both manifests */ | |
905 | if (left->len != right->len |
|
902 | if (left->len != right->len | |
906 | || memcmp(left->start, right->start, left->len) |
|
903 | || memcmp(left->start, right->start, left->len) | |
907 | || left->hash_suffix != right->hash_suffix) { |
|
904 | || left->hash_suffix != right->hash_suffix) { | |
908 | PyObject *l = hashflags(left); |
|
905 | PyObject *l = hashflags(left); | |
909 | PyObject *r; |
|
906 | PyObject *r; | |
910 | if (!l) { |
|
907 | if (!l) { | |
911 | goto nomem; |
|
908 | goto nomem; | |
912 | } |
|
909 | } | |
913 | r = hashflags(right); |
|
910 | r = hashflags(right); | |
914 | if (!r) { |
|
911 | if (!r) { | |
915 | Py_DECREF(l); |
|
912 | Py_DECREF(l); | |
916 | goto nomem; |
|
913 | goto nomem; | |
917 | } |
|
914 | } | |
918 | outer = PyTuple_Pack(2, l, r); |
|
915 | outer = PyTuple_Pack(2, l, r); | |
919 | Py_DECREF(l); |
|
916 | Py_DECREF(l); | |
920 | Py_DECREF(r); |
|
917 | Py_DECREF(r); | |
921 | if (!outer) { |
|
918 | if (!outer) { | |
922 | goto nomem; |
|
919 | goto nomem; | |
923 | } |
|
920 | } | |
924 | PyDict_SetItem(ret, key, outer); |
|
921 | PyDict_SetItem(ret, key, outer); | |
925 | Py_DECREF(outer); |
|
922 | Py_DECREF(outer); | |
926 | } else if (listclean) { |
|
923 | } else if (listclean) { | |
927 | PyDict_SetItem(ret, key, Py_None); |
|
924 | PyDict_SetItem(ret, key, Py_None); | |
928 | } |
|
925 | } | |
929 | sneedle++; |
|
926 | sneedle++; | |
930 | oneedle++; |
|
927 | oneedle++; | |
931 | } |
|
928 | } | |
932 | Py_DECREF(key); |
|
929 | Py_DECREF(key); | |
933 | } |
|
930 | } | |
934 | Py_DECREF(emptyTup); |
|
931 | Py_DECREF(emptyTup); | |
935 | return ret; |
|
932 | return ret; | |
936 | nomem: |
|
933 | nomem: | |
937 | PyErr_NoMemory(); |
|
934 | PyErr_NoMemory(); | |
938 | Py_XDECREF(ret); |
|
935 | Py_XDECREF(ret); | |
939 | Py_XDECREF(emptyTup); |
|
936 | Py_XDECREF(emptyTup); | |
940 | return NULL; |
|
937 | return NULL; | |
941 | } |
|
938 | } | |
942 |
|
939 | |||
943 | static PyMethodDef lazymanifest_methods[] = { |
|
940 | static PyMethodDef lazymanifest_methods[] = { | |
944 | {"iterkeys", (PyCFunction)lazymanifest_getkeysiter, METH_NOARGS, |
|
941 | {"iterkeys", (PyCFunction)lazymanifest_getkeysiter, METH_NOARGS, | |
945 | "Iterate over file names in this lazymanifest."}, |
|
942 | "Iterate over file names in this lazymanifest."}, | |
946 | {"iterentries", (PyCFunction)lazymanifest_getentriesiter, METH_NOARGS, |
|
943 | {"iterentries", (PyCFunction)lazymanifest_getentriesiter, METH_NOARGS, | |
947 | "Iterate over (path, nodeid, flags) tuples in this lazymanifest."}, |
|
944 | "Iterate over (path, nodeid, flags) tuples in this lazymanifest."}, | |
948 | {"copy", (PyCFunction)lazymanifest_copy, METH_NOARGS, |
|
945 | {"copy", (PyCFunction)lazymanifest_copy, METH_NOARGS, | |
949 | "Make a copy of this lazymanifest."}, |
|
946 | "Make a copy of this lazymanifest."}, | |
950 | {"filtercopy", (PyCFunction)lazymanifest_filtercopy, METH_O, |
|
947 | {"filtercopy", (PyCFunction)lazymanifest_filtercopy, METH_O, | |
951 | "Make a copy of this manifest filtered by matchfn."}, |
|
948 | "Make a copy of this manifest filtered by matchfn."}, | |
952 | {"diff", (PyCFunction)lazymanifest_diff, METH_VARARGS, |
|
949 | {"diff", (PyCFunction)lazymanifest_diff, METH_VARARGS, | |
953 | "Compare this lazymanifest to another one."}, |
|
950 | "Compare this lazymanifest to another one."}, | |
954 | {"text", (PyCFunction)lazymanifest_text, METH_NOARGS, |
|
951 | {"text", (PyCFunction)lazymanifest_text, METH_NOARGS, | |
955 | "Encode this manifest to text."}, |
|
952 | "Encode this manifest to text."}, | |
956 | {NULL}, |
|
953 | {NULL}, | |
957 | }; |
|
954 | }; | |
958 |
|
955 | |||
959 | #ifdef IS_PY3K |
|
956 | #ifdef IS_PY3K | |
960 | #define LAZYMANIFEST_TPFLAGS Py_TPFLAGS_DEFAULT |
|
957 | #define LAZYMANIFEST_TPFLAGS Py_TPFLAGS_DEFAULT | |
961 | #else |
|
958 | #else | |
962 | #define LAZYMANIFEST_TPFLAGS Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_SEQUENCE_IN |
|
959 | #define LAZYMANIFEST_TPFLAGS Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_SEQUENCE_IN | |
963 | #endif |
|
960 | #endif | |
964 |
|
961 | |||
965 | static PyTypeObject lazymanifestType = { |
|
962 | static PyTypeObject lazymanifestType = { | |
966 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ |
|
963 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ | |
967 | "parsers.lazymanifest", /* tp_name */ |
|
964 | "parsers.lazymanifest", /* tp_name */ | |
968 | sizeof(lazymanifest), /* tp_basicsize */ |
|
965 | sizeof(lazymanifest), /* tp_basicsize */ | |
969 | 0, /* tp_itemsize */ |
|
966 | 0, /* tp_itemsize */ | |
970 | (destructor)lazymanifest_dealloc, /* tp_dealloc */ |
|
967 | (destructor)lazymanifest_dealloc, /* tp_dealloc */ | |
971 | 0, /* tp_print */ |
|
968 | 0, /* tp_print */ | |
972 | 0, /* tp_getattr */ |
|
969 | 0, /* tp_getattr */ | |
973 | 0, /* tp_setattr */ |
|
970 | 0, /* tp_setattr */ | |
974 | 0, /* tp_compare */ |
|
971 | 0, /* tp_compare */ | |
975 | 0, /* tp_repr */ |
|
972 | 0, /* tp_repr */ | |
976 | 0, /* tp_as_number */ |
|
973 | 0, /* tp_as_number */ | |
977 | &lazymanifest_seq_meths, /* tp_as_sequence */ |
|
974 | &lazymanifest_seq_meths, /* tp_as_sequence */ | |
978 | &lazymanifest_mapping_methods, /* tp_as_mapping */ |
|
975 | &lazymanifest_mapping_methods, /* tp_as_mapping */ | |
979 | 0, /* tp_hash */ |
|
976 | 0, /* tp_hash */ | |
980 | 0, /* tp_call */ |
|
977 | 0, /* tp_call */ | |
981 | 0, /* tp_str */ |
|
978 | 0, /* tp_str */ | |
982 | 0, /* tp_getattro */ |
|
979 | 0, /* tp_getattro */ | |
983 | 0, /* tp_setattro */ |
|
980 | 0, /* tp_setattro */ | |
984 | 0, /* tp_as_buffer */ |
|
981 | 0, /* tp_as_buffer */ | |
985 | LAZYMANIFEST_TPFLAGS, /* tp_flags */ |
|
982 | LAZYMANIFEST_TPFLAGS, /* tp_flags */ | |
986 | "TODO(augie)", /* tp_doc */ |
|
983 | "TODO(augie)", /* tp_doc */ | |
987 | 0, /* tp_traverse */ |
|
984 | 0, /* tp_traverse */ | |
988 | 0, /* tp_clear */ |
|
985 | 0, /* tp_clear */ | |
989 | 0, /* tp_richcompare */ |
|
986 | 0, /* tp_richcompare */ | |
990 | 0, /* tp_weaklistoffset */ |
|
987 | 0, /* tp_weaklistoffset */ | |
991 | (getiterfunc)lazymanifest_getkeysiter, /* tp_iter */ |
|
988 | (getiterfunc)lazymanifest_getkeysiter, /* tp_iter */ | |
992 | 0, /* tp_iternext */ |
|
989 | 0, /* tp_iternext */ | |
993 | lazymanifest_methods, /* tp_methods */ |
|
990 | lazymanifest_methods, /* tp_methods */ | |
994 | 0, /* tp_members */ |
|
991 | 0, /* tp_members */ | |
995 | 0, /* tp_getset */ |
|
992 | 0, /* tp_getset */ | |
996 | 0, /* tp_base */ |
|
993 | 0, /* tp_base */ | |
997 | 0, /* tp_dict */ |
|
994 | 0, /* tp_dict */ | |
998 | 0, /* tp_descr_get */ |
|
995 | 0, /* tp_descr_get */ | |
999 | 0, /* tp_descr_set */ |
|
996 | 0, /* tp_descr_set */ | |
1000 | 0, /* tp_dictoffset */ |
|
997 | 0, /* tp_dictoffset */ | |
1001 | (initproc)lazymanifest_init, /* tp_init */ |
|
998 | (initproc)lazymanifest_init, /* tp_init */ | |
1002 | 0, /* tp_alloc */ |
|
999 | 0, /* tp_alloc */ | |
1003 | }; |
|
1000 | }; | |
1004 |
|
1001 | |||
1005 | void manifest_module_init(PyObject * mod) |
|
1002 | void manifest_module_init(PyObject * mod) | |
1006 | { |
|
1003 | { | |
1007 | lazymanifestType.tp_new = PyType_GenericNew; |
|
1004 | lazymanifestType.tp_new = PyType_GenericNew; | |
1008 | if (PyType_Ready(&lazymanifestType) < 0) |
|
1005 | if (PyType_Ready(&lazymanifestType) < 0) | |
1009 | return; |
|
1006 | return; | |
1010 | Py_INCREF(&lazymanifestType); |
|
1007 | Py_INCREF(&lazymanifestType); | |
1011 |
|
1008 | |||
1012 | PyModule_AddObject(mod, "lazymanifest", |
|
1009 | PyModule_AddObject(mod, "lazymanifest", | |
1013 | (PyObject *)&lazymanifestType); |
|
1010 | (PyObject *)&lazymanifestType); | |
1014 | } |
|
1011 | } |
@@ -1,2959 +1,2956 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 | #define PY_SSIZE_T_CLEAN |
|
10 | #define PY_SSIZE_T_CLEAN | |
11 | #include <Python.h> |
|
11 | #include <Python.h> | |
12 | #include <assert.h> |
|
12 | #include <assert.h> | |
13 | #include <ctype.h> |
|
13 | #include <ctype.h> | |
14 | #include <limits.h> |
|
14 | #include <limits.h> | |
15 | #include <stddef.h> |
|
15 | #include <stddef.h> | |
16 | #include <stdlib.h> |
|
16 | #include <stdlib.h> | |
17 | #include <string.h> |
|
17 | #include <string.h> | |
18 |
|
18 | |||
19 | #include "bitmanipulation.h" |
|
19 | #include "bitmanipulation.h" | |
20 | #include "charencode.h" |
|
20 | #include "charencode.h" | |
21 | #include "revlog.h" |
|
21 | #include "revlog.h" | |
22 | #include "util.h" |
|
22 | #include "util.h" | |
23 |
|
23 | |||
24 | #ifdef IS_PY3K |
|
24 | #ifdef IS_PY3K | |
25 | /* The mapping of Python types is meant to be temporary to get Python |
|
25 | /* The mapping of Python types is meant to be temporary to get Python | |
26 | * 3 to compile. We should remove this once Python 3 support is fully |
|
26 | * 3 to compile. We should remove this once Python 3 support is fully | |
27 | * supported and proper types are used in the extensions themselves. */ |
|
27 | * supported and proper types are used in the extensions themselves. */ | |
28 | #define PyInt_Check PyLong_Check |
|
28 | #define PyInt_Check PyLong_Check | |
29 | #define PyInt_FromLong PyLong_FromLong |
|
29 | #define PyInt_FromLong PyLong_FromLong | |
30 | #define PyInt_FromSsize_t PyLong_FromSsize_t |
|
30 | #define PyInt_FromSsize_t PyLong_FromSsize_t | |
31 | #define PyInt_AsLong PyLong_AsLong |
|
31 | #define PyInt_AsLong PyLong_AsLong | |
32 | #endif |
|
32 | #endif | |
33 |
|
33 | |||
34 | typedef struct indexObjectStruct indexObject; |
|
34 | typedef struct indexObjectStruct indexObject; | |
35 |
|
35 | |||
36 | typedef struct { |
|
36 | typedef struct { | |
37 | int children[16]; |
|
37 | int children[16]; | |
38 | } nodetreenode; |
|
38 | } nodetreenode; | |
39 |
|
39 | |||
40 | typedef struct { |
|
40 | typedef struct { | |
41 | int abi_version; |
|
41 | int abi_version; | |
42 | Py_ssize_t (*index_length)(const indexObject *); |
|
42 | Py_ssize_t (*index_length)(const indexObject *); | |
43 | const char *(*index_node)(indexObject *, Py_ssize_t); |
|
43 | const char *(*index_node)(indexObject *, Py_ssize_t); | |
44 | int (*index_parents)(PyObject *, int, int *); |
|
44 | int (*index_parents)(PyObject *, int, int *); | |
45 | } Revlog_CAPI; |
|
45 | } Revlog_CAPI; | |
46 |
|
46 | |||
47 | /* |
|
47 | /* | |
48 | * A base-16 trie for fast node->rev mapping. |
|
48 | * A base-16 trie for fast node->rev mapping. | |
49 | * |
|
49 | * | |
50 | * Positive value is index of the next node in the trie |
|
50 | * Positive value is index of the next node in the trie | |
51 | * Negative value is a leaf: -(rev + 2) |
|
51 | * Negative value is a leaf: -(rev + 2) | |
52 | * Zero is empty |
|
52 | * Zero is empty | |
53 | */ |
|
53 | */ | |
54 | typedef struct { |
|
54 | typedef struct { | |
55 | indexObject *index; |
|
55 | indexObject *index; | |
56 | nodetreenode *nodes; |
|
56 | nodetreenode *nodes; | |
57 | unsigned length; /* # nodes in use */ |
|
57 | unsigned length; /* # nodes in use */ | |
58 | unsigned capacity; /* # nodes allocated */ |
|
58 | unsigned capacity; /* # nodes allocated */ | |
59 | int depth; /* maximum depth of tree */ |
|
59 | int depth; /* maximum depth of tree */ | |
60 | int splits; /* # splits performed */ |
|
60 | int splits; /* # splits performed */ | |
61 | } nodetree; |
|
61 | } nodetree; | |
62 |
|
62 | |||
63 | typedef struct { |
|
63 | typedef struct { | |
64 | PyObject_HEAD /* ; */ |
|
64 | PyObject_HEAD /* ; */ | |
65 | nodetree nt; |
|
65 | nodetree nt; | |
66 | } nodetreeObject; |
|
66 | } nodetreeObject; | |
67 |
|
67 | |||
68 | /* |
|
68 | /* | |
69 | * This class has two behaviors. |
|
69 | * This class has two behaviors. | |
70 | * |
|
70 | * | |
71 | * When used in a list-like way (with integer keys), we decode an |
|
71 | * When used in a list-like way (with integer keys), we decode an | |
72 | * entry in a RevlogNG index file on demand. We have limited support for |
|
72 | * entry in a RevlogNG index file on demand. We have limited support for | |
73 | * integer-keyed insert and delete, only at elements right before the |
|
73 | * integer-keyed insert and delete, only at elements right before the | |
74 | * end. |
|
74 | * end. | |
75 | * |
|
75 | * | |
76 | * With string keys, we lazily perform a reverse mapping from node to |
|
76 | * With string keys, we lazily perform a reverse mapping from node to | |
77 | * rev, using a base-16 trie. |
|
77 | * rev, using a base-16 trie. | |
78 | */ |
|
78 | */ | |
79 | struct indexObjectStruct { |
|
79 | struct indexObjectStruct { | |
80 | PyObject_HEAD |
|
80 | PyObject_HEAD | |
81 | /* Type-specific fields go here. */ |
|
81 | /* Type-specific fields go here. */ | |
82 | PyObject *data; /* raw bytes of index */ |
|
82 | PyObject *data; /* raw bytes of index */ | |
83 | Py_buffer buf; /* buffer of data */ |
|
83 | Py_buffer buf; /* buffer of data */ | |
84 | PyObject **cache; /* cached tuples */ |
|
84 | PyObject **cache; /* cached tuples */ | |
85 | const char **offsets; /* populated on demand */ |
|
85 | const char **offsets; /* populated on demand */ | |
86 | Py_ssize_t raw_length; /* original number of elements */ |
|
86 | Py_ssize_t raw_length; /* original number of elements */ | |
87 | Py_ssize_t length; /* current number of elements */ |
|
87 | Py_ssize_t length; /* current number of elements */ | |
88 | PyObject *added; /* populated on demand */ |
|
88 | PyObject *added; /* populated on demand */ | |
89 | PyObject *headrevs; /* cache, invalidated on changes */ |
|
89 | PyObject *headrevs; /* cache, invalidated on changes */ | |
90 | PyObject *filteredrevs; /* filtered revs set */ |
|
90 | PyObject *filteredrevs; /* filtered revs set */ | |
91 | nodetree nt; /* base-16 trie */ |
|
91 | nodetree nt; /* base-16 trie */ | |
92 | int ntinitialized; /* 0 or 1 */ |
|
92 | int ntinitialized; /* 0 or 1 */ | |
93 | int ntrev; /* last rev scanned */ |
|
93 | int ntrev; /* last rev scanned */ | |
94 | int ntlookups; /* # lookups */ |
|
94 | int ntlookups; /* # lookups */ | |
95 | int ntmisses; /* # lookups that miss the cache */ |
|
95 | int ntmisses; /* # lookups that miss the cache */ | |
96 | int inlined; |
|
96 | int inlined; | |
97 | }; |
|
97 | }; | |
98 |
|
98 | |||
99 | static Py_ssize_t index_length(const indexObject *self) |
|
99 | static Py_ssize_t index_length(const indexObject *self) | |
100 | { |
|
100 | { | |
101 | if (self->added == NULL) |
|
101 | if (self->added == NULL) | |
102 | return self->length; |
|
102 | return self->length; | |
103 | return self->length + PyList_GET_SIZE(self->added); |
|
103 | return self->length + PyList_GET_SIZE(self->added); | |
104 | } |
|
104 | } | |
105 |
|
105 | |||
106 | static PyObject *nullentry = NULL; |
|
106 | static PyObject *nullentry = NULL; | |
107 | static const char nullid[20] = {0}; |
|
107 | static const char nullid[20] = {0}; | |
108 | static const Py_ssize_t nullrev = -1; |
|
108 | static const Py_ssize_t nullrev = -1; | |
109 |
|
109 | |||
110 | static Py_ssize_t inline_scan(indexObject *self, const char **offsets); |
|
110 | static Py_ssize_t inline_scan(indexObject *self, const char **offsets); | |
111 |
|
111 | |||
112 | static int index_find_node(indexObject *self, const char *node, |
|
112 | static int index_find_node(indexObject *self, const char *node, | |
113 | Py_ssize_t nodelen); |
|
113 | Py_ssize_t nodelen); | |
114 |
|
114 | |||
115 | #if LONG_MAX == 0x7fffffffL |
|
115 | #if LONG_MAX == 0x7fffffffL | |
116 | static const char *const tuple_format = PY23("Kiiiiiis#", "Kiiiiiiy#"); |
|
116 | static const char *const tuple_format = PY23("Kiiiiiis#", "Kiiiiiiy#"); | |
117 | #else |
|
117 | #else | |
118 | static const char *const tuple_format = PY23("kiiiiiis#", "kiiiiiiy#"); |
|
118 | static const char *const tuple_format = PY23("kiiiiiis#", "kiiiiiiy#"); | |
119 | #endif |
|
119 | #endif | |
120 |
|
120 | |||
121 | /* A RevlogNG v1 index entry is 64 bytes long. */ |
|
121 | /* A RevlogNG v1 index entry is 64 bytes long. */ | |
122 | static const long v1_hdrsize = 64; |
|
122 | static const long v1_hdrsize = 64; | |
123 |
|
123 | |||
124 | static void raise_revlog_error(void) |
|
124 | static void raise_revlog_error(void) | |
125 | { |
|
125 | { | |
126 | PyObject *mod = NULL, *dict = NULL, *errclass = NULL; |
|
126 | PyObject *mod = NULL, *dict = NULL, *errclass = NULL; | |
127 |
|
127 | |||
128 | mod = PyImport_ImportModule("mercurial.error"); |
|
128 | mod = PyImport_ImportModule("mercurial.error"); | |
129 | if (mod == NULL) { |
|
129 | if (mod == NULL) { | |
130 | goto cleanup; |
|
130 | goto cleanup; | |
131 | } |
|
131 | } | |
132 |
|
132 | |||
133 | dict = PyModule_GetDict(mod); |
|
133 | dict = PyModule_GetDict(mod); | |
134 | if (dict == NULL) { |
|
134 | if (dict == NULL) { | |
135 | goto cleanup; |
|
135 | goto cleanup; | |
136 | } |
|
136 | } | |
137 | Py_INCREF(dict); |
|
137 | Py_INCREF(dict); | |
138 |
|
138 | |||
139 | errclass = PyDict_GetItemString(dict, "RevlogError"); |
|
139 | errclass = PyDict_GetItemString(dict, "RevlogError"); | |
140 | if (errclass == NULL) { |
|
140 | if (errclass == NULL) { | |
141 | PyErr_SetString(PyExc_SystemError, |
|
141 | PyErr_SetString(PyExc_SystemError, | |
142 | "could not find RevlogError"); |
|
142 | "could not find RevlogError"); | |
143 | goto cleanup; |
|
143 | goto cleanup; | |
144 | } |
|
144 | } | |
145 |
|
145 | |||
146 | /* value of exception is ignored by callers */ |
|
146 | /* value of exception is ignored by callers */ | |
147 | PyErr_SetString(errclass, "RevlogError"); |
|
147 | PyErr_SetString(errclass, "RevlogError"); | |
148 |
|
148 | |||
149 | cleanup: |
|
149 | cleanup: | |
150 | Py_XDECREF(dict); |
|
150 | Py_XDECREF(dict); | |
151 | Py_XDECREF(mod); |
|
151 | Py_XDECREF(mod); | |
152 | } |
|
152 | } | |
153 |
|
153 | |||
154 | /* |
|
154 | /* | |
155 | * Return a pointer to the beginning of a RevlogNG record. |
|
155 | * Return a pointer to the beginning of a RevlogNG record. | |
156 | */ |
|
156 | */ | |
157 | static const char *index_deref(indexObject *self, Py_ssize_t pos) |
|
157 | static const char *index_deref(indexObject *self, Py_ssize_t pos) | |
158 | { |
|
158 | { | |
159 | if (self->inlined && pos > 0) { |
|
159 | if (self->inlined && pos > 0) { | |
160 | if (self->offsets == NULL) { |
|
160 | if (self->offsets == NULL) { | |
161 | Py_ssize_t ret; |
|
161 | Py_ssize_t ret; | |
162 | self->offsets = PyMem_Malloc(self->raw_length * |
|
162 | self->offsets = PyMem_Malloc(self->raw_length * | |
163 | sizeof(*self->offsets)); |
|
163 | sizeof(*self->offsets)); | |
164 | if (self->offsets == NULL) |
|
164 | if (self->offsets == NULL) | |
165 | return (const char *)PyErr_NoMemory(); |
|
165 | return (const char *)PyErr_NoMemory(); | |
166 | ret = inline_scan(self, self->offsets); |
|
166 | ret = inline_scan(self, self->offsets); | |
167 | if (ret == -1) { |
|
167 | if (ret == -1) { | |
168 | return NULL; |
|
168 | return NULL; | |
169 | }; |
|
169 | }; | |
170 | } |
|
170 | } | |
171 | return self->offsets[pos]; |
|
171 | return self->offsets[pos]; | |
172 | } |
|
172 | } | |
173 |
|
173 | |||
174 | return (const char *)(self->buf.buf) + pos * v1_hdrsize; |
|
174 | return (const char *)(self->buf.buf) + pos * v1_hdrsize; | |
175 | } |
|
175 | } | |
176 |
|
176 | |||
177 | /* |
|
177 | /* | |
178 | * Get parents of the given rev. |
|
178 | * Get parents of the given rev. | |
179 | * |
|
179 | * | |
180 | * The specified rev must be valid and must not be nullrev. A returned |
|
180 | * The specified rev must be valid and must not be nullrev. A returned | |
181 | * parent revision may be nullrev, but is guaranteed to be in valid range. |
|
181 | * parent revision may be nullrev, but is guaranteed to be in valid range. | |
182 | */ |
|
182 | */ | |
183 | static inline int index_get_parents(indexObject *self, Py_ssize_t rev, int *ps, |
|
183 | static inline int index_get_parents(indexObject *self, Py_ssize_t rev, int *ps, | |
184 | int maxrev) |
|
184 | int maxrev) | |
185 | { |
|
185 | { | |
186 | if (rev >= self->length) { |
|
186 | if (rev >= self->length) { | |
187 | long tmp; |
|
187 | long tmp; | |
188 | PyObject *tuple = |
|
188 | PyObject *tuple = | |
189 | PyList_GET_ITEM(self->added, rev - self->length); |
|
189 | PyList_GET_ITEM(self->added, rev - self->length); | |
190 | if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 5), &tmp)) { |
|
190 | if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 5), &tmp)) { | |
191 | return -1; |
|
191 | return -1; | |
192 | } |
|
192 | } | |
193 | ps[0] = (int)tmp; |
|
193 | ps[0] = (int)tmp; | |
194 | if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 6), &tmp)) { |
|
194 | if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 6), &tmp)) { | |
195 | return -1; |
|
195 | return -1; | |
196 | } |
|
196 | } | |
197 | ps[1] = (int)tmp; |
|
197 | ps[1] = (int)tmp; | |
198 | } else { |
|
198 | } else { | |
199 | const char *data = index_deref(self, rev); |
|
199 | const char *data = index_deref(self, rev); | |
200 | ps[0] = getbe32(data + 24); |
|
200 | ps[0] = getbe32(data + 24); | |
201 | ps[1] = getbe32(data + 28); |
|
201 | ps[1] = getbe32(data + 28); | |
202 | } |
|
202 | } | |
203 | /* If index file is corrupted, ps[] may point to invalid revisions. So |
|
203 | /* If index file is corrupted, ps[] may point to invalid revisions. So | |
204 | * there is a risk of buffer overflow to trust them unconditionally. */ |
|
204 | * there is a risk of buffer overflow to trust them unconditionally. */ | |
205 | if (ps[0] < -1 || ps[0] > maxrev || ps[1] < -1 || ps[1] > maxrev) { |
|
205 | if (ps[0] < -1 || ps[0] > maxrev || ps[1] < -1 || ps[1] > maxrev) { | |
206 | PyErr_SetString(PyExc_ValueError, "parent out of range"); |
|
206 | PyErr_SetString(PyExc_ValueError, "parent out of range"); | |
207 | return -1; |
|
207 | return -1; | |
208 | } |
|
208 | } | |
209 | return 0; |
|
209 | return 0; | |
210 | } |
|
210 | } | |
211 |
|
211 | |||
212 | /* |
|
212 | /* | |
213 | * Get parents of the given rev. |
|
213 | * Get parents of the given rev. | |
214 | * |
|
214 | * | |
215 | * If the specified rev is out of range, IndexError will be raised. If the |
|
215 | * If the specified rev is out of range, IndexError will be raised. If the | |
216 | * revlog entry is corrupted, ValueError may be raised. |
|
216 | * revlog entry is corrupted, ValueError may be raised. | |
217 | * |
|
217 | * | |
218 | * Returns 0 on success or -1 on failure. |
|
218 | * Returns 0 on success or -1 on failure. | |
219 | */ |
|
219 | */ | |
220 | static int HgRevlogIndex_GetParents(PyObject *op, int rev, int *ps) |
|
220 | static int HgRevlogIndex_GetParents(PyObject *op, int rev, int *ps) | |
221 | { |
|
221 | { | |
222 | int tiprev; |
|
222 | int tiprev; | |
223 | if (!op || !HgRevlogIndex_Check(op) || !ps) { |
|
223 | if (!op || !HgRevlogIndex_Check(op) || !ps) { | |
224 | PyErr_BadInternalCall(); |
|
224 | PyErr_BadInternalCall(); | |
225 | return -1; |
|
225 | return -1; | |
226 | } |
|
226 | } | |
227 | tiprev = (int)index_length((indexObject *)op) - 1; |
|
227 | tiprev = (int)index_length((indexObject *)op) - 1; | |
228 | if (rev < -1 || rev > tiprev) { |
|
228 | if (rev < -1 || rev > tiprev) { | |
229 | PyErr_Format(PyExc_IndexError, "rev out of range: %d", rev); |
|
229 | PyErr_Format(PyExc_IndexError, "rev out of range: %d", rev); | |
230 | return -1; |
|
230 | return -1; | |
231 | } else if (rev == -1) { |
|
231 | } else if (rev == -1) { | |
232 | ps[0] = ps[1] = -1; |
|
232 | ps[0] = ps[1] = -1; | |
233 | return 0; |
|
233 | return 0; | |
234 | } else { |
|
234 | } else { | |
235 | return index_get_parents((indexObject *)op, rev, ps, tiprev); |
|
235 | return index_get_parents((indexObject *)op, rev, ps, tiprev); | |
236 | } |
|
236 | } | |
237 | } |
|
237 | } | |
238 |
|
238 | |||
239 | static inline int64_t index_get_start(indexObject *self, Py_ssize_t rev) |
|
239 | static inline int64_t index_get_start(indexObject *self, Py_ssize_t rev) | |
240 | { |
|
240 | { | |
241 | uint64_t offset; |
|
241 | uint64_t offset; | |
242 | if (rev == nullrev) { |
|
242 | if (rev == nullrev) { | |
243 | return 0; |
|
243 | return 0; | |
244 | } |
|
244 | } | |
245 | if (rev >= self->length) { |
|
245 | if (rev >= self->length) { | |
246 | PyObject *tuple; |
|
246 | PyObject *tuple; | |
247 | PyObject *pylong; |
|
247 | PyObject *pylong; | |
248 | PY_LONG_LONG tmp; |
|
248 | PY_LONG_LONG tmp; | |
249 | tuple = PyList_GET_ITEM(self->added, rev - self->length); |
|
249 | tuple = PyList_GET_ITEM(self->added, rev - self->length); | |
250 | pylong = PyTuple_GET_ITEM(tuple, 0); |
|
250 | pylong = PyTuple_GET_ITEM(tuple, 0); | |
251 | tmp = PyLong_AsLongLong(pylong); |
|
251 | tmp = PyLong_AsLongLong(pylong); | |
252 | if (tmp == -1 && PyErr_Occurred()) { |
|
252 | if (tmp == -1 && PyErr_Occurred()) { | |
253 | return -1; |
|
253 | return -1; | |
254 | } |
|
254 | } | |
255 | if (tmp < 0) { |
|
255 | if (tmp < 0) { | |
256 | PyErr_Format(PyExc_OverflowError, |
|
256 | PyErr_Format(PyExc_OverflowError, | |
257 | "revlog entry size out of bound (%lld)", |
|
257 | "revlog entry size out of bound (%lld)", | |
258 | (long long)tmp); |
|
258 | (long long)tmp); | |
259 | return -1; |
|
259 | return -1; | |
260 | } |
|
260 | } | |
261 | offset = (uint64_t)tmp; |
|
261 | offset = (uint64_t)tmp; | |
262 | } else { |
|
262 | } else { | |
263 | const char *data = index_deref(self, rev); |
|
263 | const char *data = index_deref(self, rev); | |
264 | offset = getbe32(data + 4); |
|
264 | offset = getbe32(data + 4); | |
265 | if (rev == 0) { |
|
265 | if (rev == 0) { | |
266 | /* mask out version number for the first entry */ |
|
266 | /* mask out version number for the first entry */ | |
267 | offset &= 0xFFFF; |
|
267 | offset &= 0xFFFF; | |
268 | } else { |
|
268 | } else { | |
269 | uint32_t offset_high = getbe32(data); |
|
269 | uint32_t offset_high = getbe32(data); | |
270 | offset |= ((uint64_t)offset_high) << 32; |
|
270 | offset |= ((uint64_t)offset_high) << 32; | |
271 | } |
|
271 | } | |
272 | } |
|
272 | } | |
273 | return (int64_t)(offset >> 16); |
|
273 | return (int64_t)(offset >> 16); | |
274 | } |
|
274 | } | |
275 |
|
275 | |||
276 | static inline int index_get_length(indexObject *self, Py_ssize_t rev) |
|
276 | static inline int index_get_length(indexObject *self, Py_ssize_t rev) | |
277 | { |
|
277 | { | |
278 | if (rev == nullrev) { |
|
278 | if (rev == nullrev) { | |
279 | return 0; |
|
279 | return 0; | |
280 | } |
|
280 | } | |
281 | if (rev >= self->length) { |
|
281 | if (rev >= self->length) { | |
282 | PyObject *tuple; |
|
282 | PyObject *tuple; | |
283 | PyObject *pylong; |
|
283 | PyObject *pylong; | |
284 | long ret; |
|
284 | long ret; | |
285 | tuple = PyList_GET_ITEM(self->added, rev - self->length); |
|
285 | tuple = PyList_GET_ITEM(self->added, rev - self->length); | |
286 | pylong = PyTuple_GET_ITEM(tuple, 1); |
|
286 | pylong = PyTuple_GET_ITEM(tuple, 1); | |
287 | ret = PyInt_AsLong(pylong); |
|
287 | ret = PyInt_AsLong(pylong); | |
288 | if (ret == -1 && PyErr_Occurred()) { |
|
288 | if (ret == -1 && PyErr_Occurred()) { | |
289 | return -1; |
|
289 | return -1; | |
290 | } |
|
290 | } | |
291 | if (ret < 0 || ret > (long)INT_MAX) { |
|
291 | if (ret < 0 || ret > (long)INT_MAX) { | |
292 | PyErr_Format(PyExc_OverflowError, |
|
292 | PyErr_Format(PyExc_OverflowError, | |
293 | "revlog entry size out of bound (%ld)", |
|
293 | "revlog entry size out of bound (%ld)", | |
294 | ret); |
|
294 | ret); | |
295 | return -1; |
|
295 | return -1; | |
296 | } |
|
296 | } | |
297 | return (int)ret; |
|
297 | return (int)ret; | |
298 | } else { |
|
298 | } else { | |
299 | const char *data = index_deref(self, rev); |
|
299 | const char *data = index_deref(self, rev); | |
300 | int tmp = (int)getbe32(data + 8); |
|
300 | int tmp = (int)getbe32(data + 8); | |
301 | if (tmp < 0) { |
|
301 | if (tmp < 0) { | |
302 | PyErr_Format(PyExc_OverflowError, |
|
302 | PyErr_Format(PyExc_OverflowError, | |
303 | "revlog entry size out of bound (%d)", |
|
303 | "revlog entry size out of bound (%d)", | |
304 | tmp); |
|
304 | tmp); | |
305 | return -1; |
|
305 | return -1; | |
306 | } |
|
306 | } | |
307 | return tmp; |
|
307 | return tmp; | |
308 | } |
|
308 | } | |
309 | } |
|
309 | } | |
310 |
|
310 | |||
311 | /* |
|
311 | /* | |
312 | * RevlogNG format (all in big endian, data may be inlined): |
|
312 | * RevlogNG format (all in big endian, data may be inlined): | |
313 | * 6 bytes: offset |
|
313 | * 6 bytes: offset | |
314 | * 2 bytes: flags |
|
314 | * 2 bytes: flags | |
315 | * 4 bytes: compressed length |
|
315 | * 4 bytes: compressed length | |
316 | * 4 bytes: uncompressed length |
|
316 | * 4 bytes: uncompressed length | |
317 | * 4 bytes: base revision |
|
317 | * 4 bytes: base revision | |
318 | * 4 bytes: link revision |
|
318 | * 4 bytes: link revision | |
319 | * 4 bytes: parent 1 revision |
|
319 | * 4 bytes: parent 1 revision | |
320 | * 4 bytes: parent 2 revision |
|
320 | * 4 bytes: parent 2 revision | |
321 | * 32 bytes: nodeid (only 20 bytes used) |
|
321 | * 32 bytes: nodeid (only 20 bytes used) | |
322 | */ |
|
322 | */ | |
323 | static PyObject *index_get(indexObject *self, Py_ssize_t pos) |
|
323 | static PyObject *index_get(indexObject *self, Py_ssize_t pos) | |
324 | { |
|
324 | { | |
325 | uint64_t offset_flags; |
|
325 | uint64_t offset_flags; | |
326 | int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2; |
|
326 | int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2; | |
327 | const char *c_node_id; |
|
327 | const char *c_node_id; | |
328 | const char *data; |
|
328 | const char *data; | |
329 | Py_ssize_t length = index_length(self); |
|
329 | Py_ssize_t length = index_length(self); | |
330 | PyObject *entry; |
|
330 | PyObject *entry; | |
331 |
|
331 | |||
332 | if (pos == nullrev) { |
|
332 | if (pos == nullrev) { | |
333 | Py_INCREF(nullentry); |
|
333 | Py_INCREF(nullentry); | |
334 | return nullentry; |
|
334 | return nullentry; | |
335 | } |
|
335 | } | |
336 |
|
336 | |||
337 | if (pos < 0 || pos >= length) { |
|
337 | if (pos < 0 || pos >= length) { | |
338 | PyErr_SetString(PyExc_IndexError, "revlog index out of range"); |
|
338 | PyErr_SetString(PyExc_IndexError, "revlog index out of range"); | |
339 | return NULL; |
|
339 | return NULL; | |
340 | } |
|
340 | } | |
341 |
|
341 | |||
342 | if (pos >= self->length) { |
|
342 | if (pos >= self->length) { | |
343 | PyObject *obj; |
|
343 | PyObject *obj; | |
344 | obj = PyList_GET_ITEM(self->added, pos - self->length); |
|
344 | obj = PyList_GET_ITEM(self->added, pos - self->length); | |
345 | Py_INCREF(obj); |
|
345 | Py_INCREF(obj); | |
346 | return obj; |
|
346 | return obj; | |
347 | } |
|
347 | } | |
348 |
|
348 | |||
349 | if (self->cache) { |
|
349 | if (self->cache) { | |
350 | if (self->cache[pos]) { |
|
350 | if (self->cache[pos]) { | |
351 | Py_INCREF(self->cache[pos]); |
|
351 | Py_INCREF(self->cache[pos]); | |
352 | return self->cache[pos]; |
|
352 | return self->cache[pos]; | |
353 | } |
|
353 | } | |
354 | } else { |
|
354 | } else { | |
355 | self->cache = calloc(self->raw_length, sizeof(PyObject *)); |
|
355 | self->cache = calloc(self->raw_length, sizeof(PyObject *)); | |
356 | if (self->cache == NULL) |
|
356 | if (self->cache == NULL) | |
357 | return PyErr_NoMemory(); |
|
357 | return PyErr_NoMemory(); | |
358 | } |
|
358 | } | |
359 |
|
359 | |||
360 | data = index_deref(self, pos); |
|
360 | data = index_deref(self, pos); | |
361 | if (data == NULL) |
|
361 | if (data == NULL) | |
362 | return NULL; |
|
362 | return NULL; | |
363 |
|
363 | |||
364 | offset_flags = getbe32(data + 4); |
|
364 | offset_flags = getbe32(data + 4); | |
365 | if (pos == 0) /* mask out version number for the first entry */ |
|
365 | if (pos == 0) /* mask out version number for the first entry */ | |
366 | offset_flags &= 0xFFFF; |
|
366 | offset_flags &= 0xFFFF; | |
367 | else { |
|
367 | else { | |
368 | uint32_t offset_high = getbe32(data); |
|
368 | uint32_t offset_high = getbe32(data); | |
369 | offset_flags |= ((uint64_t)offset_high) << 32; |
|
369 | offset_flags |= ((uint64_t)offset_high) << 32; | |
370 | } |
|
370 | } | |
371 |
|
371 | |||
372 | comp_len = getbe32(data + 8); |
|
372 | comp_len = getbe32(data + 8); | |
373 | uncomp_len = getbe32(data + 12); |
|
373 | uncomp_len = getbe32(data + 12); | |
374 | base_rev = getbe32(data + 16); |
|
374 | base_rev = getbe32(data + 16); | |
375 | link_rev = getbe32(data + 20); |
|
375 | link_rev = getbe32(data + 20); | |
376 | parent_1 = getbe32(data + 24); |
|
376 | parent_1 = getbe32(data + 24); | |
377 | parent_2 = getbe32(data + 28); |
|
377 | parent_2 = getbe32(data + 28); | |
378 | c_node_id = data + 32; |
|
378 | c_node_id = data + 32; | |
379 |
|
379 | |||
380 | entry = Py_BuildValue(tuple_format, offset_flags, comp_len, uncomp_len, |
|
380 | entry = Py_BuildValue(tuple_format, offset_flags, comp_len, uncomp_len, | |
381 | base_rev, link_rev, parent_1, parent_2, c_node_id, |
|
381 | base_rev, link_rev, parent_1, parent_2, c_node_id, | |
382 | (Py_ssize_t)20); |
|
382 | (Py_ssize_t)20); | |
383 |
|
383 | |||
384 | if (entry) { |
|
384 | if (entry) { | |
385 | PyObject_GC_UnTrack(entry); |
|
385 | PyObject_GC_UnTrack(entry); | |
386 | Py_INCREF(entry); |
|
386 | Py_INCREF(entry); | |
387 | } |
|
387 | } | |
388 |
|
388 | |||
389 | self->cache[pos] = entry; |
|
389 | self->cache[pos] = entry; | |
390 |
|
390 | |||
391 | return entry; |
|
391 | return entry; | |
392 | } |
|
392 | } | |
393 |
|
393 | |||
394 | /* |
|
394 | /* | |
395 | * Return the 20-byte SHA of the node corresponding to the given rev. |
|
395 | * Return the 20-byte SHA of the node corresponding to the given rev. | |
396 | */ |
|
396 | */ | |
397 | static const char *index_node(indexObject *self, Py_ssize_t pos) |
|
397 | static const char *index_node(indexObject *self, Py_ssize_t pos) | |
398 | { |
|
398 | { | |
399 | Py_ssize_t length = index_length(self); |
|
399 | Py_ssize_t length = index_length(self); | |
400 | const char *data; |
|
400 | const char *data; | |
401 |
|
401 | |||
402 | if (pos == nullrev) |
|
402 | if (pos == nullrev) | |
403 | return nullid; |
|
403 | return nullid; | |
404 |
|
404 | |||
405 | if (pos >= length) |
|
405 | if (pos >= length) | |
406 | return NULL; |
|
406 | return NULL; | |
407 |
|
407 | |||
408 | if (pos >= self->length) { |
|
408 | if (pos >= self->length) { | |
409 | PyObject *tuple, *str; |
|
409 | PyObject *tuple, *str; | |
410 | tuple = PyList_GET_ITEM(self->added, pos - self->length); |
|
410 | tuple = PyList_GET_ITEM(self->added, pos - self->length); | |
411 | str = PyTuple_GetItem(tuple, 7); |
|
411 | str = PyTuple_GetItem(tuple, 7); | |
412 | return str ? PyBytes_AS_STRING(str) : NULL; |
|
412 | return str ? PyBytes_AS_STRING(str) : NULL; | |
413 | } |
|
413 | } | |
414 |
|
414 | |||
415 | data = index_deref(self, pos); |
|
415 | data = index_deref(self, pos); | |
416 | return data ? data + 32 : NULL; |
|
416 | return data ? data + 32 : NULL; | |
417 | } |
|
417 | } | |
418 |
|
418 | |||
419 | /* |
|
419 | /* | |
420 | * Return the 20-byte SHA of the node corresponding to the given rev. The |
|
420 | * Return the 20-byte SHA of the node corresponding to the given rev. The | |
421 | * rev is assumed to be existing. If not, an exception is set. |
|
421 | * rev is assumed to be existing. If not, an exception is set. | |
422 | */ |
|
422 | */ | |
423 | static const char *index_node_existing(indexObject *self, Py_ssize_t pos) |
|
423 | static const char *index_node_existing(indexObject *self, Py_ssize_t pos) | |
424 | { |
|
424 | { | |
425 | const char *node = index_node(self, pos); |
|
425 | const char *node = index_node(self, pos); | |
426 | if (node == NULL) { |
|
426 | if (node == NULL) { | |
427 | PyErr_Format(PyExc_IndexError, "could not access rev %d", |
|
427 | PyErr_Format(PyExc_IndexError, "could not access rev %d", | |
428 | (int)pos); |
|
428 | (int)pos); | |
429 | } |
|
429 | } | |
430 | return node; |
|
430 | return node; | |
431 | } |
|
431 | } | |
432 |
|
432 | |||
433 | static int nt_insert(nodetree *self, const char *node, int rev); |
|
433 | static int nt_insert(nodetree *self, const char *node, int rev); | |
434 |
|
434 | |||
435 | static int node_check(PyObject *obj, char **node) |
|
435 | static int node_check(PyObject *obj, char **node) | |
436 | { |
|
436 | { | |
437 | Py_ssize_t nodelen; |
|
437 | Py_ssize_t nodelen; | |
438 | if (PyBytes_AsStringAndSize(obj, node, &nodelen) == -1) |
|
438 | if (PyBytes_AsStringAndSize(obj, node, &nodelen) == -1) | |
439 | return -1; |
|
439 | return -1; | |
440 | if (nodelen == 20) |
|
440 | if (nodelen == 20) | |
441 | return 0; |
|
441 | return 0; | |
442 | PyErr_SetString(PyExc_ValueError, "20-byte hash required"); |
|
442 | PyErr_SetString(PyExc_ValueError, "20-byte hash required"); | |
443 | return -1; |
|
443 | return -1; | |
444 | } |
|
444 | } | |
445 |
|
445 | |||
446 | static PyObject *index_append(indexObject *self, PyObject *obj) |
|
446 | static PyObject *index_append(indexObject *self, PyObject *obj) | |
447 | { |
|
447 | { | |
448 | char *node; |
|
448 | char *node; | |
449 | Py_ssize_t len; |
|
449 | Py_ssize_t len; | |
450 |
|
450 | |||
451 | if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) { |
|
451 | if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) { | |
452 | PyErr_SetString(PyExc_TypeError, "8-tuple required"); |
|
452 | PyErr_SetString(PyExc_TypeError, "8-tuple required"); | |
453 | return NULL; |
|
453 | return NULL; | |
454 | } |
|
454 | } | |
455 |
|
455 | |||
456 | if (node_check(PyTuple_GET_ITEM(obj, 7), &node) == -1) |
|
456 | if (node_check(PyTuple_GET_ITEM(obj, 7), &node) == -1) | |
457 | return NULL; |
|
457 | return NULL; | |
458 |
|
458 | |||
459 | len = index_length(self); |
|
459 | len = index_length(self); | |
460 |
|
460 | |||
461 | if (self->added == NULL) { |
|
461 | if (self->added == NULL) { | |
462 | self->added = PyList_New(0); |
|
462 | self->added = PyList_New(0); | |
463 | if (self->added == NULL) |
|
463 | if (self->added == NULL) | |
464 | return NULL; |
|
464 | return NULL; | |
465 | } |
|
465 | } | |
466 |
|
466 | |||
467 | if (PyList_Append(self->added, obj) == -1) |
|
467 | if (PyList_Append(self->added, obj) == -1) | |
468 | return NULL; |
|
468 | return NULL; | |
469 |
|
469 | |||
470 | if (self->ntinitialized) |
|
470 | if (self->ntinitialized) | |
471 | nt_insert(&self->nt, node, (int)len); |
|
471 | nt_insert(&self->nt, node, (int)len); | |
472 |
|
472 | |||
473 | Py_CLEAR(self->headrevs); |
|
473 | Py_CLEAR(self->headrevs); | |
474 | Py_RETURN_NONE; |
|
474 | Py_RETURN_NONE; | |
475 | } |
|
475 | } | |
476 |
|
476 | |||
477 | static PyObject *index_stats(indexObject *self) |
|
477 | static PyObject *index_stats(indexObject *self) | |
478 | { |
|
478 | { | |
479 | PyObject *obj = PyDict_New(); |
|
479 | PyObject *obj = PyDict_New(); | |
480 | PyObject *s = NULL; |
|
480 | PyObject *s = NULL; | |
481 | PyObject *t = NULL; |
|
481 | PyObject *t = NULL; | |
482 |
|
482 | |||
483 | if (obj == NULL) |
|
483 | if (obj == NULL) | |
484 | return NULL; |
|
484 | return NULL; | |
485 |
|
485 | |||
486 | #define istat(__n, __d) \ |
|
486 | #define istat(__n, __d) \ | |
487 | do { \ |
|
487 | do { \ | |
488 | s = PyBytes_FromString(__d); \ |
|
488 | s = PyBytes_FromString(__d); \ | |
489 | t = PyInt_FromSsize_t(self->__n); \ |
|
489 | t = PyInt_FromSsize_t(self->__n); \ | |
490 | if (!s || !t) \ |
|
490 | if (!s || !t) \ | |
491 | goto bail; \ |
|
491 | goto bail; \ | |
492 | if (PyDict_SetItem(obj, s, t) == -1) \ |
|
492 | if (PyDict_SetItem(obj, s, t) == -1) \ | |
493 | goto bail; \ |
|
493 | goto bail; \ | |
494 | Py_CLEAR(s); \ |
|
494 | Py_CLEAR(s); \ | |
495 | Py_CLEAR(t); \ |
|
495 | Py_CLEAR(t); \ | |
496 | } while (0) |
|
496 | } while (0) | |
497 |
|
497 | |||
498 | if (self->added) { |
|
498 | if (self->added) { | |
499 | Py_ssize_t len = PyList_GET_SIZE(self->added); |
|
499 | Py_ssize_t len = PyList_GET_SIZE(self->added); | |
500 | s = PyBytes_FromString("index entries added"); |
|
500 | s = PyBytes_FromString("index entries added"); | |
501 | t = PyInt_FromSsize_t(len); |
|
501 | t = PyInt_FromSsize_t(len); | |
502 | if (!s || !t) |
|
502 | if (!s || !t) | |
503 | goto bail; |
|
503 | goto bail; | |
504 | if (PyDict_SetItem(obj, s, t) == -1) |
|
504 | if (PyDict_SetItem(obj, s, t) == -1) | |
505 | goto bail; |
|
505 | goto bail; | |
506 | Py_CLEAR(s); |
|
506 | Py_CLEAR(s); | |
507 | Py_CLEAR(t); |
|
507 | Py_CLEAR(t); | |
508 | } |
|
508 | } | |
509 |
|
509 | |||
510 | if (self->raw_length != self->length) |
|
510 | if (self->raw_length != self->length) | |
511 | istat(raw_length, "revs on disk"); |
|
511 | istat(raw_length, "revs on disk"); | |
512 | istat(length, "revs in memory"); |
|
512 | istat(length, "revs in memory"); | |
513 | istat(ntlookups, "node trie lookups"); |
|
513 | istat(ntlookups, "node trie lookups"); | |
514 | istat(ntmisses, "node trie misses"); |
|
514 | istat(ntmisses, "node trie misses"); | |
515 | istat(ntrev, "node trie last rev scanned"); |
|
515 | istat(ntrev, "node trie last rev scanned"); | |
516 | if (self->ntinitialized) { |
|
516 | if (self->ntinitialized) { | |
517 | istat(nt.capacity, "node trie capacity"); |
|
517 | istat(nt.capacity, "node trie capacity"); | |
518 | istat(nt.depth, "node trie depth"); |
|
518 | istat(nt.depth, "node trie depth"); | |
519 | istat(nt.length, "node trie count"); |
|
519 | istat(nt.length, "node trie count"); | |
520 | istat(nt.splits, "node trie splits"); |
|
520 | istat(nt.splits, "node trie splits"); | |
521 | } |
|
521 | } | |
522 |
|
522 | |||
523 | #undef istat |
|
523 | #undef istat | |
524 |
|
524 | |||
525 | return obj; |
|
525 | return obj; | |
526 |
|
526 | |||
527 | bail: |
|
527 | bail: | |
528 | Py_XDECREF(obj); |
|
528 | Py_XDECREF(obj); | |
529 | Py_XDECREF(s); |
|
529 | Py_XDECREF(s); | |
530 | Py_XDECREF(t); |
|
530 | Py_XDECREF(t); | |
531 | return NULL; |
|
531 | return NULL; | |
532 | } |
|
532 | } | |
533 |
|
533 | |||
534 | /* |
|
534 | /* | |
535 | * When we cache a list, we want to be sure the caller can't mutate |
|
535 | * When we cache a list, we want to be sure the caller can't mutate | |
536 | * the cached copy. |
|
536 | * the cached copy. | |
537 | */ |
|
537 | */ | |
538 | static PyObject *list_copy(PyObject *list) |
|
538 | static PyObject *list_copy(PyObject *list) | |
539 | { |
|
539 | { | |
540 | Py_ssize_t len = PyList_GET_SIZE(list); |
|
540 | Py_ssize_t len = PyList_GET_SIZE(list); | |
541 | PyObject *newlist = PyList_New(len); |
|
541 | PyObject *newlist = PyList_New(len); | |
542 | Py_ssize_t i; |
|
542 | Py_ssize_t i; | |
543 |
|
543 | |||
544 | if (newlist == NULL) |
|
544 | if (newlist == NULL) | |
545 | return NULL; |
|
545 | return NULL; | |
546 |
|
546 | |||
547 | for (i = 0; i < len; i++) { |
|
547 | for (i = 0; i < len; i++) { | |
548 | PyObject *obj = PyList_GET_ITEM(list, i); |
|
548 | PyObject *obj = PyList_GET_ITEM(list, i); | |
549 | Py_INCREF(obj); |
|
549 | Py_INCREF(obj); | |
550 | PyList_SET_ITEM(newlist, i, obj); |
|
550 | PyList_SET_ITEM(newlist, i, obj); | |
551 | } |
|
551 | } | |
552 |
|
552 | |||
553 | return newlist; |
|
553 | return newlist; | |
554 | } |
|
554 | } | |
555 |
|
555 | |||
556 | static int check_filter(PyObject *filter, Py_ssize_t arg) |
|
556 | static int check_filter(PyObject *filter, Py_ssize_t arg) | |
557 | { |
|
557 | { | |
558 | if (filter) { |
|
558 | if (filter) { | |
559 | PyObject *arglist, *result; |
|
559 | PyObject *arglist, *result; | |
560 | int isfiltered; |
|
560 | int isfiltered; | |
561 |
|
561 | |||
562 | arglist = Py_BuildValue("(n)", arg); |
|
562 | arglist = Py_BuildValue("(n)", arg); | |
563 | if (!arglist) { |
|
563 | if (!arglist) { | |
564 | return -1; |
|
564 | return -1; | |
565 | } |
|
565 | } | |
566 |
|
566 | |||
567 | result = PyEval_CallObject(filter, arglist); |
|
567 | result = PyEval_CallObject(filter, arglist); | |
568 | Py_DECREF(arglist); |
|
568 | Py_DECREF(arglist); | |
569 | if (!result) { |
|
569 | if (!result) { | |
570 | return -1; |
|
570 | return -1; | |
571 | } |
|
571 | } | |
572 |
|
572 | |||
573 | /* PyObject_IsTrue returns 1 if true, 0 if false, -1 if error, |
|
573 | /* PyObject_IsTrue returns 1 if true, 0 if false, -1 if error, | |
574 | * same as this function, so we can just return it directly.*/ |
|
574 | * same as this function, so we can just return it directly.*/ | |
575 | isfiltered = PyObject_IsTrue(result); |
|
575 | isfiltered = PyObject_IsTrue(result); | |
576 | Py_DECREF(result); |
|
576 | Py_DECREF(result); | |
577 | return isfiltered; |
|
577 | return isfiltered; | |
578 | } else { |
|
578 | } else { | |
579 | return 0; |
|
579 | return 0; | |
580 | } |
|
580 | } | |
581 | } |
|
581 | } | |
582 |
|
582 | |||
583 | static inline void set_phase_from_parents(char *phases, int parent_1, |
|
583 | static inline void set_phase_from_parents(char *phases, int parent_1, | |
584 | int parent_2, Py_ssize_t i) |
|
584 | int parent_2, Py_ssize_t i) | |
585 | { |
|
585 | { | |
586 | if (parent_1 >= 0 && phases[parent_1] > phases[i]) |
|
586 | if (parent_1 >= 0 && phases[parent_1] > phases[i]) | |
587 | phases[i] = phases[parent_1]; |
|
587 | phases[i] = phases[parent_1]; | |
588 | if (parent_2 >= 0 && phases[parent_2] > phases[i]) |
|
588 | if (parent_2 >= 0 && phases[parent_2] > phases[i]) | |
589 | phases[i] = phases[parent_2]; |
|
589 | phases[i] = phases[parent_2]; | |
590 | } |
|
590 | } | |
591 |
|
591 | |||
592 | static PyObject *reachableroots2(indexObject *self, PyObject *args) |
|
592 | static PyObject *reachableroots2(indexObject *self, PyObject *args) | |
593 | { |
|
593 | { | |
594 |
|
594 | |||
595 | /* Input */ |
|
595 | /* Input */ | |
596 | long minroot; |
|
596 | long minroot; | |
597 | PyObject *includepatharg = NULL; |
|
597 | PyObject *includepatharg = NULL; | |
598 | int includepath = 0; |
|
598 | int includepath = 0; | |
599 | /* heads and roots are lists */ |
|
599 | /* heads and roots are lists */ | |
600 | PyObject *heads = NULL; |
|
600 | PyObject *heads = NULL; | |
601 | PyObject *roots = NULL; |
|
601 | PyObject *roots = NULL; | |
602 | PyObject *reachable = NULL; |
|
602 | PyObject *reachable = NULL; | |
603 |
|
603 | |||
604 | PyObject *val; |
|
604 | PyObject *val; | |
605 | Py_ssize_t len = index_length(self); |
|
605 | Py_ssize_t len = index_length(self); | |
606 | long revnum; |
|
606 | long revnum; | |
607 | Py_ssize_t k; |
|
607 | Py_ssize_t k; | |
608 | Py_ssize_t i; |
|
608 | Py_ssize_t i; | |
609 | Py_ssize_t l; |
|
609 | Py_ssize_t l; | |
610 | int r; |
|
610 | int r; | |
611 | int parents[2]; |
|
611 | int parents[2]; | |
612 |
|
612 | |||
613 | /* Internal data structure: |
|
613 | /* Internal data structure: | |
614 | * tovisit: array of length len+1 (all revs + nullrev), filled upto |
|
614 | * tovisit: array of length len+1 (all revs + nullrev), filled upto | |
615 | * lentovisit |
|
615 | * lentovisit | |
616 | * |
|
616 | * | |
617 | * revstates: array of length len+1 (all revs + nullrev) */ |
|
617 | * revstates: array of length len+1 (all revs + nullrev) */ | |
618 | int *tovisit = NULL; |
|
618 | int *tovisit = NULL; | |
619 | long lentovisit = 0; |
|
619 | long lentovisit = 0; | |
620 | enum { RS_SEEN = 1, RS_ROOT = 2, RS_REACHABLE = 4 }; |
|
620 | enum { RS_SEEN = 1, RS_ROOT = 2, RS_REACHABLE = 4 }; | |
621 | char *revstates = NULL; |
|
621 | char *revstates = NULL; | |
622 |
|
622 | |||
623 | /* Get arguments */ |
|
623 | /* Get arguments */ | |
624 | if (!PyArg_ParseTuple(args, "lO!O!O!", &minroot, &PyList_Type, &heads, |
|
624 | if (!PyArg_ParseTuple(args, "lO!O!O!", &minroot, &PyList_Type, &heads, | |
625 | &PyList_Type, &roots, &PyBool_Type, |
|
625 | &PyList_Type, &roots, &PyBool_Type, | |
626 | &includepatharg)) |
|
626 | &includepatharg)) | |
627 | goto bail; |
|
627 | goto bail; | |
628 |
|
628 | |||
629 | if (includepatharg == Py_True) |
|
629 | if (includepatharg == Py_True) | |
630 | includepath = 1; |
|
630 | includepath = 1; | |
631 |
|
631 | |||
632 | /* Initialize return set */ |
|
632 | /* Initialize return set */ | |
633 | reachable = PyList_New(0); |
|
633 | reachable = PyList_New(0); | |
634 | if (reachable == NULL) |
|
634 | if (reachable == NULL) | |
635 | goto bail; |
|
635 | goto bail; | |
636 |
|
636 | |||
637 | /* Initialize internal datastructures */ |
|
637 | /* Initialize internal datastructures */ | |
638 | tovisit = (int *)malloc((len + 1) * sizeof(int)); |
|
638 | tovisit = (int *)malloc((len + 1) * sizeof(int)); | |
639 | if (tovisit == NULL) { |
|
639 | if (tovisit == NULL) { | |
640 | PyErr_NoMemory(); |
|
640 | PyErr_NoMemory(); | |
641 | goto bail; |
|
641 | goto bail; | |
642 | } |
|
642 | } | |
643 |
|
643 | |||
644 | revstates = (char *)calloc(len + 1, 1); |
|
644 | revstates = (char *)calloc(len + 1, 1); | |
645 | if (revstates == NULL) { |
|
645 | if (revstates == NULL) { | |
646 | PyErr_NoMemory(); |
|
646 | PyErr_NoMemory(); | |
647 | goto bail; |
|
647 | goto bail; | |
648 | } |
|
648 | } | |
649 |
|
649 | |||
650 | l = PyList_GET_SIZE(roots); |
|
650 | l = PyList_GET_SIZE(roots); | |
651 | for (i = 0; i < l; i++) { |
|
651 | for (i = 0; i < l; i++) { | |
652 | revnum = PyInt_AsLong(PyList_GET_ITEM(roots, i)); |
|
652 | revnum = PyInt_AsLong(PyList_GET_ITEM(roots, i)); | |
653 | if (revnum == -1 && PyErr_Occurred()) |
|
653 | if (revnum == -1 && PyErr_Occurred()) | |
654 | goto bail; |
|
654 | goto bail; | |
655 | /* If root is out of range, e.g. wdir(), it must be unreachable |
|
655 | /* If root is out of range, e.g. wdir(), it must be unreachable | |
656 | * from heads. So we can just ignore it. */ |
|
656 | * from heads. So we can just ignore it. */ | |
657 | if (revnum + 1 < 0 || revnum + 1 >= len + 1) |
|
657 | if (revnum + 1 < 0 || revnum + 1 >= len + 1) | |
658 | continue; |
|
658 | continue; | |
659 | revstates[revnum + 1] |= RS_ROOT; |
|
659 | revstates[revnum + 1] |= RS_ROOT; | |
660 | } |
|
660 | } | |
661 |
|
661 | |||
662 | /* Populate tovisit with all the heads */ |
|
662 | /* Populate tovisit with all the heads */ | |
663 | l = PyList_GET_SIZE(heads); |
|
663 | l = PyList_GET_SIZE(heads); | |
664 | for (i = 0; i < l; i++) { |
|
664 | for (i = 0; i < l; i++) { | |
665 | revnum = PyInt_AsLong(PyList_GET_ITEM(heads, i)); |
|
665 | revnum = PyInt_AsLong(PyList_GET_ITEM(heads, i)); | |
666 | if (revnum == -1 && PyErr_Occurred()) |
|
666 | if (revnum == -1 && PyErr_Occurred()) | |
667 | goto bail; |
|
667 | goto bail; | |
668 | if (revnum + 1 < 0 || revnum + 1 >= len + 1) { |
|
668 | if (revnum + 1 < 0 || revnum + 1 >= len + 1) { | |
669 | PyErr_SetString(PyExc_IndexError, "head out of range"); |
|
669 | PyErr_SetString(PyExc_IndexError, "head out of range"); | |
670 | goto bail; |
|
670 | goto bail; | |
671 | } |
|
671 | } | |
672 | if (!(revstates[revnum + 1] & RS_SEEN)) { |
|
672 | if (!(revstates[revnum + 1] & RS_SEEN)) { | |
673 | tovisit[lentovisit++] = (int)revnum; |
|
673 | tovisit[lentovisit++] = (int)revnum; | |
674 | revstates[revnum + 1] |= RS_SEEN; |
|
674 | revstates[revnum + 1] |= RS_SEEN; | |
675 | } |
|
675 | } | |
676 | } |
|
676 | } | |
677 |
|
677 | |||
678 | /* Visit the tovisit list and find the reachable roots */ |
|
678 | /* Visit the tovisit list and find the reachable roots */ | |
679 | k = 0; |
|
679 | k = 0; | |
680 | while (k < lentovisit) { |
|
680 | while (k < lentovisit) { | |
681 | /* Add the node to reachable if it is a root*/ |
|
681 | /* Add the node to reachable if it is a root*/ | |
682 | revnum = tovisit[k++]; |
|
682 | revnum = tovisit[k++]; | |
683 | if (revstates[revnum + 1] & RS_ROOT) { |
|
683 | if (revstates[revnum + 1] & RS_ROOT) { | |
684 | revstates[revnum + 1] |= RS_REACHABLE; |
|
684 | revstates[revnum + 1] |= RS_REACHABLE; | |
685 | val = PyInt_FromLong(revnum); |
|
685 | val = PyInt_FromLong(revnum); | |
686 | if (val == NULL) |
|
686 | if (val == NULL) | |
687 | goto bail; |
|
687 | goto bail; | |
688 | r = PyList_Append(reachable, val); |
|
688 | r = PyList_Append(reachable, val); | |
689 | Py_DECREF(val); |
|
689 | Py_DECREF(val); | |
690 | if (r < 0) |
|
690 | if (r < 0) | |
691 | goto bail; |
|
691 | goto bail; | |
692 | if (includepath == 0) |
|
692 | if (includepath == 0) | |
693 | continue; |
|
693 | continue; | |
694 | } |
|
694 | } | |
695 |
|
695 | |||
696 | /* Add its parents to the list of nodes to visit */ |
|
696 | /* Add its parents to the list of nodes to visit */ | |
697 | if (revnum == nullrev) |
|
697 | if (revnum == nullrev) | |
698 | continue; |
|
698 | continue; | |
699 | r = index_get_parents(self, revnum, parents, (int)len - 1); |
|
699 | r = index_get_parents(self, revnum, parents, (int)len - 1); | |
700 | if (r < 0) |
|
700 | if (r < 0) | |
701 | goto bail; |
|
701 | goto bail; | |
702 | for (i = 0; i < 2; i++) { |
|
702 | for (i = 0; i < 2; i++) { | |
703 | if (!(revstates[parents[i] + 1] & RS_SEEN) && |
|
703 | if (!(revstates[parents[i] + 1] & RS_SEEN) && | |
704 | parents[i] >= minroot) { |
|
704 | parents[i] >= minroot) { | |
705 | tovisit[lentovisit++] = parents[i]; |
|
705 | tovisit[lentovisit++] = parents[i]; | |
706 | revstates[parents[i] + 1] |= RS_SEEN; |
|
706 | revstates[parents[i] + 1] |= RS_SEEN; | |
707 | } |
|
707 | } | |
708 | } |
|
708 | } | |
709 | } |
|
709 | } | |
710 |
|
710 | |||
711 | /* Find all the nodes in between the roots we found and the heads |
|
711 | /* Find all the nodes in between the roots we found and the heads | |
712 | * and add them to the reachable set */ |
|
712 | * and add them to the reachable set */ | |
713 | if (includepath == 1) { |
|
713 | if (includepath == 1) { | |
714 | long minidx = minroot; |
|
714 | long minidx = minroot; | |
715 | if (minidx < 0) |
|
715 | if (minidx < 0) | |
716 | minidx = 0; |
|
716 | minidx = 0; | |
717 | for (i = minidx; i < len; i++) { |
|
717 | for (i = minidx; i < len; i++) { | |
718 | if (!(revstates[i + 1] & RS_SEEN)) |
|
718 | if (!(revstates[i + 1] & RS_SEEN)) | |
719 | continue; |
|
719 | continue; | |
720 | r = index_get_parents(self, i, parents, (int)len - 1); |
|
720 | r = index_get_parents(self, i, parents, (int)len - 1); | |
721 | /* Corrupted index file, error is set from |
|
721 | /* Corrupted index file, error is set from | |
722 | * index_get_parents */ |
|
722 | * index_get_parents */ | |
723 | if (r < 0) |
|
723 | if (r < 0) | |
724 | goto bail; |
|
724 | goto bail; | |
725 | if (((revstates[parents[0] + 1] | |
|
725 | if (((revstates[parents[0] + 1] | | |
726 | revstates[parents[1] + 1]) & |
|
726 | revstates[parents[1] + 1]) & | |
727 | RS_REACHABLE) && |
|
727 | RS_REACHABLE) && | |
728 | !(revstates[i + 1] & RS_REACHABLE)) { |
|
728 | !(revstates[i + 1] & RS_REACHABLE)) { | |
729 | revstates[i + 1] |= RS_REACHABLE; |
|
729 | revstates[i + 1] |= RS_REACHABLE; | |
730 | val = PyInt_FromSsize_t(i); |
|
730 | val = PyInt_FromSsize_t(i); | |
731 | if (val == NULL) |
|
731 | if (val == NULL) | |
732 | goto bail; |
|
732 | goto bail; | |
733 | r = PyList_Append(reachable, val); |
|
733 | r = PyList_Append(reachable, val); | |
734 | Py_DECREF(val); |
|
734 | Py_DECREF(val); | |
735 | if (r < 0) |
|
735 | if (r < 0) | |
736 | goto bail; |
|
736 | goto bail; | |
737 | } |
|
737 | } | |
738 | } |
|
738 | } | |
739 | } |
|
739 | } | |
740 |
|
740 | |||
741 | free(revstates); |
|
741 | free(revstates); | |
742 | free(tovisit); |
|
742 | free(tovisit); | |
743 | return reachable; |
|
743 | return reachable; | |
744 | bail: |
|
744 | bail: | |
745 | Py_XDECREF(reachable); |
|
745 | Py_XDECREF(reachable); | |
746 | free(revstates); |
|
746 | free(revstates); | |
747 | free(tovisit); |
|
747 | free(tovisit); | |
748 | return NULL; |
|
748 | return NULL; | |
749 | } |
|
749 | } | |
750 |
|
750 | |||
751 | static int add_roots_get_min(indexObject *self, PyObject *roots, char *phases, |
|
751 | static int add_roots_get_min(indexObject *self, PyObject *roots, char *phases, | |
752 | char phase) |
|
752 | char phase) | |
753 | { |
|
753 | { | |
754 | Py_ssize_t len = index_length(self); |
|
754 | Py_ssize_t len = index_length(self); | |
755 | PyObject *iter; |
|
|||
756 | PyObject *item; |
|
755 | PyObject *item; | |
757 | PyObject *iterator; |
|
756 | PyObject *iterator; | |
758 | int rev, minrev = -1; |
|
757 | int rev, minrev = -1; | |
759 | char *node; |
|
758 | char *node; | |
760 |
|
759 | |||
761 | if (!PySet_Check(roots)) |
|
760 | if (!PySet_Check(roots)) | |
762 | return -2; |
|
761 | return -2; | |
763 | iterator = PyObject_GetIter(roots); |
|
762 | iterator = PyObject_GetIter(roots); | |
764 | if (iterator == NULL) |
|
763 | if (iterator == NULL) | |
765 | return -2; |
|
764 | return -2; | |
766 | while ((item = PyIter_Next(iterator))) { |
|
765 | while ((item = PyIter_Next(iterator))) { | |
767 | if (node_check(item, &node) == -1) |
|
766 | if (node_check(item, &node) == -1) | |
768 | goto failed; |
|
767 | goto failed; | |
769 | rev = index_find_node(self, node, 20); |
|
768 | rev = index_find_node(self, node, 20); | |
770 | /* null is implicitly public, so negative is invalid */ |
|
769 | /* null is implicitly public, so negative is invalid */ | |
771 | if (rev < 0 || rev >= len) |
|
770 | if (rev < 0 || rev >= len) | |
772 | goto failed; |
|
771 | goto failed; | |
773 | phases[rev] = phase; |
|
772 | phases[rev] = phase; | |
774 | if (minrev == -1 || minrev > rev) |
|
773 | if (minrev == -1 || minrev > rev) | |
775 | minrev = rev; |
|
774 | minrev = rev; | |
776 | Py_DECREF(item); |
|
775 | Py_DECREF(item); | |
777 | } |
|
776 | } | |
778 | Py_DECREF(iterator); |
|
777 | Py_DECREF(iterator); | |
779 | return minrev; |
|
778 | return minrev; | |
780 | failed: |
|
779 | failed: | |
781 | Py_DECREF(iterator); |
|
780 | Py_DECREF(iterator); | |
782 | Py_DECREF(item); |
|
781 | Py_DECREF(item); | |
783 | return -2; |
|
782 | return -2; | |
784 | } |
|
783 | } | |
785 |
|
784 | |||
786 | static PyObject *compute_phases_map_sets(indexObject *self, PyObject *args) |
|
785 | static PyObject *compute_phases_map_sets(indexObject *self, PyObject *args) | |
787 | { |
|
786 | { | |
788 | /* 0: public (untracked), 1: draft, 2: secret, 32: archive, |
|
787 | /* 0: public (untracked), 1: draft, 2: secret, 32: archive, | |
789 | 96: internal */ |
|
788 | 96: internal */ | |
790 | static const char trackedphases[] = {1, 2, 32, 96}; |
|
789 | static const char trackedphases[] = {1, 2, 32, 96}; | |
791 | PyObject *ret = NULL; |
|
790 | PyObject *ret = NULL; | |
792 | PyObject *roots = Py_None; |
|
791 | PyObject *roots = Py_None; | |
793 | PyObject *idx = NULL; |
|
|||
794 | PyObject *pyphase = NULL; |
|
792 | PyObject *pyphase = NULL; | |
795 | PyObject *pyrev = NULL; |
|
793 | PyObject *pyrev = NULL; | |
796 | PyObject *phaseroots = NULL; |
|
794 | PyObject *phaseroots = NULL; | |
797 | PyObject *phasessize = NULL; |
|
795 | PyObject *phasessize = NULL; | |
798 | PyObject *phasesets[4] = {NULL, NULL, NULL, NULL}; |
|
796 | PyObject *phasesets[4] = {NULL, NULL, NULL, NULL}; | |
799 | Py_ssize_t len = index_length(self); |
|
797 | Py_ssize_t len = index_length(self); | |
800 | const char *currentphase; |
|
|||
801 | char *phases = NULL; |
|
798 | char *phases = NULL; | |
802 | int minphaserev = -1, rev, i; |
|
799 | int minphaserev = -1, rev, i; | |
803 | const int numphases = (int)(sizeof(phasesets) / sizeof(phasesets[0])); |
|
800 | const int numphases = (int)(sizeof(phasesets) / sizeof(phasesets[0])); | |
804 |
|
801 | |||
805 | if (!PyArg_ParseTuple(args, "O", &roots)) |
|
802 | if (!PyArg_ParseTuple(args, "O", &roots)) | |
806 | return NULL; |
|
803 | return NULL; | |
807 | if (roots == NULL || !PyDict_Check(roots)) { |
|
804 | if (roots == NULL || !PyDict_Check(roots)) { | |
808 | PyErr_SetString(PyExc_TypeError, "roots must be a dictionary"); |
|
805 | PyErr_SetString(PyExc_TypeError, "roots must be a dictionary"); | |
809 | return NULL; |
|
806 | return NULL; | |
810 | } |
|
807 | } | |
811 |
|
808 | |||
812 | phases = calloc(len, 1); |
|
809 | phases = calloc(len, 1); | |
813 | if (phases == NULL) { |
|
810 | if (phases == NULL) { | |
814 | PyErr_NoMemory(); |
|
811 | PyErr_NoMemory(); | |
815 | return NULL; |
|
812 | return NULL; | |
816 | } |
|
813 | } | |
817 |
|
814 | |||
818 | for (i = 0; i < numphases; ++i) { |
|
815 | for (i = 0; i < numphases; ++i) { | |
819 | pyphase = PyInt_FromLong(trackedphases[i]); |
|
816 | pyphase = PyInt_FromLong(trackedphases[i]); | |
820 | if (pyphase == NULL) |
|
817 | if (pyphase == NULL) | |
821 | goto release; |
|
818 | goto release; | |
822 | phaseroots = PyDict_GetItem(roots, pyphase); |
|
819 | phaseroots = PyDict_GetItem(roots, pyphase); | |
823 | Py_DECREF(pyphase); |
|
820 | Py_DECREF(pyphase); | |
824 | if (phaseroots == NULL) |
|
821 | if (phaseroots == NULL) | |
825 | continue; |
|
822 | continue; | |
826 | rev = add_roots_get_min(self, phaseroots, phases, trackedphases[i]); |
|
823 | rev = add_roots_get_min(self, phaseroots, phases, trackedphases[i]); | |
827 | phaseroots = NULL; |
|
824 | phaseroots = NULL; | |
828 | if (rev == -2) |
|
825 | if (rev == -2) | |
829 | goto release; |
|
826 | goto release; | |
830 | if (rev != -1 && (minphaserev == -1 || rev < minphaserev)) |
|
827 | if (rev != -1 && (minphaserev == -1 || rev < minphaserev)) | |
831 | minphaserev = rev; |
|
828 | minphaserev = rev; | |
832 | } |
|
829 | } | |
833 |
|
830 | |||
834 | for (i = 0; i < numphases; ++i) { |
|
831 | for (i = 0; i < numphases; ++i) { | |
835 | phasesets[i] = PySet_New(NULL); |
|
832 | phasesets[i] = PySet_New(NULL); | |
836 | if (phasesets[i] == NULL) |
|
833 | if (phasesets[i] == NULL) | |
837 | goto release; |
|
834 | goto release; | |
838 | } |
|
835 | } | |
839 |
|
836 | |||
840 | if (minphaserev == -1) |
|
837 | if (minphaserev == -1) | |
841 | minphaserev = len; |
|
838 | minphaserev = len; | |
842 | for (rev = minphaserev; rev < len; ++rev) { |
|
839 | for (rev = minphaserev; rev < len; ++rev) { | |
843 | int parents[2]; |
|
840 | int parents[2]; | |
844 | /* |
|
841 | /* | |
845 | * The parent lookup could be skipped for phaseroots, but |
|
842 | * The parent lookup could be skipped for phaseroots, but | |
846 | * phase --force would historically not recompute them |
|
843 | * phase --force would historically not recompute them | |
847 | * correctly, leaving descendents with a lower phase around. |
|
844 | * correctly, leaving descendents with a lower phase around. | |
848 | * As such, unconditionally recompute the phase. |
|
845 | * As such, unconditionally recompute the phase. | |
849 | */ |
|
846 | */ | |
850 | if (index_get_parents(self, rev, parents, (int)len - 1) < 0) |
|
847 | if (index_get_parents(self, rev, parents, (int)len - 1) < 0) | |
851 | goto release; |
|
848 | goto release; | |
852 | set_phase_from_parents(phases, parents[0], parents[1], rev); |
|
849 | set_phase_from_parents(phases, parents[0], parents[1], rev); | |
853 | switch (phases[rev]) { |
|
850 | switch (phases[rev]) { | |
854 | case 0: |
|
851 | case 0: | |
855 | continue; |
|
852 | continue; | |
856 | case 1: |
|
853 | case 1: | |
857 | pyphase = phasesets[0]; |
|
854 | pyphase = phasesets[0]; | |
858 | break; |
|
855 | break; | |
859 | case 2: |
|
856 | case 2: | |
860 | pyphase = phasesets[1]; |
|
857 | pyphase = phasesets[1]; | |
861 | break; |
|
858 | break; | |
862 | case 32: |
|
859 | case 32: | |
863 | pyphase = phasesets[2]; |
|
860 | pyphase = phasesets[2]; | |
864 | break; |
|
861 | break; | |
865 | case 96: |
|
862 | case 96: | |
866 | pyphase = phasesets[3]; |
|
863 | pyphase = phasesets[3]; | |
867 | break; |
|
864 | break; | |
868 | default: |
|
865 | default: | |
869 | goto release; |
|
866 | goto release; | |
870 | } |
|
867 | } | |
871 | pyrev = PyInt_FromLong(rev); |
|
868 | pyrev = PyInt_FromLong(rev); | |
872 | if (pyrev == NULL) |
|
869 | if (pyrev == NULL) | |
873 | goto release; |
|
870 | goto release; | |
874 | if (PySet_Add(pyphase, pyrev) == -1) { |
|
871 | if (PySet_Add(pyphase, pyrev) == -1) { | |
875 | Py_DECREF(pyrev); |
|
872 | Py_DECREF(pyrev); | |
876 | goto release; |
|
873 | goto release; | |
877 | } |
|
874 | } | |
878 | Py_DECREF(pyrev); |
|
875 | Py_DECREF(pyrev); | |
879 | } |
|
876 | } | |
880 | phaseroots = _dict_new_presized(numphases); |
|
877 | phaseroots = _dict_new_presized(numphases); | |
881 | if (phaseroots == NULL) |
|
878 | if (phaseroots == NULL) | |
882 | goto release; |
|
879 | goto release; | |
883 |
for ( |
|
880 | for (i = 0; i < numphases; ++i) { | |
884 | pyphase = PyInt_FromLong(trackedphases[i]); |
|
881 | pyphase = PyInt_FromLong(trackedphases[i]); | |
885 | if (pyphase == NULL) |
|
882 | if (pyphase == NULL) | |
886 | goto release; |
|
883 | goto release; | |
887 | if (PyDict_SetItem(phaseroots, pyphase, phasesets[i]) == -1) { |
|
884 | if (PyDict_SetItem(phaseroots, pyphase, phasesets[i]) == -1) { | |
888 | Py_DECREF(pyphase); |
|
885 | Py_DECREF(pyphase); | |
889 | goto release; |
|
886 | goto release; | |
890 | } |
|
887 | } | |
891 | Py_DECREF(phasesets[i]); |
|
888 | Py_DECREF(phasesets[i]); | |
892 | phasesets[i] = NULL; |
|
889 | phasesets[i] = NULL; | |
893 | } |
|
890 | } | |
894 | phasessize = PyInt_FromSsize_t(len); |
|
891 | phasessize = PyInt_FromSsize_t(len); | |
895 | if (phasessize == NULL) |
|
892 | if (phasessize == NULL) | |
896 | goto release; |
|
893 | goto release; | |
897 |
|
894 | |||
898 | ret = PyTuple_Pack(2, phasessize, phaseroots); |
|
895 | ret = PyTuple_Pack(2, phasessize, phaseroots); | |
899 | Py_DECREF(phasessize); |
|
896 | Py_DECREF(phasessize); | |
900 | Py_DECREF(phaseroots); |
|
897 | Py_DECREF(phaseroots); | |
901 | return ret; |
|
898 | return ret; | |
902 |
|
899 | |||
903 | release: |
|
900 | release: | |
904 | for (i = 0; i < numphases; ++i) |
|
901 | for (i = 0; i < numphases; ++i) | |
905 | Py_XDECREF(phasesets[i]); |
|
902 | Py_XDECREF(phasesets[i]); | |
906 | Py_XDECREF(phaseroots); |
|
903 | Py_XDECREF(phaseroots); | |
907 |
|
904 | |||
908 | free(phases); |
|
905 | free(phases); | |
909 | return NULL; |
|
906 | return NULL; | |
910 | } |
|
907 | } | |
911 |
|
908 | |||
912 | static PyObject *index_headrevs(indexObject *self, PyObject *args) |
|
909 | static PyObject *index_headrevs(indexObject *self, PyObject *args) | |
913 | { |
|
910 | { | |
914 | Py_ssize_t i, j, len; |
|
911 | Py_ssize_t i, j, len; | |
915 | char *nothead = NULL; |
|
912 | char *nothead = NULL; | |
916 | PyObject *heads = NULL; |
|
913 | PyObject *heads = NULL; | |
917 | PyObject *filter = NULL; |
|
914 | PyObject *filter = NULL; | |
918 | PyObject *filteredrevs = Py_None; |
|
915 | PyObject *filteredrevs = Py_None; | |
919 |
|
916 | |||
920 | if (!PyArg_ParseTuple(args, "|O", &filteredrevs)) { |
|
917 | if (!PyArg_ParseTuple(args, "|O", &filteredrevs)) { | |
921 | return NULL; |
|
918 | return NULL; | |
922 | } |
|
919 | } | |
923 |
|
920 | |||
924 | if (self->headrevs && filteredrevs == self->filteredrevs) |
|
921 | if (self->headrevs && filteredrevs == self->filteredrevs) | |
925 | return list_copy(self->headrevs); |
|
922 | return list_copy(self->headrevs); | |
926 |
|
923 | |||
927 | Py_DECREF(self->filteredrevs); |
|
924 | Py_DECREF(self->filteredrevs); | |
928 | self->filteredrevs = filteredrevs; |
|
925 | self->filteredrevs = filteredrevs; | |
929 | Py_INCREF(filteredrevs); |
|
926 | Py_INCREF(filteredrevs); | |
930 |
|
927 | |||
931 | if (filteredrevs != Py_None) { |
|
928 | if (filteredrevs != Py_None) { | |
932 | filter = PyObject_GetAttrString(filteredrevs, "__contains__"); |
|
929 | filter = PyObject_GetAttrString(filteredrevs, "__contains__"); | |
933 | if (!filter) { |
|
930 | if (!filter) { | |
934 | PyErr_SetString( |
|
931 | PyErr_SetString( | |
935 | PyExc_TypeError, |
|
932 | PyExc_TypeError, | |
936 | "filteredrevs has no attribute __contains__"); |
|
933 | "filteredrevs has no attribute __contains__"); | |
937 | goto bail; |
|
934 | goto bail; | |
938 | } |
|
935 | } | |
939 | } |
|
936 | } | |
940 |
|
937 | |||
941 | len = index_length(self); |
|
938 | len = index_length(self); | |
942 | heads = PyList_New(0); |
|
939 | heads = PyList_New(0); | |
943 | if (heads == NULL) |
|
940 | if (heads == NULL) | |
944 | goto bail; |
|
941 | goto bail; | |
945 | if (len == 0) { |
|
942 | if (len == 0) { | |
946 | PyObject *nullid = PyInt_FromLong(-1); |
|
943 | PyObject *nullid = PyInt_FromLong(-1); | |
947 | if (nullid == NULL || PyList_Append(heads, nullid) == -1) { |
|
944 | if (nullid == NULL || PyList_Append(heads, nullid) == -1) { | |
948 | Py_XDECREF(nullid); |
|
945 | Py_XDECREF(nullid); | |
949 | goto bail; |
|
946 | goto bail; | |
950 | } |
|
947 | } | |
951 | goto done; |
|
948 | goto done; | |
952 | } |
|
949 | } | |
953 |
|
950 | |||
954 | nothead = calloc(len, 1); |
|
951 | nothead = calloc(len, 1); | |
955 | if (nothead == NULL) { |
|
952 | if (nothead == NULL) { | |
956 | PyErr_NoMemory(); |
|
953 | PyErr_NoMemory(); | |
957 | goto bail; |
|
954 | goto bail; | |
958 | } |
|
955 | } | |
959 |
|
956 | |||
960 | for (i = len - 1; i >= 0; i--) { |
|
957 | for (i = len - 1; i >= 0; i--) { | |
961 | int isfiltered; |
|
958 | int isfiltered; | |
962 | int parents[2]; |
|
959 | int parents[2]; | |
963 |
|
960 | |||
964 | /* If nothead[i] == 1, it means we've seen an unfiltered child |
|
961 | /* If nothead[i] == 1, it means we've seen an unfiltered child | |
965 | * of this node already, and therefore this node is not |
|
962 | * of this node already, and therefore this node is not | |
966 | * filtered. So we can skip the expensive check_filter step. |
|
963 | * filtered. So we can skip the expensive check_filter step. | |
967 | */ |
|
964 | */ | |
968 | if (nothead[i] != 1) { |
|
965 | if (nothead[i] != 1) { | |
969 | isfiltered = check_filter(filter, i); |
|
966 | isfiltered = check_filter(filter, i); | |
970 | if (isfiltered == -1) { |
|
967 | if (isfiltered == -1) { | |
971 | PyErr_SetString(PyExc_TypeError, |
|
968 | PyErr_SetString(PyExc_TypeError, | |
972 | "unable to check filter"); |
|
969 | "unable to check filter"); | |
973 | goto bail; |
|
970 | goto bail; | |
974 | } |
|
971 | } | |
975 |
|
972 | |||
976 | if (isfiltered) { |
|
973 | if (isfiltered) { | |
977 | nothead[i] = 1; |
|
974 | nothead[i] = 1; | |
978 | continue; |
|
975 | continue; | |
979 | } |
|
976 | } | |
980 | } |
|
977 | } | |
981 |
|
978 | |||
982 | if (index_get_parents(self, i, parents, (int)len - 1) < 0) |
|
979 | if (index_get_parents(self, i, parents, (int)len - 1) < 0) | |
983 | goto bail; |
|
980 | goto bail; | |
984 | for (j = 0; j < 2; j++) { |
|
981 | for (j = 0; j < 2; j++) { | |
985 | if (parents[j] >= 0) |
|
982 | if (parents[j] >= 0) | |
986 | nothead[parents[j]] = 1; |
|
983 | nothead[parents[j]] = 1; | |
987 | } |
|
984 | } | |
988 | } |
|
985 | } | |
989 |
|
986 | |||
990 | for (i = 0; i < len; i++) { |
|
987 | for (i = 0; i < len; i++) { | |
991 | PyObject *head; |
|
988 | PyObject *head; | |
992 |
|
989 | |||
993 | if (nothead[i]) |
|
990 | if (nothead[i]) | |
994 | continue; |
|
991 | continue; | |
995 | head = PyInt_FromSsize_t(i); |
|
992 | head = PyInt_FromSsize_t(i); | |
996 | if (head == NULL || PyList_Append(heads, head) == -1) { |
|
993 | if (head == NULL || PyList_Append(heads, head) == -1) { | |
997 | Py_XDECREF(head); |
|
994 | Py_XDECREF(head); | |
998 | goto bail; |
|
995 | goto bail; | |
999 | } |
|
996 | } | |
1000 | } |
|
997 | } | |
1001 |
|
998 | |||
1002 | done: |
|
999 | done: | |
1003 | self->headrevs = heads; |
|
1000 | self->headrevs = heads; | |
1004 | Py_XDECREF(filter); |
|
1001 | Py_XDECREF(filter); | |
1005 | free(nothead); |
|
1002 | free(nothead); | |
1006 | return list_copy(self->headrevs); |
|
1003 | return list_copy(self->headrevs); | |
1007 | bail: |
|
1004 | bail: | |
1008 | Py_XDECREF(filter); |
|
1005 | Py_XDECREF(filter); | |
1009 | Py_XDECREF(heads); |
|
1006 | Py_XDECREF(heads); | |
1010 | free(nothead); |
|
1007 | free(nothead); | |
1011 | return NULL; |
|
1008 | return NULL; | |
1012 | } |
|
1009 | } | |
1013 |
|
1010 | |||
1014 | /** |
|
1011 | /** | |
1015 | * Obtain the base revision index entry. |
|
1012 | * Obtain the base revision index entry. | |
1016 | * |
|
1013 | * | |
1017 | * Callers must ensure that rev >= 0 or illegal memory access may occur. |
|
1014 | * Callers must ensure that rev >= 0 or illegal memory access may occur. | |
1018 | */ |
|
1015 | */ | |
1019 | static inline int index_baserev(indexObject *self, int rev) |
|
1016 | static inline int index_baserev(indexObject *self, int rev) | |
1020 | { |
|
1017 | { | |
1021 | const char *data; |
|
1018 | const char *data; | |
1022 | int result; |
|
1019 | int result; | |
1023 |
|
1020 | |||
1024 | if (rev >= self->length) { |
|
1021 | if (rev >= self->length) { | |
1025 | PyObject *tuple = |
|
1022 | PyObject *tuple = | |
1026 | PyList_GET_ITEM(self->added, rev - self->length); |
|
1023 | PyList_GET_ITEM(self->added, rev - self->length); | |
1027 | long ret; |
|
1024 | long ret; | |
1028 | if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 3), &ret)) { |
|
1025 | if (!pylong_to_long(PyTuple_GET_ITEM(tuple, 3), &ret)) { | |
1029 | return -2; |
|
1026 | return -2; | |
1030 | } |
|
1027 | } | |
1031 | result = (int)ret; |
|
1028 | result = (int)ret; | |
1032 | } else { |
|
1029 | } else { | |
1033 | data = index_deref(self, rev); |
|
1030 | data = index_deref(self, rev); | |
1034 | if (data == NULL) { |
|
1031 | if (data == NULL) { | |
1035 | return -2; |
|
1032 | return -2; | |
1036 | } |
|
1033 | } | |
1037 |
|
1034 | |||
1038 | result = getbe32(data + 16); |
|
1035 | result = getbe32(data + 16); | |
1039 | } |
|
1036 | } | |
1040 | if (result > rev) { |
|
1037 | if (result > rev) { | |
1041 | PyErr_Format( |
|
1038 | PyErr_Format( | |
1042 | PyExc_ValueError, |
|
1039 | PyExc_ValueError, | |
1043 | "corrupted revlog, revision base above revision: %d, %d", |
|
1040 | "corrupted revlog, revision base above revision: %d, %d", | |
1044 | rev, result); |
|
1041 | rev, result); | |
1045 | return -2; |
|
1042 | return -2; | |
1046 | } |
|
1043 | } | |
1047 | if (result < -1) { |
|
1044 | if (result < -1) { | |
1048 | PyErr_Format( |
|
1045 | PyErr_Format( | |
1049 | PyExc_ValueError, |
|
1046 | PyExc_ValueError, | |
1050 | "corrupted revlog, revision base out of range: %d, %d", rev, |
|
1047 | "corrupted revlog, revision base out of range: %d, %d", rev, | |
1051 | result); |
|
1048 | result); | |
1052 | return -2; |
|
1049 | return -2; | |
1053 | } |
|
1050 | } | |
1054 | return result; |
|
1051 | return result; | |
1055 | } |
|
1052 | } | |
1056 |
|
1053 | |||
1057 | /** |
|
1054 | /** | |
1058 | * Find if a revision is a snapshot or not |
|
1055 | * Find if a revision is a snapshot or not | |
1059 | * |
|
1056 | * | |
1060 | * Only relevant for sparse-revlog case. |
|
1057 | * Only relevant for sparse-revlog case. | |
1061 | * Callers must ensure that rev is in a valid range. |
|
1058 | * Callers must ensure that rev is in a valid range. | |
1062 | */ |
|
1059 | */ | |
1063 | static int index_issnapshotrev(indexObject *self, Py_ssize_t rev) |
|
1060 | static int index_issnapshotrev(indexObject *self, Py_ssize_t rev) | |
1064 | { |
|
1061 | { | |
1065 | int ps[2]; |
|
1062 | int ps[2]; | |
1066 | Py_ssize_t base; |
|
1063 | Py_ssize_t base; | |
1067 | while (rev >= 0) { |
|
1064 | while (rev >= 0) { | |
1068 | base = (Py_ssize_t)index_baserev(self, rev); |
|
1065 | base = (Py_ssize_t)index_baserev(self, rev); | |
1069 | if (base == rev) { |
|
1066 | if (base == rev) { | |
1070 | base = -1; |
|
1067 | base = -1; | |
1071 | } |
|
1068 | } | |
1072 | if (base == -2) { |
|
1069 | if (base == -2) { | |
1073 | assert(PyErr_Occurred()); |
|
1070 | assert(PyErr_Occurred()); | |
1074 | return -1; |
|
1071 | return -1; | |
1075 | } |
|
1072 | } | |
1076 | if (base == -1) { |
|
1073 | if (base == -1) { | |
1077 | return 1; |
|
1074 | return 1; | |
1078 | } |
|
1075 | } | |
1079 | if (index_get_parents(self, rev, ps, (int)rev) < 0) { |
|
1076 | if (index_get_parents(self, rev, ps, (int)rev) < 0) { | |
1080 | assert(PyErr_Occurred()); |
|
1077 | assert(PyErr_Occurred()); | |
1081 | return -1; |
|
1078 | return -1; | |
1082 | }; |
|
1079 | }; | |
1083 | if (base == ps[0] || base == ps[1]) { |
|
1080 | if (base == ps[0] || base == ps[1]) { | |
1084 | return 0; |
|
1081 | return 0; | |
1085 | } |
|
1082 | } | |
1086 | rev = base; |
|
1083 | rev = base; | |
1087 | } |
|
1084 | } | |
1088 | return rev == -1; |
|
1085 | return rev == -1; | |
1089 | } |
|
1086 | } | |
1090 |
|
1087 | |||
1091 | static PyObject *index_issnapshot(indexObject *self, PyObject *value) |
|
1088 | static PyObject *index_issnapshot(indexObject *self, PyObject *value) | |
1092 | { |
|
1089 | { | |
1093 | long rev; |
|
1090 | long rev; | |
1094 | int issnap; |
|
1091 | int issnap; | |
1095 | Py_ssize_t length = index_length(self); |
|
1092 | Py_ssize_t length = index_length(self); | |
1096 |
|
1093 | |||
1097 | if (!pylong_to_long(value, &rev)) { |
|
1094 | if (!pylong_to_long(value, &rev)) { | |
1098 | return NULL; |
|
1095 | return NULL; | |
1099 | } |
|
1096 | } | |
1100 | if (rev < -1 || rev >= length) { |
|
1097 | if (rev < -1 || rev >= length) { | |
1101 | PyErr_Format(PyExc_ValueError, "revlog index out of range: %ld", |
|
1098 | PyErr_Format(PyExc_ValueError, "revlog index out of range: %ld", | |
1102 | rev); |
|
1099 | rev); | |
1103 | return NULL; |
|
1100 | return NULL; | |
1104 | }; |
|
1101 | }; | |
1105 | issnap = index_issnapshotrev(self, (Py_ssize_t)rev); |
|
1102 | issnap = index_issnapshotrev(self, (Py_ssize_t)rev); | |
1106 | if (issnap < 0) { |
|
1103 | if (issnap < 0) { | |
1107 | return NULL; |
|
1104 | return NULL; | |
1108 | }; |
|
1105 | }; | |
1109 | return PyBool_FromLong((long)issnap); |
|
1106 | return PyBool_FromLong((long)issnap); | |
1110 | } |
|
1107 | } | |
1111 |
|
1108 | |||
1112 | static PyObject *index_findsnapshots(indexObject *self, PyObject *args) |
|
1109 | static PyObject *index_findsnapshots(indexObject *self, PyObject *args) | |
1113 | { |
|
1110 | { | |
1114 | Py_ssize_t start_rev; |
|
1111 | Py_ssize_t start_rev; | |
1115 | PyObject *cache; |
|
1112 | PyObject *cache; | |
1116 | Py_ssize_t base; |
|
1113 | Py_ssize_t base; | |
1117 | Py_ssize_t rev; |
|
1114 | Py_ssize_t rev; | |
1118 | PyObject *key = NULL; |
|
1115 | PyObject *key = NULL; | |
1119 | PyObject *value = NULL; |
|
1116 | PyObject *value = NULL; | |
1120 | const Py_ssize_t length = index_length(self); |
|
1117 | const Py_ssize_t length = index_length(self); | |
1121 | if (!PyArg_ParseTuple(args, "O!n", &PyDict_Type, &cache, &start_rev)) { |
|
1118 | if (!PyArg_ParseTuple(args, "O!n", &PyDict_Type, &cache, &start_rev)) { | |
1122 | return NULL; |
|
1119 | return NULL; | |
1123 | } |
|
1120 | } | |
1124 | for (rev = start_rev; rev < length; rev++) { |
|
1121 | for (rev = start_rev; rev < length; rev++) { | |
1125 | int issnap; |
|
1122 | int issnap; | |
1126 | PyObject *allvalues = NULL; |
|
1123 | PyObject *allvalues = NULL; | |
1127 | issnap = index_issnapshotrev(self, rev); |
|
1124 | issnap = index_issnapshotrev(self, rev); | |
1128 | if (issnap < 0) { |
|
1125 | if (issnap < 0) { | |
1129 | goto bail; |
|
1126 | goto bail; | |
1130 | } |
|
1127 | } | |
1131 | if (issnap == 0) { |
|
1128 | if (issnap == 0) { | |
1132 | continue; |
|
1129 | continue; | |
1133 | } |
|
1130 | } | |
1134 | base = (Py_ssize_t)index_baserev(self, rev); |
|
1131 | base = (Py_ssize_t)index_baserev(self, rev); | |
1135 | if (base == rev) { |
|
1132 | if (base == rev) { | |
1136 | base = -1; |
|
1133 | base = -1; | |
1137 | } |
|
1134 | } | |
1138 | if (base == -2) { |
|
1135 | if (base == -2) { | |
1139 | assert(PyErr_Occurred()); |
|
1136 | assert(PyErr_Occurred()); | |
1140 | goto bail; |
|
1137 | goto bail; | |
1141 | } |
|
1138 | } | |
1142 | key = PyInt_FromSsize_t(base); |
|
1139 | key = PyInt_FromSsize_t(base); | |
1143 | allvalues = PyDict_GetItem(cache, key); |
|
1140 | allvalues = PyDict_GetItem(cache, key); | |
1144 | if (allvalues == NULL && PyErr_Occurred()) { |
|
1141 | if (allvalues == NULL && PyErr_Occurred()) { | |
1145 | goto bail; |
|
1142 | goto bail; | |
1146 | } |
|
1143 | } | |
1147 | if (allvalues == NULL) { |
|
1144 | if (allvalues == NULL) { | |
1148 | int r; |
|
1145 | int r; | |
1149 | allvalues = PyList_New(0); |
|
1146 | allvalues = PyList_New(0); | |
1150 | if (!allvalues) { |
|
1147 | if (!allvalues) { | |
1151 | goto bail; |
|
1148 | goto bail; | |
1152 | } |
|
1149 | } | |
1153 | r = PyDict_SetItem(cache, key, allvalues); |
|
1150 | r = PyDict_SetItem(cache, key, allvalues); | |
1154 | Py_DECREF(allvalues); |
|
1151 | Py_DECREF(allvalues); | |
1155 | if (r < 0) { |
|
1152 | if (r < 0) { | |
1156 | goto bail; |
|
1153 | goto bail; | |
1157 | } |
|
1154 | } | |
1158 | } |
|
1155 | } | |
1159 | value = PyInt_FromSsize_t(rev); |
|
1156 | value = PyInt_FromSsize_t(rev); | |
1160 | if (PyList_Append(allvalues, value)) { |
|
1157 | if (PyList_Append(allvalues, value)) { | |
1161 | goto bail; |
|
1158 | goto bail; | |
1162 | } |
|
1159 | } | |
1163 | Py_CLEAR(key); |
|
1160 | Py_CLEAR(key); | |
1164 | Py_CLEAR(value); |
|
1161 | Py_CLEAR(value); | |
1165 | } |
|
1162 | } | |
1166 | Py_RETURN_NONE; |
|
1163 | Py_RETURN_NONE; | |
1167 | bail: |
|
1164 | bail: | |
1168 | Py_XDECREF(key); |
|
1165 | Py_XDECREF(key); | |
1169 | Py_XDECREF(value); |
|
1166 | Py_XDECREF(value); | |
1170 | return NULL; |
|
1167 | return NULL; | |
1171 | } |
|
1168 | } | |
1172 |
|
1169 | |||
1173 | static PyObject *index_deltachain(indexObject *self, PyObject *args) |
|
1170 | static PyObject *index_deltachain(indexObject *self, PyObject *args) | |
1174 | { |
|
1171 | { | |
1175 | int rev, generaldelta; |
|
1172 | int rev, generaldelta; | |
1176 | PyObject *stoparg; |
|
1173 | PyObject *stoparg; | |
1177 | int stoprev, iterrev, baserev = -1; |
|
1174 | int stoprev, iterrev, baserev = -1; | |
1178 | int stopped; |
|
1175 | int stopped; | |
1179 | PyObject *chain = NULL, *result = NULL; |
|
1176 | PyObject *chain = NULL, *result = NULL; | |
1180 | const Py_ssize_t length = index_length(self); |
|
1177 | const Py_ssize_t length = index_length(self); | |
1181 |
|
1178 | |||
1182 | if (!PyArg_ParseTuple(args, "iOi", &rev, &stoparg, &generaldelta)) { |
|
1179 | if (!PyArg_ParseTuple(args, "iOi", &rev, &stoparg, &generaldelta)) { | |
1183 | return NULL; |
|
1180 | return NULL; | |
1184 | } |
|
1181 | } | |
1185 |
|
1182 | |||
1186 | if (PyInt_Check(stoparg)) { |
|
1183 | if (PyInt_Check(stoparg)) { | |
1187 | stoprev = (int)PyInt_AsLong(stoparg); |
|
1184 | stoprev = (int)PyInt_AsLong(stoparg); | |
1188 | if (stoprev == -1 && PyErr_Occurred()) { |
|
1185 | if (stoprev == -1 && PyErr_Occurred()) { | |
1189 | return NULL; |
|
1186 | return NULL; | |
1190 | } |
|
1187 | } | |
1191 | } else if (stoparg == Py_None) { |
|
1188 | } else if (stoparg == Py_None) { | |
1192 | stoprev = -2; |
|
1189 | stoprev = -2; | |
1193 | } else { |
|
1190 | } else { | |
1194 | PyErr_SetString(PyExc_ValueError, |
|
1191 | PyErr_SetString(PyExc_ValueError, | |
1195 | "stoprev must be integer or None"); |
|
1192 | "stoprev must be integer or None"); | |
1196 | return NULL; |
|
1193 | return NULL; | |
1197 | } |
|
1194 | } | |
1198 |
|
1195 | |||
1199 | if (rev < 0 || rev >= length) { |
|
1196 | if (rev < 0 || rev >= length) { | |
1200 | PyErr_SetString(PyExc_ValueError, "revlog index out of range"); |
|
1197 | PyErr_SetString(PyExc_ValueError, "revlog index out of range"); | |
1201 | return NULL; |
|
1198 | return NULL; | |
1202 | } |
|
1199 | } | |
1203 |
|
1200 | |||
1204 | chain = PyList_New(0); |
|
1201 | chain = PyList_New(0); | |
1205 | if (chain == NULL) { |
|
1202 | if (chain == NULL) { | |
1206 | return NULL; |
|
1203 | return NULL; | |
1207 | } |
|
1204 | } | |
1208 |
|
1205 | |||
1209 | baserev = index_baserev(self, rev); |
|
1206 | baserev = index_baserev(self, rev); | |
1210 |
|
1207 | |||
1211 | /* This should never happen. */ |
|
1208 | /* This should never happen. */ | |
1212 | if (baserev <= -2) { |
|
1209 | if (baserev <= -2) { | |
1213 | /* Error should be set by index_deref() */ |
|
1210 | /* Error should be set by index_deref() */ | |
1214 | assert(PyErr_Occurred()); |
|
1211 | assert(PyErr_Occurred()); | |
1215 | goto bail; |
|
1212 | goto bail; | |
1216 | } |
|
1213 | } | |
1217 |
|
1214 | |||
1218 | iterrev = rev; |
|
1215 | iterrev = rev; | |
1219 |
|
1216 | |||
1220 | while (iterrev != baserev && iterrev != stoprev) { |
|
1217 | while (iterrev != baserev && iterrev != stoprev) { | |
1221 | PyObject *value = PyInt_FromLong(iterrev); |
|
1218 | PyObject *value = PyInt_FromLong(iterrev); | |
1222 | if (value == NULL) { |
|
1219 | if (value == NULL) { | |
1223 | goto bail; |
|
1220 | goto bail; | |
1224 | } |
|
1221 | } | |
1225 | if (PyList_Append(chain, value)) { |
|
1222 | if (PyList_Append(chain, value)) { | |
1226 | Py_DECREF(value); |
|
1223 | Py_DECREF(value); | |
1227 | goto bail; |
|
1224 | goto bail; | |
1228 | } |
|
1225 | } | |
1229 | Py_DECREF(value); |
|
1226 | Py_DECREF(value); | |
1230 |
|
1227 | |||
1231 | if (generaldelta) { |
|
1228 | if (generaldelta) { | |
1232 | iterrev = baserev; |
|
1229 | iterrev = baserev; | |
1233 | } else { |
|
1230 | } else { | |
1234 | iterrev--; |
|
1231 | iterrev--; | |
1235 | } |
|
1232 | } | |
1236 |
|
1233 | |||
1237 | if (iterrev < 0) { |
|
1234 | if (iterrev < 0) { | |
1238 | break; |
|
1235 | break; | |
1239 | } |
|
1236 | } | |
1240 |
|
1237 | |||
1241 | if (iterrev >= length) { |
|
1238 | if (iterrev >= length) { | |
1242 | PyErr_SetString(PyExc_IndexError, |
|
1239 | PyErr_SetString(PyExc_IndexError, | |
1243 | "revision outside index"); |
|
1240 | "revision outside index"); | |
1244 | return NULL; |
|
1241 | return NULL; | |
1245 | } |
|
1242 | } | |
1246 |
|
1243 | |||
1247 | baserev = index_baserev(self, iterrev); |
|
1244 | baserev = index_baserev(self, iterrev); | |
1248 |
|
1245 | |||
1249 | /* This should never happen. */ |
|
1246 | /* This should never happen. */ | |
1250 | if (baserev <= -2) { |
|
1247 | if (baserev <= -2) { | |
1251 | /* Error should be set by index_deref() */ |
|
1248 | /* Error should be set by index_deref() */ | |
1252 | assert(PyErr_Occurred()); |
|
1249 | assert(PyErr_Occurred()); | |
1253 | goto bail; |
|
1250 | goto bail; | |
1254 | } |
|
1251 | } | |
1255 | } |
|
1252 | } | |
1256 |
|
1253 | |||
1257 | if (iterrev == stoprev) { |
|
1254 | if (iterrev == stoprev) { | |
1258 | stopped = 1; |
|
1255 | stopped = 1; | |
1259 | } else { |
|
1256 | } else { | |
1260 | PyObject *value = PyInt_FromLong(iterrev); |
|
1257 | PyObject *value = PyInt_FromLong(iterrev); | |
1261 | if (value == NULL) { |
|
1258 | if (value == NULL) { | |
1262 | goto bail; |
|
1259 | goto bail; | |
1263 | } |
|
1260 | } | |
1264 | if (PyList_Append(chain, value)) { |
|
1261 | if (PyList_Append(chain, value)) { | |
1265 | Py_DECREF(value); |
|
1262 | Py_DECREF(value); | |
1266 | goto bail; |
|
1263 | goto bail; | |
1267 | } |
|
1264 | } | |
1268 | Py_DECREF(value); |
|
1265 | Py_DECREF(value); | |
1269 |
|
1266 | |||
1270 | stopped = 0; |
|
1267 | stopped = 0; | |
1271 | } |
|
1268 | } | |
1272 |
|
1269 | |||
1273 | if (PyList_Reverse(chain)) { |
|
1270 | if (PyList_Reverse(chain)) { | |
1274 | goto bail; |
|
1271 | goto bail; | |
1275 | } |
|
1272 | } | |
1276 |
|
1273 | |||
1277 | result = Py_BuildValue("OO", chain, stopped ? Py_True : Py_False); |
|
1274 | result = Py_BuildValue("OO", chain, stopped ? Py_True : Py_False); | |
1278 | Py_DECREF(chain); |
|
1275 | Py_DECREF(chain); | |
1279 | return result; |
|
1276 | return result; | |
1280 |
|
1277 | |||
1281 | bail: |
|
1278 | bail: | |
1282 | Py_DECREF(chain); |
|
1279 | Py_DECREF(chain); | |
1283 | return NULL; |
|
1280 | return NULL; | |
1284 | } |
|
1281 | } | |
1285 |
|
1282 | |||
1286 | static inline int64_t |
|
1283 | static inline int64_t | |
1287 | index_segment_span(indexObject *self, Py_ssize_t start_rev, Py_ssize_t end_rev) |
|
1284 | index_segment_span(indexObject *self, Py_ssize_t start_rev, Py_ssize_t end_rev) | |
1288 | { |
|
1285 | { | |
1289 | int64_t start_offset; |
|
1286 | int64_t start_offset; | |
1290 | int64_t end_offset; |
|
1287 | int64_t end_offset; | |
1291 | int end_size; |
|
1288 | int end_size; | |
1292 | start_offset = index_get_start(self, start_rev); |
|
1289 | start_offset = index_get_start(self, start_rev); | |
1293 | if (start_offset < 0) { |
|
1290 | if (start_offset < 0) { | |
1294 | return -1; |
|
1291 | return -1; | |
1295 | } |
|
1292 | } | |
1296 | end_offset = index_get_start(self, end_rev); |
|
1293 | end_offset = index_get_start(self, end_rev); | |
1297 | if (end_offset < 0) { |
|
1294 | if (end_offset < 0) { | |
1298 | return -1; |
|
1295 | return -1; | |
1299 | } |
|
1296 | } | |
1300 | end_size = index_get_length(self, end_rev); |
|
1297 | end_size = index_get_length(self, end_rev); | |
1301 | if (end_size < 0) { |
|
1298 | if (end_size < 0) { | |
1302 | return -1; |
|
1299 | return -1; | |
1303 | } |
|
1300 | } | |
1304 | if (end_offset < start_offset) { |
|
1301 | if (end_offset < start_offset) { | |
1305 | PyErr_Format(PyExc_ValueError, |
|
1302 | PyErr_Format(PyExc_ValueError, | |
1306 | "corrupted revlog index: inconsistent offset " |
|
1303 | "corrupted revlog index: inconsistent offset " | |
1307 | "between revisions (%zd) and (%zd)", |
|
1304 | "between revisions (%zd) and (%zd)", | |
1308 | start_rev, end_rev); |
|
1305 | start_rev, end_rev); | |
1309 | return -1; |
|
1306 | return -1; | |
1310 | } |
|
1307 | } | |
1311 | return (end_offset - start_offset) + (int64_t)end_size; |
|
1308 | return (end_offset - start_offset) + (int64_t)end_size; | |
1312 | } |
|
1309 | } | |
1313 |
|
1310 | |||
1314 | /* returns endidx so that revs[startidx:endidx] has no empty trailing revs */ |
|
1311 | /* returns endidx so that revs[startidx:endidx] has no empty trailing revs */ | |
1315 | static Py_ssize_t trim_endidx(indexObject *self, const Py_ssize_t *revs, |
|
1312 | static Py_ssize_t trim_endidx(indexObject *self, const Py_ssize_t *revs, | |
1316 | Py_ssize_t startidx, Py_ssize_t endidx) |
|
1313 | Py_ssize_t startidx, Py_ssize_t endidx) | |
1317 | { |
|
1314 | { | |
1318 | int length; |
|
1315 | int length; | |
1319 | while (endidx > 1 && endidx > startidx) { |
|
1316 | while (endidx > 1 && endidx > startidx) { | |
1320 | length = index_get_length(self, revs[endidx - 1]); |
|
1317 | length = index_get_length(self, revs[endidx - 1]); | |
1321 | if (length < 0) { |
|
1318 | if (length < 0) { | |
1322 | return -1; |
|
1319 | return -1; | |
1323 | } |
|
1320 | } | |
1324 | if (length != 0) { |
|
1321 | if (length != 0) { | |
1325 | break; |
|
1322 | break; | |
1326 | } |
|
1323 | } | |
1327 | endidx -= 1; |
|
1324 | endidx -= 1; | |
1328 | } |
|
1325 | } | |
1329 | return endidx; |
|
1326 | return endidx; | |
1330 | } |
|
1327 | } | |
1331 |
|
1328 | |||
1332 | struct Gap { |
|
1329 | struct Gap { | |
1333 | int64_t size; |
|
1330 | int64_t size; | |
1334 | Py_ssize_t idx; |
|
1331 | Py_ssize_t idx; | |
1335 | }; |
|
1332 | }; | |
1336 |
|
1333 | |||
1337 | static int gap_compare(const void *left, const void *right) |
|
1334 | static int gap_compare(const void *left, const void *right) | |
1338 | { |
|
1335 | { | |
1339 | const struct Gap *l_left = ((const struct Gap *)left); |
|
1336 | const struct Gap *l_left = ((const struct Gap *)left); | |
1340 | const struct Gap *l_right = ((const struct Gap *)right); |
|
1337 | const struct Gap *l_right = ((const struct Gap *)right); | |
1341 | if (l_left->size < l_right->size) { |
|
1338 | if (l_left->size < l_right->size) { | |
1342 | return -1; |
|
1339 | return -1; | |
1343 | } else if (l_left->size > l_right->size) { |
|
1340 | } else if (l_left->size > l_right->size) { | |
1344 | return 1; |
|
1341 | return 1; | |
1345 | } |
|
1342 | } | |
1346 | return 0; |
|
1343 | return 0; | |
1347 | } |
|
1344 | } | |
1348 | static int Py_ssize_t_compare(const void *left, const void *right) |
|
1345 | static int Py_ssize_t_compare(const void *left, const void *right) | |
1349 | { |
|
1346 | { | |
1350 | const Py_ssize_t l_left = *(const Py_ssize_t *)left; |
|
1347 | const Py_ssize_t l_left = *(const Py_ssize_t *)left; | |
1351 | const Py_ssize_t l_right = *(const Py_ssize_t *)right; |
|
1348 | const Py_ssize_t l_right = *(const Py_ssize_t *)right; | |
1352 | if (l_left < l_right) { |
|
1349 | if (l_left < l_right) { | |
1353 | return -1; |
|
1350 | return -1; | |
1354 | } else if (l_left > l_right) { |
|
1351 | } else if (l_left > l_right) { | |
1355 | return 1; |
|
1352 | return 1; | |
1356 | } |
|
1353 | } | |
1357 | return 0; |
|
1354 | return 0; | |
1358 | } |
|
1355 | } | |
1359 |
|
1356 | |||
1360 | static PyObject *index_slicechunktodensity(indexObject *self, PyObject *args) |
|
1357 | static PyObject *index_slicechunktodensity(indexObject *self, PyObject *args) | |
1361 | { |
|
1358 | { | |
1362 | /* method arguments */ |
|
1359 | /* method arguments */ | |
1363 | PyObject *list_revs = NULL; /* revisions in the chain */ |
|
1360 | PyObject *list_revs = NULL; /* revisions in the chain */ | |
1364 | double targetdensity = 0; /* min density to achieve */ |
|
1361 | double targetdensity = 0; /* min density to achieve */ | |
1365 | Py_ssize_t mingapsize = 0; /* threshold to ignore gaps */ |
|
1362 | Py_ssize_t mingapsize = 0; /* threshold to ignore gaps */ | |
1366 |
|
1363 | |||
1367 | /* other core variables */ |
|
1364 | /* other core variables */ | |
1368 | Py_ssize_t idxlen = index_length(self); |
|
1365 | Py_ssize_t idxlen = index_length(self); | |
1369 | Py_ssize_t i; /* used for various iteration */ |
|
1366 | Py_ssize_t i; /* used for various iteration */ | |
1370 | PyObject *result = NULL; /* the final return of the function */ |
|
1367 | PyObject *result = NULL; /* the final return of the function */ | |
1371 |
|
1368 | |||
1372 | /* generic information about the delta chain being slice */ |
|
1369 | /* generic information about the delta chain being slice */ | |
1373 | Py_ssize_t num_revs = 0; /* size of the full delta chain */ |
|
1370 | Py_ssize_t num_revs = 0; /* size of the full delta chain */ | |
1374 | Py_ssize_t *revs = NULL; /* native array of revision in the chain */ |
|
1371 | Py_ssize_t *revs = NULL; /* native array of revision in the chain */ | |
1375 | int64_t chainpayload = 0; /* sum of all delta in the chain */ |
|
1372 | int64_t chainpayload = 0; /* sum of all delta in the chain */ | |
1376 | int64_t deltachainspan = 0; /* distance from first byte to last byte */ |
|
1373 | int64_t deltachainspan = 0; /* distance from first byte to last byte */ | |
1377 |
|
1374 | |||
1378 | /* variable used for slicing the delta chain */ |
|
1375 | /* variable used for slicing the delta chain */ | |
1379 | int64_t readdata = 0; /* amount of data currently planned to be read */ |
|
1376 | int64_t readdata = 0; /* amount of data currently planned to be read */ | |
1380 | double density = 0; /* ration of payload data compared to read ones */ |
|
1377 | double density = 0; /* ration of payload data compared to read ones */ | |
1381 | int64_t previous_end; |
|
1378 | int64_t previous_end; | |
1382 | struct Gap *gaps = NULL; /* array of notable gap in the chain */ |
|
1379 | struct Gap *gaps = NULL; /* array of notable gap in the chain */ | |
1383 | Py_ssize_t num_gaps = |
|
1380 | Py_ssize_t num_gaps = | |
1384 | 0; /* total number of notable gap recorded so far */ |
|
1381 | 0; /* total number of notable gap recorded so far */ | |
1385 | Py_ssize_t *selected_indices = NULL; /* indices of gap skipped over */ |
|
1382 | Py_ssize_t *selected_indices = NULL; /* indices of gap skipped over */ | |
1386 | Py_ssize_t num_selected = 0; /* number of gaps skipped */ |
|
1383 | Py_ssize_t num_selected = 0; /* number of gaps skipped */ | |
1387 | PyObject *chunk = NULL; /* individual slice */ |
|
1384 | PyObject *chunk = NULL; /* individual slice */ | |
1388 | PyObject *allchunks = NULL; /* all slices */ |
|
1385 | PyObject *allchunks = NULL; /* all slices */ | |
1389 | Py_ssize_t previdx; |
|
1386 | Py_ssize_t previdx; | |
1390 |
|
1387 | |||
1391 | /* parsing argument */ |
|
1388 | /* parsing argument */ | |
1392 | if (!PyArg_ParseTuple(args, "O!dn", &PyList_Type, &list_revs, |
|
1389 | if (!PyArg_ParseTuple(args, "O!dn", &PyList_Type, &list_revs, | |
1393 | &targetdensity, &mingapsize)) { |
|
1390 | &targetdensity, &mingapsize)) { | |
1394 | goto bail; |
|
1391 | goto bail; | |
1395 | } |
|
1392 | } | |
1396 |
|
1393 | |||
1397 | /* If the delta chain contains a single element, we do not need slicing |
|
1394 | /* If the delta chain contains a single element, we do not need slicing | |
1398 | */ |
|
1395 | */ | |
1399 | num_revs = PyList_GET_SIZE(list_revs); |
|
1396 | num_revs = PyList_GET_SIZE(list_revs); | |
1400 | if (num_revs <= 1) { |
|
1397 | if (num_revs <= 1) { | |
1401 | result = PyTuple_Pack(1, list_revs); |
|
1398 | result = PyTuple_Pack(1, list_revs); | |
1402 | goto done; |
|
1399 | goto done; | |
1403 | } |
|
1400 | } | |
1404 |
|
1401 | |||
1405 | /* Turn the python list into a native integer array (for efficiency) */ |
|
1402 | /* Turn the python list into a native integer array (for efficiency) */ | |
1406 | revs = (Py_ssize_t *)calloc(num_revs, sizeof(Py_ssize_t)); |
|
1403 | revs = (Py_ssize_t *)calloc(num_revs, sizeof(Py_ssize_t)); | |
1407 | if (revs == NULL) { |
|
1404 | if (revs == NULL) { | |
1408 | PyErr_NoMemory(); |
|
1405 | PyErr_NoMemory(); | |
1409 | goto bail; |
|
1406 | goto bail; | |
1410 | } |
|
1407 | } | |
1411 | for (i = 0; i < num_revs; i++) { |
|
1408 | for (i = 0; i < num_revs; i++) { | |
1412 | Py_ssize_t revnum = PyInt_AsLong(PyList_GET_ITEM(list_revs, i)); |
|
1409 | Py_ssize_t revnum = PyInt_AsLong(PyList_GET_ITEM(list_revs, i)); | |
1413 | if (revnum == -1 && PyErr_Occurred()) { |
|
1410 | if (revnum == -1 && PyErr_Occurred()) { | |
1414 | goto bail; |
|
1411 | goto bail; | |
1415 | } |
|
1412 | } | |
1416 | if (revnum < nullrev || revnum >= idxlen) { |
|
1413 | if (revnum < nullrev || revnum >= idxlen) { | |
1417 | PyErr_Format(PyExc_IndexError, |
|
1414 | PyErr_Format(PyExc_IndexError, | |
1418 | "index out of range: %zd", revnum); |
|
1415 | "index out of range: %zd", revnum); | |
1419 | goto bail; |
|
1416 | goto bail; | |
1420 | } |
|
1417 | } | |
1421 | revs[i] = revnum; |
|
1418 | revs[i] = revnum; | |
1422 | } |
|
1419 | } | |
1423 |
|
1420 | |||
1424 | /* Compute and check various property of the unsliced delta chain */ |
|
1421 | /* Compute and check various property of the unsliced delta chain */ | |
1425 | deltachainspan = index_segment_span(self, revs[0], revs[num_revs - 1]); |
|
1422 | deltachainspan = index_segment_span(self, revs[0], revs[num_revs - 1]); | |
1426 | if (deltachainspan < 0) { |
|
1423 | if (deltachainspan < 0) { | |
1427 | goto bail; |
|
1424 | goto bail; | |
1428 | } |
|
1425 | } | |
1429 |
|
1426 | |||
1430 | if (deltachainspan <= mingapsize) { |
|
1427 | if (deltachainspan <= mingapsize) { | |
1431 | result = PyTuple_Pack(1, list_revs); |
|
1428 | result = PyTuple_Pack(1, list_revs); | |
1432 | goto done; |
|
1429 | goto done; | |
1433 | } |
|
1430 | } | |
1434 | chainpayload = 0; |
|
1431 | chainpayload = 0; | |
1435 | for (i = 0; i < num_revs; i++) { |
|
1432 | for (i = 0; i < num_revs; i++) { | |
1436 | int tmp = index_get_length(self, revs[i]); |
|
1433 | int tmp = index_get_length(self, revs[i]); | |
1437 | if (tmp < 0) { |
|
1434 | if (tmp < 0) { | |
1438 | goto bail; |
|
1435 | goto bail; | |
1439 | } |
|
1436 | } | |
1440 | chainpayload += tmp; |
|
1437 | chainpayload += tmp; | |
1441 | } |
|
1438 | } | |
1442 |
|
1439 | |||
1443 | readdata = deltachainspan; |
|
1440 | readdata = deltachainspan; | |
1444 | density = 1.0; |
|
1441 | density = 1.0; | |
1445 |
|
1442 | |||
1446 | if (0 < deltachainspan) { |
|
1443 | if (0 < deltachainspan) { | |
1447 | density = (double)chainpayload / (double)deltachainspan; |
|
1444 | density = (double)chainpayload / (double)deltachainspan; | |
1448 | } |
|
1445 | } | |
1449 |
|
1446 | |||
1450 | if (density >= targetdensity) { |
|
1447 | if (density >= targetdensity) { | |
1451 | result = PyTuple_Pack(1, list_revs); |
|
1448 | result = PyTuple_Pack(1, list_revs); | |
1452 | goto done; |
|
1449 | goto done; | |
1453 | } |
|
1450 | } | |
1454 |
|
1451 | |||
1455 | /* if chain is too sparse, look for relevant gaps */ |
|
1452 | /* if chain is too sparse, look for relevant gaps */ | |
1456 | gaps = (struct Gap *)calloc(num_revs, sizeof(struct Gap)); |
|
1453 | gaps = (struct Gap *)calloc(num_revs, sizeof(struct Gap)); | |
1457 | if (gaps == NULL) { |
|
1454 | if (gaps == NULL) { | |
1458 | PyErr_NoMemory(); |
|
1455 | PyErr_NoMemory(); | |
1459 | goto bail; |
|
1456 | goto bail; | |
1460 | } |
|
1457 | } | |
1461 |
|
1458 | |||
1462 | previous_end = -1; |
|
1459 | previous_end = -1; | |
1463 | for (i = 0; i < num_revs; i++) { |
|
1460 | for (i = 0; i < num_revs; i++) { | |
1464 | int64_t revstart; |
|
1461 | int64_t revstart; | |
1465 | int revsize; |
|
1462 | int revsize; | |
1466 | revstart = index_get_start(self, revs[i]); |
|
1463 | revstart = index_get_start(self, revs[i]); | |
1467 | if (revstart < 0) { |
|
1464 | if (revstart < 0) { | |
1468 | goto bail; |
|
1465 | goto bail; | |
1469 | }; |
|
1466 | }; | |
1470 | revsize = index_get_length(self, revs[i]); |
|
1467 | revsize = index_get_length(self, revs[i]); | |
1471 | if (revsize < 0) { |
|
1468 | if (revsize < 0) { | |
1472 | goto bail; |
|
1469 | goto bail; | |
1473 | }; |
|
1470 | }; | |
1474 | if (revsize == 0) { |
|
1471 | if (revsize == 0) { | |
1475 | continue; |
|
1472 | continue; | |
1476 | } |
|
1473 | } | |
1477 | if (previous_end >= 0) { |
|
1474 | if (previous_end >= 0) { | |
1478 | int64_t gapsize = revstart - previous_end; |
|
1475 | int64_t gapsize = revstart - previous_end; | |
1479 | if (gapsize > mingapsize) { |
|
1476 | if (gapsize > mingapsize) { | |
1480 | gaps[num_gaps].size = gapsize; |
|
1477 | gaps[num_gaps].size = gapsize; | |
1481 | gaps[num_gaps].idx = i; |
|
1478 | gaps[num_gaps].idx = i; | |
1482 | num_gaps += 1; |
|
1479 | num_gaps += 1; | |
1483 | } |
|
1480 | } | |
1484 | } |
|
1481 | } | |
1485 | previous_end = revstart + revsize; |
|
1482 | previous_end = revstart + revsize; | |
1486 | } |
|
1483 | } | |
1487 | if (num_gaps == 0) { |
|
1484 | if (num_gaps == 0) { | |
1488 | result = PyTuple_Pack(1, list_revs); |
|
1485 | result = PyTuple_Pack(1, list_revs); | |
1489 | goto done; |
|
1486 | goto done; | |
1490 | } |
|
1487 | } | |
1491 | qsort(gaps, num_gaps, sizeof(struct Gap), &gap_compare); |
|
1488 | qsort(gaps, num_gaps, sizeof(struct Gap), &gap_compare); | |
1492 |
|
1489 | |||
1493 | /* Slice the largest gap first, they improve the density the most */ |
|
1490 | /* Slice the largest gap first, they improve the density the most */ | |
1494 | selected_indices = |
|
1491 | selected_indices = | |
1495 | (Py_ssize_t *)malloc((num_gaps + 1) * sizeof(Py_ssize_t)); |
|
1492 | (Py_ssize_t *)malloc((num_gaps + 1) * sizeof(Py_ssize_t)); | |
1496 | if (selected_indices == NULL) { |
|
1493 | if (selected_indices == NULL) { | |
1497 | PyErr_NoMemory(); |
|
1494 | PyErr_NoMemory(); | |
1498 | goto bail; |
|
1495 | goto bail; | |
1499 | } |
|
1496 | } | |
1500 |
|
1497 | |||
1501 | for (i = num_gaps - 1; i >= 0; i--) { |
|
1498 | for (i = num_gaps - 1; i >= 0; i--) { | |
1502 | selected_indices[num_selected] = gaps[i].idx; |
|
1499 | selected_indices[num_selected] = gaps[i].idx; | |
1503 | readdata -= gaps[i].size; |
|
1500 | readdata -= gaps[i].size; | |
1504 | num_selected += 1; |
|
1501 | num_selected += 1; | |
1505 | if (readdata <= 0) { |
|
1502 | if (readdata <= 0) { | |
1506 | density = 1.0; |
|
1503 | density = 1.0; | |
1507 | } else { |
|
1504 | } else { | |
1508 | density = (double)chainpayload / (double)readdata; |
|
1505 | density = (double)chainpayload / (double)readdata; | |
1509 | } |
|
1506 | } | |
1510 | if (density >= targetdensity) { |
|
1507 | if (density >= targetdensity) { | |
1511 | break; |
|
1508 | break; | |
1512 | } |
|
1509 | } | |
1513 | } |
|
1510 | } | |
1514 | qsort(selected_indices, num_selected, sizeof(Py_ssize_t), |
|
1511 | qsort(selected_indices, num_selected, sizeof(Py_ssize_t), | |
1515 | &Py_ssize_t_compare); |
|
1512 | &Py_ssize_t_compare); | |
1516 |
|
1513 | |||
1517 | /* create the resulting slice */ |
|
1514 | /* create the resulting slice */ | |
1518 | allchunks = PyList_New(0); |
|
1515 | allchunks = PyList_New(0); | |
1519 | if (allchunks == NULL) { |
|
1516 | if (allchunks == NULL) { | |
1520 | goto bail; |
|
1517 | goto bail; | |
1521 | } |
|
1518 | } | |
1522 | previdx = 0; |
|
1519 | previdx = 0; | |
1523 | selected_indices[num_selected] = num_revs; |
|
1520 | selected_indices[num_selected] = num_revs; | |
1524 | for (i = 0; i <= num_selected; i++) { |
|
1521 | for (i = 0; i <= num_selected; i++) { | |
1525 | Py_ssize_t idx = selected_indices[i]; |
|
1522 | Py_ssize_t idx = selected_indices[i]; | |
1526 | Py_ssize_t endidx = trim_endidx(self, revs, previdx, idx); |
|
1523 | Py_ssize_t endidx = trim_endidx(self, revs, previdx, idx); | |
1527 | if (endidx < 0) { |
|
1524 | if (endidx < 0) { | |
1528 | goto bail; |
|
1525 | goto bail; | |
1529 | } |
|
1526 | } | |
1530 | if (previdx < endidx) { |
|
1527 | if (previdx < endidx) { | |
1531 | chunk = PyList_GetSlice(list_revs, previdx, endidx); |
|
1528 | chunk = PyList_GetSlice(list_revs, previdx, endidx); | |
1532 | if (chunk == NULL) { |
|
1529 | if (chunk == NULL) { | |
1533 | goto bail; |
|
1530 | goto bail; | |
1534 | } |
|
1531 | } | |
1535 | if (PyList_Append(allchunks, chunk) == -1) { |
|
1532 | if (PyList_Append(allchunks, chunk) == -1) { | |
1536 | goto bail; |
|
1533 | goto bail; | |
1537 | } |
|
1534 | } | |
1538 | Py_DECREF(chunk); |
|
1535 | Py_DECREF(chunk); | |
1539 | chunk = NULL; |
|
1536 | chunk = NULL; | |
1540 | } |
|
1537 | } | |
1541 | previdx = idx; |
|
1538 | previdx = idx; | |
1542 | } |
|
1539 | } | |
1543 | result = allchunks; |
|
1540 | result = allchunks; | |
1544 | goto done; |
|
1541 | goto done; | |
1545 |
|
1542 | |||
1546 | bail: |
|
1543 | bail: | |
1547 | Py_XDECREF(allchunks); |
|
1544 | Py_XDECREF(allchunks); | |
1548 | Py_XDECREF(chunk); |
|
1545 | Py_XDECREF(chunk); | |
1549 | done: |
|
1546 | done: | |
1550 | free(revs); |
|
1547 | free(revs); | |
1551 | free(gaps); |
|
1548 | free(gaps); | |
1552 | free(selected_indices); |
|
1549 | free(selected_indices); | |
1553 | return result; |
|
1550 | return result; | |
1554 | } |
|
1551 | } | |
1555 |
|
1552 | |||
1556 | static inline int nt_level(const char *node, Py_ssize_t level) |
|
1553 | static inline int nt_level(const char *node, Py_ssize_t level) | |
1557 | { |
|
1554 | { | |
1558 | int v = node[level >> 1]; |
|
1555 | int v = node[level >> 1]; | |
1559 | if (!(level & 1)) |
|
1556 | if (!(level & 1)) | |
1560 | v >>= 4; |
|
1557 | v >>= 4; | |
1561 | return v & 0xf; |
|
1558 | return v & 0xf; | |
1562 | } |
|
1559 | } | |
1563 |
|
1560 | |||
1564 | /* |
|
1561 | /* | |
1565 | * Return values: |
|
1562 | * Return values: | |
1566 | * |
|
1563 | * | |
1567 | * -4: match is ambiguous (multiple candidates) |
|
1564 | * -4: match is ambiguous (multiple candidates) | |
1568 | * -2: not found |
|
1565 | * -2: not found | |
1569 | * rest: valid rev |
|
1566 | * rest: valid rev | |
1570 | */ |
|
1567 | */ | |
1571 | static int nt_find(nodetree *self, const char *node, Py_ssize_t nodelen, |
|
1568 | static int nt_find(nodetree *self, const char *node, Py_ssize_t nodelen, | |
1572 | int hex) |
|
1569 | int hex) | |
1573 | { |
|
1570 | { | |
1574 | int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level; |
|
1571 | int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level; | |
1575 | int level, maxlevel, off; |
|
1572 | int level, maxlevel, off; | |
1576 |
|
1573 | |||
1577 | if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0) |
|
1574 | if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0) | |
1578 | return -1; |
|
1575 | return -1; | |
1579 |
|
1576 | |||
1580 | if (hex) |
|
1577 | if (hex) | |
1581 | maxlevel = nodelen > 40 ? 40 : (int)nodelen; |
|
1578 | maxlevel = nodelen > 40 ? 40 : (int)nodelen; | |
1582 | else |
|
1579 | else | |
1583 | maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2); |
|
1580 | maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2); | |
1584 |
|
1581 | |||
1585 | for (level = off = 0; level < maxlevel; level++) { |
|
1582 | for (level = off = 0; level < maxlevel; level++) { | |
1586 | int k = getnybble(node, level); |
|
1583 | int k = getnybble(node, level); | |
1587 | nodetreenode *n = &self->nodes[off]; |
|
1584 | nodetreenode *n = &self->nodes[off]; | |
1588 | int v = n->children[k]; |
|
1585 | int v = n->children[k]; | |
1589 |
|
1586 | |||
1590 | if (v < 0) { |
|
1587 | if (v < 0) { | |
1591 | const char *n; |
|
1588 | const char *n; | |
1592 | Py_ssize_t i; |
|
1589 | Py_ssize_t i; | |
1593 |
|
1590 | |||
1594 | v = -(v + 2); |
|
1591 | v = -(v + 2); | |
1595 | n = index_node(self->index, v); |
|
1592 | n = index_node(self->index, v); | |
1596 | if (n == NULL) |
|
1593 | if (n == NULL) | |
1597 | return -2; |
|
1594 | return -2; | |
1598 | for (i = level; i < maxlevel; i++) |
|
1595 | for (i = level; i < maxlevel; i++) | |
1599 | if (getnybble(node, i) != nt_level(n, i)) |
|
1596 | if (getnybble(node, i) != nt_level(n, i)) | |
1600 | return -2; |
|
1597 | return -2; | |
1601 | return v; |
|
1598 | return v; | |
1602 | } |
|
1599 | } | |
1603 | if (v == 0) |
|
1600 | if (v == 0) | |
1604 | return -2; |
|
1601 | return -2; | |
1605 | off = v; |
|
1602 | off = v; | |
1606 | } |
|
1603 | } | |
1607 | /* multiple matches against an ambiguous prefix */ |
|
1604 | /* multiple matches against an ambiguous prefix */ | |
1608 | return -4; |
|
1605 | return -4; | |
1609 | } |
|
1606 | } | |
1610 |
|
1607 | |||
1611 | static int nt_new(nodetree *self) |
|
1608 | static int nt_new(nodetree *self) | |
1612 | { |
|
1609 | { | |
1613 | if (self->length == self->capacity) { |
|
1610 | if (self->length == self->capacity) { | |
1614 | unsigned newcapacity; |
|
1611 | unsigned newcapacity; | |
1615 | nodetreenode *newnodes; |
|
1612 | nodetreenode *newnodes; | |
1616 | newcapacity = self->capacity * 2; |
|
1613 | newcapacity = self->capacity * 2; | |
1617 | if (newcapacity >= INT_MAX / sizeof(nodetreenode)) { |
|
1614 | if (newcapacity >= INT_MAX / sizeof(nodetreenode)) { | |
1618 | PyErr_SetString(PyExc_MemoryError, |
|
1615 | PyErr_SetString(PyExc_MemoryError, | |
1619 | "overflow in nt_new"); |
|
1616 | "overflow in nt_new"); | |
1620 | return -1; |
|
1617 | return -1; | |
1621 | } |
|
1618 | } | |
1622 | newnodes = |
|
1619 | newnodes = | |
1623 | realloc(self->nodes, newcapacity * sizeof(nodetreenode)); |
|
1620 | realloc(self->nodes, newcapacity * sizeof(nodetreenode)); | |
1624 | if (newnodes == NULL) { |
|
1621 | if (newnodes == NULL) { | |
1625 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
|
1622 | PyErr_SetString(PyExc_MemoryError, "out of memory"); | |
1626 | return -1; |
|
1623 | return -1; | |
1627 | } |
|
1624 | } | |
1628 | self->capacity = newcapacity; |
|
1625 | self->capacity = newcapacity; | |
1629 | self->nodes = newnodes; |
|
1626 | self->nodes = newnodes; | |
1630 | memset(&self->nodes[self->length], 0, |
|
1627 | memset(&self->nodes[self->length], 0, | |
1631 | sizeof(nodetreenode) * (self->capacity - self->length)); |
|
1628 | sizeof(nodetreenode) * (self->capacity - self->length)); | |
1632 | } |
|
1629 | } | |
1633 | return self->length++; |
|
1630 | return self->length++; | |
1634 | } |
|
1631 | } | |
1635 |
|
1632 | |||
1636 | static int nt_insert(nodetree *self, const char *node, int rev) |
|
1633 | static int nt_insert(nodetree *self, const char *node, int rev) | |
1637 | { |
|
1634 | { | |
1638 | int level = 0; |
|
1635 | int level = 0; | |
1639 | int off = 0; |
|
1636 | int off = 0; | |
1640 |
|
1637 | |||
1641 | while (level < 40) { |
|
1638 | while (level < 40) { | |
1642 | int k = nt_level(node, level); |
|
1639 | int k = nt_level(node, level); | |
1643 | nodetreenode *n; |
|
1640 | nodetreenode *n; | |
1644 | int v; |
|
1641 | int v; | |
1645 |
|
1642 | |||
1646 | n = &self->nodes[off]; |
|
1643 | n = &self->nodes[off]; | |
1647 | v = n->children[k]; |
|
1644 | v = n->children[k]; | |
1648 |
|
1645 | |||
1649 | if (v == 0) { |
|
1646 | if (v == 0) { | |
1650 | n->children[k] = -rev - 2; |
|
1647 | n->children[k] = -rev - 2; | |
1651 | return 0; |
|
1648 | return 0; | |
1652 | } |
|
1649 | } | |
1653 | if (v < 0) { |
|
1650 | if (v < 0) { | |
1654 | const char *oldnode = |
|
1651 | const char *oldnode = | |
1655 | index_node_existing(self->index, -(v + 2)); |
|
1652 | index_node_existing(self->index, -(v + 2)); | |
1656 | int noff; |
|
1653 | int noff; | |
1657 |
|
1654 | |||
1658 | if (oldnode == NULL) |
|
1655 | if (oldnode == NULL) | |
1659 | return -1; |
|
1656 | return -1; | |
1660 | if (!memcmp(oldnode, node, 20)) { |
|
1657 | if (!memcmp(oldnode, node, 20)) { | |
1661 | n->children[k] = -rev - 2; |
|
1658 | n->children[k] = -rev - 2; | |
1662 | return 0; |
|
1659 | return 0; | |
1663 | } |
|
1660 | } | |
1664 | noff = nt_new(self); |
|
1661 | noff = nt_new(self); | |
1665 | if (noff == -1) |
|
1662 | if (noff == -1) | |
1666 | return -1; |
|
1663 | return -1; | |
1667 | /* self->nodes may have been changed by realloc */ |
|
1664 | /* self->nodes may have been changed by realloc */ | |
1668 | self->nodes[off].children[k] = noff; |
|
1665 | self->nodes[off].children[k] = noff; | |
1669 | off = noff; |
|
1666 | off = noff; | |
1670 | n = &self->nodes[off]; |
|
1667 | n = &self->nodes[off]; | |
1671 | n->children[nt_level(oldnode, ++level)] = v; |
|
1668 | n->children[nt_level(oldnode, ++level)] = v; | |
1672 | if (level > self->depth) |
|
1669 | if (level > self->depth) | |
1673 | self->depth = level; |
|
1670 | self->depth = level; | |
1674 | self->splits += 1; |
|
1671 | self->splits += 1; | |
1675 | } else { |
|
1672 | } else { | |
1676 | level += 1; |
|
1673 | level += 1; | |
1677 | off = v; |
|
1674 | off = v; | |
1678 | } |
|
1675 | } | |
1679 | } |
|
1676 | } | |
1680 |
|
1677 | |||
1681 | return -1; |
|
1678 | return -1; | |
1682 | } |
|
1679 | } | |
1683 |
|
1680 | |||
1684 | static PyObject *ntobj_insert(nodetreeObject *self, PyObject *args) |
|
1681 | static PyObject *ntobj_insert(nodetreeObject *self, PyObject *args) | |
1685 | { |
|
1682 | { | |
1686 | Py_ssize_t rev; |
|
1683 | Py_ssize_t rev; | |
1687 | const char *node; |
|
1684 | const char *node; | |
1688 | Py_ssize_t length; |
|
1685 | Py_ssize_t length; | |
1689 | if (!PyArg_ParseTuple(args, "n", &rev)) |
|
1686 | if (!PyArg_ParseTuple(args, "n", &rev)) | |
1690 | return NULL; |
|
1687 | return NULL; | |
1691 | length = index_length(self->nt.index); |
|
1688 | length = index_length(self->nt.index); | |
1692 | if (rev < 0 || rev >= length) { |
|
1689 | if (rev < 0 || rev >= length) { | |
1693 | PyErr_SetString(PyExc_ValueError, "revlog index out of range"); |
|
1690 | PyErr_SetString(PyExc_ValueError, "revlog index out of range"); | |
1694 | return NULL; |
|
1691 | return NULL; | |
1695 | } |
|
1692 | } | |
1696 | node = index_node_existing(self->nt.index, rev); |
|
1693 | node = index_node_existing(self->nt.index, rev); | |
1697 | if (nt_insert(&self->nt, node, (int)rev) == -1) |
|
1694 | if (nt_insert(&self->nt, node, (int)rev) == -1) | |
1698 | return NULL; |
|
1695 | return NULL; | |
1699 | Py_RETURN_NONE; |
|
1696 | Py_RETURN_NONE; | |
1700 | } |
|
1697 | } | |
1701 |
|
1698 | |||
1702 | static int nt_delete_node(nodetree *self, const char *node) |
|
1699 | static int nt_delete_node(nodetree *self, const char *node) | |
1703 | { |
|
1700 | { | |
1704 | /* rev==-2 happens to get encoded as 0, which is interpreted as not set |
|
1701 | /* rev==-2 happens to get encoded as 0, which is interpreted as not set | |
1705 | */ |
|
1702 | */ | |
1706 | return nt_insert(self, node, -2); |
|
1703 | return nt_insert(self, node, -2); | |
1707 | } |
|
1704 | } | |
1708 |
|
1705 | |||
1709 | static int nt_init(nodetree *self, indexObject *index, unsigned capacity) |
|
1706 | static int nt_init(nodetree *self, indexObject *index, unsigned capacity) | |
1710 | { |
|
1707 | { | |
1711 | /* Initialize before overflow-checking to avoid nt_dealloc() crash. */ |
|
1708 | /* Initialize before overflow-checking to avoid nt_dealloc() crash. */ | |
1712 | self->nodes = NULL; |
|
1709 | self->nodes = NULL; | |
1713 |
|
1710 | |||
1714 | self->index = index; |
|
1711 | self->index = index; | |
1715 | /* The input capacity is in terms of revisions, while the field is in |
|
1712 | /* The input capacity is in terms of revisions, while the field is in | |
1716 | * terms of nodetree nodes. */ |
|
1713 | * terms of nodetree nodes. */ | |
1717 | self->capacity = (capacity < 4 ? 4 : capacity / 2); |
|
1714 | self->capacity = (capacity < 4 ? 4 : capacity / 2); | |
1718 | self->depth = 0; |
|
1715 | self->depth = 0; | |
1719 | self->splits = 0; |
|
1716 | self->splits = 0; | |
1720 | if ((size_t)self->capacity > INT_MAX / sizeof(nodetreenode)) { |
|
1717 | if ((size_t)self->capacity > INT_MAX / sizeof(nodetreenode)) { | |
1721 | PyErr_SetString(PyExc_ValueError, "overflow in init_nt"); |
|
1718 | PyErr_SetString(PyExc_ValueError, "overflow in init_nt"); | |
1722 | return -1; |
|
1719 | return -1; | |
1723 | } |
|
1720 | } | |
1724 | self->nodes = calloc(self->capacity, sizeof(nodetreenode)); |
|
1721 | self->nodes = calloc(self->capacity, sizeof(nodetreenode)); | |
1725 | if (self->nodes == NULL) { |
|
1722 | if (self->nodes == NULL) { | |
1726 | PyErr_NoMemory(); |
|
1723 | PyErr_NoMemory(); | |
1727 | return -1; |
|
1724 | return -1; | |
1728 | } |
|
1725 | } | |
1729 | self->length = 1; |
|
1726 | self->length = 1; | |
1730 | return 0; |
|
1727 | return 0; | |
1731 | } |
|
1728 | } | |
1732 |
|
1729 | |||
1733 | static int ntobj_init(nodetreeObject *self, PyObject *args) |
|
1730 | static int ntobj_init(nodetreeObject *self, PyObject *args) | |
1734 | { |
|
1731 | { | |
1735 | PyObject *index; |
|
1732 | PyObject *index; | |
1736 | unsigned capacity; |
|
1733 | unsigned capacity; | |
1737 | if (!PyArg_ParseTuple(args, "O!I", &HgRevlogIndex_Type, &index, |
|
1734 | if (!PyArg_ParseTuple(args, "O!I", &HgRevlogIndex_Type, &index, | |
1738 | &capacity)) |
|
1735 | &capacity)) | |
1739 | return -1; |
|
1736 | return -1; | |
1740 | Py_INCREF(index); |
|
1737 | Py_INCREF(index); | |
1741 | return nt_init(&self->nt, (indexObject *)index, capacity); |
|
1738 | return nt_init(&self->nt, (indexObject *)index, capacity); | |
1742 | } |
|
1739 | } | |
1743 |
|
1740 | |||
1744 | static int nt_partialmatch(nodetree *self, const char *node, Py_ssize_t nodelen) |
|
1741 | static int nt_partialmatch(nodetree *self, const char *node, Py_ssize_t nodelen) | |
1745 | { |
|
1742 | { | |
1746 | return nt_find(self, node, nodelen, 1); |
|
1743 | return nt_find(self, node, nodelen, 1); | |
1747 | } |
|
1744 | } | |
1748 |
|
1745 | |||
1749 | /* |
|
1746 | /* | |
1750 | * Find the length of the shortest unique prefix of node. |
|
1747 | * Find the length of the shortest unique prefix of node. | |
1751 | * |
|
1748 | * | |
1752 | * Return values: |
|
1749 | * Return values: | |
1753 | * |
|
1750 | * | |
1754 | * -3: error (exception set) |
|
1751 | * -3: error (exception set) | |
1755 | * -2: not found (no exception set) |
|
1752 | * -2: not found (no exception set) | |
1756 | * rest: length of shortest prefix |
|
1753 | * rest: length of shortest prefix | |
1757 | */ |
|
1754 | */ | |
1758 | static int nt_shortest(nodetree *self, const char *node) |
|
1755 | static int nt_shortest(nodetree *self, const char *node) | |
1759 | { |
|
1756 | { | |
1760 | int level, off; |
|
1757 | int level, off; | |
1761 |
|
1758 | |||
1762 | for (level = off = 0; level < 40; level++) { |
|
1759 | for (level = off = 0; level < 40; level++) { | |
1763 | int k, v; |
|
1760 | int k, v; | |
1764 | nodetreenode *n = &self->nodes[off]; |
|
1761 | nodetreenode *n = &self->nodes[off]; | |
1765 | k = nt_level(node, level); |
|
1762 | k = nt_level(node, level); | |
1766 | v = n->children[k]; |
|
1763 | v = n->children[k]; | |
1767 | if (v < 0) { |
|
1764 | if (v < 0) { | |
1768 | const char *n; |
|
1765 | const char *n; | |
1769 | v = -(v + 2); |
|
1766 | v = -(v + 2); | |
1770 | n = index_node_existing(self->index, v); |
|
1767 | n = index_node_existing(self->index, v); | |
1771 | if (n == NULL) |
|
1768 | if (n == NULL) | |
1772 | return -3; |
|
1769 | return -3; | |
1773 | if (memcmp(node, n, 20) != 0) |
|
1770 | if (memcmp(node, n, 20) != 0) | |
1774 | /* |
|
1771 | /* | |
1775 | * Found a unique prefix, but it wasn't for the |
|
1772 | * Found a unique prefix, but it wasn't for the | |
1776 | * requested node (i.e the requested node does |
|
1773 | * requested node (i.e the requested node does | |
1777 | * not exist). |
|
1774 | * not exist). | |
1778 | */ |
|
1775 | */ | |
1779 | return -2; |
|
1776 | return -2; | |
1780 | return level + 1; |
|
1777 | return level + 1; | |
1781 | } |
|
1778 | } | |
1782 | if (v == 0) |
|
1779 | if (v == 0) | |
1783 | return -2; |
|
1780 | return -2; | |
1784 | off = v; |
|
1781 | off = v; | |
1785 | } |
|
1782 | } | |
1786 | /* |
|
1783 | /* | |
1787 | * The node was still not unique after 40 hex digits, so this won't |
|
1784 | * The node was still not unique after 40 hex digits, so this won't | |
1788 | * happen. Also, if we get here, then there's a programming error in |
|
1785 | * happen. Also, if we get here, then there's a programming error in | |
1789 | * this file that made us insert a node longer than 40 hex digits. |
|
1786 | * this file that made us insert a node longer than 40 hex digits. | |
1790 | */ |
|
1787 | */ | |
1791 | PyErr_SetString(PyExc_Exception, "broken node tree"); |
|
1788 | PyErr_SetString(PyExc_Exception, "broken node tree"); | |
1792 | return -3; |
|
1789 | return -3; | |
1793 | } |
|
1790 | } | |
1794 |
|
1791 | |||
1795 | static PyObject *ntobj_shortest(nodetreeObject *self, PyObject *args) |
|
1792 | static PyObject *ntobj_shortest(nodetreeObject *self, PyObject *args) | |
1796 | { |
|
1793 | { | |
1797 | PyObject *val; |
|
1794 | PyObject *val; | |
1798 | char *node; |
|
1795 | char *node; | |
1799 | int length; |
|
1796 | int length; | |
1800 |
|
1797 | |||
1801 | if (!PyArg_ParseTuple(args, "O", &val)) |
|
1798 | if (!PyArg_ParseTuple(args, "O", &val)) | |
1802 | return NULL; |
|
1799 | return NULL; | |
1803 | if (node_check(val, &node) == -1) |
|
1800 | if (node_check(val, &node) == -1) | |
1804 | return NULL; |
|
1801 | return NULL; | |
1805 |
|
1802 | |||
1806 | length = nt_shortest(&self->nt, node); |
|
1803 | length = nt_shortest(&self->nt, node); | |
1807 | if (length == -3) |
|
1804 | if (length == -3) | |
1808 | return NULL; |
|
1805 | return NULL; | |
1809 | if (length == -2) { |
|
1806 | if (length == -2) { | |
1810 | raise_revlog_error(); |
|
1807 | raise_revlog_error(); | |
1811 | return NULL; |
|
1808 | return NULL; | |
1812 | } |
|
1809 | } | |
1813 | return PyInt_FromLong(length); |
|
1810 | return PyInt_FromLong(length); | |
1814 | } |
|
1811 | } | |
1815 |
|
1812 | |||
1816 | static void nt_dealloc(nodetree *self) |
|
1813 | static void nt_dealloc(nodetree *self) | |
1817 | { |
|
1814 | { | |
1818 | free(self->nodes); |
|
1815 | free(self->nodes); | |
1819 | self->nodes = NULL; |
|
1816 | self->nodes = NULL; | |
1820 | } |
|
1817 | } | |
1821 |
|
1818 | |||
1822 | static void ntobj_dealloc(nodetreeObject *self) |
|
1819 | static void ntobj_dealloc(nodetreeObject *self) | |
1823 | { |
|
1820 | { | |
1824 | Py_XDECREF(self->nt.index); |
|
1821 | Py_XDECREF(self->nt.index); | |
1825 | nt_dealloc(&self->nt); |
|
1822 | nt_dealloc(&self->nt); | |
1826 | PyObject_Del(self); |
|
1823 | PyObject_Del(self); | |
1827 | } |
|
1824 | } | |
1828 |
|
1825 | |||
1829 | static PyMethodDef ntobj_methods[] = { |
|
1826 | static PyMethodDef ntobj_methods[] = { | |
1830 | {"insert", (PyCFunction)ntobj_insert, METH_VARARGS, |
|
1827 | {"insert", (PyCFunction)ntobj_insert, METH_VARARGS, | |
1831 | "insert an index entry"}, |
|
1828 | "insert an index entry"}, | |
1832 | {"shortest", (PyCFunction)ntobj_shortest, METH_VARARGS, |
|
1829 | {"shortest", (PyCFunction)ntobj_shortest, METH_VARARGS, | |
1833 | "find length of shortest hex nodeid of a binary ID"}, |
|
1830 | "find length of shortest hex nodeid of a binary ID"}, | |
1834 | {NULL} /* Sentinel */ |
|
1831 | {NULL} /* Sentinel */ | |
1835 | }; |
|
1832 | }; | |
1836 |
|
1833 | |||
1837 | static PyTypeObject nodetreeType = { |
|
1834 | static PyTypeObject nodetreeType = { | |
1838 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ |
|
1835 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ | |
1839 | "parsers.nodetree", /* tp_name */ |
|
1836 | "parsers.nodetree", /* tp_name */ | |
1840 | sizeof(nodetreeObject), /* tp_basicsize */ |
|
1837 | sizeof(nodetreeObject), /* tp_basicsize */ | |
1841 | 0, /* tp_itemsize */ |
|
1838 | 0, /* tp_itemsize */ | |
1842 | (destructor)ntobj_dealloc, /* tp_dealloc */ |
|
1839 | (destructor)ntobj_dealloc, /* tp_dealloc */ | |
1843 | 0, /* tp_print */ |
|
1840 | 0, /* tp_print */ | |
1844 | 0, /* tp_getattr */ |
|
1841 | 0, /* tp_getattr */ | |
1845 | 0, /* tp_setattr */ |
|
1842 | 0, /* tp_setattr */ | |
1846 | 0, /* tp_compare */ |
|
1843 | 0, /* tp_compare */ | |
1847 | 0, /* tp_repr */ |
|
1844 | 0, /* tp_repr */ | |
1848 | 0, /* tp_as_number */ |
|
1845 | 0, /* tp_as_number */ | |
1849 | 0, /* tp_as_sequence */ |
|
1846 | 0, /* tp_as_sequence */ | |
1850 | 0, /* tp_as_mapping */ |
|
1847 | 0, /* tp_as_mapping */ | |
1851 | 0, /* tp_hash */ |
|
1848 | 0, /* tp_hash */ | |
1852 | 0, /* tp_call */ |
|
1849 | 0, /* tp_call */ | |
1853 | 0, /* tp_str */ |
|
1850 | 0, /* tp_str */ | |
1854 | 0, /* tp_getattro */ |
|
1851 | 0, /* tp_getattro */ | |
1855 | 0, /* tp_setattro */ |
|
1852 | 0, /* tp_setattro */ | |
1856 | 0, /* tp_as_buffer */ |
|
1853 | 0, /* tp_as_buffer */ | |
1857 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
1854 | Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
1858 | "nodetree", /* tp_doc */ |
|
1855 | "nodetree", /* tp_doc */ | |
1859 | 0, /* tp_traverse */ |
|
1856 | 0, /* tp_traverse */ | |
1860 | 0, /* tp_clear */ |
|
1857 | 0, /* tp_clear */ | |
1861 | 0, /* tp_richcompare */ |
|
1858 | 0, /* tp_richcompare */ | |
1862 | 0, /* tp_weaklistoffset */ |
|
1859 | 0, /* tp_weaklistoffset */ | |
1863 | 0, /* tp_iter */ |
|
1860 | 0, /* tp_iter */ | |
1864 | 0, /* tp_iternext */ |
|
1861 | 0, /* tp_iternext */ | |
1865 | ntobj_methods, /* tp_methods */ |
|
1862 | ntobj_methods, /* tp_methods */ | |
1866 | 0, /* tp_members */ |
|
1863 | 0, /* tp_members */ | |
1867 | 0, /* tp_getset */ |
|
1864 | 0, /* tp_getset */ | |
1868 | 0, /* tp_base */ |
|
1865 | 0, /* tp_base */ | |
1869 | 0, /* tp_dict */ |
|
1866 | 0, /* tp_dict */ | |
1870 | 0, /* tp_descr_get */ |
|
1867 | 0, /* tp_descr_get */ | |
1871 | 0, /* tp_descr_set */ |
|
1868 | 0, /* tp_descr_set */ | |
1872 | 0, /* tp_dictoffset */ |
|
1869 | 0, /* tp_dictoffset */ | |
1873 | (initproc)ntobj_init, /* tp_init */ |
|
1870 | (initproc)ntobj_init, /* tp_init */ | |
1874 | 0, /* tp_alloc */ |
|
1871 | 0, /* tp_alloc */ | |
1875 | }; |
|
1872 | }; | |
1876 |
|
1873 | |||
1877 | static int index_init_nt(indexObject *self) |
|
1874 | static int index_init_nt(indexObject *self) | |
1878 | { |
|
1875 | { | |
1879 | if (!self->ntinitialized) { |
|
1876 | if (!self->ntinitialized) { | |
1880 | if (nt_init(&self->nt, self, (int)self->raw_length) == -1) { |
|
1877 | if (nt_init(&self->nt, self, (int)self->raw_length) == -1) { | |
1881 | nt_dealloc(&self->nt); |
|
1878 | nt_dealloc(&self->nt); | |
1882 | return -1; |
|
1879 | return -1; | |
1883 | } |
|
1880 | } | |
1884 | if (nt_insert(&self->nt, nullid, -1) == -1) { |
|
1881 | if (nt_insert(&self->nt, nullid, -1) == -1) { | |
1885 | nt_dealloc(&self->nt); |
|
1882 | nt_dealloc(&self->nt); | |
1886 | return -1; |
|
1883 | return -1; | |
1887 | } |
|
1884 | } | |
1888 | self->ntinitialized = 1; |
|
1885 | self->ntinitialized = 1; | |
1889 | self->ntrev = (int)index_length(self); |
|
1886 | self->ntrev = (int)index_length(self); | |
1890 | self->ntlookups = 1; |
|
1887 | self->ntlookups = 1; | |
1891 | self->ntmisses = 0; |
|
1888 | self->ntmisses = 0; | |
1892 | } |
|
1889 | } | |
1893 | return 0; |
|
1890 | return 0; | |
1894 | } |
|
1891 | } | |
1895 |
|
1892 | |||
1896 | /* |
|
1893 | /* | |
1897 | * Return values: |
|
1894 | * Return values: | |
1898 | * |
|
1895 | * | |
1899 | * -3: error (exception set) |
|
1896 | * -3: error (exception set) | |
1900 | * -2: not found (no exception set) |
|
1897 | * -2: not found (no exception set) | |
1901 | * rest: valid rev |
|
1898 | * rest: valid rev | |
1902 | */ |
|
1899 | */ | |
1903 | static int index_find_node(indexObject *self, const char *node, |
|
1900 | static int index_find_node(indexObject *self, const char *node, | |
1904 | Py_ssize_t nodelen) |
|
1901 | Py_ssize_t nodelen) | |
1905 | { |
|
1902 | { | |
1906 | int rev; |
|
1903 | int rev; | |
1907 |
|
1904 | |||
1908 | if (index_init_nt(self) == -1) |
|
1905 | if (index_init_nt(self) == -1) | |
1909 | return -3; |
|
1906 | return -3; | |
1910 |
|
1907 | |||
1911 | self->ntlookups++; |
|
1908 | self->ntlookups++; | |
1912 | rev = nt_find(&self->nt, node, nodelen, 0); |
|
1909 | rev = nt_find(&self->nt, node, nodelen, 0); | |
1913 | if (rev >= -1) |
|
1910 | if (rev >= -1) | |
1914 | return rev; |
|
1911 | return rev; | |
1915 |
|
1912 | |||
1916 | /* |
|
1913 | /* | |
1917 | * For the first handful of lookups, we scan the entire index, |
|
1914 | * For the first handful of lookups, we scan the entire index, | |
1918 | * and cache only the matching nodes. This optimizes for cases |
|
1915 | * and cache only the matching nodes. This optimizes for cases | |
1919 | * like "hg tip", where only a few nodes are accessed. |
|
1916 | * like "hg tip", where only a few nodes are accessed. | |
1920 | * |
|
1917 | * | |
1921 | * After that, we cache every node we visit, using a single |
|
1918 | * After that, we cache every node we visit, using a single | |
1922 | * scan amortized over multiple lookups. This gives the best |
|
1919 | * scan amortized over multiple lookups. This gives the best | |
1923 | * bulk performance, e.g. for "hg log". |
|
1920 | * bulk performance, e.g. for "hg log". | |
1924 | */ |
|
1921 | */ | |
1925 | if (self->ntmisses++ < 4) { |
|
1922 | if (self->ntmisses++ < 4) { | |
1926 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
1923 | for (rev = self->ntrev - 1; rev >= 0; rev--) { | |
1927 | const char *n = index_node_existing(self, rev); |
|
1924 | const char *n = index_node_existing(self, rev); | |
1928 | if (n == NULL) |
|
1925 | if (n == NULL) | |
1929 | return -3; |
|
1926 | return -3; | |
1930 | if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) { |
|
1927 | if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) { | |
1931 | if (nt_insert(&self->nt, n, rev) == -1) |
|
1928 | if (nt_insert(&self->nt, n, rev) == -1) | |
1932 | return -3; |
|
1929 | return -3; | |
1933 | break; |
|
1930 | break; | |
1934 | } |
|
1931 | } | |
1935 | } |
|
1932 | } | |
1936 | } else { |
|
1933 | } else { | |
1937 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
1934 | for (rev = self->ntrev - 1; rev >= 0; rev--) { | |
1938 | const char *n = index_node_existing(self, rev); |
|
1935 | const char *n = index_node_existing(self, rev); | |
1939 | if (n == NULL) |
|
1936 | if (n == NULL) | |
1940 | return -3; |
|
1937 | return -3; | |
1941 | if (nt_insert(&self->nt, n, rev) == -1) { |
|
1938 | if (nt_insert(&self->nt, n, rev) == -1) { | |
1942 | self->ntrev = rev + 1; |
|
1939 | self->ntrev = rev + 1; | |
1943 | return -3; |
|
1940 | return -3; | |
1944 | } |
|
1941 | } | |
1945 | if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) { |
|
1942 | if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) { | |
1946 | break; |
|
1943 | break; | |
1947 | } |
|
1944 | } | |
1948 | } |
|
1945 | } | |
1949 | self->ntrev = rev; |
|
1946 | self->ntrev = rev; | |
1950 | } |
|
1947 | } | |
1951 |
|
1948 | |||
1952 | if (rev >= 0) |
|
1949 | if (rev >= 0) | |
1953 | return rev; |
|
1950 | return rev; | |
1954 | return -2; |
|
1951 | return -2; | |
1955 | } |
|
1952 | } | |
1956 |
|
1953 | |||
1957 | static PyObject *index_getitem(indexObject *self, PyObject *value) |
|
1954 | static PyObject *index_getitem(indexObject *self, PyObject *value) | |
1958 | { |
|
1955 | { | |
1959 | char *node; |
|
1956 | char *node; | |
1960 | int rev; |
|
1957 | int rev; | |
1961 |
|
1958 | |||
1962 | if (PyInt_Check(value)) { |
|
1959 | if (PyInt_Check(value)) { | |
1963 | long idx; |
|
1960 | long idx; | |
1964 | if (!pylong_to_long(value, &idx)) { |
|
1961 | if (!pylong_to_long(value, &idx)) { | |
1965 | return NULL; |
|
1962 | return NULL; | |
1966 | } |
|
1963 | } | |
1967 | return index_get(self, idx); |
|
1964 | return index_get(self, idx); | |
1968 | } |
|
1965 | } | |
1969 |
|
1966 | |||
1970 | if (node_check(value, &node) == -1) |
|
1967 | if (node_check(value, &node) == -1) | |
1971 | return NULL; |
|
1968 | return NULL; | |
1972 | rev = index_find_node(self, node, 20); |
|
1969 | rev = index_find_node(self, node, 20); | |
1973 | if (rev >= -1) |
|
1970 | if (rev >= -1) | |
1974 | return PyInt_FromLong(rev); |
|
1971 | return PyInt_FromLong(rev); | |
1975 | if (rev == -2) |
|
1972 | if (rev == -2) | |
1976 | raise_revlog_error(); |
|
1973 | raise_revlog_error(); | |
1977 | return NULL; |
|
1974 | return NULL; | |
1978 | } |
|
1975 | } | |
1979 |
|
1976 | |||
1980 | /* |
|
1977 | /* | |
1981 | * Fully populate the radix tree. |
|
1978 | * Fully populate the radix tree. | |
1982 | */ |
|
1979 | */ | |
1983 | static int index_populate_nt(indexObject *self) |
|
1980 | static int index_populate_nt(indexObject *self) | |
1984 | { |
|
1981 | { | |
1985 | int rev; |
|
1982 | int rev; | |
1986 | if (self->ntrev > 0) { |
|
1983 | if (self->ntrev > 0) { | |
1987 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
1984 | for (rev = self->ntrev - 1; rev >= 0; rev--) { | |
1988 | const char *n = index_node_existing(self, rev); |
|
1985 | const char *n = index_node_existing(self, rev); | |
1989 | if (n == NULL) |
|
1986 | if (n == NULL) | |
1990 | return -1; |
|
1987 | return -1; | |
1991 | if (nt_insert(&self->nt, n, rev) == -1) |
|
1988 | if (nt_insert(&self->nt, n, rev) == -1) | |
1992 | return -1; |
|
1989 | return -1; | |
1993 | } |
|
1990 | } | |
1994 | self->ntrev = -1; |
|
1991 | self->ntrev = -1; | |
1995 | } |
|
1992 | } | |
1996 | return 0; |
|
1993 | return 0; | |
1997 | } |
|
1994 | } | |
1998 |
|
1995 | |||
1999 | static PyObject *index_partialmatch(indexObject *self, PyObject *args) |
|
1996 | static PyObject *index_partialmatch(indexObject *self, PyObject *args) | |
2000 | { |
|
1997 | { | |
2001 | const char *fullnode; |
|
1998 | const char *fullnode; | |
2002 | Py_ssize_t nodelen; |
|
1999 | Py_ssize_t nodelen; | |
2003 | char *node; |
|
2000 | char *node; | |
2004 | int rev, i; |
|
2001 | int rev, i; | |
2005 |
|
2002 | |||
2006 | if (!PyArg_ParseTuple(args, PY23("s#", "y#"), &node, &nodelen)) |
|
2003 | if (!PyArg_ParseTuple(args, PY23("s#", "y#"), &node, &nodelen)) | |
2007 | return NULL; |
|
2004 | return NULL; | |
2008 |
|
2005 | |||
2009 | if (nodelen < 1) { |
|
2006 | if (nodelen < 1) { | |
2010 | PyErr_SetString(PyExc_ValueError, "key too short"); |
|
2007 | PyErr_SetString(PyExc_ValueError, "key too short"); | |
2011 | return NULL; |
|
2008 | return NULL; | |
2012 | } |
|
2009 | } | |
2013 |
|
2010 | |||
2014 | if (nodelen > 40) { |
|
2011 | if (nodelen > 40) { | |
2015 | PyErr_SetString(PyExc_ValueError, "key too long"); |
|
2012 | PyErr_SetString(PyExc_ValueError, "key too long"); | |
2016 | return NULL; |
|
2013 | return NULL; | |
2017 | } |
|
2014 | } | |
2018 |
|
2015 | |||
2019 | for (i = 0; i < nodelen; i++) |
|
2016 | for (i = 0; i < nodelen; i++) | |
2020 | hexdigit(node, i); |
|
2017 | hexdigit(node, i); | |
2021 | if (PyErr_Occurred()) { |
|
2018 | if (PyErr_Occurred()) { | |
2022 | /* input contains non-hex characters */ |
|
2019 | /* input contains non-hex characters */ | |
2023 | PyErr_Clear(); |
|
2020 | PyErr_Clear(); | |
2024 | Py_RETURN_NONE; |
|
2021 | Py_RETURN_NONE; | |
2025 | } |
|
2022 | } | |
2026 |
|
2023 | |||
2027 | if (index_init_nt(self) == -1) |
|
2024 | if (index_init_nt(self) == -1) | |
2028 | return NULL; |
|
2025 | return NULL; | |
2029 | if (index_populate_nt(self) == -1) |
|
2026 | if (index_populate_nt(self) == -1) | |
2030 | return NULL; |
|
2027 | return NULL; | |
2031 | rev = nt_partialmatch(&self->nt, node, nodelen); |
|
2028 | rev = nt_partialmatch(&self->nt, node, nodelen); | |
2032 |
|
2029 | |||
2033 | switch (rev) { |
|
2030 | switch (rev) { | |
2034 | case -4: |
|
2031 | case -4: | |
2035 | raise_revlog_error(); |
|
2032 | raise_revlog_error(); | |
2036 | return NULL; |
|
2033 | return NULL; | |
2037 | case -2: |
|
2034 | case -2: | |
2038 | Py_RETURN_NONE; |
|
2035 | Py_RETURN_NONE; | |
2039 | case -1: |
|
2036 | case -1: | |
2040 | return PyBytes_FromStringAndSize(nullid, 20); |
|
2037 | return PyBytes_FromStringAndSize(nullid, 20); | |
2041 | } |
|
2038 | } | |
2042 |
|
2039 | |||
2043 | fullnode = index_node_existing(self, rev); |
|
2040 | fullnode = index_node_existing(self, rev); | |
2044 | if (fullnode == NULL) { |
|
2041 | if (fullnode == NULL) { | |
2045 | return NULL; |
|
2042 | return NULL; | |
2046 | } |
|
2043 | } | |
2047 | return PyBytes_FromStringAndSize(fullnode, 20); |
|
2044 | return PyBytes_FromStringAndSize(fullnode, 20); | |
2048 | } |
|
2045 | } | |
2049 |
|
2046 | |||
2050 | static PyObject *index_shortest(indexObject *self, PyObject *args) |
|
2047 | static PyObject *index_shortest(indexObject *self, PyObject *args) | |
2051 | { |
|
2048 | { | |
2052 | PyObject *val; |
|
2049 | PyObject *val; | |
2053 | char *node; |
|
2050 | char *node; | |
2054 | int length; |
|
2051 | int length; | |
2055 |
|
2052 | |||
2056 | if (!PyArg_ParseTuple(args, "O", &val)) |
|
2053 | if (!PyArg_ParseTuple(args, "O", &val)) | |
2057 | return NULL; |
|
2054 | return NULL; | |
2058 | if (node_check(val, &node) == -1) |
|
2055 | if (node_check(val, &node) == -1) | |
2059 | return NULL; |
|
2056 | return NULL; | |
2060 |
|
2057 | |||
2061 | self->ntlookups++; |
|
2058 | self->ntlookups++; | |
2062 | if (index_init_nt(self) == -1) |
|
2059 | if (index_init_nt(self) == -1) | |
2063 | return NULL; |
|
2060 | return NULL; | |
2064 | if (index_populate_nt(self) == -1) |
|
2061 | if (index_populate_nt(self) == -1) | |
2065 | return NULL; |
|
2062 | return NULL; | |
2066 | length = nt_shortest(&self->nt, node); |
|
2063 | length = nt_shortest(&self->nt, node); | |
2067 | if (length == -3) |
|
2064 | if (length == -3) | |
2068 | return NULL; |
|
2065 | return NULL; | |
2069 | if (length == -2) { |
|
2066 | if (length == -2) { | |
2070 | raise_revlog_error(); |
|
2067 | raise_revlog_error(); | |
2071 | return NULL; |
|
2068 | return NULL; | |
2072 | } |
|
2069 | } | |
2073 | return PyInt_FromLong(length); |
|
2070 | return PyInt_FromLong(length); | |
2074 | } |
|
2071 | } | |
2075 |
|
2072 | |||
2076 | static PyObject *index_m_get(indexObject *self, PyObject *args) |
|
2073 | static PyObject *index_m_get(indexObject *self, PyObject *args) | |
2077 | { |
|
2074 | { | |
2078 | PyObject *val; |
|
2075 | PyObject *val; | |
2079 | char *node; |
|
2076 | char *node; | |
2080 | int rev; |
|
2077 | int rev; | |
2081 |
|
2078 | |||
2082 | if (!PyArg_ParseTuple(args, "O", &val)) |
|
2079 | if (!PyArg_ParseTuple(args, "O", &val)) | |
2083 | return NULL; |
|
2080 | return NULL; | |
2084 | if (node_check(val, &node) == -1) |
|
2081 | if (node_check(val, &node) == -1) | |
2085 | return NULL; |
|
2082 | return NULL; | |
2086 | rev = index_find_node(self, node, 20); |
|
2083 | rev = index_find_node(self, node, 20); | |
2087 | if (rev == -3) |
|
2084 | if (rev == -3) | |
2088 | return NULL; |
|
2085 | return NULL; | |
2089 | if (rev == -2) |
|
2086 | if (rev == -2) | |
2090 | Py_RETURN_NONE; |
|
2087 | Py_RETURN_NONE; | |
2091 | return PyInt_FromLong(rev); |
|
2088 | return PyInt_FromLong(rev); | |
2092 | } |
|
2089 | } | |
2093 |
|
2090 | |||
2094 | static int index_contains(indexObject *self, PyObject *value) |
|
2091 | static int index_contains(indexObject *self, PyObject *value) | |
2095 | { |
|
2092 | { | |
2096 | char *node; |
|
2093 | char *node; | |
2097 |
|
2094 | |||
2098 | if (PyInt_Check(value)) { |
|
2095 | if (PyInt_Check(value)) { | |
2099 | long rev; |
|
2096 | long rev; | |
2100 | if (!pylong_to_long(value, &rev)) { |
|
2097 | if (!pylong_to_long(value, &rev)) { | |
2101 | return -1; |
|
2098 | return -1; | |
2102 | } |
|
2099 | } | |
2103 | return rev >= -1 && rev < index_length(self); |
|
2100 | return rev >= -1 && rev < index_length(self); | |
2104 | } |
|
2101 | } | |
2105 |
|
2102 | |||
2106 | if (node_check(value, &node) == -1) |
|
2103 | if (node_check(value, &node) == -1) | |
2107 | return -1; |
|
2104 | return -1; | |
2108 |
|
2105 | |||
2109 | switch (index_find_node(self, node, 20)) { |
|
2106 | switch (index_find_node(self, node, 20)) { | |
2110 | case -3: |
|
2107 | case -3: | |
2111 | return -1; |
|
2108 | return -1; | |
2112 | case -2: |
|
2109 | case -2: | |
2113 | return 0; |
|
2110 | return 0; | |
2114 | default: |
|
2111 | default: | |
2115 | return 1; |
|
2112 | return 1; | |
2116 | } |
|
2113 | } | |
2117 | } |
|
2114 | } | |
2118 |
|
2115 | |||
2119 | static PyObject *index_m_has_node(indexObject *self, PyObject *args) |
|
2116 | static PyObject *index_m_has_node(indexObject *self, PyObject *args) | |
2120 | { |
|
2117 | { | |
2121 | int ret = index_contains(self, args); |
|
2118 | int ret = index_contains(self, args); | |
2122 | if (ret < 0) |
|
2119 | if (ret < 0) | |
2123 | return NULL; |
|
2120 | return NULL; | |
2124 | return PyBool_FromLong((long)ret); |
|
2121 | return PyBool_FromLong((long)ret); | |
2125 | } |
|
2122 | } | |
2126 |
|
2123 | |||
2127 | static PyObject *index_m_rev(indexObject *self, PyObject *val) |
|
2124 | static PyObject *index_m_rev(indexObject *self, PyObject *val) | |
2128 | { |
|
2125 | { | |
2129 | char *node; |
|
2126 | char *node; | |
2130 | int rev; |
|
2127 | int rev; | |
2131 |
|
2128 | |||
2132 | if (node_check(val, &node) == -1) |
|
2129 | if (node_check(val, &node) == -1) | |
2133 | return NULL; |
|
2130 | return NULL; | |
2134 | rev = index_find_node(self, node, 20); |
|
2131 | rev = index_find_node(self, node, 20); | |
2135 | if (rev >= -1) |
|
2132 | if (rev >= -1) | |
2136 | return PyInt_FromLong(rev); |
|
2133 | return PyInt_FromLong(rev); | |
2137 | if (rev == -2) |
|
2134 | if (rev == -2) | |
2138 | raise_revlog_error(); |
|
2135 | raise_revlog_error(); | |
2139 | return NULL; |
|
2136 | return NULL; | |
2140 | } |
|
2137 | } | |
2141 |
|
2138 | |||
2142 | typedef uint64_t bitmask; |
|
2139 | typedef uint64_t bitmask; | |
2143 |
|
2140 | |||
2144 | /* |
|
2141 | /* | |
2145 | * Given a disjoint set of revs, return all candidates for the |
|
2142 | * Given a disjoint set of revs, return all candidates for the | |
2146 | * greatest common ancestor. In revset notation, this is the set |
|
2143 | * greatest common ancestor. In revset notation, this is the set | |
2147 | * "heads(::a and ::b and ...)" |
|
2144 | * "heads(::a and ::b and ...)" | |
2148 | */ |
|
2145 | */ | |
2149 | static PyObject *find_gca_candidates(indexObject *self, const int *revs, |
|
2146 | static PyObject *find_gca_candidates(indexObject *self, const int *revs, | |
2150 | int revcount) |
|
2147 | int revcount) | |
2151 | { |
|
2148 | { | |
2152 | const bitmask allseen = (1ull << revcount) - 1; |
|
2149 | const bitmask allseen = (1ull << revcount) - 1; | |
2153 | const bitmask poison = 1ull << revcount; |
|
2150 | const bitmask poison = 1ull << revcount; | |
2154 | PyObject *gca = PyList_New(0); |
|
2151 | PyObject *gca = PyList_New(0); | |
2155 | int i, v, interesting; |
|
2152 | int i, v, interesting; | |
2156 | int maxrev = -1; |
|
2153 | int maxrev = -1; | |
2157 | bitmask sp; |
|
2154 | bitmask sp; | |
2158 | bitmask *seen; |
|
2155 | bitmask *seen; | |
2159 |
|
2156 | |||
2160 | if (gca == NULL) |
|
2157 | if (gca == NULL) | |
2161 | return PyErr_NoMemory(); |
|
2158 | return PyErr_NoMemory(); | |
2162 |
|
2159 | |||
2163 | for (i = 0; i < revcount; i++) { |
|
2160 | for (i = 0; i < revcount; i++) { | |
2164 | if (revs[i] > maxrev) |
|
2161 | if (revs[i] > maxrev) | |
2165 | maxrev = revs[i]; |
|
2162 | maxrev = revs[i]; | |
2166 | } |
|
2163 | } | |
2167 |
|
2164 | |||
2168 | seen = calloc(sizeof(*seen), maxrev + 1); |
|
2165 | seen = calloc(sizeof(*seen), maxrev + 1); | |
2169 | if (seen == NULL) { |
|
2166 | if (seen == NULL) { | |
2170 | Py_DECREF(gca); |
|
2167 | Py_DECREF(gca); | |
2171 | return PyErr_NoMemory(); |
|
2168 | return PyErr_NoMemory(); | |
2172 | } |
|
2169 | } | |
2173 |
|
2170 | |||
2174 | for (i = 0; i < revcount; i++) |
|
2171 | for (i = 0; i < revcount; i++) | |
2175 | seen[revs[i]] = 1ull << i; |
|
2172 | seen[revs[i]] = 1ull << i; | |
2176 |
|
2173 | |||
2177 | interesting = revcount; |
|
2174 | interesting = revcount; | |
2178 |
|
2175 | |||
2179 | for (v = maxrev; v >= 0 && interesting; v--) { |
|
2176 | for (v = maxrev; v >= 0 && interesting; v--) { | |
2180 | bitmask sv = seen[v]; |
|
2177 | bitmask sv = seen[v]; | |
2181 | int parents[2]; |
|
2178 | int parents[2]; | |
2182 |
|
2179 | |||
2183 | if (!sv) |
|
2180 | if (!sv) | |
2184 | continue; |
|
2181 | continue; | |
2185 |
|
2182 | |||
2186 | if (sv < poison) { |
|
2183 | if (sv < poison) { | |
2187 | interesting -= 1; |
|
2184 | interesting -= 1; | |
2188 | if (sv == allseen) { |
|
2185 | if (sv == allseen) { | |
2189 | PyObject *obj = PyInt_FromLong(v); |
|
2186 | PyObject *obj = PyInt_FromLong(v); | |
2190 | if (obj == NULL) |
|
2187 | if (obj == NULL) | |
2191 | goto bail; |
|
2188 | goto bail; | |
2192 | if (PyList_Append(gca, obj) == -1) { |
|
2189 | if (PyList_Append(gca, obj) == -1) { | |
2193 | Py_DECREF(obj); |
|
2190 | Py_DECREF(obj); | |
2194 | goto bail; |
|
2191 | goto bail; | |
2195 | } |
|
2192 | } | |
2196 | sv |= poison; |
|
2193 | sv |= poison; | |
2197 | for (i = 0; i < revcount; i++) { |
|
2194 | for (i = 0; i < revcount; i++) { | |
2198 | if (revs[i] == v) |
|
2195 | if (revs[i] == v) | |
2199 | goto done; |
|
2196 | goto done; | |
2200 | } |
|
2197 | } | |
2201 | } |
|
2198 | } | |
2202 | } |
|
2199 | } | |
2203 | if (index_get_parents(self, v, parents, maxrev) < 0) |
|
2200 | if (index_get_parents(self, v, parents, maxrev) < 0) | |
2204 | goto bail; |
|
2201 | goto bail; | |
2205 |
|
2202 | |||
2206 | for (i = 0; i < 2; i++) { |
|
2203 | for (i = 0; i < 2; i++) { | |
2207 | int p = parents[i]; |
|
2204 | int p = parents[i]; | |
2208 | if (p == -1) |
|
2205 | if (p == -1) | |
2209 | continue; |
|
2206 | continue; | |
2210 | sp = seen[p]; |
|
2207 | sp = seen[p]; | |
2211 | if (sv < poison) { |
|
2208 | if (sv < poison) { | |
2212 | if (sp == 0) { |
|
2209 | if (sp == 0) { | |
2213 | seen[p] = sv; |
|
2210 | seen[p] = sv; | |
2214 | interesting++; |
|
2211 | interesting++; | |
2215 | } else if (sp != sv) |
|
2212 | } else if (sp != sv) | |
2216 | seen[p] |= sv; |
|
2213 | seen[p] |= sv; | |
2217 | } else { |
|
2214 | } else { | |
2218 | if (sp && sp < poison) |
|
2215 | if (sp && sp < poison) | |
2219 | interesting--; |
|
2216 | interesting--; | |
2220 | seen[p] = sv; |
|
2217 | seen[p] = sv; | |
2221 | } |
|
2218 | } | |
2222 | } |
|
2219 | } | |
2223 | } |
|
2220 | } | |
2224 |
|
2221 | |||
2225 | done: |
|
2222 | done: | |
2226 | free(seen); |
|
2223 | free(seen); | |
2227 | return gca; |
|
2224 | return gca; | |
2228 | bail: |
|
2225 | bail: | |
2229 | free(seen); |
|
2226 | free(seen); | |
2230 | Py_XDECREF(gca); |
|
2227 | Py_XDECREF(gca); | |
2231 | return NULL; |
|
2228 | return NULL; | |
2232 | } |
|
2229 | } | |
2233 |
|
2230 | |||
2234 | /* |
|
2231 | /* | |
2235 | * Given a disjoint set of revs, return the subset with the longest |
|
2232 | * Given a disjoint set of revs, return the subset with the longest | |
2236 | * path to the root. |
|
2233 | * path to the root. | |
2237 | */ |
|
2234 | */ | |
2238 | static PyObject *find_deepest(indexObject *self, PyObject *revs) |
|
2235 | static PyObject *find_deepest(indexObject *self, PyObject *revs) | |
2239 | { |
|
2236 | { | |
2240 | const Py_ssize_t revcount = PyList_GET_SIZE(revs); |
|
2237 | const Py_ssize_t revcount = PyList_GET_SIZE(revs); | |
2241 | static const Py_ssize_t capacity = 24; |
|
2238 | static const Py_ssize_t capacity = 24; | |
2242 | int *depth, *interesting = NULL; |
|
2239 | int *depth, *interesting = NULL; | |
2243 | int i, j, v, ninteresting; |
|
2240 | int i, j, v, ninteresting; | |
2244 | PyObject *dict = NULL, *keys = NULL; |
|
2241 | PyObject *dict = NULL, *keys = NULL; | |
2245 | long *seen = NULL; |
|
2242 | long *seen = NULL; | |
2246 | int maxrev = -1; |
|
2243 | int maxrev = -1; | |
2247 | long final; |
|
2244 | long final; | |
2248 |
|
2245 | |||
2249 | if (revcount > capacity) { |
|
2246 | if (revcount > capacity) { | |
2250 | PyErr_Format(PyExc_OverflowError, |
|
2247 | PyErr_Format(PyExc_OverflowError, | |
2251 | "bitset size (%ld) > capacity (%ld)", |
|
2248 | "bitset size (%ld) > capacity (%ld)", | |
2252 | (long)revcount, (long)capacity); |
|
2249 | (long)revcount, (long)capacity); | |
2253 | return NULL; |
|
2250 | return NULL; | |
2254 | } |
|
2251 | } | |
2255 |
|
2252 | |||
2256 | for (i = 0; i < revcount; i++) { |
|
2253 | for (i = 0; i < revcount; i++) { | |
2257 | int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i)); |
|
2254 | int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i)); | |
2258 | if (n > maxrev) |
|
2255 | if (n > maxrev) | |
2259 | maxrev = n; |
|
2256 | maxrev = n; | |
2260 | } |
|
2257 | } | |
2261 |
|
2258 | |||
2262 | depth = calloc(sizeof(*depth), maxrev + 1); |
|
2259 | depth = calloc(sizeof(*depth), maxrev + 1); | |
2263 | if (depth == NULL) |
|
2260 | if (depth == NULL) | |
2264 | return PyErr_NoMemory(); |
|
2261 | return PyErr_NoMemory(); | |
2265 |
|
2262 | |||
2266 | seen = calloc(sizeof(*seen), maxrev + 1); |
|
2263 | seen = calloc(sizeof(*seen), maxrev + 1); | |
2267 | if (seen == NULL) { |
|
2264 | if (seen == NULL) { | |
2268 | PyErr_NoMemory(); |
|
2265 | PyErr_NoMemory(); | |
2269 | goto bail; |
|
2266 | goto bail; | |
2270 | } |
|
2267 | } | |
2271 |
|
2268 | |||
2272 | interesting = calloc(sizeof(*interesting), ((size_t)1) << revcount); |
|
2269 | interesting = calloc(sizeof(*interesting), ((size_t)1) << revcount); | |
2273 | if (interesting == NULL) { |
|
2270 | if (interesting == NULL) { | |
2274 | PyErr_NoMemory(); |
|
2271 | PyErr_NoMemory(); | |
2275 | goto bail; |
|
2272 | goto bail; | |
2276 | } |
|
2273 | } | |
2277 |
|
2274 | |||
2278 | if (PyList_Sort(revs) == -1) |
|
2275 | if (PyList_Sort(revs) == -1) | |
2279 | goto bail; |
|
2276 | goto bail; | |
2280 |
|
2277 | |||
2281 | for (i = 0; i < revcount; i++) { |
|
2278 | for (i = 0; i < revcount; i++) { | |
2282 | int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i)); |
|
2279 | int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i)); | |
2283 | long b = 1l << i; |
|
2280 | long b = 1l << i; | |
2284 | depth[n] = 1; |
|
2281 | depth[n] = 1; | |
2285 | seen[n] = b; |
|
2282 | seen[n] = b; | |
2286 | interesting[b] = 1; |
|
2283 | interesting[b] = 1; | |
2287 | } |
|
2284 | } | |
2288 |
|
2285 | |||
2289 | /* invariant: ninteresting is the number of non-zero entries in |
|
2286 | /* invariant: ninteresting is the number of non-zero entries in | |
2290 | * interesting. */ |
|
2287 | * interesting. */ | |
2291 | ninteresting = (int)revcount; |
|
2288 | ninteresting = (int)revcount; | |
2292 |
|
2289 | |||
2293 | for (v = maxrev; v >= 0 && ninteresting > 1; v--) { |
|
2290 | for (v = maxrev; v >= 0 && ninteresting > 1; v--) { | |
2294 | int dv = depth[v]; |
|
2291 | int dv = depth[v]; | |
2295 | int parents[2]; |
|
2292 | int parents[2]; | |
2296 | long sv; |
|
2293 | long sv; | |
2297 |
|
2294 | |||
2298 | if (dv == 0) |
|
2295 | if (dv == 0) | |
2299 | continue; |
|
2296 | continue; | |
2300 |
|
2297 | |||
2301 | sv = seen[v]; |
|
2298 | sv = seen[v]; | |
2302 | if (index_get_parents(self, v, parents, maxrev) < 0) |
|
2299 | if (index_get_parents(self, v, parents, maxrev) < 0) | |
2303 | goto bail; |
|
2300 | goto bail; | |
2304 |
|
2301 | |||
2305 | for (i = 0; i < 2; i++) { |
|
2302 | for (i = 0; i < 2; i++) { | |
2306 | int p = parents[i]; |
|
2303 | int p = parents[i]; | |
2307 | long sp; |
|
2304 | long sp; | |
2308 | int dp; |
|
2305 | int dp; | |
2309 |
|
2306 | |||
2310 | if (p == -1) |
|
2307 | if (p == -1) | |
2311 | continue; |
|
2308 | continue; | |
2312 |
|
2309 | |||
2313 | dp = depth[p]; |
|
2310 | dp = depth[p]; | |
2314 | sp = seen[p]; |
|
2311 | sp = seen[p]; | |
2315 | if (dp <= dv) { |
|
2312 | if (dp <= dv) { | |
2316 | depth[p] = dv + 1; |
|
2313 | depth[p] = dv + 1; | |
2317 | if (sp != sv) { |
|
2314 | if (sp != sv) { | |
2318 | interesting[sv] += 1; |
|
2315 | interesting[sv] += 1; | |
2319 | seen[p] = sv; |
|
2316 | seen[p] = sv; | |
2320 | if (sp) { |
|
2317 | if (sp) { | |
2321 | interesting[sp] -= 1; |
|
2318 | interesting[sp] -= 1; | |
2322 | if (interesting[sp] == 0) |
|
2319 | if (interesting[sp] == 0) | |
2323 | ninteresting -= 1; |
|
2320 | ninteresting -= 1; | |
2324 | } |
|
2321 | } | |
2325 | } |
|
2322 | } | |
2326 | } else if (dv == dp - 1) { |
|
2323 | } else if (dv == dp - 1) { | |
2327 | long nsp = sp | sv; |
|
2324 | long nsp = sp | sv; | |
2328 | if (nsp == sp) |
|
2325 | if (nsp == sp) | |
2329 | continue; |
|
2326 | continue; | |
2330 | seen[p] = nsp; |
|
2327 | seen[p] = nsp; | |
2331 | interesting[sp] -= 1; |
|
2328 | interesting[sp] -= 1; | |
2332 | if (interesting[sp] == 0) |
|
2329 | if (interesting[sp] == 0) | |
2333 | ninteresting -= 1; |
|
2330 | ninteresting -= 1; | |
2334 | if (interesting[nsp] == 0) |
|
2331 | if (interesting[nsp] == 0) | |
2335 | ninteresting += 1; |
|
2332 | ninteresting += 1; | |
2336 | interesting[nsp] += 1; |
|
2333 | interesting[nsp] += 1; | |
2337 | } |
|
2334 | } | |
2338 | } |
|
2335 | } | |
2339 | interesting[sv] -= 1; |
|
2336 | interesting[sv] -= 1; | |
2340 | if (interesting[sv] == 0) |
|
2337 | if (interesting[sv] == 0) | |
2341 | ninteresting -= 1; |
|
2338 | ninteresting -= 1; | |
2342 | } |
|
2339 | } | |
2343 |
|
2340 | |||
2344 | final = 0; |
|
2341 | final = 0; | |
2345 | j = ninteresting; |
|
2342 | j = ninteresting; | |
2346 | for (i = 0; i < (int)(2 << revcount) && j > 0; i++) { |
|
2343 | for (i = 0; i < (int)(2 << revcount) && j > 0; i++) { | |
2347 | if (interesting[i] == 0) |
|
2344 | if (interesting[i] == 0) | |
2348 | continue; |
|
2345 | continue; | |
2349 | final |= i; |
|
2346 | final |= i; | |
2350 | j -= 1; |
|
2347 | j -= 1; | |
2351 | } |
|
2348 | } | |
2352 | if (final == 0) { |
|
2349 | if (final == 0) { | |
2353 | keys = PyList_New(0); |
|
2350 | keys = PyList_New(0); | |
2354 | goto bail; |
|
2351 | goto bail; | |
2355 | } |
|
2352 | } | |
2356 |
|
2353 | |||
2357 | dict = PyDict_New(); |
|
2354 | dict = PyDict_New(); | |
2358 | if (dict == NULL) |
|
2355 | if (dict == NULL) | |
2359 | goto bail; |
|
2356 | goto bail; | |
2360 |
|
2357 | |||
2361 | for (i = 0; i < revcount; i++) { |
|
2358 | for (i = 0; i < revcount; i++) { | |
2362 | PyObject *key; |
|
2359 | PyObject *key; | |
2363 |
|
2360 | |||
2364 | if ((final & (1 << i)) == 0) |
|
2361 | if ((final & (1 << i)) == 0) | |
2365 | continue; |
|
2362 | continue; | |
2366 |
|
2363 | |||
2367 | key = PyList_GET_ITEM(revs, i); |
|
2364 | key = PyList_GET_ITEM(revs, i); | |
2368 | Py_INCREF(key); |
|
2365 | Py_INCREF(key); | |
2369 | Py_INCREF(Py_None); |
|
2366 | Py_INCREF(Py_None); | |
2370 | if (PyDict_SetItem(dict, key, Py_None) == -1) { |
|
2367 | if (PyDict_SetItem(dict, key, Py_None) == -1) { | |
2371 | Py_DECREF(key); |
|
2368 | Py_DECREF(key); | |
2372 | Py_DECREF(Py_None); |
|
2369 | Py_DECREF(Py_None); | |
2373 | goto bail; |
|
2370 | goto bail; | |
2374 | } |
|
2371 | } | |
2375 | } |
|
2372 | } | |
2376 |
|
2373 | |||
2377 | keys = PyDict_Keys(dict); |
|
2374 | keys = PyDict_Keys(dict); | |
2378 |
|
2375 | |||
2379 | bail: |
|
2376 | bail: | |
2380 | free(depth); |
|
2377 | free(depth); | |
2381 | free(seen); |
|
2378 | free(seen); | |
2382 | free(interesting); |
|
2379 | free(interesting); | |
2383 | Py_XDECREF(dict); |
|
2380 | Py_XDECREF(dict); | |
2384 |
|
2381 | |||
2385 | return keys; |
|
2382 | return keys; | |
2386 | } |
|
2383 | } | |
2387 |
|
2384 | |||
2388 | /* |
|
2385 | /* | |
2389 | * Given a (possibly overlapping) set of revs, return all the |
|
2386 | * Given a (possibly overlapping) set of revs, return all the | |
2390 | * common ancestors heads: heads(::args[0] and ::a[1] and ...) |
|
2387 | * common ancestors heads: heads(::args[0] and ::a[1] and ...) | |
2391 | */ |
|
2388 | */ | |
2392 | static PyObject *index_commonancestorsheads(indexObject *self, PyObject *args) |
|
2389 | static PyObject *index_commonancestorsheads(indexObject *self, PyObject *args) | |
2393 | { |
|
2390 | { | |
2394 | PyObject *ret = NULL; |
|
2391 | PyObject *ret = NULL; | |
2395 | Py_ssize_t argcount, i, len; |
|
2392 | Py_ssize_t argcount, i, len; | |
2396 | bitmask repeat = 0; |
|
2393 | bitmask repeat = 0; | |
2397 | int revcount = 0; |
|
2394 | int revcount = 0; | |
2398 | int *revs; |
|
2395 | int *revs; | |
2399 |
|
2396 | |||
2400 | argcount = PySequence_Length(args); |
|
2397 | argcount = PySequence_Length(args); | |
2401 | revs = PyMem_Malloc(argcount * sizeof(*revs)); |
|
2398 | revs = PyMem_Malloc(argcount * sizeof(*revs)); | |
2402 | if (argcount > 0 && revs == NULL) |
|
2399 | if (argcount > 0 && revs == NULL) | |
2403 | return PyErr_NoMemory(); |
|
2400 | return PyErr_NoMemory(); | |
2404 | len = index_length(self); |
|
2401 | len = index_length(self); | |
2405 |
|
2402 | |||
2406 | for (i = 0; i < argcount; i++) { |
|
2403 | for (i = 0; i < argcount; i++) { | |
2407 | static const int capacity = 24; |
|
2404 | static const int capacity = 24; | |
2408 | PyObject *obj = PySequence_GetItem(args, i); |
|
2405 | PyObject *obj = PySequence_GetItem(args, i); | |
2409 | bitmask x; |
|
2406 | bitmask x; | |
2410 | long val; |
|
2407 | long val; | |
2411 |
|
2408 | |||
2412 | if (!PyInt_Check(obj)) { |
|
2409 | if (!PyInt_Check(obj)) { | |
2413 | PyErr_SetString(PyExc_TypeError, |
|
2410 | PyErr_SetString(PyExc_TypeError, | |
2414 | "arguments must all be ints"); |
|
2411 | "arguments must all be ints"); | |
2415 | Py_DECREF(obj); |
|
2412 | Py_DECREF(obj); | |
2416 | goto bail; |
|
2413 | goto bail; | |
2417 | } |
|
2414 | } | |
2418 | val = PyInt_AsLong(obj); |
|
2415 | val = PyInt_AsLong(obj); | |
2419 | Py_DECREF(obj); |
|
2416 | Py_DECREF(obj); | |
2420 | if (val == -1) { |
|
2417 | if (val == -1) { | |
2421 | ret = PyList_New(0); |
|
2418 | ret = PyList_New(0); | |
2422 | goto done; |
|
2419 | goto done; | |
2423 | } |
|
2420 | } | |
2424 | if (val < 0 || val >= len) { |
|
2421 | if (val < 0 || val >= len) { | |
2425 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
|
2422 | PyErr_SetString(PyExc_IndexError, "index out of range"); | |
2426 | goto bail; |
|
2423 | goto bail; | |
2427 | } |
|
2424 | } | |
2428 | /* this cheesy bloom filter lets us avoid some more |
|
2425 | /* this cheesy bloom filter lets us avoid some more | |
2429 | * expensive duplicate checks in the common set-is-disjoint |
|
2426 | * expensive duplicate checks in the common set-is-disjoint | |
2430 | * case */ |
|
2427 | * case */ | |
2431 | x = 1ull << (val & 0x3f); |
|
2428 | x = 1ull << (val & 0x3f); | |
2432 | if (repeat & x) { |
|
2429 | if (repeat & x) { | |
2433 | int k; |
|
2430 | int k; | |
2434 | for (k = 0; k < revcount; k++) { |
|
2431 | for (k = 0; k < revcount; k++) { | |
2435 | if (val == revs[k]) |
|
2432 | if (val == revs[k]) | |
2436 | goto duplicate; |
|
2433 | goto duplicate; | |
2437 | } |
|
2434 | } | |
2438 | } else |
|
2435 | } else | |
2439 | repeat |= x; |
|
2436 | repeat |= x; | |
2440 | if (revcount >= capacity) { |
|
2437 | if (revcount >= capacity) { | |
2441 | PyErr_Format(PyExc_OverflowError, |
|
2438 | PyErr_Format(PyExc_OverflowError, | |
2442 | "bitset size (%d) > capacity (%d)", |
|
2439 | "bitset size (%d) > capacity (%d)", | |
2443 | revcount, capacity); |
|
2440 | revcount, capacity); | |
2444 | goto bail; |
|
2441 | goto bail; | |
2445 | } |
|
2442 | } | |
2446 | revs[revcount++] = (int)val; |
|
2443 | revs[revcount++] = (int)val; | |
2447 | duplicate:; |
|
2444 | duplicate:; | |
2448 | } |
|
2445 | } | |
2449 |
|
2446 | |||
2450 | if (revcount == 0) { |
|
2447 | if (revcount == 0) { | |
2451 | ret = PyList_New(0); |
|
2448 | ret = PyList_New(0); | |
2452 | goto done; |
|
2449 | goto done; | |
2453 | } |
|
2450 | } | |
2454 | if (revcount == 1) { |
|
2451 | if (revcount == 1) { | |
2455 | PyObject *obj; |
|
2452 | PyObject *obj; | |
2456 | ret = PyList_New(1); |
|
2453 | ret = PyList_New(1); | |
2457 | if (ret == NULL) |
|
2454 | if (ret == NULL) | |
2458 | goto bail; |
|
2455 | goto bail; | |
2459 | obj = PyInt_FromLong(revs[0]); |
|
2456 | obj = PyInt_FromLong(revs[0]); | |
2460 | if (obj == NULL) |
|
2457 | if (obj == NULL) | |
2461 | goto bail; |
|
2458 | goto bail; | |
2462 | PyList_SET_ITEM(ret, 0, obj); |
|
2459 | PyList_SET_ITEM(ret, 0, obj); | |
2463 | goto done; |
|
2460 | goto done; | |
2464 | } |
|
2461 | } | |
2465 |
|
2462 | |||
2466 | ret = find_gca_candidates(self, revs, revcount); |
|
2463 | ret = find_gca_candidates(self, revs, revcount); | |
2467 | if (ret == NULL) |
|
2464 | if (ret == NULL) | |
2468 | goto bail; |
|
2465 | goto bail; | |
2469 |
|
2466 | |||
2470 | done: |
|
2467 | done: | |
2471 | PyMem_Free(revs); |
|
2468 | PyMem_Free(revs); | |
2472 | return ret; |
|
2469 | return ret; | |
2473 |
|
2470 | |||
2474 | bail: |
|
2471 | bail: | |
2475 | PyMem_Free(revs); |
|
2472 | PyMem_Free(revs); | |
2476 | Py_XDECREF(ret); |
|
2473 | Py_XDECREF(ret); | |
2477 | return NULL; |
|
2474 | return NULL; | |
2478 | } |
|
2475 | } | |
2479 |
|
2476 | |||
2480 | /* |
|
2477 | /* | |
2481 | * Given a (possibly overlapping) set of revs, return the greatest |
|
2478 | * Given a (possibly overlapping) set of revs, return the greatest | |
2482 | * common ancestors: those with the longest path to the root. |
|
2479 | * common ancestors: those with the longest path to the root. | |
2483 | */ |
|
2480 | */ | |
2484 | static PyObject *index_ancestors(indexObject *self, PyObject *args) |
|
2481 | static PyObject *index_ancestors(indexObject *self, PyObject *args) | |
2485 | { |
|
2482 | { | |
2486 | PyObject *ret; |
|
2483 | PyObject *ret; | |
2487 | PyObject *gca = index_commonancestorsheads(self, args); |
|
2484 | PyObject *gca = index_commonancestorsheads(self, args); | |
2488 | if (gca == NULL) |
|
2485 | if (gca == NULL) | |
2489 | return NULL; |
|
2486 | return NULL; | |
2490 |
|
2487 | |||
2491 | if (PyList_GET_SIZE(gca) <= 1) { |
|
2488 | if (PyList_GET_SIZE(gca) <= 1) { | |
2492 | return gca; |
|
2489 | return gca; | |
2493 | } |
|
2490 | } | |
2494 |
|
2491 | |||
2495 | ret = find_deepest(self, gca); |
|
2492 | ret = find_deepest(self, gca); | |
2496 | Py_DECREF(gca); |
|
2493 | Py_DECREF(gca); | |
2497 | return ret; |
|
2494 | return ret; | |
2498 | } |
|
2495 | } | |
2499 |
|
2496 | |||
2500 | /* |
|
2497 | /* | |
2501 | * Invalidate any trie entries introduced by added revs. |
|
2498 | * Invalidate any trie entries introduced by added revs. | |
2502 | */ |
|
2499 | */ | |
2503 | static void index_invalidate_added(indexObject *self, Py_ssize_t start) |
|
2500 | static void index_invalidate_added(indexObject *self, Py_ssize_t start) | |
2504 | { |
|
2501 | { | |
2505 | Py_ssize_t i, len = PyList_GET_SIZE(self->added); |
|
2502 | Py_ssize_t i, len = PyList_GET_SIZE(self->added); | |
2506 |
|
2503 | |||
2507 | for (i = start; i < len; i++) { |
|
2504 | for (i = start; i < len; i++) { | |
2508 | PyObject *tuple = PyList_GET_ITEM(self->added, i); |
|
2505 | PyObject *tuple = PyList_GET_ITEM(self->added, i); | |
2509 | PyObject *node = PyTuple_GET_ITEM(tuple, 7); |
|
2506 | PyObject *node = PyTuple_GET_ITEM(tuple, 7); | |
2510 |
|
2507 | |||
2511 | nt_delete_node(&self->nt, PyBytes_AS_STRING(node)); |
|
2508 | nt_delete_node(&self->nt, PyBytes_AS_STRING(node)); | |
2512 | } |
|
2509 | } | |
2513 |
|
2510 | |||
2514 | if (start == 0) |
|
2511 | if (start == 0) | |
2515 | Py_CLEAR(self->added); |
|
2512 | Py_CLEAR(self->added); | |
2516 | } |
|
2513 | } | |
2517 |
|
2514 | |||
2518 | /* |
|
2515 | /* | |
2519 | * Delete a numeric range of revs, which must be at the end of the |
|
2516 | * Delete a numeric range of revs, which must be at the end of the | |
2520 | * range. |
|
2517 | * range. | |
2521 | */ |
|
2518 | */ | |
2522 | static int index_slice_del(indexObject *self, PyObject *item) |
|
2519 | static int index_slice_del(indexObject *self, PyObject *item) | |
2523 | { |
|
2520 | { | |
2524 | Py_ssize_t start, stop, step, slicelength; |
|
2521 | Py_ssize_t start, stop, step, slicelength; | |
2525 | Py_ssize_t length = index_length(self) + 1; |
|
2522 | Py_ssize_t length = index_length(self) + 1; | |
2526 | int ret = 0; |
|
2523 | int ret = 0; | |
2527 |
|
2524 | |||
2528 | /* Argument changed from PySliceObject* to PyObject* in Python 3. */ |
|
2525 | /* Argument changed from PySliceObject* to PyObject* in Python 3. */ | |
2529 | #ifdef IS_PY3K |
|
2526 | #ifdef IS_PY3K | |
2530 | if (PySlice_GetIndicesEx(item, length, &start, &stop, &step, |
|
2527 | if (PySlice_GetIndicesEx(item, length, &start, &stop, &step, | |
2531 | &slicelength) < 0) |
|
2528 | &slicelength) < 0) | |
2532 | #else |
|
2529 | #else | |
2533 | if (PySlice_GetIndicesEx((PySliceObject *)item, length, &start, &stop, |
|
2530 | if (PySlice_GetIndicesEx((PySliceObject *)item, length, &start, &stop, | |
2534 | &step, &slicelength) < 0) |
|
2531 | &step, &slicelength) < 0) | |
2535 | #endif |
|
2532 | #endif | |
2536 | return -1; |
|
2533 | return -1; | |
2537 |
|
2534 | |||
2538 | if (slicelength <= 0) |
|
2535 | if (slicelength <= 0) | |
2539 | return 0; |
|
2536 | return 0; | |
2540 |
|
2537 | |||
2541 | if ((step < 0 && start < stop) || (step > 0 && start > stop)) |
|
2538 | if ((step < 0 && start < stop) || (step > 0 && start > stop)) | |
2542 | stop = start; |
|
2539 | stop = start; | |
2543 |
|
2540 | |||
2544 | if (step < 0) { |
|
2541 | if (step < 0) { | |
2545 | stop = start + 1; |
|
2542 | stop = start + 1; | |
2546 | start = stop + step * (slicelength - 1) - 1; |
|
2543 | start = stop + step * (slicelength - 1) - 1; | |
2547 | step = -step; |
|
2544 | step = -step; | |
2548 | } |
|
2545 | } | |
2549 |
|
2546 | |||
2550 | if (step != 1) { |
|
2547 | if (step != 1) { | |
2551 | PyErr_SetString(PyExc_ValueError, |
|
2548 | PyErr_SetString(PyExc_ValueError, | |
2552 | "revlog index delete requires step size of 1"); |
|
2549 | "revlog index delete requires step size of 1"); | |
2553 | return -1; |
|
2550 | return -1; | |
2554 | } |
|
2551 | } | |
2555 |
|
2552 | |||
2556 | if (stop != length - 1) { |
|
2553 | if (stop != length - 1) { | |
2557 | PyErr_SetString(PyExc_IndexError, |
|
2554 | PyErr_SetString(PyExc_IndexError, | |
2558 | "revlog index deletion indices are invalid"); |
|
2555 | "revlog index deletion indices are invalid"); | |
2559 | return -1; |
|
2556 | return -1; | |
2560 | } |
|
2557 | } | |
2561 |
|
2558 | |||
2562 | if (start < self->length) { |
|
2559 | if (start < self->length) { | |
2563 | if (self->ntinitialized) { |
|
2560 | if (self->ntinitialized) { | |
2564 | Py_ssize_t i; |
|
2561 | Py_ssize_t i; | |
2565 |
|
2562 | |||
2566 | for (i = start; i < self->length; i++) { |
|
2563 | for (i = start; i < self->length; i++) { | |
2567 | const char *node = index_node_existing(self, i); |
|
2564 | const char *node = index_node_existing(self, i); | |
2568 | if (node == NULL) |
|
2565 | if (node == NULL) | |
2569 | return -1; |
|
2566 | return -1; | |
2570 |
|
2567 | |||
2571 | nt_delete_node(&self->nt, node); |
|
2568 | nt_delete_node(&self->nt, node); | |
2572 | } |
|
2569 | } | |
2573 | if (self->added) |
|
2570 | if (self->added) | |
2574 | index_invalidate_added(self, 0); |
|
2571 | index_invalidate_added(self, 0); | |
2575 | if (self->ntrev > start) |
|
2572 | if (self->ntrev > start) | |
2576 | self->ntrev = (int)start; |
|
2573 | self->ntrev = (int)start; | |
2577 | } else if (self->added) { |
|
2574 | } else if (self->added) { | |
2578 | Py_CLEAR(self->added); |
|
2575 | Py_CLEAR(self->added); | |
2579 | } |
|
2576 | } | |
2580 |
|
2577 | |||
2581 | self->length = start; |
|
2578 | self->length = start; | |
2582 | if (start < self->raw_length) { |
|
2579 | if (start < self->raw_length) { | |
2583 | if (self->cache) { |
|
2580 | if (self->cache) { | |
2584 | Py_ssize_t i; |
|
2581 | Py_ssize_t i; | |
2585 | for (i = start; i < self->raw_length; i++) |
|
2582 | for (i = start; i < self->raw_length; i++) | |
2586 | Py_CLEAR(self->cache[i]); |
|
2583 | Py_CLEAR(self->cache[i]); | |
2587 | } |
|
2584 | } | |
2588 | self->raw_length = start; |
|
2585 | self->raw_length = start; | |
2589 | } |
|
2586 | } | |
2590 | goto done; |
|
2587 | goto done; | |
2591 | } |
|
2588 | } | |
2592 |
|
2589 | |||
2593 | if (self->ntinitialized) { |
|
2590 | if (self->ntinitialized) { | |
2594 | index_invalidate_added(self, start - self->length); |
|
2591 | index_invalidate_added(self, start - self->length); | |
2595 | if (self->ntrev > start) |
|
2592 | if (self->ntrev > start) | |
2596 | self->ntrev = (int)start; |
|
2593 | self->ntrev = (int)start; | |
2597 | } |
|
2594 | } | |
2598 | if (self->added) |
|
2595 | if (self->added) | |
2599 | ret = PyList_SetSlice(self->added, start - self->length, |
|
2596 | ret = PyList_SetSlice(self->added, start - self->length, | |
2600 | PyList_GET_SIZE(self->added), NULL); |
|
2597 | PyList_GET_SIZE(self->added), NULL); | |
2601 | done: |
|
2598 | done: | |
2602 | Py_CLEAR(self->headrevs); |
|
2599 | Py_CLEAR(self->headrevs); | |
2603 | return ret; |
|
2600 | return ret; | |
2604 | } |
|
2601 | } | |
2605 |
|
2602 | |||
2606 | /* |
|
2603 | /* | |
2607 | * Supported ops: |
|
2604 | * Supported ops: | |
2608 | * |
|
2605 | * | |
2609 | * slice deletion |
|
2606 | * slice deletion | |
2610 | * string assignment (extend node->rev mapping) |
|
2607 | * string assignment (extend node->rev mapping) | |
2611 | * string deletion (shrink node->rev mapping) |
|
2608 | * string deletion (shrink node->rev mapping) | |
2612 | */ |
|
2609 | */ | |
2613 | static int index_assign_subscript(indexObject *self, PyObject *item, |
|
2610 | static int index_assign_subscript(indexObject *self, PyObject *item, | |
2614 | PyObject *value) |
|
2611 | PyObject *value) | |
2615 | { |
|
2612 | { | |
2616 | char *node; |
|
2613 | char *node; | |
2617 | long rev; |
|
2614 | long rev; | |
2618 |
|
2615 | |||
2619 | if (PySlice_Check(item) && value == NULL) |
|
2616 | if (PySlice_Check(item) && value == NULL) | |
2620 | return index_slice_del(self, item); |
|
2617 | return index_slice_del(self, item); | |
2621 |
|
2618 | |||
2622 | if (node_check(item, &node) == -1) |
|
2619 | if (node_check(item, &node) == -1) | |
2623 | return -1; |
|
2620 | return -1; | |
2624 |
|
2621 | |||
2625 | if (value == NULL) |
|
2622 | if (value == NULL) | |
2626 | return self->ntinitialized ? nt_delete_node(&self->nt, node) |
|
2623 | return self->ntinitialized ? nt_delete_node(&self->nt, node) | |
2627 | : 0; |
|
2624 | : 0; | |
2628 | rev = PyInt_AsLong(value); |
|
2625 | rev = PyInt_AsLong(value); | |
2629 | if (rev > INT_MAX || rev < 0) { |
|
2626 | if (rev > INT_MAX || rev < 0) { | |
2630 | if (!PyErr_Occurred()) |
|
2627 | if (!PyErr_Occurred()) | |
2631 | PyErr_SetString(PyExc_ValueError, "rev out of range"); |
|
2628 | PyErr_SetString(PyExc_ValueError, "rev out of range"); | |
2632 | return -1; |
|
2629 | return -1; | |
2633 | } |
|
2630 | } | |
2634 |
|
2631 | |||
2635 | if (index_init_nt(self) == -1) |
|
2632 | if (index_init_nt(self) == -1) | |
2636 | return -1; |
|
2633 | return -1; | |
2637 | return nt_insert(&self->nt, node, (int)rev); |
|
2634 | return nt_insert(&self->nt, node, (int)rev); | |
2638 | } |
|
2635 | } | |
2639 |
|
2636 | |||
2640 | /* |
|
2637 | /* | |
2641 | * Find all RevlogNG entries in an index that has inline data. Update |
|
2638 | * Find all RevlogNG entries in an index that has inline data. Update | |
2642 | * the optional "offsets" table with those entries. |
|
2639 | * the optional "offsets" table with those entries. | |
2643 | */ |
|
2640 | */ | |
2644 | static Py_ssize_t inline_scan(indexObject *self, const char **offsets) |
|
2641 | static Py_ssize_t inline_scan(indexObject *self, const char **offsets) | |
2645 | { |
|
2642 | { | |
2646 | const char *data = (const char *)self->buf.buf; |
|
2643 | const char *data = (const char *)self->buf.buf; | |
2647 | Py_ssize_t pos = 0; |
|
2644 | Py_ssize_t pos = 0; | |
2648 | Py_ssize_t end = self->buf.len; |
|
2645 | Py_ssize_t end = self->buf.len; | |
2649 | long incr = v1_hdrsize; |
|
2646 | long incr = v1_hdrsize; | |
2650 | Py_ssize_t len = 0; |
|
2647 | Py_ssize_t len = 0; | |
2651 |
|
2648 | |||
2652 | while (pos + v1_hdrsize <= end && pos >= 0) { |
|
2649 | while (pos + v1_hdrsize <= end && pos >= 0) { | |
2653 | uint32_t comp_len; |
|
2650 | uint32_t comp_len; | |
2654 | /* 3rd element of header is length of compressed inline data */ |
|
2651 | /* 3rd element of header is length of compressed inline data */ | |
2655 | comp_len = getbe32(data + pos + 8); |
|
2652 | comp_len = getbe32(data + pos + 8); | |
2656 | incr = v1_hdrsize + comp_len; |
|
2653 | incr = v1_hdrsize + comp_len; | |
2657 | if (offsets) |
|
2654 | if (offsets) | |
2658 | offsets[len] = data + pos; |
|
2655 | offsets[len] = data + pos; | |
2659 | len++; |
|
2656 | len++; | |
2660 | pos += incr; |
|
2657 | pos += incr; | |
2661 | } |
|
2658 | } | |
2662 |
|
2659 | |||
2663 | if (pos != end) { |
|
2660 | if (pos != end) { | |
2664 | if (!PyErr_Occurred()) |
|
2661 | if (!PyErr_Occurred()) | |
2665 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); |
|
2662 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); | |
2666 | return -1; |
|
2663 | return -1; | |
2667 | } |
|
2664 | } | |
2668 |
|
2665 | |||
2669 | return len; |
|
2666 | return len; | |
2670 | } |
|
2667 | } | |
2671 |
|
2668 | |||
2672 | static int index_init(indexObject *self, PyObject *args) |
|
2669 | static int index_init(indexObject *self, PyObject *args) | |
2673 | { |
|
2670 | { | |
2674 | PyObject *data_obj, *inlined_obj; |
|
2671 | PyObject *data_obj, *inlined_obj; | |
2675 | Py_ssize_t size; |
|
2672 | Py_ssize_t size; | |
2676 |
|
2673 | |||
2677 | /* Initialize before argument-checking to avoid index_dealloc() crash. |
|
2674 | /* Initialize before argument-checking to avoid index_dealloc() crash. | |
2678 | */ |
|
2675 | */ | |
2679 | self->raw_length = 0; |
|
2676 | self->raw_length = 0; | |
2680 | self->added = NULL; |
|
2677 | self->added = NULL; | |
2681 | self->cache = NULL; |
|
2678 | self->cache = NULL; | |
2682 | self->data = NULL; |
|
2679 | self->data = NULL; | |
2683 | memset(&self->buf, 0, sizeof(self->buf)); |
|
2680 | memset(&self->buf, 0, sizeof(self->buf)); | |
2684 | self->headrevs = NULL; |
|
2681 | self->headrevs = NULL; | |
2685 | self->filteredrevs = Py_None; |
|
2682 | self->filteredrevs = Py_None; | |
2686 | Py_INCREF(Py_None); |
|
2683 | Py_INCREF(Py_None); | |
2687 | self->ntinitialized = 0; |
|
2684 | self->ntinitialized = 0; | |
2688 | self->offsets = NULL; |
|
2685 | self->offsets = NULL; | |
2689 |
|
2686 | |||
2690 | if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj)) |
|
2687 | if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj)) | |
2691 | return -1; |
|
2688 | return -1; | |
2692 | if (!PyObject_CheckBuffer(data_obj)) { |
|
2689 | if (!PyObject_CheckBuffer(data_obj)) { | |
2693 | PyErr_SetString(PyExc_TypeError, |
|
2690 | PyErr_SetString(PyExc_TypeError, | |
2694 | "data does not support buffer interface"); |
|
2691 | "data does not support buffer interface"); | |
2695 | return -1; |
|
2692 | return -1; | |
2696 | } |
|
2693 | } | |
2697 |
|
2694 | |||
2698 | if (PyObject_GetBuffer(data_obj, &self->buf, PyBUF_SIMPLE) == -1) |
|
2695 | if (PyObject_GetBuffer(data_obj, &self->buf, PyBUF_SIMPLE) == -1) | |
2699 | return -1; |
|
2696 | return -1; | |
2700 | size = self->buf.len; |
|
2697 | size = self->buf.len; | |
2701 |
|
2698 | |||
2702 | self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj); |
|
2699 | self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj); | |
2703 | self->data = data_obj; |
|
2700 | self->data = data_obj; | |
2704 |
|
2701 | |||
2705 | self->ntlookups = self->ntmisses = 0; |
|
2702 | self->ntlookups = self->ntmisses = 0; | |
2706 | self->ntrev = -1; |
|
2703 | self->ntrev = -1; | |
2707 | Py_INCREF(self->data); |
|
2704 | Py_INCREF(self->data); | |
2708 |
|
2705 | |||
2709 | if (self->inlined) { |
|
2706 | if (self->inlined) { | |
2710 | Py_ssize_t len = inline_scan(self, NULL); |
|
2707 | Py_ssize_t len = inline_scan(self, NULL); | |
2711 | if (len == -1) |
|
2708 | if (len == -1) | |
2712 | goto bail; |
|
2709 | goto bail; | |
2713 | self->raw_length = len; |
|
2710 | self->raw_length = len; | |
2714 | self->length = len; |
|
2711 | self->length = len; | |
2715 | } else { |
|
2712 | } else { | |
2716 | if (size % v1_hdrsize) { |
|
2713 | if (size % v1_hdrsize) { | |
2717 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); |
|
2714 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); | |
2718 | goto bail; |
|
2715 | goto bail; | |
2719 | } |
|
2716 | } | |
2720 | self->raw_length = size / v1_hdrsize; |
|
2717 | self->raw_length = size / v1_hdrsize; | |
2721 | self->length = self->raw_length; |
|
2718 | self->length = self->raw_length; | |
2722 | } |
|
2719 | } | |
2723 |
|
2720 | |||
2724 | return 0; |
|
2721 | return 0; | |
2725 | bail: |
|
2722 | bail: | |
2726 | return -1; |
|
2723 | return -1; | |
2727 | } |
|
2724 | } | |
2728 |
|
2725 | |||
2729 | static PyObject *index_nodemap(indexObject *self) |
|
2726 | static PyObject *index_nodemap(indexObject *self) | |
2730 | { |
|
2727 | { | |
2731 | Py_INCREF(self); |
|
2728 | Py_INCREF(self); | |
2732 | return (PyObject *)self; |
|
2729 | return (PyObject *)self; | |
2733 | } |
|
2730 | } | |
2734 |
|
2731 | |||
2735 | static void _index_clearcaches(indexObject *self) |
|
2732 | static void _index_clearcaches(indexObject *self) | |
2736 | { |
|
2733 | { | |
2737 | if (self->cache) { |
|
2734 | if (self->cache) { | |
2738 | Py_ssize_t i; |
|
2735 | Py_ssize_t i; | |
2739 |
|
2736 | |||
2740 | for (i = 0; i < self->raw_length; i++) |
|
2737 | for (i = 0; i < self->raw_length; i++) | |
2741 | Py_CLEAR(self->cache[i]); |
|
2738 | Py_CLEAR(self->cache[i]); | |
2742 | free(self->cache); |
|
2739 | free(self->cache); | |
2743 | self->cache = NULL; |
|
2740 | self->cache = NULL; | |
2744 | } |
|
2741 | } | |
2745 | if (self->offsets) { |
|
2742 | if (self->offsets) { | |
2746 | PyMem_Free((void *)self->offsets); |
|
2743 | PyMem_Free((void *)self->offsets); | |
2747 | self->offsets = NULL; |
|
2744 | self->offsets = NULL; | |
2748 | } |
|
2745 | } | |
2749 | if (self->ntinitialized) { |
|
2746 | if (self->ntinitialized) { | |
2750 | nt_dealloc(&self->nt); |
|
2747 | nt_dealloc(&self->nt); | |
2751 | } |
|
2748 | } | |
2752 | self->ntinitialized = 0; |
|
2749 | self->ntinitialized = 0; | |
2753 | Py_CLEAR(self->headrevs); |
|
2750 | Py_CLEAR(self->headrevs); | |
2754 | } |
|
2751 | } | |
2755 |
|
2752 | |||
2756 | static PyObject *index_clearcaches(indexObject *self) |
|
2753 | static PyObject *index_clearcaches(indexObject *self) | |
2757 | { |
|
2754 | { | |
2758 | _index_clearcaches(self); |
|
2755 | _index_clearcaches(self); | |
2759 | self->ntrev = -1; |
|
2756 | self->ntrev = -1; | |
2760 | self->ntlookups = self->ntmisses = 0; |
|
2757 | self->ntlookups = self->ntmisses = 0; | |
2761 | Py_RETURN_NONE; |
|
2758 | Py_RETURN_NONE; | |
2762 | } |
|
2759 | } | |
2763 |
|
2760 | |||
2764 | static void index_dealloc(indexObject *self) |
|
2761 | static void index_dealloc(indexObject *self) | |
2765 | { |
|
2762 | { | |
2766 | _index_clearcaches(self); |
|
2763 | _index_clearcaches(self); | |
2767 | Py_XDECREF(self->filteredrevs); |
|
2764 | Py_XDECREF(self->filteredrevs); | |
2768 | if (self->buf.buf) { |
|
2765 | if (self->buf.buf) { | |
2769 | PyBuffer_Release(&self->buf); |
|
2766 | PyBuffer_Release(&self->buf); | |
2770 | memset(&self->buf, 0, sizeof(self->buf)); |
|
2767 | memset(&self->buf, 0, sizeof(self->buf)); | |
2771 | } |
|
2768 | } | |
2772 | Py_XDECREF(self->data); |
|
2769 | Py_XDECREF(self->data); | |
2773 | Py_XDECREF(self->added); |
|
2770 | Py_XDECREF(self->added); | |
2774 | PyObject_Del(self); |
|
2771 | PyObject_Del(self); | |
2775 | } |
|
2772 | } | |
2776 |
|
2773 | |||
2777 | static PySequenceMethods index_sequence_methods = { |
|
2774 | static PySequenceMethods index_sequence_methods = { | |
2778 | (lenfunc)index_length, /* sq_length */ |
|
2775 | (lenfunc)index_length, /* sq_length */ | |
2779 | 0, /* sq_concat */ |
|
2776 | 0, /* sq_concat */ | |
2780 | 0, /* sq_repeat */ |
|
2777 | 0, /* sq_repeat */ | |
2781 | (ssizeargfunc)index_get, /* sq_item */ |
|
2778 | (ssizeargfunc)index_get, /* sq_item */ | |
2782 | 0, /* sq_slice */ |
|
2779 | 0, /* sq_slice */ | |
2783 | 0, /* sq_ass_item */ |
|
2780 | 0, /* sq_ass_item */ | |
2784 | 0, /* sq_ass_slice */ |
|
2781 | 0, /* sq_ass_slice */ | |
2785 | (objobjproc)index_contains, /* sq_contains */ |
|
2782 | (objobjproc)index_contains, /* sq_contains */ | |
2786 | }; |
|
2783 | }; | |
2787 |
|
2784 | |||
2788 | static PyMappingMethods index_mapping_methods = { |
|
2785 | static PyMappingMethods index_mapping_methods = { | |
2789 | (lenfunc)index_length, /* mp_length */ |
|
2786 | (lenfunc)index_length, /* mp_length */ | |
2790 | (binaryfunc)index_getitem, /* mp_subscript */ |
|
2787 | (binaryfunc)index_getitem, /* mp_subscript */ | |
2791 | (objobjargproc)index_assign_subscript, /* mp_ass_subscript */ |
|
2788 | (objobjargproc)index_assign_subscript, /* mp_ass_subscript */ | |
2792 | }; |
|
2789 | }; | |
2793 |
|
2790 | |||
2794 | static PyMethodDef index_methods[] = { |
|
2791 | static PyMethodDef index_methods[] = { | |
2795 | {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS, |
|
2792 | {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS, | |
2796 | "return the gca set of the given revs"}, |
|
2793 | "return the gca set of the given revs"}, | |
2797 | {"commonancestorsheads", (PyCFunction)index_commonancestorsheads, |
|
2794 | {"commonancestorsheads", (PyCFunction)index_commonancestorsheads, | |
2798 | METH_VARARGS, |
|
2795 | METH_VARARGS, | |
2799 | "return the heads of the common ancestors of the given revs"}, |
|
2796 | "return the heads of the common ancestors of the given revs"}, | |
2800 | {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS, |
|
2797 | {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS, | |
2801 | "clear the index caches"}, |
|
2798 | "clear the index caches"}, | |
2802 | {"get", (PyCFunction)index_m_get, METH_VARARGS, "get an index entry"}, |
|
2799 | {"get", (PyCFunction)index_m_get, METH_VARARGS, "get an index entry"}, | |
2803 | {"get_rev", (PyCFunction)index_m_get, METH_VARARGS, |
|
2800 | {"get_rev", (PyCFunction)index_m_get, METH_VARARGS, | |
2804 | "return `rev` associated with a node or None"}, |
|
2801 | "return `rev` associated with a node or None"}, | |
2805 | {"has_node", (PyCFunction)index_m_has_node, METH_O, |
|
2802 | {"has_node", (PyCFunction)index_m_has_node, METH_O, | |
2806 | "return True if the node exist in the index"}, |
|
2803 | "return True if the node exist in the index"}, | |
2807 | {"rev", (PyCFunction)index_m_rev, METH_O, |
|
2804 | {"rev", (PyCFunction)index_m_rev, METH_O, | |
2808 | "return `rev` associated with a node or raise RevlogError"}, |
|
2805 | "return `rev` associated with a node or raise RevlogError"}, | |
2809 | {"computephasesmapsets", (PyCFunction)compute_phases_map_sets, METH_VARARGS, |
|
2806 | {"computephasesmapsets", (PyCFunction)compute_phases_map_sets, METH_VARARGS, | |
2810 | "compute phases"}, |
|
2807 | "compute phases"}, | |
2811 | {"reachableroots2", (PyCFunction)reachableroots2, METH_VARARGS, |
|
2808 | {"reachableroots2", (PyCFunction)reachableroots2, METH_VARARGS, | |
2812 | "reachableroots"}, |
|
2809 | "reachableroots"}, | |
2813 | {"headrevs", (PyCFunction)index_headrevs, METH_VARARGS, |
|
2810 | {"headrevs", (PyCFunction)index_headrevs, METH_VARARGS, | |
2814 | "get head revisions"}, /* Can do filtering since 3.2 */ |
|
2811 | "get head revisions"}, /* Can do filtering since 3.2 */ | |
2815 | {"headrevsfiltered", (PyCFunction)index_headrevs, METH_VARARGS, |
|
2812 | {"headrevsfiltered", (PyCFunction)index_headrevs, METH_VARARGS, | |
2816 | "get filtered head revisions"}, /* Can always do filtering */ |
|
2813 | "get filtered head revisions"}, /* Can always do filtering */ | |
2817 | {"issnapshot", (PyCFunction)index_issnapshot, METH_O, |
|
2814 | {"issnapshot", (PyCFunction)index_issnapshot, METH_O, | |
2818 | "True if the object is a snapshot"}, |
|
2815 | "True if the object is a snapshot"}, | |
2819 | {"findsnapshots", (PyCFunction)index_findsnapshots, METH_VARARGS, |
|
2816 | {"findsnapshots", (PyCFunction)index_findsnapshots, METH_VARARGS, | |
2820 | "Gather snapshot data in a cache dict"}, |
|
2817 | "Gather snapshot data in a cache dict"}, | |
2821 | {"deltachain", (PyCFunction)index_deltachain, METH_VARARGS, |
|
2818 | {"deltachain", (PyCFunction)index_deltachain, METH_VARARGS, | |
2822 | "determine revisions with deltas to reconstruct fulltext"}, |
|
2819 | "determine revisions with deltas to reconstruct fulltext"}, | |
2823 | {"slicechunktodensity", (PyCFunction)index_slicechunktodensity, |
|
2820 | {"slicechunktodensity", (PyCFunction)index_slicechunktodensity, | |
2824 | METH_VARARGS, "determine revisions with deltas to reconstruct fulltext"}, |
|
2821 | METH_VARARGS, "determine revisions with deltas to reconstruct fulltext"}, | |
2825 | {"append", (PyCFunction)index_append, METH_O, "append an index entry"}, |
|
2822 | {"append", (PyCFunction)index_append, METH_O, "append an index entry"}, | |
2826 | {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS, |
|
2823 | {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS, | |
2827 | "match a potentially ambiguous node ID"}, |
|
2824 | "match a potentially ambiguous node ID"}, | |
2828 | {"shortest", (PyCFunction)index_shortest, METH_VARARGS, |
|
2825 | {"shortest", (PyCFunction)index_shortest, METH_VARARGS, | |
2829 | "find length of shortest hex nodeid of a binary ID"}, |
|
2826 | "find length of shortest hex nodeid of a binary ID"}, | |
2830 | {"stats", (PyCFunction)index_stats, METH_NOARGS, "stats for the index"}, |
|
2827 | {"stats", (PyCFunction)index_stats, METH_NOARGS, "stats for the index"}, | |
2831 | {NULL} /* Sentinel */ |
|
2828 | {NULL} /* Sentinel */ | |
2832 | }; |
|
2829 | }; | |
2833 |
|
2830 | |||
2834 | static PyGetSetDef index_getset[] = { |
|
2831 | static PyGetSetDef index_getset[] = { | |
2835 | {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL}, |
|
2832 | {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL}, | |
2836 | {NULL} /* Sentinel */ |
|
2833 | {NULL} /* Sentinel */ | |
2837 | }; |
|
2834 | }; | |
2838 |
|
2835 | |||
2839 | PyTypeObject HgRevlogIndex_Type = { |
|
2836 | PyTypeObject HgRevlogIndex_Type = { | |
2840 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ |
|
2837 | PyVarObject_HEAD_INIT(NULL, 0) /* header */ | |
2841 | "parsers.index", /* tp_name */ |
|
2838 | "parsers.index", /* tp_name */ | |
2842 | sizeof(indexObject), /* tp_basicsize */ |
|
2839 | sizeof(indexObject), /* tp_basicsize */ | |
2843 | 0, /* tp_itemsize */ |
|
2840 | 0, /* tp_itemsize */ | |
2844 | (destructor)index_dealloc, /* tp_dealloc */ |
|
2841 | (destructor)index_dealloc, /* tp_dealloc */ | |
2845 | 0, /* tp_print */ |
|
2842 | 0, /* tp_print */ | |
2846 | 0, /* tp_getattr */ |
|
2843 | 0, /* tp_getattr */ | |
2847 | 0, /* tp_setattr */ |
|
2844 | 0, /* tp_setattr */ | |
2848 | 0, /* tp_compare */ |
|
2845 | 0, /* tp_compare */ | |
2849 | 0, /* tp_repr */ |
|
2846 | 0, /* tp_repr */ | |
2850 | 0, /* tp_as_number */ |
|
2847 | 0, /* tp_as_number */ | |
2851 | &index_sequence_methods, /* tp_as_sequence */ |
|
2848 | &index_sequence_methods, /* tp_as_sequence */ | |
2852 | &index_mapping_methods, /* tp_as_mapping */ |
|
2849 | &index_mapping_methods, /* tp_as_mapping */ | |
2853 | 0, /* tp_hash */ |
|
2850 | 0, /* tp_hash */ | |
2854 | 0, /* tp_call */ |
|
2851 | 0, /* tp_call */ | |
2855 | 0, /* tp_str */ |
|
2852 | 0, /* tp_str */ | |
2856 | 0, /* tp_getattro */ |
|
2853 | 0, /* tp_getattro */ | |
2857 | 0, /* tp_setattro */ |
|
2854 | 0, /* tp_setattro */ | |
2858 | 0, /* tp_as_buffer */ |
|
2855 | 0, /* tp_as_buffer */ | |
2859 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
2856 | Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
2860 | "revlog index", /* tp_doc */ |
|
2857 | "revlog index", /* tp_doc */ | |
2861 | 0, /* tp_traverse */ |
|
2858 | 0, /* tp_traverse */ | |
2862 | 0, /* tp_clear */ |
|
2859 | 0, /* tp_clear */ | |
2863 | 0, /* tp_richcompare */ |
|
2860 | 0, /* tp_richcompare */ | |
2864 | 0, /* tp_weaklistoffset */ |
|
2861 | 0, /* tp_weaklistoffset */ | |
2865 | 0, /* tp_iter */ |
|
2862 | 0, /* tp_iter */ | |
2866 | 0, /* tp_iternext */ |
|
2863 | 0, /* tp_iternext */ | |
2867 | index_methods, /* tp_methods */ |
|
2864 | index_methods, /* tp_methods */ | |
2868 | 0, /* tp_members */ |
|
2865 | 0, /* tp_members */ | |
2869 | index_getset, /* tp_getset */ |
|
2866 | index_getset, /* tp_getset */ | |
2870 | 0, /* tp_base */ |
|
2867 | 0, /* tp_base */ | |
2871 | 0, /* tp_dict */ |
|
2868 | 0, /* tp_dict */ | |
2872 | 0, /* tp_descr_get */ |
|
2869 | 0, /* tp_descr_get */ | |
2873 | 0, /* tp_descr_set */ |
|
2870 | 0, /* tp_descr_set */ | |
2874 | 0, /* tp_dictoffset */ |
|
2871 | 0, /* tp_dictoffset */ | |
2875 | (initproc)index_init, /* tp_init */ |
|
2872 | (initproc)index_init, /* tp_init */ | |
2876 | 0, /* tp_alloc */ |
|
2873 | 0, /* tp_alloc */ | |
2877 | }; |
|
2874 | }; | |
2878 |
|
2875 | |||
2879 | /* |
|
2876 | /* | |
2880 | * returns a tuple of the form (index, index, cache) with elements as |
|
2877 | * returns a tuple of the form (index, index, cache) with elements as | |
2881 | * follows: |
|
2878 | * follows: | |
2882 | * |
|
2879 | * | |
2883 | * index: an index object that lazily parses RevlogNG records |
|
2880 | * index: an index object that lazily parses RevlogNG records | |
2884 | * cache: if data is inlined, a tuple (0, index_file_content), else None |
|
2881 | * cache: if data is inlined, a tuple (0, index_file_content), else None | |
2885 | * index_file_content could be a string, or a buffer |
|
2882 | * index_file_content could be a string, or a buffer | |
2886 | * |
|
2883 | * | |
2887 | * added complications are for backwards compatibility |
|
2884 | * added complications are for backwards compatibility | |
2888 | */ |
|
2885 | */ | |
2889 | PyObject *parse_index2(PyObject *self, PyObject *args) |
|
2886 | PyObject *parse_index2(PyObject *self, PyObject *args) | |
2890 | { |
|
2887 | { | |
2891 | PyObject *tuple = NULL, *cache = NULL; |
|
2888 | PyObject *tuple = NULL, *cache = NULL; | |
2892 | indexObject *idx; |
|
2889 | indexObject *idx; | |
2893 | int ret; |
|
2890 | int ret; | |
2894 |
|
2891 | |||
2895 | idx = PyObject_New(indexObject, &HgRevlogIndex_Type); |
|
2892 | idx = PyObject_New(indexObject, &HgRevlogIndex_Type); | |
2896 | if (idx == NULL) |
|
2893 | if (idx == NULL) | |
2897 | goto bail; |
|
2894 | goto bail; | |
2898 |
|
2895 | |||
2899 | ret = index_init(idx, args); |
|
2896 | ret = index_init(idx, args); | |
2900 | if (ret == -1) |
|
2897 | if (ret == -1) | |
2901 | goto bail; |
|
2898 | goto bail; | |
2902 |
|
2899 | |||
2903 | if (idx->inlined) { |
|
2900 | if (idx->inlined) { | |
2904 | cache = Py_BuildValue("iO", 0, idx->data); |
|
2901 | cache = Py_BuildValue("iO", 0, idx->data); | |
2905 | if (cache == NULL) |
|
2902 | if (cache == NULL) | |
2906 | goto bail; |
|
2903 | goto bail; | |
2907 | } else { |
|
2904 | } else { | |
2908 | cache = Py_None; |
|
2905 | cache = Py_None; | |
2909 | Py_INCREF(cache); |
|
2906 | Py_INCREF(cache); | |
2910 | } |
|
2907 | } | |
2911 |
|
2908 | |||
2912 | tuple = Py_BuildValue("NN", idx, cache); |
|
2909 | tuple = Py_BuildValue("NN", idx, cache); | |
2913 | if (!tuple) |
|
2910 | if (!tuple) | |
2914 | goto bail; |
|
2911 | goto bail; | |
2915 | return tuple; |
|
2912 | return tuple; | |
2916 |
|
2913 | |||
2917 | bail: |
|
2914 | bail: | |
2918 | Py_XDECREF(idx); |
|
2915 | Py_XDECREF(idx); | |
2919 | Py_XDECREF(cache); |
|
2916 | Py_XDECREF(cache); | |
2920 | Py_XDECREF(tuple); |
|
2917 | Py_XDECREF(tuple); | |
2921 | return NULL; |
|
2918 | return NULL; | |
2922 | } |
|
2919 | } | |
2923 |
|
2920 | |||
2924 | static Revlog_CAPI CAPI = { |
|
2921 | static Revlog_CAPI CAPI = { | |
2925 | /* increment the abi_version field upon each change in the Revlog_CAPI |
|
2922 | /* increment the abi_version field upon each change in the Revlog_CAPI | |
2926 | struct or in the ABI of the listed functions */ |
|
2923 | struct or in the ABI of the listed functions */ | |
2927 | 2, |
|
2924 | 2, | |
2928 | index_length, |
|
2925 | index_length, | |
2929 | index_node, |
|
2926 | index_node, | |
2930 | HgRevlogIndex_GetParents, |
|
2927 | HgRevlogIndex_GetParents, | |
2931 | }; |
|
2928 | }; | |
2932 |
|
2929 | |||
2933 | void revlog_module_init(PyObject *mod) |
|
2930 | void revlog_module_init(PyObject *mod) | |
2934 | { |
|
2931 | { | |
2935 | PyObject *caps = NULL; |
|
2932 | PyObject *caps = NULL; | |
2936 | HgRevlogIndex_Type.tp_new = PyType_GenericNew; |
|
2933 | HgRevlogIndex_Type.tp_new = PyType_GenericNew; | |
2937 | if (PyType_Ready(&HgRevlogIndex_Type) < 0) |
|
2934 | if (PyType_Ready(&HgRevlogIndex_Type) < 0) | |
2938 | return; |
|
2935 | return; | |
2939 | Py_INCREF(&HgRevlogIndex_Type); |
|
2936 | Py_INCREF(&HgRevlogIndex_Type); | |
2940 | PyModule_AddObject(mod, "index", (PyObject *)&HgRevlogIndex_Type); |
|
2937 | PyModule_AddObject(mod, "index", (PyObject *)&HgRevlogIndex_Type); | |
2941 |
|
2938 | |||
2942 | nodetreeType.tp_new = PyType_GenericNew; |
|
2939 | nodetreeType.tp_new = PyType_GenericNew; | |
2943 | if (PyType_Ready(&nodetreeType) < 0) |
|
2940 | if (PyType_Ready(&nodetreeType) < 0) | |
2944 | return; |
|
2941 | return; | |
2945 | Py_INCREF(&nodetreeType); |
|
2942 | Py_INCREF(&nodetreeType); | |
2946 | PyModule_AddObject(mod, "nodetree", (PyObject *)&nodetreeType); |
|
2943 | PyModule_AddObject(mod, "nodetree", (PyObject *)&nodetreeType); | |
2947 |
|
2944 | |||
2948 | if (!nullentry) { |
|
2945 | if (!nullentry) { | |
2949 | nullentry = |
|
2946 | nullentry = | |
2950 | Py_BuildValue(PY23("iiiiiiis#", "iiiiiiiy#"), 0, 0, 0, -1, |
|
2947 | Py_BuildValue(PY23("iiiiiiis#", "iiiiiiiy#"), 0, 0, 0, -1, | |
2951 | -1, -1, -1, nullid, (Py_ssize_t)20); |
|
2948 | -1, -1, -1, nullid, (Py_ssize_t)20); | |
2952 | } |
|
2949 | } | |
2953 | if (nullentry) |
|
2950 | if (nullentry) | |
2954 | PyObject_GC_UnTrack(nullentry); |
|
2951 | PyObject_GC_UnTrack(nullentry); | |
2955 |
|
2952 | |||
2956 | caps = PyCapsule_New(&CAPI, "mercurial.cext.parsers.revlog_CAPI", NULL); |
|
2953 | caps = PyCapsule_New(&CAPI, "mercurial.cext.parsers.revlog_CAPI", NULL); | |
2957 | if (caps != NULL) |
|
2954 | if (caps != NULL) | |
2958 | PyModule_AddObject(mod, "revlog_CAPI", caps); |
|
2955 | PyModule_AddObject(mod, "revlog_CAPI", caps); | |
2959 | } |
|
2956 | } |
General Comments 0
You need to be logged in to leave comments.
Login now