Show More
@@ -1,2638 +1,2649 | |||||
1 | /* |
|
1 | /* | |
2 | parsers.c - efficient content parsing |
|
2 | parsers.c - efficient content parsing | |
3 |
|
3 | |||
4 | Copyright 2008 Matt Mackall <mpm@selenic.com> and others |
|
4 | Copyright 2008 Matt Mackall <mpm@selenic.com> and others | |
5 |
|
5 | |||
6 | This software may be used and distributed according to the terms of |
|
6 | This software may be used and distributed according to the terms of | |
7 | the GNU General Public License, incorporated herein by reference. |
|
7 | the GNU General Public License, incorporated herein by reference. | |
8 | */ |
|
8 | */ | |
9 |
|
9 | |||
10 | #include <Python.h> |
|
10 | #include <Python.h> | |
11 | #include <ctype.h> |
|
11 | #include <ctype.h> | |
12 | #include <stddef.h> |
|
12 | #include <stddef.h> | |
13 | #include <string.h> |
|
13 | #include <string.h> | |
14 |
|
14 | |||
15 | #include "util.h" |
|
15 | #include "util.h" | |
16 |
|
16 | |||
17 | static char *versionerrortext = "Python minor version mismatch"; |
|
17 | static char *versionerrortext = "Python minor version mismatch"; | |
18 |
|
18 | |||
19 | static int8_t hextable[256] = { |
|
19 | static int8_t hextable[256] = { | |
20 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
20 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
21 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
21 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
22 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
22 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
23 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0-9 */ |
|
23 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0-9 */ | |
24 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* A-F */ |
|
24 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* A-F */ | |
25 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
25 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
26 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a-f */ |
|
26 | -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a-f */ | |
27 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
27 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
28 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
28 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
29 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
29 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
30 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
30 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
31 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
31 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
32 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
32 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
33 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
33 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
34 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
34 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
35 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
|
35 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 | |
36 | }; |
|
36 | }; | |
37 |
|
37 | |||
38 | static char lowertable[128] = { |
|
38 | static char lowertable[128] = { | |
39 | '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', |
|
39 | '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', | |
40 | '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f', |
|
40 | '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f', | |
41 | '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', |
|
41 | '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', | |
42 | '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', |
|
42 | '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', | |
43 | '\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27', |
|
43 | '\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27', | |
44 | '\x28', '\x29', '\x2a', '\x2b', '\x2c', '\x2d', '\x2e', '\x2f', |
|
44 | '\x28', '\x29', '\x2a', '\x2b', '\x2c', '\x2d', '\x2e', '\x2f', | |
45 | '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37', |
|
45 | '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37', | |
46 | '\x38', '\x39', '\x3a', '\x3b', '\x3c', '\x3d', '\x3e', '\x3f', |
|
46 | '\x38', '\x39', '\x3a', '\x3b', '\x3c', '\x3d', '\x3e', '\x3f', | |
47 | '\x40', |
|
47 | '\x40', | |
48 | '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67', /* A-G */ |
|
48 | '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67', /* A-G */ | |
49 | '\x68', '\x69', '\x6a', '\x6b', '\x6c', '\x6d', '\x6e', '\x6f', /* H-O */ |
|
49 | '\x68', '\x69', '\x6a', '\x6b', '\x6c', '\x6d', '\x6e', '\x6f', /* H-O */ | |
50 | '\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77', /* P-W */ |
|
50 | '\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77', /* P-W */ | |
51 | '\x78', '\x79', '\x7a', /* X-Z */ |
|
51 | '\x78', '\x79', '\x7a', /* X-Z */ | |
52 | '\x5b', '\x5c', '\x5d', '\x5e', '\x5f', |
|
52 | '\x5b', '\x5c', '\x5d', '\x5e', '\x5f', | |
53 | '\x60', '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67', |
|
53 | '\x60', '\x61', '\x62', '\x63', '\x64', '\x65', '\x66', '\x67', | |
54 | '\x68', '\x69', '\x6a', '\x6b', '\x6c', '\x6d', '\x6e', '\x6f', |
|
54 | '\x68', '\x69', '\x6a', '\x6b', '\x6c', '\x6d', '\x6e', '\x6f', | |
55 | '\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77', |
|
55 | '\x70', '\x71', '\x72', '\x73', '\x74', '\x75', '\x76', '\x77', | |
56 | '\x78', '\x79', '\x7a', '\x7b', '\x7c', '\x7d', '\x7e', '\x7f' |
|
56 | '\x78', '\x79', '\x7a', '\x7b', '\x7c', '\x7d', '\x7e', '\x7f' | |
57 | }; |
|
57 | }; | |
58 |
|
58 | |||
59 | static char uppertable[128] = { |
|
59 | static char uppertable[128] = { | |
60 | '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', |
|
60 | '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', | |
61 | '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f', |
|
61 | '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f', | |
62 | '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', |
|
62 | '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', | |
63 | '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', |
|
63 | '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', | |
64 | '\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27', |
|
64 | '\x20', '\x21', '\x22', '\x23', '\x24', '\x25', '\x26', '\x27', | |
65 | '\x28', '\x29', '\x2a', '\x2b', '\x2c', '\x2d', '\x2e', '\x2f', |
|
65 | '\x28', '\x29', '\x2a', '\x2b', '\x2c', '\x2d', '\x2e', '\x2f', | |
66 | '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37', |
|
66 | '\x30', '\x31', '\x32', '\x33', '\x34', '\x35', '\x36', '\x37', | |
67 | '\x38', '\x39', '\x3a', '\x3b', '\x3c', '\x3d', '\x3e', '\x3f', |
|
67 | '\x38', '\x39', '\x3a', '\x3b', '\x3c', '\x3d', '\x3e', '\x3f', | |
68 | '\x40', '\x41', '\x42', '\x43', '\x44', '\x45', '\x46', '\x47', |
|
68 | '\x40', '\x41', '\x42', '\x43', '\x44', '\x45', '\x46', '\x47', | |
69 | '\x48', '\x49', '\x4a', '\x4b', '\x4c', '\x4d', '\x4e', '\x4f', |
|
69 | '\x48', '\x49', '\x4a', '\x4b', '\x4c', '\x4d', '\x4e', '\x4f', | |
70 | '\x50', '\x51', '\x52', '\x53', '\x54', '\x55', '\x56', '\x57', |
|
70 | '\x50', '\x51', '\x52', '\x53', '\x54', '\x55', '\x56', '\x57', | |
71 | '\x58', '\x59', '\x5a', '\x5b', '\x5c', '\x5d', '\x5e', '\x5f', |
|
71 | '\x58', '\x59', '\x5a', '\x5b', '\x5c', '\x5d', '\x5e', '\x5f', | |
72 | '\x60', |
|
72 | '\x60', | |
73 | '\x41', '\x42', '\x43', '\x44', '\x45', '\x46', '\x47', /* a-g */ |
|
73 | '\x41', '\x42', '\x43', '\x44', '\x45', '\x46', '\x47', /* a-g */ | |
74 | '\x48', '\x49', '\x4a', '\x4b', '\x4c', '\x4d', '\x4e', '\x4f', /* h-o */ |
|
74 | '\x48', '\x49', '\x4a', '\x4b', '\x4c', '\x4d', '\x4e', '\x4f', /* h-o */ | |
75 | '\x50', '\x51', '\x52', '\x53', '\x54', '\x55', '\x56', '\x57', /* p-w */ |
|
75 | '\x50', '\x51', '\x52', '\x53', '\x54', '\x55', '\x56', '\x57', /* p-w */ | |
76 | '\x58', '\x59', '\x5a', /* x-z */ |
|
76 | '\x58', '\x59', '\x5a', /* x-z */ | |
77 | '\x7b', '\x7c', '\x7d', '\x7e', '\x7f' |
|
77 | '\x7b', '\x7c', '\x7d', '\x7e', '\x7f' | |
78 | }; |
|
78 | }; | |
79 |
|
79 | |||
80 | static inline int hexdigit(const char *p, Py_ssize_t off) |
|
80 | static inline int hexdigit(const char *p, Py_ssize_t off) | |
81 | { |
|
81 | { | |
82 | int8_t val = hextable[(unsigned char)p[off]]; |
|
82 | int8_t val = hextable[(unsigned char)p[off]]; | |
83 |
|
83 | |||
84 | if (val >= 0) { |
|
84 | if (val >= 0) { | |
85 | return val; |
|
85 | return val; | |
86 | } |
|
86 | } | |
87 |
|
87 | |||
88 | PyErr_SetString(PyExc_ValueError, "input contains non-hex character"); |
|
88 | PyErr_SetString(PyExc_ValueError, "input contains non-hex character"); | |
89 | return 0; |
|
89 | return 0; | |
90 | } |
|
90 | } | |
91 |
|
91 | |||
92 | /* |
|
92 | /* | |
93 | * Turn a hex-encoded string into binary. |
|
93 | * Turn a hex-encoded string into binary. | |
94 | */ |
|
94 | */ | |
95 | PyObject *unhexlify(const char *str, int len) |
|
95 | PyObject *unhexlify(const char *str, int len) | |
96 | { |
|
96 | { | |
97 | PyObject *ret; |
|
97 | PyObject *ret; | |
98 | char *d; |
|
98 | char *d; | |
99 | int i; |
|
99 | int i; | |
100 |
|
100 | |||
101 | ret = PyBytes_FromStringAndSize(NULL, len / 2); |
|
101 | ret = PyBytes_FromStringAndSize(NULL, len / 2); | |
102 |
|
102 | |||
103 | if (!ret) |
|
103 | if (!ret) | |
104 | return NULL; |
|
104 | return NULL; | |
105 |
|
105 | |||
106 | d = PyBytes_AsString(ret); |
|
106 | d = PyBytes_AsString(ret); | |
107 |
|
107 | |||
108 | for (i = 0; i < len;) { |
|
108 | for (i = 0; i < len;) { | |
109 | int hi = hexdigit(str, i++); |
|
109 | int hi = hexdigit(str, i++); | |
110 | int lo = hexdigit(str, i++); |
|
110 | int lo = hexdigit(str, i++); | |
111 | *d++ = (hi << 4) | lo; |
|
111 | *d++ = (hi << 4) | lo; | |
112 | } |
|
112 | } | |
113 |
|
113 | |||
114 | return ret; |
|
114 | return ret; | |
115 | } |
|
115 | } | |
116 |
|
116 | |||
117 | static inline PyObject *_asciitransform(PyObject *str_obj, |
|
117 | static inline PyObject *_asciitransform(PyObject *str_obj, | |
118 | const char table[128], |
|
118 | const char table[128], | |
119 | PyObject *fallback_fn) |
|
119 | PyObject *fallback_fn) | |
120 | { |
|
120 | { | |
121 | char *str, *newstr; |
|
121 | char *str, *newstr; | |
122 | Py_ssize_t i, len; |
|
122 | Py_ssize_t i, len; | |
123 | PyObject *newobj = NULL; |
|
123 | PyObject *newobj = NULL; | |
124 | PyObject *ret = NULL; |
|
124 | PyObject *ret = NULL; | |
125 |
|
125 | |||
126 | str = PyBytes_AS_STRING(str_obj); |
|
126 | str = PyBytes_AS_STRING(str_obj); | |
127 | len = PyBytes_GET_SIZE(str_obj); |
|
127 | len = PyBytes_GET_SIZE(str_obj); | |
128 |
|
128 | |||
129 | newobj = PyBytes_FromStringAndSize(NULL, len); |
|
129 | newobj = PyBytes_FromStringAndSize(NULL, len); | |
130 | if (!newobj) |
|
130 | if (!newobj) | |
131 | goto quit; |
|
131 | goto quit; | |
132 |
|
132 | |||
133 | newstr = PyBytes_AS_STRING(newobj); |
|
133 | newstr = PyBytes_AS_STRING(newobj); | |
134 |
|
134 | |||
135 | for (i = 0; i < len; i++) { |
|
135 | for (i = 0; i < len; i++) { | |
136 | char c = str[i]; |
|
136 | char c = str[i]; | |
137 | if (c & 0x80) { |
|
137 | if (c & 0x80) { | |
138 | if (fallback_fn != NULL) { |
|
138 | if (fallback_fn != NULL) { | |
139 | ret = PyObject_CallFunctionObjArgs(fallback_fn, |
|
139 | ret = PyObject_CallFunctionObjArgs(fallback_fn, | |
140 | str_obj, NULL); |
|
140 | str_obj, NULL); | |
141 | } else { |
|
141 | } else { | |
142 | PyObject *err = PyUnicodeDecodeError_Create( |
|
142 | PyObject *err = PyUnicodeDecodeError_Create( | |
143 | "ascii", str, len, i, (i + 1), |
|
143 | "ascii", str, len, i, (i + 1), | |
144 | "unexpected code byte"); |
|
144 | "unexpected code byte"); | |
145 | PyErr_SetObject(PyExc_UnicodeDecodeError, err); |
|
145 | PyErr_SetObject(PyExc_UnicodeDecodeError, err); | |
146 | Py_XDECREF(err); |
|
146 | Py_XDECREF(err); | |
147 | } |
|
147 | } | |
148 | goto quit; |
|
148 | goto quit; | |
149 | } |
|
149 | } | |
150 | newstr[i] = table[(unsigned char)c]; |
|
150 | newstr[i] = table[(unsigned char)c]; | |
151 | } |
|
151 | } | |
152 |
|
152 | |||
153 | ret = newobj; |
|
153 | ret = newobj; | |
154 | Py_INCREF(ret); |
|
154 | Py_INCREF(ret); | |
155 | quit: |
|
155 | quit: | |
156 | Py_XDECREF(newobj); |
|
156 | Py_XDECREF(newobj); | |
157 | return ret; |
|
157 | return ret; | |
158 | } |
|
158 | } | |
159 |
|
159 | |||
160 | static PyObject *asciilower(PyObject *self, PyObject *args) |
|
160 | static PyObject *asciilower(PyObject *self, PyObject *args) | |
161 | { |
|
161 | { | |
162 | PyObject *str_obj; |
|
162 | PyObject *str_obj; | |
163 | if (!PyArg_ParseTuple(args, "O!:asciilower", &PyBytes_Type, &str_obj)) |
|
163 | if (!PyArg_ParseTuple(args, "O!:asciilower", &PyBytes_Type, &str_obj)) | |
164 | return NULL; |
|
164 | return NULL; | |
165 | return _asciitransform(str_obj, lowertable, NULL); |
|
165 | return _asciitransform(str_obj, lowertable, NULL); | |
166 | } |
|
166 | } | |
167 |
|
167 | |||
168 | static PyObject *asciiupper(PyObject *self, PyObject *args) |
|
168 | static PyObject *asciiupper(PyObject *self, PyObject *args) | |
169 | { |
|
169 | { | |
170 | PyObject *str_obj; |
|
170 | PyObject *str_obj; | |
171 | if (!PyArg_ParseTuple(args, "O!:asciiupper", &PyBytes_Type, &str_obj)) |
|
171 | if (!PyArg_ParseTuple(args, "O!:asciiupper", &PyBytes_Type, &str_obj)) | |
172 | return NULL; |
|
172 | return NULL; | |
173 | return _asciitransform(str_obj, uppertable, NULL); |
|
173 | return _asciitransform(str_obj, uppertable, NULL); | |
174 | } |
|
174 | } | |
175 |
|
175 | |||
176 | static inline PyObject *_dict_new_presized(Py_ssize_t expected_size) |
|
176 | static inline PyObject *_dict_new_presized(Py_ssize_t expected_size) | |
177 | { |
|
177 | { | |
178 | /* _PyDict_NewPresized expects a minused parameter, but it actually |
|
178 | /* _PyDict_NewPresized expects a minused parameter, but it actually | |
179 | creates a dictionary that's the nearest power of two bigger than the |
|
179 | creates a dictionary that's the nearest power of two bigger than the | |
180 | parameter. For example, with the initial minused = 1000, the |
|
180 | parameter. For example, with the initial minused = 1000, the | |
181 | dictionary created has size 1024. Of course in a lot of cases that |
|
181 | dictionary created has size 1024. Of course in a lot of cases that | |
182 | can be greater than the maximum load factor Python's dict object |
|
182 | can be greater than the maximum load factor Python's dict object | |
183 | expects (= 2/3), so as soon as we cross the threshold we'll resize |
|
183 | expects (= 2/3), so as soon as we cross the threshold we'll resize | |
184 | anyway. So create a dictionary that's at least 3/2 the size. */ |
|
184 | anyway. So create a dictionary that's at least 3/2 the size. */ | |
185 | return _PyDict_NewPresized(((1 + expected_size) / 2) * 3); |
|
185 | return _PyDict_NewPresized(((1 + expected_size) / 2) * 3); | |
186 | } |
|
186 | } | |
187 |
|
187 | |||
188 | static PyObject *dict_new_presized(PyObject *self, PyObject *args) |
|
188 | static PyObject *dict_new_presized(PyObject *self, PyObject *args) | |
189 | { |
|
189 | { | |
190 | Py_ssize_t expected_size; |
|
190 | Py_ssize_t expected_size; | |
191 |
|
191 | |||
192 | if (!PyArg_ParseTuple(args, "n:make_presized_dict", &expected_size)) |
|
192 | if (!PyArg_ParseTuple(args, "n:make_presized_dict", &expected_size)) | |
193 | return NULL; |
|
193 | return NULL; | |
194 |
|
194 | |||
195 | return _dict_new_presized(expected_size); |
|
195 | return _dict_new_presized(expected_size); | |
196 | } |
|
196 | } | |
197 |
|
197 | |||
198 | static PyObject *make_file_foldmap(PyObject *self, PyObject *args) |
|
198 | static PyObject *make_file_foldmap(PyObject *self, PyObject *args) | |
199 | { |
|
199 | { | |
200 | PyObject *dmap, *spec_obj, *normcase_fallback; |
|
200 | PyObject *dmap, *spec_obj, *normcase_fallback; | |
201 | PyObject *file_foldmap = NULL; |
|
201 | PyObject *file_foldmap = NULL; | |
202 | enum normcase_spec spec; |
|
202 | enum normcase_spec spec; | |
203 | PyObject *k, *v; |
|
203 | PyObject *k, *v; | |
204 | dirstateTupleObject *tuple; |
|
204 | dirstateTupleObject *tuple; | |
205 | Py_ssize_t pos = 0; |
|
205 | Py_ssize_t pos = 0; | |
206 | const char *table; |
|
206 | const char *table; | |
207 |
|
207 | |||
208 | if (!PyArg_ParseTuple(args, "O!O!O!:make_file_foldmap", |
|
208 | if (!PyArg_ParseTuple(args, "O!O!O!:make_file_foldmap", | |
209 | &PyDict_Type, &dmap, |
|
209 | &PyDict_Type, &dmap, | |
210 | &PyInt_Type, &spec_obj, |
|
210 | &PyInt_Type, &spec_obj, | |
211 | &PyFunction_Type, &normcase_fallback)) |
|
211 | &PyFunction_Type, &normcase_fallback)) | |
212 | goto quit; |
|
212 | goto quit; | |
213 |
|
213 | |||
214 | spec = (int)PyInt_AS_LONG(spec_obj); |
|
214 | spec = (int)PyInt_AS_LONG(spec_obj); | |
215 | switch (spec) { |
|
215 | switch (spec) { | |
216 | case NORMCASE_LOWER: |
|
216 | case NORMCASE_LOWER: | |
217 | table = lowertable; |
|
217 | table = lowertable; | |
218 | break; |
|
218 | break; | |
219 | case NORMCASE_UPPER: |
|
219 | case NORMCASE_UPPER: | |
220 | table = uppertable; |
|
220 | table = uppertable; | |
221 | break; |
|
221 | break; | |
222 | case NORMCASE_OTHER: |
|
222 | case NORMCASE_OTHER: | |
223 | table = NULL; |
|
223 | table = NULL; | |
224 | break; |
|
224 | break; | |
225 | default: |
|
225 | default: | |
226 | PyErr_SetString(PyExc_TypeError, "invalid normcasespec"); |
|
226 | PyErr_SetString(PyExc_TypeError, "invalid normcasespec"); | |
227 | goto quit; |
|
227 | goto quit; | |
228 | } |
|
228 | } | |
229 |
|
229 | |||
230 | /* Add some more entries to deal with additions outside this |
|
230 | /* Add some more entries to deal with additions outside this | |
231 | function. */ |
|
231 | function. */ | |
232 | file_foldmap = _dict_new_presized((PyDict_Size(dmap) / 10) * 11); |
|
232 | file_foldmap = _dict_new_presized((PyDict_Size(dmap) / 10) * 11); | |
233 | if (file_foldmap == NULL) |
|
233 | if (file_foldmap == NULL) | |
234 | goto quit; |
|
234 | goto quit; | |
235 |
|
235 | |||
236 | while (PyDict_Next(dmap, &pos, &k, &v)) { |
|
236 | while (PyDict_Next(dmap, &pos, &k, &v)) { | |
237 | if (!dirstate_tuple_check(v)) { |
|
237 | if (!dirstate_tuple_check(v)) { | |
238 | PyErr_SetString(PyExc_TypeError, |
|
238 | PyErr_SetString(PyExc_TypeError, | |
239 | "expected a dirstate tuple"); |
|
239 | "expected a dirstate tuple"); | |
240 | goto quit; |
|
240 | goto quit; | |
241 | } |
|
241 | } | |
242 |
|
242 | |||
243 | tuple = (dirstateTupleObject *)v; |
|
243 | tuple = (dirstateTupleObject *)v; | |
244 | if (tuple->state != 'r') { |
|
244 | if (tuple->state != 'r') { | |
245 | PyObject *normed; |
|
245 | PyObject *normed; | |
246 | if (table != NULL) { |
|
246 | if (table != NULL) { | |
247 | normed = _asciitransform(k, table, |
|
247 | normed = _asciitransform(k, table, | |
248 | normcase_fallback); |
|
248 | normcase_fallback); | |
249 | } else { |
|
249 | } else { | |
250 | normed = PyObject_CallFunctionObjArgs( |
|
250 | normed = PyObject_CallFunctionObjArgs( | |
251 | normcase_fallback, k, NULL); |
|
251 | normcase_fallback, k, NULL); | |
252 | } |
|
252 | } | |
253 |
|
253 | |||
254 | if (normed == NULL) |
|
254 | if (normed == NULL) | |
255 | goto quit; |
|
255 | goto quit; | |
256 | if (PyDict_SetItem(file_foldmap, normed, k) == -1) |
|
256 | if (PyDict_SetItem(file_foldmap, normed, k) == -1) | |
257 | goto quit; |
|
257 | goto quit; | |
258 | } |
|
258 | } | |
259 | } |
|
259 | } | |
260 | return file_foldmap; |
|
260 | return file_foldmap; | |
261 | quit: |
|
261 | quit: | |
262 | Py_XDECREF(file_foldmap); |
|
262 | Py_XDECREF(file_foldmap); | |
263 | return NULL; |
|
263 | return NULL; | |
264 | } |
|
264 | } | |
265 |
|
265 | |||
266 | /* |
|
266 | /* | |
267 | * This code assumes that a manifest is stitched together with newline |
|
267 | * This code assumes that a manifest is stitched together with newline | |
268 | * ('\n') characters. |
|
268 | * ('\n') characters. | |
269 | */ |
|
269 | */ | |
270 | static PyObject *parse_manifest(PyObject *self, PyObject *args) |
|
270 | static PyObject *parse_manifest(PyObject *self, PyObject *args) | |
271 | { |
|
271 | { | |
272 | PyObject *mfdict, *fdict; |
|
272 | PyObject *mfdict, *fdict; | |
273 | char *str, *start, *end; |
|
273 | char *str, *start, *end; | |
274 | int len; |
|
274 | int len; | |
275 |
|
275 | |||
276 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest", |
|
276 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest", | |
277 | &PyDict_Type, &mfdict, |
|
277 | &PyDict_Type, &mfdict, | |
278 | &PyDict_Type, &fdict, |
|
278 | &PyDict_Type, &fdict, | |
279 | &str, &len)) |
|
279 | &str, &len)) | |
280 | goto quit; |
|
280 | goto quit; | |
281 |
|
281 | |||
282 | start = str; |
|
282 | start = str; | |
283 | end = str + len; |
|
283 | end = str + len; | |
284 | while (start < end) { |
|
284 | while (start < end) { | |
285 | PyObject *file = NULL, *node = NULL; |
|
285 | PyObject *file = NULL, *node = NULL; | |
286 | PyObject *flags = NULL; |
|
286 | PyObject *flags = NULL; | |
287 | char *zero = NULL, *newline = NULL; |
|
287 | char *zero = NULL, *newline = NULL; | |
288 | ptrdiff_t nlen; |
|
288 | ptrdiff_t nlen; | |
289 |
|
289 | |||
290 | zero = memchr(start, '\0', end - start); |
|
290 | zero = memchr(start, '\0', end - start); | |
291 | if (!zero) { |
|
291 | if (!zero) { | |
292 | PyErr_SetString(PyExc_ValueError, |
|
292 | PyErr_SetString(PyExc_ValueError, | |
293 | "manifest entry has no separator"); |
|
293 | "manifest entry has no separator"); | |
294 | goto quit; |
|
294 | goto quit; | |
295 | } |
|
295 | } | |
296 |
|
296 | |||
297 | newline = memchr(zero + 1, '\n', end - (zero + 1)); |
|
297 | newline = memchr(zero + 1, '\n', end - (zero + 1)); | |
298 | if (!newline) { |
|
298 | if (!newline) { | |
299 | PyErr_SetString(PyExc_ValueError, |
|
299 | PyErr_SetString(PyExc_ValueError, | |
300 | "manifest contains trailing garbage"); |
|
300 | "manifest contains trailing garbage"); | |
301 | goto quit; |
|
301 | goto quit; | |
302 | } |
|
302 | } | |
303 |
|
303 | |||
304 | file = PyBytes_FromStringAndSize(start, zero - start); |
|
304 | file = PyBytes_FromStringAndSize(start, zero - start); | |
305 |
|
305 | |||
306 | if (!file) |
|
306 | if (!file) | |
307 | goto bail; |
|
307 | goto bail; | |
308 |
|
308 | |||
309 | nlen = newline - zero - 1; |
|
309 | nlen = newline - zero - 1; | |
310 |
|
310 | |||
311 | node = unhexlify(zero + 1, nlen > 40 ? 40 : (int)nlen); |
|
311 | node = unhexlify(zero + 1, nlen > 40 ? 40 : (int)nlen); | |
312 | if (!node) |
|
312 | if (!node) | |
313 | goto bail; |
|
313 | goto bail; | |
314 |
|
314 | |||
315 | if (nlen > 40) { |
|
315 | if (nlen > 40) { | |
316 | flags = PyBytes_FromStringAndSize(zero + 41, |
|
316 | flags = PyBytes_FromStringAndSize(zero + 41, | |
317 | nlen - 40); |
|
317 | nlen - 40); | |
318 | if (!flags) |
|
318 | if (!flags) | |
319 | goto bail; |
|
319 | goto bail; | |
320 |
|
320 | |||
321 | if (PyDict_SetItem(fdict, file, flags) == -1) |
|
321 | if (PyDict_SetItem(fdict, file, flags) == -1) | |
322 | goto bail; |
|
322 | goto bail; | |
323 | } |
|
323 | } | |
324 |
|
324 | |||
325 | if (PyDict_SetItem(mfdict, file, node) == -1) |
|
325 | if (PyDict_SetItem(mfdict, file, node) == -1) | |
326 | goto bail; |
|
326 | goto bail; | |
327 |
|
327 | |||
328 | start = newline + 1; |
|
328 | start = newline + 1; | |
329 |
|
329 | |||
330 | Py_XDECREF(flags); |
|
330 | Py_XDECREF(flags); | |
331 | Py_XDECREF(node); |
|
331 | Py_XDECREF(node); | |
332 | Py_XDECREF(file); |
|
332 | Py_XDECREF(file); | |
333 | continue; |
|
333 | continue; | |
334 | bail: |
|
334 | bail: | |
335 | Py_XDECREF(flags); |
|
335 | Py_XDECREF(flags); | |
336 | Py_XDECREF(node); |
|
336 | Py_XDECREF(node); | |
337 | Py_XDECREF(file); |
|
337 | Py_XDECREF(file); | |
338 | goto quit; |
|
338 | goto quit; | |
339 | } |
|
339 | } | |
340 |
|
340 | |||
341 | Py_INCREF(Py_None); |
|
341 | Py_INCREF(Py_None); | |
342 | return Py_None; |
|
342 | return Py_None; | |
343 | quit: |
|
343 | quit: | |
344 | return NULL; |
|
344 | return NULL; | |
345 | } |
|
345 | } | |
346 |
|
346 | |||
347 | static inline dirstateTupleObject *make_dirstate_tuple(char state, int mode, |
|
347 | static inline dirstateTupleObject *make_dirstate_tuple(char state, int mode, | |
348 | int size, int mtime) |
|
348 | int size, int mtime) | |
349 | { |
|
349 | { | |
350 | dirstateTupleObject *t = PyObject_New(dirstateTupleObject, |
|
350 | dirstateTupleObject *t = PyObject_New(dirstateTupleObject, | |
351 | &dirstateTupleType); |
|
351 | &dirstateTupleType); | |
352 | if (!t) |
|
352 | if (!t) | |
353 | return NULL; |
|
353 | return NULL; | |
354 | t->state = state; |
|
354 | t->state = state; | |
355 | t->mode = mode; |
|
355 | t->mode = mode; | |
356 | t->size = size; |
|
356 | t->size = size; | |
357 | t->mtime = mtime; |
|
357 | t->mtime = mtime; | |
358 | return t; |
|
358 | return t; | |
359 | } |
|
359 | } | |
360 |
|
360 | |||
361 | static PyObject *dirstate_tuple_new(PyTypeObject *subtype, PyObject *args, |
|
361 | static PyObject *dirstate_tuple_new(PyTypeObject *subtype, PyObject *args, | |
362 | PyObject *kwds) |
|
362 | PyObject *kwds) | |
363 | { |
|
363 | { | |
364 | /* We do all the initialization here and not a tp_init function because |
|
364 | /* We do all the initialization here and not a tp_init function because | |
365 | * dirstate_tuple is immutable. */ |
|
365 | * dirstate_tuple is immutable. */ | |
366 | dirstateTupleObject *t; |
|
366 | dirstateTupleObject *t; | |
367 | char state; |
|
367 | char state; | |
368 | int size, mode, mtime; |
|
368 | int size, mode, mtime; | |
369 | if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime)) |
|
369 | if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime)) | |
370 | return NULL; |
|
370 | return NULL; | |
371 |
|
371 | |||
372 | t = (dirstateTupleObject *)subtype->tp_alloc(subtype, 1); |
|
372 | t = (dirstateTupleObject *)subtype->tp_alloc(subtype, 1); | |
373 | if (!t) |
|
373 | if (!t) | |
374 | return NULL; |
|
374 | return NULL; | |
375 | t->state = state; |
|
375 | t->state = state; | |
376 | t->mode = mode; |
|
376 | t->mode = mode; | |
377 | t->size = size; |
|
377 | t->size = size; | |
378 | t->mtime = mtime; |
|
378 | t->mtime = mtime; | |
379 |
|
379 | |||
380 | return (PyObject *)t; |
|
380 | return (PyObject *)t; | |
381 | } |
|
381 | } | |
382 |
|
382 | |||
383 | static void dirstate_tuple_dealloc(PyObject *o) |
|
383 | static void dirstate_tuple_dealloc(PyObject *o) | |
384 | { |
|
384 | { | |
385 | PyObject_Del(o); |
|
385 | PyObject_Del(o); | |
386 | } |
|
386 | } | |
387 |
|
387 | |||
388 | static Py_ssize_t dirstate_tuple_length(PyObject *o) |
|
388 | static Py_ssize_t dirstate_tuple_length(PyObject *o) | |
389 | { |
|
389 | { | |
390 | return 4; |
|
390 | return 4; | |
391 | } |
|
391 | } | |
392 |
|
392 | |||
393 | static PyObject *dirstate_tuple_item(PyObject *o, Py_ssize_t i) |
|
393 | static PyObject *dirstate_tuple_item(PyObject *o, Py_ssize_t i) | |
394 | { |
|
394 | { | |
395 | dirstateTupleObject *t = (dirstateTupleObject *)o; |
|
395 | dirstateTupleObject *t = (dirstateTupleObject *)o; | |
396 | switch (i) { |
|
396 | switch (i) { | |
397 | case 0: |
|
397 | case 0: | |
398 | return PyBytes_FromStringAndSize(&t->state, 1); |
|
398 | return PyBytes_FromStringAndSize(&t->state, 1); | |
399 | case 1: |
|
399 | case 1: | |
400 | return PyInt_FromLong(t->mode); |
|
400 | return PyInt_FromLong(t->mode); | |
401 | case 2: |
|
401 | case 2: | |
402 | return PyInt_FromLong(t->size); |
|
402 | return PyInt_FromLong(t->size); | |
403 | case 3: |
|
403 | case 3: | |
404 | return PyInt_FromLong(t->mtime); |
|
404 | return PyInt_FromLong(t->mtime); | |
405 | default: |
|
405 | default: | |
406 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
|
406 | PyErr_SetString(PyExc_IndexError, "index out of range"); | |
407 | return NULL; |
|
407 | return NULL; | |
408 | } |
|
408 | } | |
409 | } |
|
409 | } | |
410 |
|
410 | |||
411 | static PySequenceMethods dirstate_tuple_sq = { |
|
411 | static PySequenceMethods dirstate_tuple_sq = { | |
412 | dirstate_tuple_length, /* sq_length */ |
|
412 | dirstate_tuple_length, /* sq_length */ | |
413 | 0, /* sq_concat */ |
|
413 | 0, /* sq_concat */ | |
414 | 0, /* sq_repeat */ |
|
414 | 0, /* sq_repeat */ | |
415 | dirstate_tuple_item, /* sq_item */ |
|
415 | dirstate_tuple_item, /* sq_item */ | |
416 | 0, /* sq_ass_item */ |
|
416 | 0, /* sq_ass_item */ | |
417 | 0, /* sq_contains */ |
|
417 | 0, /* sq_contains */ | |
418 | 0, /* sq_inplace_concat */ |
|
418 | 0, /* sq_inplace_concat */ | |
419 | 0 /* sq_inplace_repeat */ |
|
419 | 0 /* sq_inplace_repeat */ | |
420 | }; |
|
420 | }; | |
421 |
|
421 | |||
422 | PyTypeObject dirstateTupleType = { |
|
422 | PyTypeObject dirstateTupleType = { | |
423 | PyVarObject_HEAD_INIT(NULL, 0) |
|
423 | PyVarObject_HEAD_INIT(NULL, 0) | |
424 | "dirstate_tuple", /* tp_name */ |
|
424 | "dirstate_tuple", /* tp_name */ | |
425 | sizeof(dirstateTupleObject),/* tp_basicsize */ |
|
425 | sizeof(dirstateTupleObject),/* tp_basicsize */ | |
426 | 0, /* tp_itemsize */ |
|
426 | 0, /* tp_itemsize */ | |
427 | (destructor)dirstate_tuple_dealloc, /* tp_dealloc */ |
|
427 | (destructor)dirstate_tuple_dealloc, /* tp_dealloc */ | |
428 | 0, /* tp_print */ |
|
428 | 0, /* tp_print */ | |
429 | 0, /* tp_getattr */ |
|
429 | 0, /* tp_getattr */ | |
430 | 0, /* tp_setattr */ |
|
430 | 0, /* tp_setattr */ | |
431 | 0, /* tp_compare */ |
|
431 | 0, /* tp_compare */ | |
432 | 0, /* tp_repr */ |
|
432 | 0, /* tp_repr */ | |
433 | 0, /* tp_as_number */ |
|
433 | 0, /* tp_as_number */ | |
434 | &dirstate_tuple_sq, /* tp_as_sequence */ |
|
434 | &dirstate_tuple_sq, /* tp_as_sequence */ | |
435 | 0, /* tp_as_mapping */ |
|
435 | 0, /* tp_as_mapping */ | |
436 | 0, /* tp_hash */ |
|
436 | 0, /* tp_hash */ | |
437 | 0, /* tp_call */ |
|
437 | 0, /* tp_call */ | |
438 | 0, /* tp_str */ |
|
438 | 0, /* tp_str */ | |
439 | 0, /* tp_getattro */ |
|
439 | 0, /* tp_getattro */ | |
440 | 0, /* tp_setattro */ |
|
440 | 0, /* tp_setattro */ | |
441 | 0, /* tp_as_buffer */ |
|
441 | 0, /* tp_as_buffer */ | |
442 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
442 | Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
443 | "dirstate tuple", /* tp_doc */ |
|
443 | "dirstate tuple", /* tp_doc */ | |
444 | 0, /* tp_traverse */ |
|
444 | 0, /* tp_traverse */ | |
445 | 0, /* tp_clear */ |
|
445 | 0, /* tp_clear */ | |
446 | 0, /* tp_richcompare */ |
|
446 | 0, /* tp_richcompare */ | |
447 | 0, /* tp_weaklistoffset */ |
|
447 | 0, /* tp_weaklistoffset */ | |
448 | 0, /* tp_iter */ |
|
448 | 0, /* tp_iter */ | |
449 | 0, /* tp_iternext */ |
|
449 | 0, /* tp_iternext */ | |
450 | 0, /* tp_methods */ |
|
450 | 0, /* tp_methods */ | |
451 | 0, /* tp_members */ |
|
451 | 0, /* tp_members */ | |
452 | 0, /* tp_getset */ |
|
452 | 0, /* tp_getset */ | |
453 | 0, /* tp_base */ |
|
453 | 0, /* tp_base */ | |
454 | 0, /* tp_dict */ |
|
454 | 0, /* tp_dict */ | |
455 | 0, /* tp_descr_get */ |
|
455 | 0, /* tp_descr_get */ | |
456 | 0, /* tp_descr_set */ |
|
456 | 0, /* tp_descr_set */ | |
457 | 0, /* tp_dictoffset */ |
|
457 | 0, /* tp_dictoffset */ | |
458 | 0, /* tp_init */ |
|
458 | 0, /* tp_init */ | |
459 | 0, /* tp_alloc */ |
|
459 | 0, /* tp_alloc */ | |
460 | dirstate_tuple_new, /* tp_new */ |
|
460 | dirstate_tuple_new, /* tp_new */ | |
461 | }; |
|
461 | }; | |
462 |
|
462 | |||
463 | static PyObject *parse_dirstate(PyObject *self, PyObject *args) |
|
463 | static PyObject *parse_dirstate(PyObject *self, PyObject *args) | |
464 | { |
|
464 | { | |
465 | PyObject *dmap, *cmap, *parents = NULL, *ret = NULL; |
|
465 | PyObject *dmap, *cmap, *parents = NULL, *ret = NULL; | |
466 | PyObject *fname = NULL, *cname = NULL, *entry = NULL; |
|
466 | PyObject *fname = NULL, *cname = NULL, *entry = NULL; | |
467 | char state, *cur, *str, *cpos; |
|
467 | char state, *cur, *str, *cpos; | |
468 | int mode, size, mtime; |
|
468 | int mode, size, mtime; | |
469 | unsigned int flen, len, pos = 40; |
|
469 | unsigned int flen, len, pos = 40; | |
470 | int readlen; |
|
470 | int readlen; | |
471 |
|
471 | |||
472 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate", |
|
472 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate", | |
473 | &PyDict_Type, &dmap, |
|
473 | &PyDict_Type, &dmap, | |
474 | &PyDict_Type, &cmap, |
|
474 | &PyDict_Type, &cmap, | |
475 | &str, &readlen)) |
|
475 | &str, &readlen)) | |
476 | goto quit; |
|
476 | goto quit; | |
477 |
|
477 | |||
478 | if (readlen < 0) |
|
478 | if (readlen < 0) | |
479 | goto quit; |
|
479 | goto quit; | |
480 |
|
480 | |||
481 | len = readlen; |
|
481 | len = readlen; | |
482 |
|
482 | |||
483 | /* read parents */ |
|
483 | /* read parents */ | |
484 | if (len < 40) |
|
484 | if (len < 40) | |
485 | goto quit; |
|
485 | goto quit; | |
486 |
|
486 | |||
487 | parents = Py_BuildValue("s#s#", str, 20, str + 20, 20); |
|
487 | parents = Py_BuildValue("s#s#", str, 20, str + 20, 20); | |
488 | if (!parents) |
|
488 | if (!parents) | |
489 | goto quit; |
|
489 | goto quit; | |
490 |
|
490 | |||
491 | /* read filenames */ |
|
491 | /* read filenames */ | |
492 | while (pos >= 40 && pos < len) { |
|
492 | while (pos >= 40 && pos < len) { | |
493 | cur = str + pos; |
|
493 | cur = str + pos; | |
494 | /* unpack header */ |
|
494 | /* unpack header */ | |
495 | state = *cur; |
|
495 | state = *cur; | |
496 | mode = getbe32(cur + 1); |
|
496 | mode = getbe32(cur + 1); | |
497 | size = getbe32(cur + 5); |
|
497 | size = getbe32(cur + 5); | |
498 | mtime = getbe32(cur + 9); |
|
498 | mtime = getbe32(cur + 9); | |
499 | flen = getbe32(cur + 13); |
|
499 | flen = getbe32(cur + 13); | |
500 | pos += 17; |
|
500 | pos += 17; | |
501 | cur += 17; |
|
501 | cur += 17; | |
502 | if (flen > len - pos) { |
|
502 | if (flen > len - pos) { | |
503 | PyErr_SetString(PyExc_ValueError, "overflow in dirstate"); |
|
503 | PyErr_SetString(PyExc_ValueError, "overflow in dirstate"); | |
504 | goto quit; |
|
504 | goto quit; | |
505 | } |
|
505 | } | |
506 |
|
506 | |||
507 | entry = (PyObject *)make_dirstate_tuple(state, mode, size, |
|
507 | entry = (PyObject *)make_dirstate_tuple(state, mode, size, | |
508 | mtime); |
|
508 | mtime); | |
509 | cpos = memchr(cur, 0, flen); |
|
509 | cpos = memchr(cur, 0, flen); | |
510 | if (cpos) { |
|
510 | if (cpos) { | |
511 | fname = PyBytes_FromStringAndSize(cur, cpos - cur); |
|
511 | fname = PyBytes_FromStringAndSize(cur, cpos - cur); | |
512 | cname = PyBytes_FromStringAndSize(cpos + 1, |
|
512 | cname = PyBytes_FromStringAndSize(cpos + 1, | |
513 | flen - (cpos - cur) - 1); |
|
513 | flen - (cpos - cur) - 1); | |
514 | if (!fname || !cname || |
|
514 | if (!fname || !cname || | |
515 | PyDict_SetItem(cmap, fname, cname) == -1 || |
|
515 | PyDict_SetItem(cmap, fname, cname) == -1 || | |
516 | PyDict_SetItem(dmap, fname, entry) == -1) |
|
516 | PyDict_SetItem(dmap, fname, entry) == -1) | |
517 | goto quit; |
|
517 | goto quit; | |
518 | Py_DECREF(cname); |
|
518 | Py_DECREF(cname); | |
519 | } else { |
|
519 | } else { | |
520 | fname = PyBytes_FromStringAndSize(cur, flen); |
|
520 | fname = PyBytes_FromStringAndSize(cur, flen); | |
521 | if (!fname || |
|
521 | if (!fname || | |
522 | PyDict_SetItem(dmap, fname, entry) == -1) |
|
522 | PyDict_SetItem(dmap, fname, entry) == -1) | |
523 | goto quit; |
|
523 | goto quit; | |
524 | } |
|
524 | } | |
525 | Py_DECREF(fname); |
|
525 | Py_DECREF(fname); | |
526 | Py_DECREF(entry); |
|
526 | Py_DECREF(entry); | |
527 | fname = cname = entry = NULL; |
|
527 | fname = cname = entry = NULL; | |
528 | pos += flen; |
|
528 | pos += flen; | |
529 | } |
|
529 | } | |
530 |
|
530 | |||
531 | ret = parents; |
|
531 | ret = parents; | |
532 | Py_INCREF(ret); |
|
532 | Py_INCREF(ret); | |
533 | quit: |
|
533 | quit: | |
534 | Py_XDECREF(fname); |
|
534 | Py_XDECREF(fname); | |
535 | Py_XDECREF(cname); |
|
535 | Py_XDECREF(cname); | |
536 | Py_XDECREF(entry); |
|
536 | Py_XDECREF(entry); | |
537 | Py_XDECREF(parents); |
|
537 | Py_XDECREF(parents); | |
538 | return ret; |
|
538 | return ret; | |
539 | } |
|
539 | } | |
540 |
|
540 | |||
541 | /* |
|
541 | /* | |
542 | * Efficiently pack a dirstate object into its on-disk format. |
|
542 | * Efficiently pack a dirstate object into its on-disk format. | |
543 | */ |
|
543 | */ | |
544 | static PyObject *pack_dirstate(PyObject *self, PyObject *args) |
|
544 | static PyObject *pack_dirstate(PyObject *self, PyObject *args) | |
545 | { |
|
545 | { | |
546 | PyObject *packobj = NULL; |
|
546 | PyObject *packobj = NULL; | |
547 | PyObject *map, *copymap, *pl, *mtime_unset = NULL; |
|
547 | PyObject *map, *copymap, *pl, *mtime_unset = NULL; | |
548 | Py_ssize_t nbytes, pos, l; |
|
548 | Py_ssize_t nbytes, pos, l; | |
549 | PyObject *k, *v = NULL, *pn; |
|
549 | PyObject *k, *v = NULL, *pn; | |
550 | char *p, *s; |
|
550 | char *p, *s; | |
551 | double now; |
|
551 | double now; | |
552 |
|
552 | |||
553 | if (!PyArg_ParseTuple(args, "O!O!Od:pack_dirstate", |
|
553 | if (!PyArg_ParseTuple(args, "O!O!Od:pack_dirstate", | |
554 | &PyDict_Type, &map, &PyDict_Type, ©map, |
|
554 | &PyDict_Type, &map, &PyDict_Type, ©map, | |
555 | &pl, &now)) |
|
555 | &pl, &now)) | |
556 | return NULL; |
|
556 | return NULL; | |
557 |
|
557 | |||
558 | if (!PySequence_Check(pl) || PySequence_Size(pl) != 2) { |
|
558 | if (!PySequence_Check(pl) || PySequence_Size(pl) != 2) { | |
559 | PyErr_SetString(PyExc_TypeError, "expected 2-element sequence"); |
|
559 | PyErr_SetString(PyExc_TypeError, "expected 2-element sequence"); | |
560 | return NULL; |
|
560 | return NULL; | |
561 | } |
|
561 | } | |
562 |
|
562 | |||
563 | /* Figure out how much we need to allocate. */ |
|
563 | /* Figure out how much we need to allocate. */ | |
564 | for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) { |
|
564 | for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) { | |
565 | PyObject *c; |
|
565 | PyObject *c; | |
566 | if (!PyString_Check(k)) { |
|
566 | if (!PyString_Check(k)) { | |
567 | PyErr_SetString(PyExc_TypeError, "expected string key"); |
|
567 | PyErr_SetString(PyExc_TypeError, "expected string key"); | |
568 | goto bail; |
|
568 | goto bail; | |
569 | } |
|
569 | } | |
570 | nbytes += PyString_GET_SIZE(k) + 17; |
|
570 | nbytes += PyString_GET_SIZE(k) + 17; | |
571 | c = PyDict_GetItem(copymap, k); |
|
571 | c = PyDict_GetItem(copymap, k); | |
572 | if (c) { |
|
572 | if (c) { | |
573 | if (!PyString_Check(c)) { |
|
573 | if (!PyString_Check(c)) { | |
574 | PyErr_SetString(PyExc_TypeError, |
|
574 | PyErr_SetString(PyExc_TypeError, | |
575 | "expected string key"); |
|
575 | "expected string key"); | |
576 | goto bail; |
|
576 | goto bail; | |
577 | } |
|
577 | } | |
578 | nbytes += PyString_GET_SIZE(c) + 1; |
|
578 | nbytes += PyString_GET_SIZE(c) + 1; | |
579 | } |
|
579 | } | |
580 | } |
|
580 | } | |
581 |
|
581 | |||
582 | packobj = PyString_FromStringAndSize(NULL, nbytes); |
|
582 | packobj = PyString_FromStringAndSize(NULL, nbytes); | |
583 | if (packobj == NULL) |
|
583 | if (packobj == NULL) | |
584 | goto bail; |
|
584 | goto bail; | |
585 |
|
585 | |||
586 | p = PyString_AS_STRING(packobj); |
|
586 | p = PyString_AS_STRING(packobj); | |
587 |
|
587 | |||
588 | pn = PySequence_ITEM(pl, 0); |
|
588 | pn = PySequence_ITEM(pl, 0); | |
589 | if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { |
|
589 | if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { | |
590 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); |
|
590 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); | |
591 | goto bail; |
|
591 | goto bail; | |
592 | } |
|
592 | } | |
593 | memcpy(p, s, l); |
|
593 | memcpy(p, s, l); | |
594 | p += 20; |
|
594 | p += 20; | |
595 | pn = PySequence_ITEM(pl, 1); |
|
595 | pn = PySequence_ITEM(pl, 1); | |
596 | if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { |
|
596 | if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { | |
597 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); |
|
597 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); | |
598 | goto bail; |
|
598 | goto bail; | |
599 | } |
|
599 | } | |
600 | memcpy(p, s, l); |
|
600 | memcpy(p, s, l); | |
601 | p += 20; |
|
601 | p += 20; | |
602 |
|
602 | |||
603 | for (pos = 0; PyDict_Next(map, &pos, &k, &v); ) { |
|
603 | for (pos = 0; PyDict_Next(map, &pos, &k, &v); ) { | |
604 | dirstateTupleObject *tuple; |
|
604 | dirstateTupleObject *tuple; | |
605 | char state; |
|
605 | char state; | |
606 | uint32_t mode, size, mtime; |
|
606 | uint32_t mode, size, mtime; | |
607 | Py_ssize_t len, l; |
|
607 | Py_ssize_t len, l; | |
608 | PyObject *o; |
|
608 | PyObject *o; | |
609 | char *t; |
|
609 | char *t; | |
610 |
|
610 | |||
611 | if (!dirstate_tuple_check(v)) { |
|
611 | if (!dirstate_tuple_check(v)) { | |
612 | PyErr_SetString(PyExc_TypeError, |
|
612 | PyErr_SetString(PyExc_TypeError, | |
613 | "expected a dirstate tuple"); |
|
613 | "expected a dirstate tuple"); | |
614 | goto bail; |
|
614 | goto bail; | |
615 | } |
|
615 | } | |
616 | tuple = (dirstateTupleObject *)v; |
|
616 | tuple = (dirstateTupleObject *)v; | |
617 |
|
617 | |||
618 | state = tuple->state; |
|
618 | state = tuple->state; | |
619 | mode = tuple->mode; |
|
619 | mode = tuple->mode; | |
620 | size = tuple->size; |
|
620 | size = tuple->size; | |
621 | mtime = tuple->mtime; |
|
621 | mtime = tuple->mtime; | |
622 | if (state == 'n' && mtime == (uint32_t)now) { |
|
622 | if (state == 'n' && mtime == (uint32_t)now) { | |
623 | /* See pure/parsers.py:pack_dirstate for why we do |
|
623 | /* See pure/parsers.py:pack_dirstate for why we do | |
624 | * this. */ |
|
624 | * this. */ | |
625 | mtime = -1; |
|
625 | mtime = -1; | |
626 | mtime_unset = (PyObject *)make_dirstate_tuple( |
|
626 | mtime_unset = (PyObject *)make_dirstate_tuple( | |
627 | state, mode, size, mtime); |
|
627 | state, mode, size, mtime); | |
628 | if (!mtime_unset) |
|
628 | if (!mtime_unset) | |
629 | goto bail; |
|
629 | goto bail; | |
630 | if (PyDict_SetItem(map, k, mtime_unset) == -1) |
|
630 | if (PyDict_SetItem(map, k, mtime_unset) == -1) | |
631 | goto bail; |
|
631 | goto bail; | |
632 | Py_DECREF(mtime_unset); |
|
632 | Py_DECREF(mtime_unset); | |
633 | mtime_unset = NULL; |
|
633 | mtime_unset = NULL; | |
634 | } |
|
634 | } | |
635 | *p++ = state; |
|
635 | *p++ = state; | |
636 | putbe32(mode, p); |
|
636 | putbe32(mode, p); | |
637 | putbe32(size, p + 4); |
|
637 | putbe32(size, p + 4); | |
638 | putbe32(mtime, p + 8); |
|
638 | putbe32(mtime, p + 8); | |
639 | t = p + 12; |
|
639 | t = p + 12; | |
640 | p += 16; |
|
640 | p += 16; | |
641 | len = PyString_GET_SIZE(k); |
|
641 | len = PyString_GET_SIZE(k); | |
642 | memcpy(p, PyString_AS_STRING(k), len); |
|
642 | memcpy(p, PyString_AS_STRING(k), len); | |
643 | p += len; |
|
643 | p += len; | |
644 | o = PyDict_GetItem(copymap, k); |
|
644 | o = PyDict_GetItem(copymap, k); | |
645 | if (o) { |
|
645 | if (o) { | |
646 | *p++ = '\0'; |
|
646 | *p++ = '\0'; | |
647 | l = PyString_GET_SIZE(o); |
|
647 | l = PyString_GET_SIZE(o); | |
648 | memcpy(p, PyString_AS_STRING(o), l); |
|
648 | memcpy(p, PyString_AS_STRING(o), l); | |
649 | p += l; |
|
649 | p += l; | |
650 | len += l + 1; |
|
650 | len += l + 1; | |
651 | } |
|
651 | } | |
652 | putbe32((uint32_t)len, t); |
|
652 | putbe32((uint32_t)len, t); | |
653 | } |
|
653 | } | |
654 |
|
654 | |||
655 | pos = p - PyString_AS_STRING(packobj); |
|
655 | pos = p - PyString_AS_STRING(packobj); | |
656 | if (pos != nbytes) { |
|
656 | if (pos != nbytes) { | |
657 | PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld", |
|
657 | PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld", | |
658 | (long)pos, (long)nbytes); |
|
658 | (long)pos, (long)nbytes); | |
659 | goto bail; |
|
659 | goto bail; | |
660 | } |
|
660 | } | |
661 |
|
661 | |||
662 | return packobj; |
|
662 | return packobj; | |
663 | bail: |
|
663 | bail: | |
664 | Py_XDECREF(mtime_unset); |
|
664 | Py_XDECREF(mtime_unset); | |
665 | Py_XDECREF(packobj); |
|
665 | Py_XDECREF(packobj); | |
666 | Py_XDECREF(v); |
|
666 | Py_XDECREF(v); | |
667 | return NULL; |
|
667 | return NULL; | |
668 | } |
|
668 | } | |
669 |
|
669 | |||
670 | /* |
|
670 | /* | |
671 | * A base-16 trie for fast node->rev mapping. |
|
671 | * A base-16 trie for fast node->rev mapping. | |
672 | * |
|
672 | * | |
673 | * Positive value is index of the next node in the trie |
|
673 | * Positive value is index of the next node in the trie | |
674 | * Negative value is a leaf: -(rev + 1) |
|
674 | * Negative value is a leaf: -(rev + 1) | |
675 | * Zero is empty |
|
675 | * Zero is empty | |
676 | */ |
|
676 | */ | |
677 | typedef struct { |
|
677 | typedef struct { | |
678 | int children[16]; |
|
678 | int children[16]; | |
679 | } nodetree; |
|
679 | } nodetree; | |
680 |
|
680 | |||
681 | /* |
|
681 | /* | |
682 | * This class has two behaviours. |
|
682 | * This class has two behaviours. | |
683 | * |
|
683 | * | |
684 | * When used in a list-like way (with integer keys), we decode an |
|
684 | * When used in a list-like way (with integer keys), we decode an | |
685 | * entry in a RevlogNG index file on demand. Our last entry is a |
|
685 | * entry in a RevlogNG index file on demand. Our last entry is a | |
686 | * sentinel, always a nullid. We have limited support for |
|
686 | * sentinel, always a nullid. We have limited support for | |
687 | * integer-keyed insert and delete, only at elements right before the |
|
687 | * integer-keyed insert and delete, only at elements right before the | |
688 | * sentinel. |
|
688 | * sentinel. | |
689 | * |
|
689 | * | |
690 | * With string keys, we lazily perform a reverse mapping from node to |
|
690 | * With string keys, we lazily perform a reverse mapping from node to | |
691 | * rev, using a base-16 trie. |
|
691 | * rev, using a base-16 trie. | |
692 | */ |
|
692 | */ | |
693 | typedef struct { |
|
693 | typedef struct { | |
694 | PyObject_HEAD |
|
694 | PyObject_HEAD | |
695 | /* Type-specific fields go here. */ |
|
695 | /* Type-specific fields go here. */ | |
696 | PyObject *data; /* raw bytes of index */ |
|
696 | PyObject *data; /* raw bytes of index */ | |
697 | PyObject **cache; /* cached tuples */ |
|
697 | PyObject **cache; /* cached tuples */ | |
698 | const char **offsets; /* populated on demand */ |
|
698 | const char **offsets; /* populated on demand */ | |
699 | Py_ssize_t raw_length; /* original number of elements */ |
|
699 | Py_ssize_t raw_length; /* original number of elements */ | |
700 | Py_ssize_t length; /* current number of elements */ |
|
700 | Py_ssize_t length; /* current number of elements */ | |
701 | PyObject *added; /* populated on demand */ |
|
701 | PyObject *added; /* populated on demand */ | |
702 | PyObject *headrevs; /* cache, invalidated on changes */ |
|
702 | PyObject *headrevs; /* cache, invalidated on changes */ | |
703 | PyObject *filteredrevs;/* filtered revs set */ |
|
703 | PyObject *filteredrevs;/* filtered revs set */ | |
704 | nodetree *nt; /* base-16 trie */ |
|
704 | nodetree *nt; /* base-16 trie */ | |
705 | int ntlength; /* # nodes in use */ |
|
705 | int ntlength; /* # nodes in use */ | |
706 | int ntcapacity; /* # nodes allocated */ |
|
706 | int ntcapacity; /* # nodes allocated */ | |
707 | int ntdepth; /* maximum depth of tree */ |
|
707 | int ntdepth; /* maximum depth of tree */ | |
708 | int ntsplits; /* # splits performed */ |
|
708 | int ntsplits; /* # splits performed */ | |
709 | int ntrev; /* last rev scanned */ |
|
709 | int ntrev; /* last rev scanned */ | |
710 | int ntlookups; /* # lookups */ |
|
710 | int ntlookups; /* # lookups */ | |
711 | int ntmisses; /* # lookups that miss the cache */ |
|
711 | int ntmisses; /* # lookups that miss the cache */ | |
712 | int inlined; |
|
712 | int inlined; | |
713 | } indexObject; |
|
713 | } indexObject; | |
714 |
|
714 | |||
715 | static Py_ssize_t index_length(const indexObject *self) |
|
715 | static Py_ssize_t index_length(const indexObject *self) | |
716 | { |
|
716 | { | |
717 | if (self->added == NULL) |
|
717 | if (self->added == NULL) | |
718 | return self->length; |
|
718 | return self->length; | |
719 | return self->length + PyList_GET_SIZE(self->added); |
|
719 | return self->length + PyList_GET_SIZE(self->added); | |
720 | } |
|
720 | } | |
721 |
|
721 | |||
722 | static PyObject *nullentry; |
|
722 | static PyObject *nullentry; | |
723 | static const char nullid[20]; |
|
723 | static const char nullid[20]; | |
724 |
|
724 | |||
725 | static Py_ssize_t inline_scan(indexObject *self, const char **offsets); |
|
725 | static Py_ssize_t inline_scan(indexObject *self, const char **offsets); | |
726 |
|
726 | |||
727 | #if LONG_MAX == 0x7fffffffL |
|
727 | #if LONG_MAX == 0x7fffffffL | |
728 | static char *tuple_format = "Kiiiiiis#"; |
|
728 | static char *tuple_format = "Kiiiiiis#"; | |
729 | #else |
|
729 | #else | |
730 | static char *tuple_format = "kiiiiiis#"; |
|
730 | static char *tuple_format = "kiiiiiis#"; | |
731 | #endif |
|
731 | #endif | |
732 |
|
732 | |||
733 | /* A RevlogNG v1 index entry is 64 bytes long. */ |
|
733 | /* A RevlogNG v1 index entry is 64 bytes long. */ | |
734 | static const long v1_hdrsize = 64; |
|
734 | static const long v1_hdrsize = 64; | |
735 |
|
735 | |||
736 | /* |
|
736 | /* | |
737 | * Return a pointer to the beginning of a RevlogNG record. |
|
737 | * Return a pointer to the beginning of a RevlogNG record. | |
738 | */ |
|
738 | */ | |
739 | static const char *index_deref(indexObject *self, Py_ssize_t pos) |
|
739 | static const char *index_deref(indexObject *self, Py_ssize_t pos) | |
740 | { |
|
740 | { | |
741 | if (self->inlined && pos > 0) { |
|
741 | if (self->inlined && pos > 0) { | |
742 | if (self->offsets == NULL) { |
|
742 | if (self->offsets == NULL) { | |
743 | self->offsets = malloc(self->raw_length * |
|
743 | self->offsets = malloc(self->raw_length * | |
744 | sizeof(*self->offsets)); |
|
744 | sizeof(*self->offsets)); | |
745 | if (self->offsets == NULL) |
|
745 | if (self->offsets == NULL) | |
746 | return (const char *)PyErr_NoMemory(); |
|
746 | return (const char *)PyErr_NoMemory(); | |
747 | inline_scan(self, self->offsets); |
|
747 | inline_scan(self, self->offsets); | |
748 | } |
|
748 | } | |
749 | return self->offsets[pos]; |
|
749 | return self->offsets[pos]; | |
750 | } |
|
750 | } | |
751 |
|
751 | |||
752 | return PyString_AS_STRING(self->data) + pos * v1_hdrsize; |
|
752 | return PyString_AS_STRING(self->data) + pos * v1_hdrsize; | |
753 | } |
|
753 | } | |
754 |
|
754 | |||
755 |
static inline |
|
755 | static inline int index_get_parents(indexObject *self, Py_ssize_t rev, | |
756 | int *ps) |
|
756 | int *ps, int maxrev) | |
757 | { |
|
757 | { | |
758 | if (rev >= self->length - 1) { |
|
758 | if (rev >= self->length - 1) { | |
759 | PyObject *tuple = PyList_GET_ITEM(self->added, |
|
759 | PyObject *tuple = PyList_GET_ITEM(self->added, | |
760 | rev - self->length + 1); |
|
760 | rev - self->length + 1); | |
761 | ps[0] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 5)); |
|
761 | ps[0] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 5)); | |
762 | ps[1] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 6)); |
|
762 | ps[1] = (int)PyInt_AS_LONG(PyTuple_GET_ITEM(tuple, 6)); | |
763 | } else { |
|
763 | } else { | |
764 | const char *data = index_deref(self, rev); |
|
764 | const char *data = index_deref(self, rev); | |
765 | ps[0] = getbe32(data + 24); |
|
765 | ps[0] = getbe32(data + 24); | |
766 | ps[1] = getbe32(data + 28); |
|
766 | ps[1] = getbe32(data + 28); | |
767 | } |
|
767 | } | |
|
768 | /* If index file is corrupted, ps[] may point to invalid revisions. So | |||
|
769 | * there is a risk of buffer overflow to trust them unconditionally. */ | |||
|
770 | if (ps[0] > maxrev || ps[1] > maxrev) { | |||
|
771 | PyErr_SetString(PyExc_ValueError, "parent out of range"); | |||
|
772 | return -1; | |||
|
773 | } | |||
|
774 | return 0; | |||
768 | } |
|
775 | } | |
769 |
|
776 | |||
770 |
|
777 | |||
771 | /* |
|
778 | /* | |
772 | * RevlogNG format (all in big endian, data may be inlined): |
|
779 | * RevlogNG format (all in big endian, data may be inlined): | |
773 | * 6 bytes: offset |
|
780 | * 6 bytes: offset | |
774 | * 2 bytes: flags |
|
781 | * 2 bytes: flags | |
775 | * 4 bytes: compressed length |
|
782 | * 4 bytes: compressed length | |
776 | * 4 bytes: uncompressed length |
|
783 | * 4 bytes: uncompressed length | |
777 | * 4 bytes: base revision |
|
784 | * 4 bytes: base revision | |
778 | * 4 bytes: link revision |
|
785 | * 4 bytes: link revision | |
779 | * 4 bytes: parent 1 revision |
|
786 | * 4 bytes: parent 1 revision | |
780 | * 4 bytes: parent 2 revision |
|
787 | * 4 bytes: parent 2 revision | |
781 | * 32 bytes: nodeid (only 20 bytes used) |
|
788 | * 32 bytes: nodeid (only 20 bytes used) | |
782 | */ |
|
789 | */ | |
783 | static PyObject *index_get(indexObject *self, Py_ssize_t pos) |
|
790 | static PyObject *index_get(indexObject *self, Py_ssize_t pos) | |
784 | { |
|
791 | { | |
785 | uint64_t offset_flags; |
|
792 | uint64_t offset_flags; | |
786 | int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2; |
|
793 | int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2; | |
787 | const char *c_node_id; |
|
794 | const char *c_node_id; | |
788 | const char *data; |
|
795 | const char *data; | |
789 | Py_ssize_t length = index_length(self); |
|
796 | Py_ssize_t length = index_length(self); | |
790 | PyObject *entry; |
|
797 | PyObject *entry; | |
791 |
|
798 | |||
792 | if (pos < 0) |
|
799 | if (pos < 0) | |
793 | pos += length; |
|
800 | pos += length; | |
794 |
|
801 | |||
795 | if (pos < 0 || pos >= length) { |
|
802 | if (pos < 0 || pos >= length) { | |
796 | PyErr_SetString(PyExc_IndexError, "revlog index out of range"); |
|
803 | PyErr_SetString(PyExc_IndexError, "revlog index out of range"); | |
797 | return NULL; |
|
804 | return NULL; | |
798 | } |
|
805 | } | |
799 |
|
806 | |||
800 | if (pos == length - 1) { |
|
807 | if (pos == length - 1) { | |
801 | Py_INCREF(nullentry); |
|
808 | Py_INCREF(nullentry); | |
802 | return nullentry; |
|
809 | return nullentry; | |
803 | } |
|
810 | } | |
804 |
|
811 | |||
805 | if (pos >= self->length - 1) { |
|
812 | if (pos >= self->length - 1) { | |
806 | PyObject *obj; |
|
813 | PyObject *obj; | |
807 | obj = PyList_GET_ITEM(self->added, pos - self->length + 1); |
|
814 | obj = PyList_GET_ITEM(self->added, pos - self->length + 1); | |
808 | Py_INCREF(obj); |
|
815 | Py_INCREF(obj); | |
809 | return obj; |
|
816 | return obj; | |
810 | } |
|
817 | } | |
811 |
|
818 | |||
812 | if (self->cache) { |
|
819 | if (self->cache) { | |
813 | if (self->cache[pos]) { |
|
820 | if (self->cache[pos]) { | |
814 | Py_INCREF(self->cache[pos]); |
|
821 | Py_INCREF(self->cache[pos]); | |
815 | return self->cache[pos]; |
|
822 | return self->cache[pos]; | |
816 | } |
|
823 | } | |
817 | } else { |
|
824 | } else { | |
818 | self->cache = calloc(self->raw_length, sizeof(PyObject *)); |
|
825 | self->cache = calloc(self->raw_length, sizeof(PyObject *)); | |
819 | if (self->cache == NULL) |
|
826 | if (self->cache == NULL) | |
820 | return PyErr_NoMemory(); |
|
827 | return PyErr_NoMemory(); | |
821 | } |
|
828 | } | |
822 |
|
829 | |||
823 | data = index_deref(self, pos); |
|
830 | data = index_deref(self, pos); | |
824 | if (data == NULL) |
|
831 | if (data == NULL) | |
825 | return NULL; |
|
832 | return NULL; | |
826 |
|
833 | |||
827 | offset_flags = getbe32(data + 4); |
|
834 | offset_flags = getbe32(data + 4); | |
828 | if (pos == 0) /* mask out version number for the first entry */ |
|
835 | if (pos == 0) /* mask out version number for the first entry */ | |
829 | offset_flags &= 0xFFFF; |
|
836 | offset_flags &= 0xFFFF; | |
830 | else { |
|
837 | else { | |
831 | uint32_t offset_high = getbe32(data); |
|
838 | uint32_t offset_high = getbe32(data); | |
832 | offset_flags |= ((uint64_t)offset_high) << 32; |
|
839 | offset_flags |= ((uint64_t)offset_high) << 32; | |
833 | } |
|
840 | } | |
834 |
|
841 | |||
835 | comp_len = getbe32(data + 8); |
|
842 | comp_len = getbe32(data + 8); | |
836 | uncomp_len = getbe32(data + 12); |
|
843 | uncomp_len = getbe32(data + 12); | |
837 | base_rev = getbe32(data + 16); |
|
844 | base_rev = getbe32(data + 16); | |
838 | link_rev = getbe32(data + 20); |
|
845 | link_rev = getbe32(data + 20); | |
839 | parent_1 = getbe32(data + 24); |
|
846 | parent_1 = getbe32(data + 24); | |
840 | parent_2 = getbe32(data + 28); |
|
847 | parent_2 = getbe32(data + 28); | |
841 | c_node_id = data + 32; |
|
848 | c_node_id = data + 32; | |
842 |
|
849 | |||
843 | entry = Py_BuildValue(tuple_format, offset_flags, comp_len, |
|
850 | entry = Py_BuildValue(tuple_format, offset_flags, comp_len, | |
844 | uncomp_len, base_rev, link_rev, |
|
851 | uncomp_len, base_rev, link_rev, | |
845 | parent_1, parent_2, c_node_id, 20); |
|
852 | parent_1, parent_2, c_node_id, 20); | |
846 |
|
853 | |||
847 | if (entry) { |
|
854 | if (entry) { | |
848 | PyObject_GC_UnTrack(entry); |
|
855 | PyObject_GC_UnTrack(entry); | |
849 | Py_INCREF(entry); |
|
856 | Py_INCREF(entry); | |
850 | } |
|
857 | } | |
851 |
|
858 | |||
852 | self->cache[pos] = entry; |
|
859 | self->cache[pos] = entry; | |
853 |
|
860 | |||
854 | return entry; |
|
861 | return entry; | |
855 | } |
|
862 | } | |
856 |
|
863 | |||
857 | /* |
|
864 | /* | |
858 | * Return the 20-byte SHA of the node corresponding to the given rev. |
|
865 | * Return the 20-byte SHA of the node corresponding to the given rev. | |
859 | */ |
|
866 | */ | |
860 | static const char *index_node(indexObject *self, Py_ssize_t pos) |
|
867 | static const char *index_node(indexObject *self, Py_ssize_t pos) | |
861 | { |
|
868 | { | |
862 | Py_ssize_t length = index_length(self); |
|
869 | Py_ssize_t length = index_length(self); | |
863 | const char *data; |
|
870 | const char *data; | |
864 |
|
871 | |||
865 | if (pos == length - 1 || pos == INT_MAX) |
|
872 | if (pos == length - 1 || pos == INT_MAX) | |
866 | return nullid; |
|
873 | return nullid; | |
867 |
|
874 | |||
868 | if (pos >= length) |
|
875 | if (pos >= length) | |
869 | return NULL; |
|
876 | return NULL; | |
870 |
|
877 | |||
871 | if (pos >= self->length - 1) { |
|
878 | if (pos >= self->length - 1) { | |
872 | PyObject *tuple, *str; |
|
879 | PyObject *tuple, *str; | |
873 | tuple = PyList_GET_ITEM(self->added, pos - self->length + 1); |
|
880 | tuple = PyList_GET_ITEM(self->added, pos - self->length + 1); | |
874 | str = PyTuple_GetItem(tuple, 7); |
|
881 | str = PyTuple_GetItem(tuple, 7); | |
875 | return str ? PyString_AS_STRING(str) : NULL; |
|
882 | return str ? PyString_AS_STRING(str) : NULL; | |
876 | } |
|
883 | } | |
877 |
|
884 | |||
878 | data = index_deref(self, pos); |
|
885 | data = index_deref(self, pos); | |
879 | return data ? data + 32 : NULL; |
|
886 | return data ? data + 32 : NULL; | |
880 | } |
|
887 | } | |
881 |
|
888 | |||
882 | static int nt_insert(indexObject *self, const char *node, int rev); |
|
889 | static int nt_insert(indexObject *self, const char *node, int rev); | |
883 |
|
890 | |||
884 | static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen) |
|
891 | static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen) | |
885 | { |
|
892 | { | |
886 | if (PyString_AsStringAndSize(obj, node, nodelen) == -1) |
|
893 | if (PyString_AsStringAndSize(obj, node, nodelen) == -1) | |
887 | return -1; |
|
894 | return -1; | |
888 | if (*nodelen == 20) |
|
895 | if (*nodelen == 20) | |
889 | return 0; |
|
896 | return 0; | |
890 | PyErr_SetString(PyExc_ValueError, "20-byte hash required"); |
|
897 | PyErr_SetString(PyExc_ValueError, "20-byte hash required"); | |
891 | return -1; |
|
898 | return -1; | |
892 | } |
|
899 | } | |
893 |
|
900 | |||
894 | static PyObject *index_insert(indexObject *self, PyObject *args) |
|
901 | static PyObject *index_insert(indexObject *self, PyObject *args) | |
895 | { |
|
902 | { | |
896 | PyObject *obj; |
|
903 | PyObject *obj; | |
897 | char *node; |
|
904 | char *node; | |
898 | int index; |
|
905 | int index; | |
899 | Py_ssize_t len, nodelen; |
|
906 | Py_ssize_t len, nodelen; | |
900 |
|
907 | |||
901 | if (!PyArg_ParseTuple(args, "iO", &index, &obj)) |
|
908 | if (!PyArg_ParseTuple(args, "iO", &index, &obj)) | |
902 | return NULL; |
|
909 | return NULL; | |
903 |
|
910 | |||
904 | if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) { |
|
911 | if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) { | |
905 | PyErr_SetString(PyExc_TypeError, "8-tuple required"); |
|
912 | PyErr_SetString(PyExc_TypeError, "8-tuple required"); | |
906 | return NULL; |
|
913 | return NULL; | |
907 | } |
|
914 | } | |
908 |
|
915 | |||
909 | if (node_check(PyTuple_GET_ITEM(obj, 7), &node, &nodelen) == -1) |
|
916 | if (node_check(PyTuple_GET_ITEM(obj, 7), &node, &nodelen) == -1) | |
910 | return NULL; |
|
917 | return NULL; | |
911 |
|
918 | |||
912 | len = index_length(self); |
|
919 | len = index_length(self); | |
913 |
|
920 | |||
914 | if (index < 0) |
|
921 | if (index < 0) | |
915 | index += len; |
|
922 | index += len; | |
916 |
|
923 | |||
917 | if (index != len - 1) { |
|
924 | if (index != len - 1) { | |
918 | PyErr_SetString(PyExc_IndexError, |
|
925 | PyErr_SetString(PyExc_IndexError, | |
919 | "insert only supported at index -1"); |
|
926 | "insert only supported at index -1"); | |
920 | return NULL; |
|
927 | return NULL; | |
921 | } |
|
928 | } | |
922 |
|
929 | |||
923 | if (self->added == NULL) { |
|
930 | if (self->added == NULL) { | |
924 | self->added = PyList_New(0); |
|
931 | self->added = PyList_New(0); | |
925 | if (self->added == NULL) |
|
932 | if (self->added == NULL) | |
926 | return NULL; |
|
933 | return NULL; | |
927 | } |
|
934 | } | |
928 |
|
935 | |||
929 | if (PyList_Append(self->added, obj) == -1) |
|
936 | if (PyList_Append(self->added, obj) == -1) | |
930 | return NULL; |
|
937 | return NULL; | |
931 |
|
938 | |||
932 | if (self->nt) |
|
939 | if (self->nt) | |
933 | nt_insert(self, node, index); |
|
940 | nt_insert(self, node, index); | |
934 |
|
941 | |||
935 | Py_CLEAR(self->headrevs); |
|
942 | Py_CLEAR(self->headrevs); | |
936 | Py_RETURN_NONE; |
|
943 | Py_RETURN_NONE; | |
937 | } |
|
944 | } | |
938 |
|
945 | |||
939 | static void _index_clearcaches(indexObject *self) |
|
946 | static void _index_clearcaches(indexObject *self) | |
940 | { |
|
947 | { | |
941 | if (self->cache) { |
|
948 | if (self->cache) { | |
942 | Py_ssize_t i; |
|
949 | Py_ssize_t i; | |
943 |
|
950 | |||
944 | for (i = 0; i < self->raw_length; i++) |
|
951 | for (i = 0; i < self->raw_length; i++) | |
945 | Py_CLEAR(self->cache[i]); |
|
952 | Py_CLEAR(self->cache[i]); | |
946 | free(self->cache); |
|
953 | free(self->cache); | |
947 | self->cache = NULL; |
|
954 | self->cache = NULL; | |
948 | } |
|
955 | } | |
949 | if (self->offsets) { |
|
956 | if (self->offsets) { | |
950 | free(self->offsets); |
|
957 | free(self->offsets); | |
951 | self->offsets = NULL; |
|
958 | self->offsets = NULL; | |
952 | } |
|
959 | } | |
953 | if (self->nt) { |
|
960 | if (self->nt) { | |
954 | free(self->nt); |
|
961 | free(self->nt); | |
955 | self->nt = NULL; |
|
962 | self->nt = NULL; | |
956 | } |
|
963 | } | |
957 | Py_CLEAR(self->headrevs); |
|
964 | Py_CLEAR(self->headrevs); | |
958 | } |
|
965 | } | |
959 |
|
966 | |||
960 | static PyObject *index_clearcaches(indexObject *self) |
|
967 | static PyObject *index_clearcaches(indexObject *self) | |
961 | { |
|
968 | { | |
962 | _index_clearcaches(self); |
|
969 | _index_clearcaches(self); | |
963 | self->ntlength = self->ntcapacity = 0; |
|
970 | self->ntlength = self->ntcapacity = 0; | |
964 | self->ntdepth = self->ntsplits = 0; |
|
971 | self->ntdepth = self->ntsplits = 0; | |
965 | self->ntrev = -1; |
|
972 | self->ntrev = -1; | |
966 | self->ntlookups = self->ntmisses = 0; |
|
973 | self->ntlookups = self->ntmisses = 0; | |
967 | Py_RETURN_NONE; |
|
974 | Py_RETURN_NONE; | |
968 | } |
|
975 | } | |
969 |
|
976 | |||
970 | static PyObject *index_stats(indexObject *self) |
|
977 | static PyObject *index_stats(indexObject *self) | |
971 | { |
|
978 | { | |
972 | PyObject *obj = PyDict_New(); |
|
979 | PyObject *obj = PyDict_New(); | |
973 | PyObject *t = NULL; |
|
980 | PyObject *t = NULL; | |
974 |
|
981 | |||
975 | if (obj == NULL) |
|
982 | if (obj == NULL) | |
976 | return NULL; |
|
983 | return NULL; | |
977 |
|
984 | |||
978 | #define istat(__n, __d) \ |
|
985 | #define istat(__n, __d) \ | |
979 | t = PyInt_FromSsize_t(self->__n); \ |
|
986 | t = PyInt_FromSsize_t(self->__n); \ | |
980 | if (!t) \ |
|
987 | if (!t) \ | |
981 | goto bail; \ |
|
988 | goto bail; \ | |
982 | if (PyDict_SetItemString(obj, __d, t) == -1) \ |
|
989 | if (PyDict_SetItemString(obj, __d, t) == -1) \ | |
983 | goto bail; \ |
|
990 | goto bail; \ | |
984 | Py_DECREF(t); |
|
991 | Py_DECREF(t); | |
985 |
|
992 | |||
986 | if (self->added) { |
|
993 | if (self->added) { | |
987 | Py_ssize_t len = PyList_GET_SIZE(self->added); |
|
994 | Py_ssize_t len = PyList_GET_SIZE(self->added); | |
988 | t = PyInt_FromSsize_t(len); |
|
995 | t = PyInt_FromSsize_t(len); | |
989 | if (!t) |
|
996 | if (!t) | |
990 | goto bail; |
|
997 | goto bail; | |
991 | if (PyDict_SetItemString(obj, "index entries added", t) == -1) |
|
998 | if (PyDict_SetItemString(obj, "index entries added", t) == -1) | |
992 | goto bail; |
|
999 | goto bail; | |
993 | Py_DECREF(t); |
|
1000 | Py_DECREF(t); | |
994 | } |
|
1001 | } | |
995 |
|
1002 | |||
996 | if (self->raw_length != self->length - 1) |
|
1003 | if (self->raw_length != self->length - 1) | |
997 | istat(raw_length, "revs on disk"); |
|
1004 | istat(raw_length, "revs on disk"); | |
998 | istat(length, "revs in memory"); |
|
1005 | istat(length, "revs in memory"); | |
999 | istat(ntcapacity, "node trie capacity"); |
|
1006 | istat(ntcapacity, "node trie capacity"); | |
1000 | istat(ntdepth, "node trie depth"); |
|
1007 | istat(ntdepth, "node trie depth"); | |
1001 | istat(ntlength, "node trie count"); |
|
1008 | istat(ntlength, "node trie count"); | |
1002 | istat(ntlookups, "node trie lookups"); |
|
1009 | istat(ntlookups, "node trie lookups"); | |
1003 | istat(ntmisses, "node trie misses"); |
|
1010 | istat(ntmisses, "node trie misses"); | |
1004 | istat(ntrev, "node trie last rev scanned"); |
|
1011 | istat(ntrev, "node trie last rev scanned"); | |
1005 | istat(ntsplits, "node trie splits"); |
|
1012 | istat(ntsplits, "node trie splits"); | |
1006 |
|
1013 | |||
1007 | #undef istat |
|
1014 | #undef istat | |
1008 |
|
1015 | |||
1009 | return obj; |
|
1016 | return obj; | |
1010 |
|
1017 | |||
1011 | bail: |
|
1018 | bail: | |
1012 | Py_XDECREF(obj); |
|
1019 | Py_XDECREF(obj); | |
1013 | Py_XDECREF(t); |
|
1020 | Py_XDECREF(t); | |
1014 | return NULL; |
|
1021 | return NULL; | |
1015 | } |
|
1022 | } | |
1016 |
|
1023 | |||
1017 | /* |
|
1024 | /* | |
1018 | * When we cache a list, we want to be sure the caller can't mutate |
|
1025 | * When we cache a list, we want to be sure the caller can't mutate | |
1019 | * the cached copy. |
|
1026 | * the cached copy. | |
1020 | */ |
|
1027 | */ | |
1021 | static PyObject *list_copy(PyObject *list) |
|
1028 | static PyObject *list_copy(PyObject *list) | |
1022 | { |
|
1029 | { | |
1023 | Py_ssize_t len = PyList_GET_SIZE(list); |
|
1030 | Py_ssize_t len = PyList_GET_SIZE(list); | |
1024 | PyObject *newlist = PyList_New(len); |
|
1031 | PyObject *newlist = PyList_New(len); | |
1025 | Py_ssize_t i; |
|
1032 | Py_ssize_t i; | |
1026 |
|
1033 | |||
1027 | if (newlist == NULL) |
|
1034 | if (newlist == NULL) | |
1028 | return NULL; |
|
1035 | return NULL; | |
1029 |
|
1036 | |||
1030 | for (i = 0; i < len; i++) { |
|
1037 | for (i = 0; i < len; i++) { | |
1031 | PyObject *obj = PyList_GET_ITEM(list, i); |
|
1038 | PyObject *obj = PyList_GET_ITEM(list, i); | |
1032 | Py_INCREF(obj); |
|
1039 | Py_INCREF(obj); | |
1033 | PyList_SET_ITEM(newlist, i, obj); |
|
1040 | PyList_SET_ITEM(newlist, i, obj); | |
1034 | } |
|
1041 | } | |
1035 |
|
1042 | |||
1036 | return newlist; |
|
1043 | return newlist; | |
1037 | } |
|
1044 | } | |
1038 |
|
1045 | |||
1039 | /* arg should be Py_ssize_t but Python 2.4 do not support the n format */ |
|
1046 | /* arg should be Py_ssize_t but Python 2.4 do not support the n format */ | |
1040 | static int check_filter(PyObject *filter, unsigned long arg) { |
|
1047 | static int check_filter(PyObject *filter, unsigned long arg) { | |
1041 | if (filter) { |
|
1048 | if (filter) { | |
1042 | PyObject *arglist, *result; |
|
1049 | PyObject *arglist, *result; | |
1043 | int isfiltered; |
|
1050 | int isfiltered; | |
1044 |
|
1051 | |||
1045 | arglist = Py_BuildValue("(k)", arg); |
|
1052 | arglist = Py_BuildValue("(k)", arg); | |
1046 | if (!arglist) { |
|
1053 | if (!arglist) { | |
1047 | return -1; |
|
1054 | return -1; | |
1048 | } |
|
1055 | } | |
1049 |
|
1056 | |||
1050 | result = PyEval_CallObject(filter, arglist); |
|
1057 | result = PyEval_CallObject(filter, arglist); | |
1051 | Py_DECREF(arglist); |
|
1058 | Py_DECREF(arglist); | |
1052 | if (!result) { |
|
1059 | if (!result) { | |
1053 | return -1; |
|
1060 | return -1; | |
1054 | } |
|
1061 | } | |
1055 |
|
1062 | |||
1056 | /* PyObject_IsTrue returns 1 if true, 0 if false, -1 if error, |
|
1063 | /* PyObject_IsTrue returns 1 if true, 0 if false, -1 if error, | |
1057 | * same as this function, so we can just return it directly.*/ |
|
1064 | * same as this function, so we can just return it directly.*/ | |
1058 | isfiltered = PyObject_IsTrue(result); |
|
1065 | isfiltered = PyObject_IsTrue(result); | |
1059 | Py_DECREF(result); |
|
1066 | Py_DECREF(result); | |
1060 | return isfiltered; |
|
1067 | return isfiltered; | |
1061 | } else { |
|
1068 | } else { | |
1062 | return 0; |
|
1069 | return 0; | |
1063 | } |
|
1070 | } | |
1064 | } |
|
1071 | } | |
1065 |
|
1072 | |||
1066 | static Py_ssize_t add_roots_get_min(indexObject *self, PyObject *list, |
|
1073 | static Py_ssize_t add_roots_get_min(indexObject *self, PyObject *list, | |
1067 | Py_ssize_t marker, char *phases) |
|
1074 | Py_ssize_t marker, char *phases) | |
1068 | { |
|
1075 | { | |
1069 | PyObject *iter = NULL; |
|
1076 | PyObject *iter = NULL; | |
1070 | PyObject *iter_item = NULL; |
|
1077 | PyObject *iter_item = NULL; | |
1071 | Py_ssize_t min_idx = index_length(self) + 1; |
|
1078 | Py_ssize_t min_idx = index_length(self) + 1; | |
1072 | long iter_item_long; |
|
1079 | long iter_item_long; | |
1073 |
|
1080 | |||
1074 | if (PyList_GET_SIZE(list) != 0) { |
|
1081 | if (PyList_GET_SIZE(list) != 0) { | |
1075 | iter = PyObject_GetIter(list); |
|
1082 | iter = PyObject_GetIter(list); | |
1076 | if (iter == NULL) |
|
1083 | if (iter == NULL) | |
1077 | return -2; |
|
1084 | return -2; | |
1078 | while ((iter_item = PyIter_Next(iter))) |
|
1085 | while ((iter_item = PyIter_Next(iter))) | |
1079 | { |
|
1086 | { | |
1080 | iter_item_long = PyInt_AS_LONG(iter_item); |
|
1087 | iter_item_long = PyInt_AS_LONG(iter_item); | |
1081 | Py_DECREF(iter_item); |
|
1088 | Py_DECREF(iter_item); | |
1082 | if (iter_item_long < min_idx) |
|
1089 | if (iter_item_long < min_idx) | |
1083 | min_idx = iter_item_long; |
|
1090 | min_idx = iter_item_long; | |
1084 | phases[iter_item_long] = marker; |
|
1091 | phases[iter_item_long] = marker; | |
1085 | } |
|
1092 | } | |
1086 | Py_DECREF(iter); |
|
1093 | Py_DECREF(iter); | |
1087 | } |
|
1094 | } | |
1088 |
|
1095 | |||
1089 | return min_idx; |
|
1096 | return min_idx; | |
1090 | } |
|
1097 | } | |
1091 |
|
1098 | |||
1092 | static inline void set_phase_from_parents(char *phases, int parent_1, |
|
1099 | static inline void set_phase_from_parents(char *phases, int parent_1, | |
1093 | int parent_2, Py_ssize_t i) |
|
1100 | int parent_2, Py_ssize_t i) | |
1094 | { |
|
1101 | { | |
1095 | if (parent_1 >= 0 && phases[parent_1] > phases[i]) |
|
1102 | if (parent_1 >= 0 && phases[parent_1] > phases[i]) | |
1096 | phases[i] = phases[parent_1]; |
|
1103 | phases[i] = phases[parent_1]; | |
1097 | if (parent_2 >= 0 && phases[parent_2] > phases[i]) |
|
1104 | if (parent_2 >= 0 && phases[parent_2] > phases[i]) | |
1098 | phases[i] = phases[parent_2]; |
|
1105 | phases[i] = phases[parent_2]; | |
1099 | } |
|
1106 | } | |
1100 |
|
1107 | |||
1101 | static PyObject *compute_phases_map_sets(indexObject *self, PyObject *args) |
|
1108 | static PyObject *compute_phases_map_sets(indexObject *self, PyObject *args) | |
1102 | { |
|
1109 | { | |
1103 | PyObject *roots = Py_None; |
|
1110 | PyObject *roots = Py_None; | |
1104 | PyObject *ret = NULL; |
|
1111 | PyObject *ret = NULL; | |
1105 | PyObject *phaseslist = NULL; |
|
1112 | PyObject *phaseslist = NULL; | |
1106 | PyObject *phaseroots = NULL; |
|
1113 | PyObject *phaseroots = NULL; | |
1107 | PyObject *phaseset = NULL; |
|
1114 | PyObject *phaseset = NULL; | |
1108 | PyObject *phasessetlist = NULL; |
|
1115 | PyObject *phasessetlist = NULL; | |
1109 | Py_ssize_t len = index_length(self) - 1; |
|
1116 | Py_ssize_t len = index_length(self) - 1; | |
1110 | Py_ssize_t numphase = 0; |
|
1117 | Py_ssize_t numphase = 0; | |
1111 | Py_ssize_t minrevallphases = 0; |
|
1118 | Py_ssize_t minrevallphases = 0; | |
1112 | Py_ssize_t minrevphase = 0; |
|
1119 | Py_ssize_t minrevphase = 0; | |
1113 | Py_ssize_t i = 0; |
|
1120 | Py_ssize_t i = 0; | |
1114 | char *phases = NULL; |
|
1121 | char *phases = NULL; | |
1115 | long phase; |
|
1122 | long phase; | |
1116 |
|
1123 | |||
1117 | if (!PyArg_ParseTuple(args, "O", &roots)) |
|
1124 | if (!PyArg_ParseTuple(args, "O", &roots)) | |
1118 | goto release_none; |
|
1125 | goto release_none; | |
1119 | if (roots == NULL || !PyList_Check(roots)) |
|
1126 | if (roots == NULL || !PyList_Check(roots)) | |
1120 | goto release_none; |
|
1127 | goto release_none; | |
1121 |
|
1128 | |||
1122 | phases = calloc(len, 1); /* phase per rev: {0: public, 1: draft, 2: secret} */ |
|
1129 | phases = calloc(len, 1); /* phase per rev: {0: public, 1: draft, 2: secret} */ | |
1123 | if (phases == NULL) |
|
1130 | if (phases == NULL) | |
1124 | goto release_none; |
|
1131 | goto release_none; | |
1125 | /* Put the phase information of all the roots in phases */ |
|
1132 | /* Put the phase information of all the roots in phases */ | |
1126 | numphase = PyList_GET_SIZE(roots)+1; |
|
1133 | numphase = PyList_GET_SIZE(roots)+1; | |
1127 | minrevallphases = len + 1; |
|
1134 | minrevallphases = len + 1; | |
1128 | phasessetlist = PyList_New(numphase); |
|
1135 | phasessetlist = PyList_New(numphase); | |
1129 | if (phasessetlist == NULL) |
|
1136 | if (phasessetlist == NULL) | |
1130 | goto release_none; |
|
1137 | goto release_none; | |
1131 |
|
1138 | |||
1132 | PyList_SET_ITEM(phasessetlist, 0, Py_None); |
|
1139 | PyList_SET_ITEM(phasessetlist, 0, Py_None); | |
1133 | Py_INCREF(Py_None); |
|
1140 | Py_INCREF(Py_None); | |
1134 |
|
1141 | |||
1135 | for (i = 0; i < numphase-1; i++) { |
|
1142 | for (i = 0; i < numphase-1; i++) { | |
1136 | phaseroots = PyList_GET_ITEM(roots, i); |
|
1143 | phaseroots = PyList_GET_ITEM(roots, i); | |
1137 | phaseset = PySet_New(NULL); |
|
1144 | phaseset = PySet_New(NULL); | |
1138 | if (phaseset == NULL) |
|
1145 | if (phaseset == NULL) | |
1139 | goto release_phasesetlist; |
|
1146 | goto release_phasesetlist; | |
1140 | PyList_SET_ITEM(phasessetlist, i+1, phaseset); |
|
1147 | PyList_SET_ITEM(phasessetlist, i+1, phaseset); | |
1141 | if (!PyList_Check(phaseroots)) |
|
1148 | if (!PyList_Check(phaseroots)) | |
1142 | goto release_phasesetlist; |
|
1149 | goto release_phasesetlist; | |
1143 | minrevphase = add_roots_get_min(self, phaseroots, i+1, phases); |
|
1150 | minrevphase = add_roots_get_min(self, phaseroots, i+1, phases); | |
1144 | if (minrevphase == -2) /* Error from add_roots_get_min */ |
|
1151 | if (minrevphase == -2) /* Error from add_roots_get_min */ | |
1145 | goto release_phasesetlist; |
|
1152 | goto release_phasesetlist; | |
1146 | minrevallphases = MIN(minrevallphases, minrevphase); |
|
1153 | minrevallphases = MIN(minrevallphases, minrevphase); | |
1147 | } |
|
1154 | } | |
1148 | /* Propagate the phase information from the roots to the revs */ |
|
1155 | /* Propagate the phase information from the roots to the revs */ | |
1149 | if (minrevallphases != -1) { |
|
1156 | if (minrevallphases != -1) { | |
1150 | int parents[2]; |
|
1157 | int parents[2]; | |
1151 | for (i = minrevallphases; i < len; i++) { |
|
1158 | for (i = minrevallphases; i < len; i++) { | |
1152 |
index_get_parents(self, i, parents) |
|
1159 | if (index_get_parents(self, i, parents, len - 1) < 0) | |
|
1160 | goto release_phasesetlist; | |||
1153 | set_phase_from_parents(phases, parents[0], parents[1], i); |
|
1161 | set_phase_from_parents(phases, parents[0], parents[1], i); | |
1154 | } |
|
1162 | } | |
1155 | } |
|
1163 | } | |
1156 | /* Transform phase list to a python list */ |
|
1164 | /* Transform phase list to a python list */ | |
1157 | phaseslist = PyList_New(len); |
|
1165 | phaseslist = PyList_New(len); | |
1158 | if (phaseslist == NULL) |
|
1166 | if (phaseslist == NULL) | |
1159 | goto release_phasesetlist; |
|
1167 | goto release_phasesetlist; | |
1160 | for (i = 0; i < len; i++) { |
|
1168 | for (i = 0; i < len; i++) { | |
1161 | phase = phases[i]; |
|
1169 | phase = phases[i]; | |
1162 | /* We only store the sets of phase for non public phase, the public phase |
|
1170 | /* We only store the sets of phase for non public phase, the public phase | |
1163 | * is computed as a difference */ |
|
1171 | * is computed as a difference */ | |
1164 | if (phase != 0) { |
|
1172 | if (phase != 0) { | |
1165 | phaseset = PyList_GET_ITEM(phasessetlist, phase); |
|
1173 | phaseset = PyList_GET_ITEM(phasessetlist, phase); | |
1166 | PySet_Add(phaseset, PyInt_FromLong(i)); |
|
1174 | PySet_Add(phaseset, PyInt_FromLong(i)); | |
1167 | } |
|
1175 | } | |
1168 | PyList_SET_ITEM(phaseslist, i, PyInt_FromLong(phase)); |
|
1176 | PyList_SET_ITEM(phaseslist, i, PyInt_FromLong(phase)); | |
1169 | } |
|
1177 | } | |
1170 | ret = PyList_New(2); |
|
1178 | ret = PyList_New(2); | |
1171 | if (ret == NULL) |
|
1179 | if (ret == NULL) | |
1172 | goto release_phaseslist; |
|
1180 | goto release_phaseslist; | |
1173 |
|
1181 | |||
1174 | PyList_SET_ITEM(ret, 0, phaseslist); |
|
1182 | PyList_SET_ITEM(ret, 0, phaseslist); | |
1175 | PyList_SET_ITEM(ret, 1, phasessetlist); |
|
1183 | PyList_SET_ITEM(ret, 1, phasessetlist); | |
1176 | /* We don't release phaseslist and phasessetlist as we return them to |
|
1184 | /* We don't release phaseslist and phasessetlist as we return them to | |
1177 | * python */ |
|
1185 | * python */ | |
1178 | goto release_phases; |
|
1186 | goto release_phases; | |
1179 |
|
1187 | |||
1180 | release_phaseslist: |
|
1188 | release_phaseslist: | |
1181 | Py_XDECREF(phaseslist); |
|
1189 | Py_XDECREF(phaseslist); | |
1182 | release_phasesetlist: |
|
1190 | release_phasesetlist: | |
1183 | Py_XDECREF(phasessetlist); |
|
1191 | Py_XDECREF(phasessetlist); | |
1184 | release_phases: |
|
1192 | release_phases: | |
1185 | free(phases); |
|
1193 | free(phases); | |
1186 | release_none: |
|
1194 | release_none: | |
1187 | return ret; |
|
1195 | return ret; | |
1188 | } |
|
1196 | } | |
1189 |
|
1197 | |||
1190 | static PyObject *index_headrevs(indexObject *self, PyObject *args) |
|
1198 | static PyObject *index_headrevs(indexObject *self, PyObject *args) | |
1191 | { |
|
1199 | { | |
1192 | Py_ssize_t i, j, len; |
|
1200 | Py_ssize_t i, j, len; | |
1193 | char *nothead = NULL; |
|
1201 | char *nothead = NULL; | |
1194 | PyObject *heads = NULL; |
|
1202 | PyObject *heads = NULL; | |
1195 | PyObject *filter = NULL; |
|
1203 | PyObject *filter = NULL; | |
1196 | PyObject *filteredrevs = Py_None; |
|
1204 | PyObject *filteredrevs = Py_None; | |
1197 |
|
1205 | |||
1198 | if (!PyArg_ParseTuple(args, "|O", &filteredrevs)) { |
|
1206 | if (!PyArg_ParseTuple(args, "|O", &filteredrevs)) { | |
1199 | return NULL; |
|
1207 | return NULL; | |
1200 | } |
|
1208 | } | |
1201 |
|
1209 | |||
1202 | if (self->headrevs && filteredrevs == self->filteredrevs) |
|
1210 | if (self->headrevs && filteredrevs == self->filteredrevs) | |
1203 | return list_copy(self->headrevs); |
|
1211 | return list_copy(self->headrevs); | |
1204 |
|
1212 | |||
1205 | Py_DECREF(self->filteredrevs); |
|
1213 | Py_DECREF(self->filteredrevs); | |
1206 | self->filteredrevs = filteredrevs; |
|
1214 | self->filteredrevs = filteredrevs; | |
1207 | Py_INCREF(filteredrevs); |
|
1215 | Py_INCREF(filteredrevs); | |
1208 |
|
1216 | |||
1209 | if (filteredrevs != Py_None) { |
|
1217 | if (filteredrevs != Py_None) { | |
1210 | filter = PyObject_GetAttrString(filteredrevs, "__contains__"); |
|
1218 | filter = PyObject_GetAttrString(filteredrevs, "__contains__"); | |
1211 | if (!filter) { |
|
1219 | if (!filter) { | |
1212 | PyErr_SetString(PyExc_TypeError, |
|
1220 | PyErr_SetString(PyExc_TypeError, | |
1213 | "filteredrevs has no attribute __contains__"); |
|
1221 | "filteredrevs has no attribute __contains__"); | |
1214 | goto bail; |
|
1222 | goto bail; | |
1215 | } |
|
1223 | } | |
1216 | } |
|
1224 | } | |
1217 |
|
1225 | |||
1218 | len = index_length(self) - 1; |
|
1226 | len = index_length(self) - 1; | |
1219 | heads = PyList_New(0); |
|
1227 | heads = PyList_New(0); | |
1220 | if (heads == NULL) |
|
1228 | if (heads == NULL) | |
1221 | goto bail; |
|
1229 | goto bail; | |
1222 | if (len == 0) { |
|
1230 | if (len == 0) { | |
1223 | PyObject *nullid = PyInt_FromLong(-1); |
|
1231 | PyObject *nullid = PyInt_FromLong(-1); | |
1224 | if (nullid == NULL || PyList_Append(heads, nullid) == -1) { |
|
1232 | if (nullid == NULL || PyList_Append(heads, nullid) == -1) { | |
1225 | Py_XDECREF(nullid); |
|
1233 | Py_XDECREF(nullid); | |
1226 | goto bail; |
|
1234 | goto bail; | |
1227 | } |
|
1235 | } | |
1228 | goto done; |
|
1236 | goto done; | |
1229 | } |
|
1237 | } | |
1230 |
|
1238 | |||
1231 | nothead = calloc(len, 1); |
|
1239 | nothead = calloc(len, 1); | |
1232 | if (nothead == NULL) |
|
1240 | if (nothead == NULL) | |
1233 | goto bail; |
|
1241 | goto bail; | |
1234 |
|
1242 | |||
1235 | for (i = 0; i < len; i++) { |
|
1243 | for (i = 0; i < len; i++) { | |
1236 | int isfiltered; |
|
1244 | int isfiltered; | |
1237 | int parents[2]; |
|
1245 | int parents[2]; | |
1238 |
|
1246 | |||
1239 | isfiltered = check_filter(filter, i); |
|
1247 | isfiltered = check_filter(filter, i); | |
1240 | if (isfiltered == -1) { |
|
1248 | if (isfiltered == -1) { | |
1241 | PyErr_SetString(PyExc_TypeError, |
|
1249 | PyErr_SetString(PyExc_TypeError, | |
1242 | "unable to check filter"); |
|
1250 | "unable to check filter"); | |
1243 | goto bail; |
|
1251 | goto bail; | |
1244 | } |
|
1252 | } | |
1245 |
|
1253 | |||
1246 | if (isfiltered) { |
|
1254 | if (isfiltered) { | |
1247 | nothead[i] = 1; |
|
1255 | nothead[i] = 1; | |
1248 | continue; |
|
1256 | continue; | |
1249 | } |
|
1257 | } | |
1250 |
|
1258 | |||
1251 |
index_get_parents(self, i, parents) |
|
1259 | if (index_get_parents(self, i, parents, len - 1) < 0) | |
|
1260 | goto bail; | |||
1252 | for (j = 0; j < 2; j++) { |
|
1261 | for (j = 0; j < 2; j++) { | |
1253 | if (parents[j] >= 0) |
|
1262 | if (parents[j] >= 0) | |
1254 | nothead[parents[j]] = 1; |
|
1263 | nothead[parents[j]] = 1; | |
1255 | } |
|
1264 | } | |
1256 | } |
|
1265 | } | |
1257 |
|
1266 | |||
1258 | for (i = 0; i < len; i++) { |
|
1267 | for (i = 0; i < len; i++) { | |
1259 | PyObject *head; |
|
1268 | PyObject *head; | |
1260 |
|
1269 | |||
1261 | if (nothead[i]) |
|
1270 | if (nothead[i]) | |
1262 | continue; |
|
1271 | continue; | |
1263 | head = PyInt_FromSsize_t(i); |
|
1272 | head = PyInt_FromSsize_t(i); | |
1264 | if (head == NULL || PyList_Append(heads, head) == -1) { |
|
1273 | if (head == NULL || PyList_Append(heads, head) == -1) { | |
1265 | Py_XDECREF(head); |
|
1274 | Py_XDECREF(head); | |
1266 | goto bail; |
|
1275 | goto bail; | |
1267 | } |
|
1276 | } | |
1268 | } |
|
1277 | } | |
1269 |
|
1278 | |||
1270 | done: |
|
1279 | done: | |
1271 | self->headrevs = heads; |
|
1280 | self->headrevs = heads; | |
1272 | Py_XDECREF(filter); |
|
1281 | Py_XDECREF(filter); | |
1273 | free(nothead); |
|
1282 | free(nothead); | |
1274 | return list_copy(self->headrevs); |
|
1283 | return list_copy(self->headrevs); | |
1275 | bail: |
|
1284 | bail: | |
1276 | Py_XDECREF(filter); |
|
1285 | Py_XDECREF(filter); | |
1277 | Py_XDECREF(heads); |
|
1286 | Py_XDECREF(heads); | |
1278 | free(nothead); |
|
1287 | free(nothead); | |
1279 | return NULL; |
|
1288 | return NULL; | |
1280 | } |
|
1289 | } | |
1281 |
|
1290 | |||
1282 | static inline int nt_level(const char *node, Py_ssize_t level) |
|
1291 | static inline int nt_level(const char *node, Py_ssize_t level) | |
1283 | { |
|
1292 | { | |
1284 | int v = node[level>>1]; |
|
1293 | int v = node[level>>1]; | |
1285 | if (!(level & 1)) |
|
1294 | if (!(level & 1)) | |
1286 | v >>= 4; |
|
1295 | v >>= 4; | |
1287 | return v & 0xf; |
|
1296 | return v & 0xf; | |
1288 | } |
|
1297 | } | |
1289 |
|
1298 | |||
1290 | /* |
|
1299 | /* | |
1291 | * Return values: |
|
1300 | * Return values: | |
1292 | * |
|
1301 | * | |
1293 | * -4: match is ambiguous (multiple candidates) |
|
1302 | * -4: match is ambiguous (multiple candidates) | |
1294 | * -2: not found |
|
1303 | * -2: not found | |
1295 | * rest: valid rev |
|
1304 | * rest: valid rev | |
1296 | */ |
|
1305 | */ | |
1297 | static int nt_find(indexObject *self, const char *node, Py_ssize_t nodelen, |
|
1306 | static int nt_find(indexObject *self, const char *node, Py_ssize_t nodelen, | |
1298 | int hex) |
|
1307 | int hex) | |
1299 | { |
|
1308 | { | |
1300 | int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level; |
|
1309 | int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level; | |
1301 | int level, maxlevel, off; |
|
1310 | int level, maxlevel, off; | |
1302 |
|
1311 | |||
1303 | if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0) |
|
1312 | if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0) | |
1304 | return -1; |
|
1313 | return -1; | |
1305 |
|
1314 | |||
1306 | if (self->nt == NULL) |
|
1315 | if (self->nt == NULL) | |
1307 | return -2; |
|
1316 | return -2; | |
1308 |
|
1317 | |||
1309 | if (hex) |
|
1318 | if (hex) | |
1310 | maxlevel = nodelen > 40 ? 40 : (int)nodelen; |
|
1319 | maxlevel = nodelen > 40 ? 40 : (int)nodelen; | |
1311 | else |
|
1320 | else | |
1312 | maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2); |
|
1321 | maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2); | |
1313 |
|
1322 | |||
1314 | for (level = off = 0; level < maxlevel; level++) { |
|
1323 | for (level = off = 0; level < maxlevel; level++) { | |
1315 | int k = getnybble(node, level); |
|
1324 | int k = getnybble(node, level); | |
1316 | nodetree *n = &self->nt[off]; |
|
1325 | nodetree *n = &self->nt[off]; | |
1317 | int v = n->children[k]; |
|
1326 | int v = n->children[k]; | |
1318 |
|
1327 | |||
1319 | if (v < 0) { |
|
1328 | if (v < 0) { | |
1320 | const char *n; |
|
1329 | const char *n; | |
1321 | Py_ssize_t i; |
|
1330 | Py_ssize_t i; | |
1322 |
|
1331 | |||
1323 | v = -(v + 1); |
|
1332 | v = -(v + 1); | |
1324 | n = index_node(self, v); |
|
1333 | n = index_node(self, v); | |
1325 | if (n == NULL) |
|
1334 | if (n == NULL) | |
1326 | return -2; |
|
1335 | return -2; | |
1327 | for (i = level; i < maxlevel; i++) |
|
1336 | for (i = level; i < maxlevel; i++) | |
1328 | if (getnybble(node, i) != nt_level(n, i)) |
|
1337 | if (getnybble(node, i) != nt_level(n, i)) | |
1329 | return -2; |
|
1338 | return -2; | |
1330 | return v; |
|
1339 | return v; | |
1331 | } |
|
1340 | } | |
1332 | if (v == 0) |
|
1341 | if (v == 0) | |
1333 | return -2; |
|
1342 | return -2; | |
1334 | off = v; |
|
1343 | off = v; | |
1335 | } |
|
1344 | } | |
1336 | /* multiple matches against an ambiguous prefix */ |
|
1345 | /* multiple matches against an ambiguous prefix */ | |
1337 | return -4; |
|
1346 | return -4; | |
1338 | } |
|
1347 | } | |
1339 |
|
1348 | |||
1340 | static int nt_new(indexObject *self) |
|
1349 | static int nt_new(indexObject *self) | |
1341 | { |
|
1350 | { | |
1342 | if (self->ntlength == self->ntcapacity) { |
|
1351 | if (self->ntlength == self->ntcapacity) { | |
1343 | if (self->ntcapacity >= INT_MAX / (sizeof(nodetree) * 2)) { |
|
1352 | if (self->ntcapacity >= INT_MAX / (sizeof(nodetree) * 2)) { | |
1344 | PyErr_SetString(PyExc_MemoryError, |
|
1353 | PyErr_SetString(PyExc_MemoryError, | |
1345 | "overflow in nt_new"); |
|
1354 | "overflow in nt_new"); | |
1346 | return -1; |
|
1355 | return -1; | |
1347 | } |
|
1356 | } | |
1348 | self->ntcapacity *= 2; |
|
1357 | self->ntcapacity *= 2; | |
1349 | self->nt = realloc(self->nt, |
|
1358 | self->nt = realloc(self->nt, | |
1350 | self->ntcapacity * sizeof(nodetree)); |
|
1359 | self->ntcapacity * sizeof(nodetree)); | |
1351 | if (self->nt == NULL) { |
|
1360 | if (self->nt == NULL) { | |
1352 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
|
1361 | PyErr_SetString(PyExc_MemoryError, "out of memory"); | |
1353 | return -1; |
|
1362 | return -1; | |
1354 | } |
|
1363 | } | |
1355 | memset(&self->nt[self->ntlength], 0, |
|
1364 | memset(&self->nt[self->ntlength], 0, | |
1356 | sizeof(nodetree) * (self->ntcapacity - self->ntlength)); |
|
1365 | sizeof(nodetree) * (self->ntcapacity - self->ntlength)); | |
1357 | } |
|
1366 | } | |
1358 | return self->ntlength++; |
|
1367 | return self->ntlength++; | |
1359 | } |
|
1368 | } | |
1360 |
|
1369 | |||
1361 | static int nt_insert(indexObject *self, const char *node, int rev) |
|
1370 | static int nt_insert(indexObject *self, const char *node, int rev) | |
1362 | { |
|
1371 | { | |
1363 | int level = 0; |
|
1372 | int level = 0; | |
1364 | int off = 0; |
|
1373 | int off = 0; | |
1365 |
|
1374 | |||
1366 | while (level < 40) { |
|
1375 | while (level < 40) { | |
1367 | int k = nt_level(node, level); |
|
1376 | int k = nt_level(node, level); | |
1368 | nodetree *n; |
|
1377 | nodetree *n; | |
1369 | int v; |
|
1378 | int v; | |
1370 |
|
1379 | |||
1371 | n = &self->nt[off]; |
|
1380 | n = &self->nt[off]; | |
1372 | v = n->children[k]; |
|
1381 | v = n->children[k]; | |
1373 |
|
1382 | |||
1374 | if (v == 0) { |
|
1383 | if (v == 0) { | |
1375 | n->children[k] = -rev - 1; |
|
1384 | n->children[k] = -rev - 1; | |
1376 | return 0; |
|
1385 | return 0; | |
1377 | } |
|
1386 | } | |
1378 | if (v < 0) { |
|
1387 | if (v < 0) { | |
1379 | const char *oldnode = index_node(self, -(v + 1)); |
|
1388 | const char *oldnode = index_node(self, -(v + 1)); | |
1380 | int noff; |
|
1389 | int noff; | |
1381 |
|
1390 | |||
1382 | if (!oldnode || !memcmp(oldnode, node, 20)) { |
|
1391 | if (!oldnode || !memcmp(oldnode, node, 20)) { | |
1383 | n->children[k] = -rev - 1; |
|
1392 | n->children[k] = -rev - 1; | |
1384 | return 0; |
|
1393 | return 0; | |
1385 | } |
|
1394 | } | |
1386 | noff = nt_new(self); |
|
1395 | noff = nt_new(self); | |
1387 | if (noff == -1) |
|
1396 | if (noff == -1) | |
1388 | return -1; |
|
1397 | return -1; | |
1389 | /* self->nt may have been changed by realloc */ |
|
1398 | /* self->nt may have been changed by realloc */ | |
1390 | self->nt[off].children[k] = noff; |
|
1399 | self->nt[off].children[k] = noff; | |
1391 | off = noff; |
|
1400 | off = noff; | |
1392 | n = &self->nt[off]; |
|
1401 | n = &self->nt[off]; | |
1393 | n->children[nt_level(oldnode, ++level)] = v; |
|
1402 | n->children[nt_level(oldnode, ++level)] = v; | |
1394 | if (level > self->ntdepth) |
|
1403 | if (level > self->ntdepth) | |
1395 | self->ntdepth = level; |
|
1404 | self->ntdepth = level; | |
1396 | self->ntsplits += 1; |
|
1405 | self->ntsplits += 1; | |
1397 | } else { |
|
1406 | } else { | |
1398 | level += 1; |
|
1407 | level += 1; | |
1399 | off = v; |
|
1408 | off = v; | |
1400 | } |
|
1409 | } | |
1401 | } |
|
1410 | } | |
1402 |
|
1411 | |||
1403 | return -1; |
|
1412 | return -1; | |
1404 | } |
|
1413 | } | |
1405 |
|
1414 | |||
1406 | static int nt_init(indexObject *self) |
|
1415 | static int nt_init(indexObject *self) | |
1407 | { |
|
1416 | { | |
1408 | if (self->nt == NULL) { |
|
1417 | if (self->nt == NULL) { | |
1409 | if (self->raw_length > INT_MAX / sizeof(nodetree)) { |
|
1418 | if (self->raw_length > INT_MAX / sizeof(nodetree)) { | |
1410 | PyErr_SetString(PyExc_ValueError, "overflow in nt_init"); |
|
1419 | PyErr_SetString(PyExc_ValueError, "overflow in nt_init"); | |
1411 | return -1; |
|
1420 | return -1; | |
1412 | } |
|
1421 | } | |
1413 | self->ntcapacity = self->raw_length < 4 |
|
1422 | self->ntcapacity = self->raw_length < 4 | |
1414 | ? 4 : (int)self->raw_length / 2; |
|
1423 | ? 4 : (int)self->raw_length / 2; | |
1415 |
|
1424 | |||
1416 | self->nt = calloc(self->ntcapacity, sizeof(nodetree)); |
|
1425 | self->nt = calloc(self->ntcapacity, sizeof(nodetree)); | |
1417 | if (self->nt == NULL) { |
|
1426 | if (self->nt == NULL) { | |
1418 | PyErr_NoMemory(); |
|
1427 | PyErr_NoMemory(); | |
1419 | return -1; |
|
1428 | return -1; | |
1420 | } |
|
1429 | } | |
1421 | self->ntlength = 1; |
|
1430 | self->ntlength = 1; | |
1422 | self->ntrev = (int)index_length(self) - 1; |
|
1431 | self->ntrev = (int)index_length(self) - 1; | |
1423 | self->ntlookups = 1; |
|
1432 | self->ntlookups = 1; | |
1424 | self->ntmisses = 0; |
|
1433 | self->ntmisses = 0; | |
1425 | if (nt_insert(self, nullid, INT_MAX) == -1) |
|
1434 | if (nt_insert(self, nullid, INT_MAX) == -1) | |
1426 | return -1; |
|
1435 | return -1; | |
1427 | } |
|
1436 | } | |
1428 | return 0; |
|
1437 | return 0; | |
1429 | } |
|
1438 | } | |
1430 |
|
1439 | |||
1431 | /* |
|
1440 | /* | |
1432 | * Return values: |
|
1441 | * Return values: | |
1433 | * |
|
1442 | * | |
1434 | * -3: error (exception set) |
|
1443 | * -3: error (exception set) | |
1435 | * -2: not found (no exception set) |
|
1444 | * -2: not found (no exception set) | |
1436 | * rest: valid rev |
|
1445 | * rest: valid rev | |
1437 | */ |
|
1446 | */ | |
1438 | static int index_find_node(indexObject *self, |
|
1447 | static int index_find_node(indexObject *self, | |
1439 | const char *node, Py_ssize_t nodelen) |
|
1448 | const char *node, Py_ssize_t nodelen) | |
1440 | { |
|
1449 | { | |
1441 | int rev; |
|
1450 | int rev; | |
1442 |
|
1451 | |||
1443 | self->ntlookups++; |
|
1452 | self->ntlookups++; | |
1444 | rev = nt_find(self, node, nodelen, 0); |
|
1453 | rev = nt_find(self, node, nodelen, 0); | |
1445 | if (rev >= -1) |
|
1454 | if (rev >= -1) | |
1446 | return rev; |
|
1455 | return rev; | |
1447 |
|
1456 | |||
1448 | if (nt_init(self) == -1) |
|
1457 | if (nt_init(self) == -1) | |
1449 | return -3; |
|
1458 | return -3; | |
1450 |
|
1459 | |||
1451 | /* |
|
1460 | /* | |
1452 | * For the first handful of lookups, we scan the entire index, |
|
1461 | * For the first handful of lookups, we scan the entire index, | |
1453 | * and cache only the matching nodes. This optimizes for cases |
|
1462 | * and cache only the matching nodes. This optimizes for cases | |
1454 | * like "hg tip", where only a few nodes are accessed. |
|
1463 | * like "hg tip", where only a few nodes are accessed. | |
1455 | * |
|
1464 | * | |
1456 | * After that, we cache every node we visit, using a single |
|
1465 | * After that, we cache every node we visit, using a single | |
1457 | * scan amortized over multiple lookups. This gives the best |
|
1466 | * scan amortized over multiple lookups. This gives the best | |
1458 | * bulk performance, e.g. for "hg log". |
|
1467 | * bulk performance, e.g. for "hg log". | |
1459 | */ |
|
1468 | */ | |
1460 | if (self->ntmisses++ < 4) { |
|
1469 | if (self->ntmisses++ < 4) { | |
1461 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
1470 | for (rev = self->ntrev - 1; rev >= 0; rev--) { | |
1462 | const char *n = index_node(self, rev); |
|
1471 | const char *n = index_node(self, rev); | |
1463 | if (n == NULL) |
|
1472 | if (n == NULL) | |
1464 | return -2; |
|
1473 | return -2; | |
1465 | if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) { |
|
1474 | if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) { | |
1466 | if (nt_insert(self, n, rev) == -1) |
|
1475 | if (nt_insert(self, n, rev) == -1) | |
1467 | return -3; |
|
1476 | return -3; | |
1468 | break; |
|
1477 | break; | |
1469 | } |
|
1478 | } | |
1470 | } |
|
1479 | } | |
1471 | } else { |
|
1480 | } else { | |
1472 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
1481 | for (rev = self->ntrev - 1; rev >= 0; rev--) { | |
1473 | const char *n = index_node(self, rev); |
|
1482 | const char *n = index_node(self, rev); | |
1474 | if (n == NULL) { |
|
1483 | if (n == NULL) { | |
1475 | self->ntrev = rev + 1; |
|
1484 | self->ntrev = rev + 1; | |
1476 | return -2; |
|
1485 | return -2; | |
1477 | } |
|
1486 | } | |
1478 | if (nt_insert(self, n, rev) == -1) { |
|
1487 | if (nt_insert(self, n, rev) == -1) { | |
1479 | self->ntrev = rev + 1; |
|
1488 | self->ntrev = rev + 1; | |
1480 | return -3; |
|
1489 | return -3; | |
1481 | } |
|
1490 | } | |
1482 | if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) { |
|
1491 | if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) { | |
1483 | break; |
|
1492 | break; | |
1484 | } |
|
1493 | } | |
1485 | } |
|
1494 | } | |
1486 | self->ntrev = rev; |
|
1495 | self->ntrev = rev; | |
1487 | } |
|
1496 | } | |
1488 |
|
1497 | |||
1489 | if (rev >= 0) |
|
1498 | if (rev >= 0) | |
1490 | return rev; |
|
1499 | return rev; | |
1491 | return -2; |
|
1500 | return -2; | |
1492 | } |
|
1501 | } | |
1493 |
|
1502 | |||
1494 | static void raise_revlog_error(void) |
|
1503 | static void raise_revlog_error(void) | |
1495 | { |
|
1504 | { | |
1496 | PyObject *mod = NULL, *dict = NULL, *errclass = NULL; |
|
1505 | PyObject *mod = NULL, *dict = NULL, *errclass = NULL; | |
1497 |
|
1506 | |||
1498 | mod = PyImport_ImportModule("mercurial.error"); |
|
1507 | mod = PyImport_ImportModule("mercurial.error"); | |
1499 | if (mod == NULL) { |
|
1508 | if (mod == NULL) { | |
1500 | goto cleanup; |
|
1509 | goto cleanup; | |
1501 | } |
|
1510 | } | |
1502 |
|
1511 | |||
1503 | dict = PyModule_GetDict(mod); |
|
1512 | dict = PyModule_GetDict(mod); | |
1504 | if (dict == NULL) { |
|
1513 | if (dict == NULL) { | |
1505 | goto cleanup; |
|
1514 | goto cleanup; | |
1506 | } |
|
1515 | } | |
1507 | Py_INCREF(dict); |
|
1516 | Py_INCREF(dict); | |
1508 |
|
1517 | |||
1509 | errclass = PyDict_GetItemString(dict, "RevlogError"); |
|
1518 | errclass = PyDict_GetItemString(dict, "RevlogError"); | |
1510 | if (errclass == NULL) { |
|
1519 | if (errclass == NULL) { | |
1511 | PyErr_SetString(PyExc_SystemError, |
|
1520 | PyErr_SetString(PyExc_SystemError, | |
1512 | "could not find RevlogError"); |
|
1521 | "could not find RevlogError"); | |
1513 | goto cleanup; |
|
1522 | goto cleanup; | |
1514 | } |
|
1523 | } | |
1515 |
|
1524 | |||
1516 | /* value of exception is ignored by callers */ |
|
1525 | /* value of exception is ignored by callers */ | |
1517 | PyErr_SetString(errclass, "RevlogError"); |
|
1526 | PyErr_SetString(errclass, "RevlogError"); | |
1518 |
|
1527 | |||
1519 | cleanup: |
|
1528 | cleanup: | |
1520 | Py_XDECREF(dict); |
|
1529 | Py_XDECREF(dict); | |
1521 | Py_XDECREF(mod); |
|
1530 | Py_XDECREF(mod); | |
1522 | } |
|
1531 | } | |
1523 |
|
1532 | |||
1524 | static PyObject *index_getitem(indexObject *self, PyObject *value) |
|
1533 | static PyObject *index_getitem(indexObject *self, PyObject *value) | |
1525 | { |
|
1534 | { | |
1526 | char *node; |
|
1535 | char *node; | |
1527 | Py_ssize_t nodelen; |
|
1536 | Py_ssize_t nodelen; | |
1528 | int rev; |
|
1537 | int rev; | |
1529 |
|
1538 | |||
1530 | if (PyInt_Check(value)) |
|
1539 | if (PyInt_Check(value)) | |
1531 | return index_get(self, PyInt_AS_LONG(value)); |
|
1540 | return index_get(self, PyInt_AS_LONG(value)); | |
1532 |
|
1541 | |||
1533 | if (node_check(value, &node, &nodelen) == -1) |
|
1542 | if (node_check(value, &node, &nodelen) == -1) | |
1534 | return NULL; |
|
1543 | return NULL; | |
1535 | rev = index_find_node(self, node, nodelen); |
|
1544 | rev = index_find_node(self, node, nodelen); | |
1536 | if (rev >= -1) |
|
1545 | if (rev >= -1) | |
1537 | return PyInt_FromLong(rev); |
|
1546 | return PyInt_FromLong(rev); | |
1538 | if (rev == -2) |
|
1547 | if (rev == -2) | |
1539 | raise_revlog_error(); |
|
1548 | raise_revlog_error(); | |
1540 | return NULL; |
|
1549 | return NULL; | |
1541 | } |
|
1550 | } | |
1542 |
|
1551 | |||
1543 | static int nt_partialmatch(indexObject *self, const char *node, |
|
1552 | static int nt_partialmatch(indexObject *self, const char *node, | |
1544 | Py_ssize_t nodelen) |
|
1553 | Py_ssize_t nodelen) | |
1545 | { |
|
1554 | { | |
1546 | int rev; |
|
1555 | int rev; | |
1547 |
|
1556 | |||
1548 | if (nt_init(self) == -1) |
|
1557 | if (nt_init(self) == -1) | |
1549 | return -3; |
|
1558 | return -3; | |
1550 |
|
1559 | |||
1551 | if (self->ntrev > 0) { |
|
1560 | if (self->ntrev > 0) { | |
1552 | /* ensure that the radix tree is fully populated */ |
|
1561 | /* ensure that the radix tree is fully populated */ | |
1553 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
1562 | for (rev = self->ntrev - 1; rev >= 0; rev--) { | |
1554 | const char *n = index_node(self, rev); |
|
1563 | const char *n = index_node(self, rev); | |
1555 | if (n == NULL) |
|
1564 | if (n == NULL) | |
1556 | return -2; |
|
1565 | return -2; | |
1557 | if (nt_insert(self, n, rev) == -1) |
|
1566 | if (nt_insert(self, n, rev) == -1) | |
1558 | return -3; |
|
1567 | return -3; | |
1559 | } |
|
1568 | } | |
1560 | self->ntrev = rev; |
|
1569 | self->ntrev = rev; | |
1561 | } |
|
1570 | } | |
1562 |
|
1571 | |||
1563 | return nt_find(self, node, nodelen, 1); |
|
1572 | return nt_find(self, node, nodelen, 1); | |
1564 | } |
|
1573 | } | |
1565 |
|
1574 | |||
1566 | static PyObject *index_partialmatch(indexObject *self, PyObject *args) |
|
1575 | static PyObject *index_partialmatch(indexObject *self, PyObject *args) | |
1567 | { |
|
1576 | { | |
1568 | const char *fullnode; |
|
1577 | const char *fullnode; | |
1569 | int nodelen; |
|
1578 | int nodelen; | |
1570 | char *node; |
|
1579 | char *node; | |
1571 | int rev, i; |
|
1580 | int rev, i; | |
1572 |
|
1581 | |||
1573 | if (!PyArg_ParseTuple(args, "s#", &node, &nodelen)) |
|
1582 | if (!PyArg_ParseTuple(args, "s#", &node, &nodelen)) | |
1574 | return NULL; |
|
1583 | return NULL; | |
1575 |
|
1584 | |||
1576 | if (nodelen < 4) { |
|
1585 | if (nodelen < 4) { | |
1577 | PyErr_SetString(PyExc_ValueError, "key too short"); |
|
1586 | PyErr_SetString(PyExc_ValueError, "key too short"); | |
1578 | return NULL; |
|
1587 | return NULL; | |
1579 | } |
|
1588 | } | |
1580 |
|
1589 | |||
1581 | if (nodelen > 40) { |
|
1590 | if (nodelen > 40) { | |
1582 | PyErr_SetString(PyExc_ValueError, "key too long"); |
|
1591 | PyErr_SetString(PyExc_ValueError, "key too long"); | |
1583 | return NULL; |
|
1592 | return NULL; | |
1584 | } |
|
1593 | } | |
1585 |
|
1594 | |||
1586 | for (i = 0; i < nodelen; i++) |
|
1595 | for (i = 0; i < nodelen; i++) | |
1587 | hexdigit(node, i); |
|
1596 | hexdigit(node, i); | |
1588 | if (PyErr_Occurred()) { |
|
1597 | if (PyErr_Occurred()) { | |
1589 | /* input contains non-hex characters */ |
|
1598 | /* input contains non-hex characters */ | |
1590 | PyErr_Clear(); |
|
1599 | PyErr_Clear(); | |
1591 | Py_RETURN_NONE; |
|
1600 | Py_RETURN_NONE; | |
1592 | } |
|
1601 | } | |
1593 |
|
1602 | |||
1594 | rev = nt_partialmatch(self, node, nodelen); |
|
1603 | rev = nt_partialmatch(self, node, nodelen); | |
1595 |
|
1604 | |||
1596 | switch (rev) { |
|
1605 | switch (rev) { | |
1597 | case -4: |
|
1606 | case -4: | |
1598 | raise_revlog_error(); |
|
1607 | raise_revlog_error(); | |
1599 | case -3: |
|
1608 | case -3: | |
1600 | return NULL; |
|
1609 | return NULL; | |
1601 | case -2: |
|
1610 | case -2: | |
1602 | Py_RETURN_NONE; |
|
1611 | Py_RETURN_NONE; | |
1603 | case -1: |
|
1612 | case -1: | |
1604 | return PyString_FromStringAndSize(nullid, 20); |
|
1613 | return PyString_FromStringAndSize(nullid, 20); | |
1605 | } |
|
1614 | } | |
1606 |
|
1615 | |||
1607 | fullnode = index_node(self, rev); |
|
1616 | fullnode = index_node(self, rev); | |
1608 | if (fullnode == NULL) { |
|
1617 | if (fullnode == NULL) { | |
1609 | PyErr_Format(PyExc_IndexError, |
|
1618 | PyErr_Format(PyExc_IndexError, | |
1610 | "could not access rev %d", rev); |
|
1619 | "could not access rev %d", rev); | |
1611 | return NULL; |
|
1620 | return NULL; | |
1612 | } |
|
1621 | } | |
1613 | return PyString_FromStringAndSize(fullnode, 20); |
|
1622 | return PyString_FromStringAndSize(fullnode, 20); | |
1614 | } |
|
1623 | } | |
1615 |
|
1624 | |||
1616 | static PyObject *index_m_get(indexObject *self, PyObject *args) |
|
1625 | static PyObject *index_m_get(indexObject *self, PyObject *args) | |
1617 | { |
|
1626 | { | |
1618 | Py_ssize_t nodelen; |
|
1627 | Py_ssize_t nodelen; | |
1619 | PyObject *val; |
|
1628 | PyObject *val; | |
1620 | char *node; |
|
1629 | char *node; | |
1621 | int rev; |
|
1630 | int rev; | |
1622 |
|
1631 | |||
1623 | if (!PyArg_ParseTuple(args, "O", &val)) |
|
1632 | if (!PyArg_ParseTuple(args, "O", &val)) | |
1624 | return NULL; |
|
1633 | return NULL; | |
1625 | if (node_check(val, &node, &nodelen) == -1) |
|
1634 | if (node_check(val, &node, &nodelen) == -1) | |
1626 | return NULL; |
|
1635 | return NULL; | |
1627 | rev = index_find_node(self, node, nodelen); |
|
1636 | rev = index_find_node(self, node, nodelen); | |
1628 | if (rev == -3) |
|
1637 | if (rev == -3) | |
1629 | return NULL; |
|
1638 | return NULL; | |
1630 | if (rev == -2) |
|
1639 | if (rev == -2) | |
1631 | Py_RETURN_NONE; |
|
1640 | Py_RETURN_NONE; | |
1632 | return PyInt_FromLong(rev); |
|
1641 | return PyInt_FromLong(rev); | |
1633 | } |
|
1642 | } | |
1634 |
|
1643 | |||
1635 | static int index_contains(indexObject *self, PyObject *value) |
|
1644 | static int index_contains(indexObject *self, PyObject *value) | |
1636 | { |
|
1645 | { | |
1637 | char *node; |
|
1646 | char *node; | |
1638 | Py_ssize_t nodelen; |
|
1647 | Py_ssize_t nodelen; | |
1639 |
|
1648 | |||
1640 | if (PyInt_Check(value)) { |
|
1649 | if (PyInt_Check(value)) { | |
1641 | long rev = PyInt_AS_LONG(value); |
|
1650 | long rev = PyInt_AS_LONG(value); | |
1642 | return rev >= -1 && rev < index_length(self); |
|
1651 | return rev >= -1 && rev < index_length(self); | |
1643 | } |
|
1652 | } | |
1644 |
|
1653 | |||
1645 | if (node_check(value, &node, &nodelen) == -1) |
|
1654 | if (node_check(value, &node, &nodelen) == -1) | |
1646 | return -1; |
|
1655 | return -1; | |
1647 |
|
1656 | |||
1648 | switch (index_find_node(self, node, nodelen)) { |
|
1657 | switch (index_find_node(self, node, nodelen)) { | |
1649 | case -3: |
|
1658 | case -3: | |
1650 | return -1; |
|
1659 | return -1; | |
1651 | case -2: |
|
1660 | case -2: | |
1652 | return 0; |
|
1661 | return 0; | |
1653 | default: |
|
1662 | default: | |
1654 | return 1; |
|
1663 | return 1; | |
1655 | } |
|
1664 | } | |
1656 | } |
|
1665 | } | |
1657 |
|
1666 | |||
1658 | typedef uint64_t bitmask; |
|
1667 | typedef uint64_t bitmask; | |
1659 |
|
1668 | |||
1660 | /* |
|
1669 | /* | |
1661 | * Given a disjoint set of revs, return all candidates for the |
|
1670 | * Given a disjoint set of revs, return all candidates for the | |
1662 | * greatest common ancestor. In revset notation, this is the set |
|
1671 | * greatest common ancestor. In revset notation, this is the set | |
1663 | * "heads(::a and ::b and ...)" |
|
1672 | * "heads(::a and ::b and ...)" | |
1664 | */ |
|
1673 | */ | |
1665 | static PyObject *find_gca_candidates(indexObject *self, const int *revs, |
|
1674 | static PyObject *find_gca_candidates(indexObject *self, const int *revs, | |
1666 | int revcount) |
|
1675 | int revcount) | |
1667 | { |
|
1676 | { | |
1668 | const bitmask allseen = (1ull << revcount) - 1; |
|
1677 | const bitmask allseen = (1ull << revcount) - 1; | |
1669 | const bitmask poison = 1ull << revcount; |
|
1678 | const bitmask poison = 1ull << revcount; | |
1670 | PyObject *gca = PyList_New(0); |
|
1679 | PyObject *gca = PyList_New(0); | |
1671 | int i, v, interesting; |
|
1680 | int i, v, interesting; | |
1672 | int maxrev = -1; |
|
1681 | int maxrev = -1; | |
1673 | bitmask sp; |
|
1682 | bitmask sp; | |
1674 | bitmask *seen; |
|
1683 | bitmask *seen; | |
1675 |
|
1684 | |||
1676 | if (gca == NULL) |
|
1685 | if (gca == NULL) | |
1677 | return PyErr_NoMemory(); |
|
1686 | return PyErr_NoMemory(); | |
1678 |
|
1687 | |||
1679 | for (i = 0; i < revcount; i++) { |
|
1688 | for (i = 0; i < revcount; i++) { | |
1680 | if (revs[i] > maxrev) |
|
1689 | if (revs[i] > maxrev) | |
1681 | maxrev = revs[i]; |
|
1690 | maxrev = revs[i]; | |
1682 | } |
|
1691 | } | |
1683 |
|
1692 | |||
1684 | seen = calloc(sizeof(*seen), maxrev + 1); |
|
1693 | seen = calloc(sizeof(*seen), maxrev + 1); | |
1685 | if (seen == NULL) { |
|
1694 | if (seen == NULL) { | |
1686 | Py_DECREF(gca); |
|
1695 | Py_DECREF(gca); | |
1687 | return PyErr_NoMemory(); |
|
1696 | return PyErr_NoMemory(); | |
1688 | } |
|
1697 | } | |
1689 |
|
1698 | |||
1690 | for (i = 0; i < revcount; i++) |
|
1699 | for (i = 0; i < revcount; i++) | |
1691 | seen[revs[i]] = 1ull << i; |
|
1700 | seen[revs[i]] = 1ull << i; | |
1692 |
|
1701 | |||
1693 | interesting = revcount; |
|
1702 | interesting = revcount; | |
1694 |
|
1703 | |||
1695 | for (v = maxrev; v >= 0 && interesting; v--) { |
|
1704 | for (v = maxrev; v >= 0 && interesting; v--) { | |
1696 | bitmask sv = seen[v]; |
|
1705 | bitmask sv = seen[v]; | |
1697 | int parents[2]; |
|
1706 | int parents[2]; | |
1698 |
|
1707 | |||
1699 | if (!sv) |
|
1708 | if (!sv) | |
1700 | continue; |
|
1709 | continue; | |
1701 |
|
1710 | |||
1702 | if (sv < poison) { |
|
1711 | if (sv < poison) { | |
1703 | interesting -= 1; |
|
1712 | interesting -= 1; | |
1704 | if (sv == allseen) { |
|
1713 | if (sv == allseen) { | |
1705 | PyObject *obj = PyInt_FromLong(v); |
|
1714 | PyObject *obj = PyInt_FromLong(v); | |
1706 | if (obj == NULL) |
|
1715 | if (obj == NULL) | |
1707 | goto bail; |
|
1716 | goto bail; | |
1708 | if (PyList_Append(gca, obj) == -1) { |
|
1717 | if (PyList_Append(gca, obj) == -1) { | |
1709 | Py_DECREF(obj); |
|
1718 | Py_DECREF(obj); | |
1710 | goto bail; |
|
1719 | goto bail; | |
1711 | } |
|
1720 | } | |
1712 | sv |= poison; |
|
1721 | sv |= poison; | |
1713 | for (i = 0; i < revcount; i++) { |
|
1722 | for (i = 0; i < revcount; i++) { | |
1714 | if (revs[i] == v) |
|
1723 | if (revs[i] == v) | |
1715 | goto done; |
|
1724 | goto done; | |
1716 | } |
|
1725 | } | |
1717 | } |
|
1726 | } | |
1718 | } |
|
1727 | } | |
1719 |
index_get_parents(self, v, parents) |
|
1728 | if (index_get_parents(self, v, parents, maxrev) < 0) | |
|
1729 | goto bail; | |||
1720 |
|
1730 | |||
1721 | for (i = 0; i < 2; i++) { |
|
1731 | for (i = 0; i < 2; i++) { | |
1722 | int p = parents[i]; |
|
1732 | int p = parents[i]; | |
1723 | if (p == -1) |
|
1733 | if (p == -1) | |
1724 | continue; |
|
1734 | continue; | |
1725 | sp = seen[p]; |
|
1735 | sp = seen[p]; | |
1726 | if (sv < poison) { |
|
1736 | if (sv < poison) { | |
1727 | if (sp == 0) { |
|
1737 | if (sp == 0) { | |
1728 | seen[p] = sv; |
|
1738 | seen[p] = sv; | |
1729 | interesting++; |
|
1739 | interesting++; | |
1730 | } |
|
1740 | } | |
1731 | else if (sp != sv) |
|
1741 | else if (sp != sv) | |
1732 | seen[p] |= sv; |
|
1742 | seen[p] |= sv; | |
1733 | } else { |
|
1743 | } else { | |
1734 | if (sp && sp < poison) |
|
1744 | if (sp && sp < poison) | |
1735 | interesting--; |
|
1745 | interesting--; | |
1736 | seen[p] = sv; |
|
1746 | seen[p] = sv; | |
1737 | } |
|
1747 | } | |
1738 | } |
|
1748 | } | |
1739 | } |
|
1749 | } | |
1740 |
|
1750 | |||
1741 | done: |
|
1751 | done: | |
1742 | free(seen); |
|
1752 | free(seen); | |
1743 | return gca; |
|
1753 | return gca; | |
1744 | bail: |
|
1754 | bail: | |
1745 | free(seen); |
|
1755 | free(seen); | |
1746 | Py_XDECREF(gca); |
|
1756 | Py_XDECREF(gca); | |
1747 | return NULL; |
|
1757 | return NULL; | |
1748 | } |
|
1758 | } | |
1749 |
|
1759 | |||
1750 | /* |
|
1760 | /* | |
1751 | * Given a disjoint set of revs, return the subset with the longest |
|
1761 | * Given a disjoint set of revs, return the subset with the longest | |
1752 | * path to the root. |
|
1762 | * path to the root. | |
1753 | */ |
|
1763 | */ | |
1754 | static PyObject *find_deepest(indexObject *self, PyObject *revs) |
|
1764 | static PyObject *find_deepest(indexObject *self, PyObject *revs) | |
1755 | { |
|
1765 | { | |
1756 | const Py_ssize_t revcount = PyList_GET_SIZE(revs); |
|
1766 | const Py_ssize_t revcount = PyList_GET_SIZE(revs); | |
1757 | static const Py_ssize_t capacity = 24; |
|
1767 | static const Py_ssize_t capacity = 24; | |
1758 | int *depth, *interesting = NULL; |
|
1768 | int *depth, *interesting = NULL; | |
1759 | int i, j, v, ninteresting; |
|
1769 | int i, j, v, ninteresting; | |
1760 | PyObject *dict = NULL, *keys = NULL; |
|
1770 | PyObject *dict = NULL, *keys = NULL; | |
1761 | long *seen = NULL; |
|
1771 | long *seen = NULL; | |
1762 | int maxrev = -1; |
|
1772 | int maxrev = -1; | |
1763 | long final; |
|
1773 | long final; | |
1764 |
|
1774 | |||
1765 | if (revcount > capacity) { |
|
1775 | if (revcount > capacity) { | |
1766 | PyErr_Format(PyExc_OverflowError, |
|
1776 | PyErr_Format(PyExc_OverflowError, | |
1767 | "bitset size (%ld) > capacity (%ld)", |
|
1777 | "bitset size (%ld) > capacity (%ld)", | |
1768 | (long)revcount, (long)capacity); |
|
1778 | (long)revcount, (long)capacity); | |
1769 | return NULL; |
|
1779 | return NULL; | |
1770 | } |
|
1780 | } | |
1771 |
|
1781 | |||
1772 | for (i = 0; i < revcount; i++) { |
|
1782 | for (i = 0; i < revcount; i++) { | |
1773 | int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i)); |
|
1783 | int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i)); | |
1774 | if (n > maxrev) |
|
1784 | if (n > maxrev) | |
1775 | maxrev = n; |
|
1785 | maxrev = n; | |
1776 | } |
|
1786 | } | |
1777 |
|
1787 | |||
1778 | depth = calloc(sizeof(*depth), maxrev + 1); |
|
1788 | depth = calloc(sizeof(*depth), maxrev + 1); | |
1779 | if (depth == NULL) |
|
1789 | if (depth == NULL) | |
1780 | return PyErr_NoMemory(); |
|
1790 | return PyErr_NoMemory(); | |
1781 |
|
1791 | |||
1782 | seen = calloc(sizeof(*seen), maxrev + 1); |
|
1792 | seen = calloc(sizeof(*seen), maxrev + 1); | |
1783 | if (seen == NULL) { |
|
1793 | if (seen == NULL) { | |
1784 | PyErr_NoMemory(); |
|
1794 | PyErr_NoMemory(); | |
1785 | goto bail; |
|
1795 | goto bail; | |
1786 | } |
|
1796 | } | |
1787 |
|
1797 | |||
1788 | interesting = calloc(sizeof(*interesting), 2 << revcount); |
|
1798 | interesting = calloc(sizeof(*interesting), 2 << revcount); | |
1789 | if (interesting == NULL) { |
|
1799 | if (interesting == NULL) { | |
1790 | PyErr_NoMemory(); |
|
1800 | PyErr_NoMemory(); | |
1791 | goto bail; |
|
1801 | goto bail; | |
1792 | } |
|
1802 | } | |
1793 |
|
1803 | |||
1794 | if (PyList_Sort(revs) == -1) |
|
1804 | if (PyList_Sort(revs) == -1) | |
1795 | goto bail; |
|
1805 | goto bail; | |
1796 |
|
1806 | |||
1797 | for (i = 0; i < revcount; i++) { |
|
1807 | for (i = 0; i < revcount; i++) { | |
1798 | int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i)); |
|
1808 | int n = (int)PyInt_AsLong(PyList_GET_ITEM(revs, i)); | |
1799 | long b = 1l << i; |
|
1809 | long b = 1l << i; | |
1800 | depth[n] = 1; |
|
1810 | depth[n] = 1; | |
1801 | seen[n] = b; |
|
1811 | seen[n] = b; | |
1802 | interesting[b] = 1; |
|
1812 | interesting[b] = 1; | |
1803 | } |
|
1813 | } | |
1804 |
|
1814 | |||
1805 | ninteresting = (int)revcount; |
|
1815 | ninteresting = (int)revcount; | |
1806 |
|
1816 | |||
1807 | for (v = maxrev; v >= 0 && ninteresting > 1; v--) { |
|
1817 | for (v = maxrev; v >= 0 && ninteresting > 1; v--) { | |
1808 | int dv = depth[v]; |
|
1818 | int dv = depth[v]; | |
1809 | int parents[2]; |
|
1819 | int parents[2]; | |
1810 | long sv; |
|
1820 | long sv; | |
1811 |
|
1821 | |||
1812 | if (dv == 0) |
|
1822 | if (dv == 0) | |
1813 | continue; |
|
1823 | continue; | |
1814 |
|
1824 | |||
1815 | sv = seen[v]; |
|
1825 | sv = seen[v]; | |
1816 |
index_get_parents(self, v, parents) |
|
1826 | if (index_get_parents(self, v, parents, maxrev) < 0) | |
|
1827 | goto bail; | |||
1817 |
|
1828 | |||
1818 | for (i = 0; i < 2; i++) { |
|
1829 | for (i = 0; i < 2; i++) { | |
1819 | int p = parents[i]; |
|
1830 | int p = parents[i]; | |
1820 | long nsp, sp; |
|
1831 | long nsp, sp; | |
1821 | int dp; |
|
1832 | int dp; | |
1822 |
|
1833 | |||
1823 | if (p == -1) |
|
1834 | if (p == -1) | |
1824 | continue; |
|
1835 | continue; | |
1825 |
|
1836 | |||
1826 | dp = depth[p]; |
|
1837 | dp = depth[p]; | |
1827 | nsp = sp = seen[p]; |
|
1838 | nsp = sp = seen[p]; | |
1828 | if (dp <= dv) { |
|
1839 | if (dp <= dv) { | |
1829 | depth[p] = dv + 1; |
|
1840 | depth[p] = dv + 1; | |
1830 | if (sp != sv) { |
|
1841 | if (sp != sv) { | |
1831 | interesting[sv] += 1; |
|
1842 | interesting[sv] += 1; | |
1832 | nsp = seen[p] = sv; |
|
1843 | nsp = seen[p] = sv; | |
1833 | if (sp) { |
|
1844 | if (sp) { | |
1834 | interesting[sp] -= 1; |
|
1845 | interesting[sp] -= 1; | |
1835 | if (interesting[sp] == 0) |
|
1846 | if (interesting[sp] == 0) | |
1836 | ninteresting -= 1; |
|
1847 | ninteresting -= 1; | |
1837 | } |
|
1848 | } | |
1838 | } |
|
1849 | } | |
1839 | } |
|
1850 | } | |
1840 | else if (dv == dp - 1) { |
|
1851 | else if (dv == dp - 1) { | |
1841 | nsp = sp | sv; |
|
1852 | nsp = sp | sv; | |
1842 | if (nsp == sp) |
|
1853 | if (nsp == sp) | |
1843 | continue; |
|
1854 | continue; | |
1844 | seen[p] = nsp; |
|
1855 | seen[p] = nsp; | |
1845 | interesting[sp] -= 1; |
|
1856 | interesting[sp] -= 1; | |
1846 | if (interesting[sp] == 0 && interesting[nsp] > 0) |
|
1857 | if (interesting[sp] == 0 && interesting[nsp] > 0) | |
1847 | ninteresting -= 1; |
|
1858 | ninteresting -= 1; | |
1848 | interesting[nsp] += 1; |
|
1859 | interesting[nsp] += 1; | |
1849 | } |
|
1860 | } | |
1850 | } |
|
1861 | } | |
1851 | interesting[sv] -= 1; |
|
1862 | interesting[sv] -= 1; | |
1852 | if (interesting[sv] == 0) |
|
1863 | if (interesting[sv] == 0) | |
1853 | ninteresting -= 1; |
|
1864 | ninteresting -= 1; | |
1854 | } |
|
1865 | } | |
1855 |
|
1866 | |||
1856 | final = 0; |
|
1867 | final = 0; | |
1857 | j = ninteresting; |
|
1868 | j = ninteresting; | |
1858 | for (i = 0; i < (int)(2 << revcount) && j > 0; i++) { |
|
1869 | for (i = 0; i < (int)(2 << revcount) && j > 0; i++) { | |
1859 | if (interesting[i] == 0) |
|
1870 | if (interesting[i] == 0) | |
1860 | continue; |
|
1871 | continue; | |
1861 | final |= i; |
|
1872 | final |= i; | |
1862 | j -= 1; |
|
1873 | j -= 1; | |
1863 | } |
|
1874 | } | |
1864 | if (final == 0) { |
|
1875 | if (final == 0) { | |
1865 | keys = PyList_New(0); |
|
1876 | keys = PyList_New(0); | |
1866 | goto bail; |
|
1877 | goto bail; | |
1867 | } |
|
1878 | } | |
1868 |
|
1879 | |||
1869 | dict = PyDict_New(); |
|
1880 | dict = PyDict_New(); | |
1870 | if (dict == NULL) |
|
1881 | if (dict == NULL) | |
1871 | goto bail; |
|
1882 | goto bail; | |
1872 |
|
1883 | |||
1873 | for (i = 0; i < revcount; i++) { |
|
1884 | for (i = 0; i < revcount; i++) { | |
1874 | PyObject *key; |
|
1885 | PyObject *key; | |
1875 |
|
1886 | |||
1876 | if ((final & (1 << i)) == 0) |
|
1887 | if ((final & (1 << i)) == 0) | |
1877 | continue; |
|
1888 | continue; | |
1878 |
|
1889 | |||
1879 | key = PyList_GET_ITEM(revs, i); |
|
1890 | key = PyList_GET_ITEM(revs, i); | |
1880 | Py_INCREF(key); |
|
1891 | Py_INCREF(key); | |
1881 | Py_INCREF(Py_None); |
|
1892 | Py_INCREF(Py_None); | |
1882 | if (PyDict_SetItem(dict, key, Py_None) == -1) { |
|
1893 | if (PyDict_SetItem(dict, key, Py_None) == -1) { | |
1883 | Py_DECREF(key); |
|
1894 | Py_DECREF(key); | |
1884 | Py_DECREF(Py_None); |
|
1895 | Py_DECREF(Py_None); | |
1885 | goto bail; |
|
1896 | goto bail; | |
1886 | } |
|
1897 | } | |
1887 | } |
|
1898 | } | |
1888 |
|
1899 | |||
1889 | keys = PyDict_Keys(dict); |
|
1900 | keys = PyDict_Keys(dict); | |
1890 |
|
1901 | |||
1891 | bail: |
|
1902 | bail: | |
1892 | free(depth); |
|
1903 | free(depth); | |
1893 | free(seen); |
|
1904 | free(seen); | |
1894 | free(interesting); |
|
1905 | free(interesting); | |
1895 | Py_XDECREF(dict); |
|
1906 | Py_XDECREF(dict); | |
1896 |
|
1907 | |||
1897 | return keys; |
|
1908 | return keys; | |
1898 | } |
|
1909 | } | |
1899 |
|
1910 | |||
1900 | /* |
|
1911 | /* | |
1901 | * Given a (possibly overlapping) set of revs, return all the |
|
1912 | * Given a (possibly overlapping) set of revs, return all the | |
1902 | * common ancestors heads: heads(::args[0] and ::a[1] and ...) |
|
1913 | * common ancestors heads: heads(::args[0] and ::a[1] and ...) | |
1903 | */ |
|
1914 | */ | |
1904 | static PyObject *index_commonancestorsheads(indexObject *self, PyObject *args) |
|
1915 | static PyObject *index_commonancestorsheads(indexObject *self, PyObject *args) | |
1905 | { |
|
1916 | { | |
1906 | PyObject *ret = NULL; |
|
1917 | PyObject *ret = NULL; | |
1907 | Py_ssize_t argcount, i, len; |
|
1918 | Py_ssize_t argcount, i, len; | |
1908 | bitmask repeat = 0; |
|
1919 | bitmask repeat = 0; | |
1909 | int revcount = 0; |
|
1920 | int revcount = 0; | |
1910 | int *revs; |
|
1921 | int *revs; | |
1911 |
|
1922 | |||
1912 | argcount = PySequence_Length(args); |
|
1923 | argcount = PySequence_Length(args); | |
1913 | revs = malloc(argcount * sizeof(*revs)); |
|
1924 | revs = malloc(argcount * sizeof(*revs)); | |
1914 | if (argcount > 0 && revs == NULL) |
|
1925 | if (argcount > 0 && revs == NULL) | |
1915 | return PyErr_NoMemory(); |
|
1926 | return PyErr_NoMemory(); | |
1916 | len = index_length(self) - 1; |
|
1927 | len = index_length(self) - 1; | |
1917 |
|
1928 | |||
1918 | for (i = 0; i < argcount; i++) { |
|
1929 | for (i = 0; i < argcount; i++) { | |
1919 | static const int capacity = 24; |
|
1930 | static const int capacity = 24; | |
1920 | PyObject *obj = PySequence_GetItem(args, i); |
|
1931 | PyObject *obj = PySequence_GetItem(args, i); | |
1921 | bitmask x; |
|
1932 | bitmask x; | |
1922 | long val; |
|
1933 | long val; | |
1923 |
|
1934 | |||
1924 | if (!PyInt_Check(obj)) { |
|
1935 | if (!PyInt_Check(obj)) { | |
1925 | PyErr_SetString(PyExc_TypeError, |
|
1936 | PyErr_SetString(PyExc_TypeError, | |
1926 | "arguments must all be ints"); |
|
1937 | "arguments must all be ints"); | |
1927 | Py_DECREF(obj); |
|
1938 | Py_DECREF(obj); | |
1928 | goto bail; |
|
1939 | goto bail; | |
1929 | } |
|
1940 | } | |
1930 | val = PyInt_AsLong(obj); |
|
1941 | val = PyInt_AsLong(obj); | |
1931 | Py_DECREF(obj); |
|
1942 | Py_DECREF(obj); | |
1932 | if (val == -1) { |
|
1943 | if (val == -1) { | |
1933 | ret = PyList_New(0); |
|
1944 | ret = PyList_New(0); | |
1934 | goto done; |
|
1945 | goto done; | |
1935 | } |
|
1946 | } | |
1936 | if (val < 0 || val >= len) { |
|
1947 | if (val < 0 || val >= len) { | |
1937 | PyErr_SetString(PyExc_IndexError, |
|
1948 | PyErr_SetString(PyExc_IndexError, | |
1938 | "index out of range"); |
|
1949 | "index out of range"); | |
1939 | goto bail; |
|
1950 | goto bail; | |
1940 | } |
|
1951 | } | |
1941 | /* this cheesy bloom filter lets us avoid some more |
|
1952 | /* this cheesy bloom filter lets us avoid some more | |
1942 | * expensive duplicate checks in the common set-is-disjoint |
|
1953 | * expensive duplicate checks in the common set-is-disjoint | |
1943 | * case */ |
|
1954 | * case */ | |
1944 | x = 1ull << (val & 0x3f); |
|
1955 | x = 1ull << (val & 0x3f); | |
1945 | if (repeat & x) { |
|
1956 | if (repeat & x) { | |
1946 | int k; |
|
1957 | int k; | |
1947 | for (k = 0; k < revcount; k++) { |
|
1958 | for (k = 0; k < revcount; k++) { | |
1948 | if (val == revs[k]) |
|
1959 | if (val == revs[k]) | |
1949 | goto duplicate; |
|
1960 | goto duplicate; | |
1950 | } |
|
1961 | } | |
1951 | } |
|
1962 | } | |
1952 | else repeat |= x; |
|
1963 | else repeat |= x; | |
1953 | if (revcount >= capacity) { |
|
1964 | if (revcount >= capacity) { | |
1954 | PyErr_Format(PyExc_OverflowError, |
|
1965 | PyErr_Format(PyExc_OverflowError, | |
1955 | "bitset size (%d) > capacity (%d)", |
|
1966 | "bitset size (%d) > capacity (%d)", | |
1956 | revcount, capacity); |
|
1967 | revcount, capacity); | |
1957 | goto bail; |
|
1968 | goto bail; | |
1958 | } |
|
1969 | } | |
1959 | revs[revcount++] = (int)val; |
|
1970 | revs[revcount++] = (int)val; | |
1960 | duplicate:; |
|
1971 | duplicate:; | |
1961 | } |
|
1972 | } | |
1962 |
|
1973 | |||
1963 | if (revcount == 0) { |
|
1974 | if (revcount == 0) { | |
1964 | ret = PyList_New(0); |
|
1975 | ret = PyList_New(0); | |
1965 | goto done; |
|
1976 | goto done; | |
1966 | } |
|
1977 | } | |
1967 | if (revcount == 1) { |
|
1978 | if (revcount == 1) { | |
1968 | PyObject *obj; |
|
1979 | PyObject *obj; | |
1969 | ret = PyList_New(1); |
|
1980 | ret = PyList_New(1); | |
1970 | if (ret == NULL) |
|
1981 | if (ret == NULL) | |
1971 | goto bail; |
|
1982 | goto bail; | |
1972 | obj = PyInt_FromLong(revs[0]); |
|
1983 | obj = PyInt_FromLong(revs[0]); | |
1973 | if (obj == NULL) |
|
1984 | if (obj == NULL) | |
1974 | goto bail; |
|
1985 | goto bail; | |
1975 | PyList_SET_ITEM(ret, 0, obj); |
|
1986 | PyList_SET_ITEM(ret, 0, obj); | |
1976 | goto done; |
|
1987 | goto done; | |
1977 | } |
|
1988 | } | |
1978 |
|
1989 | |||
1979 | ret = find_gca_candidates(self, revs, revcount); |
|
1990 | ret = find_gca_candidates(self, revs, revcount); | |
1980 | if (ret == NULL) |
|
1991 | if (ret == NULL) | |
1981 | goto bail; |
|
1992 | goto bail; | |
1982 |
|
1993 | |||
1983 | done: |
|
1994 | done: | |
1984 | free(revs); |
|
1995 | free(revs); | |
1985 | return ret; |
|
1996 | return ret; | |
1986 |
|
1997 | |||
1987 | bail: |
|
1998 | bail: | |
1988 | free(revs); |
|
1999 | free(revs); | |
1989 | Py_XDECREF(ret); |
|
2000 | Py_XDECREF(ret); | |
1990 | return NULL; |
|
2001 | return NULL; | |
1991 | } |
|
2002 | } | |
1992 |
|
2003 | |||
1993 | /* |
|
2004 | /* | |
1994 | * Given a (possibly overlapping) set of revs, return the greatest |
|
2005 | * Given a (possibly overlapping) set of revs, return the greatest | |
1995 | * common ancestors: those with the longest path to the root. |
|
2006 | * common ancestors: those with the longest path to the root. | |
1996 | */ |
|
2007 | */ | |
1997 | static PyObject *index_ancestors(indexObject *self, PyObject *args) |
|
2008 | static PyObject *index_ancestors(indexObject *self, PyObject *args) | |
1998 | { |
|
2009 | { | |
1999 | PyObject *gca = index_commonancestorsheads(self, args); |
|
2010 | PyObject *gca = index_commonancestorsheads(self, args); | |
2000 | if (gca == NULL) |
|
2011 | if (gca == NULL) | |
2001 | return NULL; |
|
2012 | return NULL; | |
2002 |
|
2013 | |||
2003 | if (PyList_GET_SIZE(gca) <= 1) { |
|
2014 | if (PyList_GET_SIZE(gca) <= 1) { | |
2004 | Py_INCREF(gca); |
|
2015 | Py_INCREF(gca); | |
2005 | return gca; |
|
2016 | return gca; | |
2006 | } |
|
2017 | } | |
2007 |
|
2018 | |||
2008 | return find_deepest(self, gca); |
|
2019 | return find_deepest(self, gca); | |
2009 | } |
|
2020 | } | |
2010 |
|
2021 | |||
2011 | /* |
|
2022 | /* | |
2012 | * Invalidate any trie entries introduced by added revs. |
|
2023 | * Invalidate any trie entries introduced by added revs. | |
2013 | */ |
|
2024 | */ | |
2014 | static void nt_invalidate_added(indexObject *self, Py_ssize_t start) |
|
2025 | static void nt_invalidate_added(indexObject *self, Py_ssize_t start) | |
2015 | { |
|
2026 | { | |
2016 | Py_ssize_t i, len = PyList_GET_SIZE(self->added); |
|
2027 | Py_ssize_t i, len = PyList_GET_SIZE(self->added); | |
2017 |
|
2028 | |||
2018 | for (i = start; i < len; i++) { |
|
2029 | for (i = start; i < len; i++) { | |
2019 | PyObject *tuple = PyList_GET_ITEM(self->added, i); |
|
2030 | PyObject *tuple = PyList_GET_ITEM(self->added, i); | |
2020 | PyObject *node = PyTuple_GET_ITEM(tuple, 7); |
|
2031 | PyObject *node = PyTuple_GET_ITEM(tuple, 7); | |
2021 |
|
2032 | |||
2022 | nt_insert(self, PyString_AS_STRING(node), -1); |
|
2033 | nt_insert(self, PyString_AS_STRING(node), -1); | |
2023 | } |
|
2034 | } | |
2024 |
|
2035 | |||
2025 | if (start == 0) |
|
2036 | if (start == 0) | |
2026 | Py_CLEAR(self->added); |
|
2037 | Py_CLEAR(self->added); | |
2027 | } |
|
2038 | } | |
2028 |
|
2039 | |||
2029 | /* |
|
2040 | /* | |
2030 | * Delete a numeric range of revs, which must be at the end of the |
|
2041 | * Delete a numeric range of revs, which must be at the end of the | |
2031 | * range, but exclude the sentinel nullid entry. |
|
2042 | * range, but exclude the sentinel nullid entry. | |
2032 | */ |
|
2043 | */ | |
2033 | static int index_slice_del(indexObject *self, PyObject *item) |
|
2044 | static int index_slice_del(indexObject *self, PyObject *item) | |
2034 | { |
|
2045 | { | |
2035 | Py_ssize_t start, stop, step, slicelength; |
|
2046 | Py_ssize_t start, stop, step, slicelength; | |
2036 | Py_ssize_t length = index_length(self); |
|
2047 | Py_ssize_t length = index_length(self); | |
2037 | int ret = 0; |
|
2048 | int ret = 0; | |
2038 |
|
2049 | |||
2039 | if (PySlice_GetIndicesEx((PySliceObject*)item, length, |
|
2050 | if (PySlice_GetIndicesEx((PySliceObject*)item, length, | |
2040 | &start, &stop, &step, &slicelength) < 0) |
|
2051 | &start, &stop, &step, &slicelength) < 0) | |
2041 | return -1; |
|
2052 | return -1; | |
2042 |
|
2053 | |||
2043 | if (slicelength <= 0) |
|
2054 | if (slicelength <= 0) | |
2044 | return 0; |
|
2055 | return 0; | |
2045 |
|
2056 | |||
2046 | if ((step < 0 && start < stop) || (step > 0 && start > stop)) |
|
2057 | if ((step < 0 && start < stop) || (step > 0 && start > stop)) | |
2047 | stop = start; |
|
2058 | stop = start; | |
2048 |
|
2059 | |||
2049 | if (step < 0) { |
|
2060 | if (step < 0) { | |
2050 | stop = start + 1; |
|
2061 | stop = start + 1; | |
2051 | start = stop + step*(slicelength - 1) - 1; |
|
2062 | start = stop + step*(slicelength - 1) - 1; | |
2052 | step = -step; |
|
2063 | step = -step; | |
2053 | } |
|
2064 | } | |
2054 |
|
2065 | |||
2055 | if (step != 1) { |
|
2066 | if (step != 1) { | |
2056 | PyErr_SetString(PyExc_ValueError, |
|
2067 | PyErr_SetString(PyExc_ValueError, | |
2057 | "revlog index delete requires step size of 1"); |
|
2068 | "revlog index delete requires step size of 1"); | |
2058 | return -1; |
|
2069 | return -1; | |
2059 | } |
|
2070 | } | |
2060 |
|
2071 | |||
2061 | if (stop != length - 1) { |
|
2072 | if (stop != length - 1) { | |
2062 | PyErr_SetString(PyExc_IndexError, |
|
2073 | PyErr_SetString(PyExc_IndexError, | |
2063 | "revlog index deletion indices are invalid"); |
|
2074 | "revlog index deletion indices are invalid"); | |
2064 | return -1; |
|
2075 | return -1; | |
2065 | } |
|
2076 | } | |
2066 |
|
2077 | |||
2067 | if (start < self->length - 1) { |
|
2078 | if (start < self->length - 1) { | |
2068 | if (self->nt) { |
|
2079 | if (self->nt) { | |
2069 | Py_ssize_t i; |
|
2080 | Py_ssize_t i; | |
2070 |
|
2081 | |||
2071 | for (i = start + 1; i < self->length - 1; i++) { |
|
2082 | for (i = start + 1; i < self->length - 1; i++) { | |
2072 | const char *node = index_node(self, i); |
|
2083 | const char *node = index_node(self, i); | |
2073 |
|
2084 | |||
2074 | if (node) |
|
2085 | if (node) | |
2075 | nt_insert(self, node, -1); |
|
2086 | nt_insert(self, node, -1); | |
2076 | } |
|
2087 | } | |
2077 | if (self->added) |
|
2088 | if (self->added) | |
2078 | nt_invalidate_added(self, 0); |
|
2089 | nt_invalidate_added(self, 0); | |
2079 | if (self->ntrev > start) |
|
2090 | if (self->ntrev > start) | |
2080 | self->ntrev = (int)start; |
|
2091 | self->ntrev = (int)start; | |
2081 | } |
|
2092 | } | |
2082 | self->length = start + 1; |
|
2093 | self->length = start + 1; | |
2083 | if (start < self->raw_length) { |
|
2094 | if (start < self->raw_length) { | |
2084 | if (self->cache) { |
|
2095 | if (self->cache) { | |
2085 | Py_ssize_t i; |
|
2096 | Py_ssize_t i; | |
2086 | for (i = start; i < self->raw_length; i++) |
|
2097 | for (i = start; i < self->raw_length; i++) | |
2087 | Py_CLEAR(self->cache[i]); |
|
2098 | Py_CLEAR(self->cache[i]); | |
2088 | } |
|
2099 | } | |
2089 | self->raw_length = start; |
|
2100 | self->raw_length = start; | |
2090 | } |
|
2101 | } | |
2091 | goto done; |
|
2102 | goto done; | |
2092 | } |
|
2103 | } | |
2093 |
|
2104 | |||
2094 | if (self->nt) { |
|
2105 | if (self->nt) { | |
2095 | nt_invalidate_added(self, start - self->length + 1); |
|
2106 | nt_invalidate_added(self, start - self->length + 1); | |
2096 | if (self->ntrev > start) |
|
2107 | if (self->ntrev > start) | |
2097 | self->ntrev = (int)start; |
|
2108 | self->ntrev = (int)start; | |
2098 | } |
|
2109 | } | |
2099 | if (self->added) |
|
2110 | if (self->added) | |
2100 | ret = PyList_SetSlice(self->added, start - self->length + 1, |
|
2111 | ret = PyList_SetSlice(self->added, start - self->length + 1, | |
2101 | PyList_GET_SIZE(self->added), NULL); |
|
2112 | PyList_GET_SIZE(self->added), NULL); | |
2102 | done: |
|
2113 | done: | |
2103 | Py_CLEAR(self->headrevs); |
|
2114 | Py_CLEAR(self->headrevs); | |
2104 | return ret; |
|
2115 | return ret; | |
2105 | } |
|
2116 | } | |
2106 |
|
2117 | |||
2107 | /* |
|
2118 | /* | |
2108 | * Supported ops: |
|
2119 | * Supported ops: | |
2109 | * |
|
2120 | * | |
2110 | * slice deletion |
|
2121 | * slice deletion | |
2111 | * string assignment (extend node->rev mapping) |
|
2122 | * string assignment (extend node->rev mapping) | |
2112 | * string deletion (shrink node->rev mapping) |
|
2123 | * string deletion (shrink node->rev mapping) | |
2113 | */ |
|
2124 | */ | |
2114 | static int index_assign_subscript(indexObject *self, PyObject *item, |
|
2125 | static int index_assign_subscript(indexObject *self, PyObject *item, | |
2115 | PyObject *value) |
|
2126 | PyObject *value) | |
2116 | { |
|
2127 | { | |
2117 | char *node; |
|
2128 | char *node; | |
2118 | Py_ssize_t nodelen; |
|
2129 | Py_ssize_t nodelen; | |
2119 | long rev; |
|
2130 | long rev; | |
2120 |
|
2131 | |||
2121 | if (PySlice_Check(item) && value == NULL) |
|
2132 | if (PySlice_Check(item) && value == NULL) | |
2122 | return index_slice_del(self, item); |
|
2133 | return index_slice_del(self, item); | |
2123 |
|
2134 | |||
2124 | if (node_check(item, &node, &nodelen) == -1) |
|
2135 | if (node_check(item, &node, &nodelen) == -1) | |
2125 | return -1; |
|
2136 | return -1; | |
2126 |
|
2137 | |||
2127 | if (value == NULL) |
|
2138 | if (value == NULL) | |
2128 | return self->nt ? nt_insert(self, node, -1) : 0; |
|
2139 | return self->nt ? nt_insert(self, node, -1) : 0; | |
2129 | rev = PyInt_AsLong(value); |
|
2140 | rev = PyInt_AsLong(value); | |
2130 | if (rev > INT_MAX || rev < 0) { |
|
2141 | if (rev > INT_MAX || rev < 0) { | |
2131 | if (!PyErr_Occurred()) |
|
2142 | if (!PyErr_Occurred()) | |
2132 | PyErr_SetString(PyExc_ValueError, "rev out of range"); |
|
2143 | PyErr_SetString(PyExc_ValueError, "rev out of range"); | |
2133 | return -1; |
|
2144 | return -1; | |
2134 | } |
|
2145 | } | |
2135 |
|
2146 | |||
2136 | if (nt_init(self) == -1) |
|
2147 | if (nt_init(self) == -1) | |
2137 | return -1; |
|
2148 | return -1; | |
2138 | return nt_insert(self, node, (int)rev); |
|
2149 | return nt_insert(self, node, (int)rev); | |
2139 | } |
|
2150 | } | |
2140 |
|
2151 | |||
2141 | /* |
|
2152 | /* | |
2142 | * Find all RevlogNG entries in an index that has inline data. Update |
|
2153 | * Find all RevlogNG entries in an index that has inline data. Update | |
2143 | * the optional "offsets" table with those entries. |
|
2154 | * the optional "offsets" table with those entries. | |
2144 | */ |
|
2155 | */ | |
2145 | static Py_ssize_t inline_scan(indexObject *self, const char **offsets) |
|
2156 | static Py_ssize_t inline_scan(indexObject *self, const char **offsets) | |
2146 | { |
|
2157 | { | |
2147 | const char *data = PyString_AS_STRING(self->data); |
|
2158 | const char *data = PyString_AS_STRING(self->data); | |
2148 | Py_ssize_t pos = 0; |
|
2159 | Py_ssize_t pos = 0; | |
2149 | Py_ssize_t end = PyString_GET_SIZE(self->data); |
|
2160 | Py_ssize_t end = PyString_GET_SIZE(self->data); | |
2150 | long incr = v1_hdrsize; |
|
2161 | long incr = v1_hdrsize; | |
2151 | Py_ssize_t len = 0; |
|
2162 | Py_ssize_t len = 0; | |
2152 |
|
2163 | |||
2153 | while (pos + v1_hdrsize <= end && pos >= 0) { |
|
2164 | while (pos + v1_hdrsize <= end && pos >= 0) { | |
2154 | uint32_t comp_len; |
|
2165 | uint32_t comp_len; | |
2155 | /* 3rd element of header is length of compressed inline data */ |
|
2166 | /* 3rd element of header is length of compressed inline data */ | |
2156 | comp_len = getbe32(data + pos + 8); |
|
2167 | comp_len = getbe32(data + pos + 8); | |
2157 | incr = v1_hdrsize + comp_len; |
|
2168 | incr = v1_hdrsize + comp_len; | |
2158 | if (offsets) |
|
2169 | if (offsets) | |
2159 | offsets[len] = data + pos; |
|
2170 | offsets[len] = data + pos; | |
2160 | len++; |
|
2171 | len++; | |
2161 | pos += incr; |
|
2172 | pos += incr; | |
2162 | } |
|
2173 | } | |
2163 |
|
2174 | |||
2164 | if (pos != end) { |
|
2175 | if (pos != end) { | |
2165 | if (!PyErr_Occurred()) |
|
2176 | if (!PyErr_Occurred()) | |
2166 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); |
|
2177 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); | |
2167 | return -1; |
|
2178 | return -1; | |
2168 | } |
|
2179 | } | |
2169 |
|
2180 | |||
2170 | return len; |
|
2181 | return len; | |
2171 | } |
|
2182 | } | |
2172 |
|
2183 | |||
2173 | static int index_init(indexObject *self, PyObject *args) |
|
2184 | static int index_init(indexObject *self, PyObject *args) | |
2174 | { |
|
2185 | { | |
2175 | PyObject *data_obj, *inlined_obj; |
|
2186 | PyObject *data_obj, *inlined_obj; | |
2176 | Py_ssize_t size; |
|
2187 | Py_ssize_t size; | |
2177 |
|
2188 | |||
2178 | /* Initialize before argument-checking to avoid index_dealloc() crash. */ |
|
2189 | /* Initialize before argument-checking to avoid index_dealloc() crash. */ | |
2179 | self->raw_length = 0; |
|
2190 | self->raw_length = 0; | |
2180 | self->added = NULL; |
|
2191 | self->added = NULL; | |
2181 | self->cache = NULL; |
|
2192 | self->cache = NULL; | |
2182 | self->data = NULL; |
|
2193 | self->data = NULL; | |
2183 | self->headrevs = NULL; |
|
2194 | self->headrevs = NULL; | |
2184 | self->filteredrevs = Py_None; |
|
2195 | self->filteredrevs = Py_None; | |
2185 | Py_INCREF(Py_None); |
|
2196 | Py_INCREF(Py_None); | |
2186 | self->nt = NULL; |
|
2197 | self->nt = NULL; | |
2187 | self->offsets = NULL; |
|
2198 | self->offsets = NULL; | |
2188 |
|
2199 | |||
2189 | if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj)) |
|
2200 | if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj)) | |
2190 | return -1; |
|
2201 | return -1; | |
2191 | if (!PyString_Check(data_obj)) { |
|
2202 | if (!PyString_Check(data_obj)) { | |
2192 | PyErr_SetString(PyExc_TypeError, "data is not a string"); |
|
2203 | PyErr_SetString(PyExc_TypeError, "data is not a string"); | |
2193 | return -1; |
|
2204 | return -1; | |
2194 | } |
|
2205 | } | |
2195 | size = PyString_GET_SIZE(data_obj); |
|
2206 | size = PyString_GET_SIZE(data_obj); | |
2196 |
|
2207 | |||
2197 | self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj); |
|
2208 | self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj); | |
2198 | self->data = data_obj; |
|
2209 | self->data = data_obj; | |
2199 |
|
2210 | |||
2200 | self->ntlength = self->ntcapacity = 0; |
|
2211 | self->ntlength = self->ntcapacity = 0; | |
2201 | self->ntdepth = self->ntsplits = 0; |
|
2212 | self->ntdepth = self->ntsplits = 0; | |
2202 | self->ntlookups = self->ntmisses = 0; |
|
2213 | self->ntlookups = self->ntmisses = 0; | |
2203 | self->ntrev = -1; |
|
2214 | self->ntrev = -1; | |
2204 | Py_INCREF(self->data); |
|
2215 | Py_INCREF(self->data); | |
2205 |
|
2216 | |||
2206 | if (self->inlined) { |
|
2217 | if (self->inlined) { | |
2207 | Py_ssize_t len = inline_scan(self, NULL); |
|
2218 | Py_ssize_t len = inline_scan(self, NULL); | |
2208 | if (len == -1) |
|
2219 | if (len == -1) | |
2209 | goto bail; |
|
2220 | goto bail; | |
2210 | self->raw_length = len; |
|
2221 | self->raw_length = len; | |
2211 | self->length = len + 1; |
|
2222 | self->length = len + 1; | |
2212 | } else { |
|
2223 | } else { | |
2213 | if (size % v1_hdrsize) { |
|
2224 | if (size % v1_hdrsize) { | |
2214 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); |
|
2225 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); | |
2215 | goto bail; |
|
2226 | goto bail; | |
2216 | } |
|
2227 | } | |
2217 | self->raw_length = size / v1_hdrsize; |
|
2228 | self->raw_length = size / v1_hdrsize; | |
2218 | self->length = self->raw_length + 1; |
|
2229 | self->length = self->raw_length + 1; | |
2219 | } |
|
2230 | } | |
2220 |
|
2231 | |||
2221 | return 0; |
|
2232 | return 0; | |
2222 | bail: |
|
2233 | bail: | |
2223 | return -1; |
|
2234 | return -1; | |
2224 | } |
|
2235 | } | |
2225 |
|
2236 | |||
2226 | static PyObject *index_nodemap(indexObject *self) |
|
2237 | static PyObject *index_nodemap(indexObject *self) | |
2227 | { |
|
2238 | { | |
2228 | Py_INCREF(self); |
|
2239 | Py_INCREF(self); | |
2229 | return (PyObject *)self; |
|
2240 | return (PyObject *)self; | |
2230 | } |
|
2241 | } | |
2231 |
|
2242 | |||
2232 | static void index_dealloc(indexObject *self) |
|
2243 | static void index_dealloc(indexObject *self) | |
2233 | { |
|
2244 | { | |
2234 | _index_clearcaches(self); |
|
2245 | _index_clearcaches(self); | |
2235 | Py_XDECREF(self->filteredrevs); |
|
2246 | Py_XDECREF(self->filteredrevs); | |
2236 | Py_XDECREF(self->data); |
|
2247 | Py_XDECREF(self->data); | |
2237 | Py_XDECREF(self->added); |
|
2248 | Py_XDECREF(self->added); | |
2238 | PyObject_Del(self); |
|
2249 | PyObject_Del(self); | |
2239 | } |
|
2250 | } | |
2240 |
|
2251 | |||
2241 | static PySequenceMethods index_sequence_methods = { |
|
2252 | static PySequenceMethods index_sequence_methods = { | |
2242 | (lenfunc)index_length, /* sq_length */ |
|
2253 | (lenfunc)index_length, /* sq_length */ | |
2243 | 0, /* sq_concat */ |
|
2254 | 0, /* sq_concat */ | |
2244 | 0, /* sq_repeat */ |
|
2255 | 0, /* sq_repeat */ | |
2245 | (ssizeargfunc)index_get, /* sq_item */ |
|
2256 | (ssizeargfunc)index_get, /* sq_item */ | |
2246 | 0, /* sq_slice */ |
|
2257 | 0, /* sq_slice */ | |
2247 | 0, /* sq_ass_item */ |
|
2258 | 0, /* sq_ass_item */ | |
2248 | 0, /* sq_ass_slice */ |
|
2259 | 0, /* sq_ass_slice */ | |
2249 | (objobjproc)index_contains, /* sq_contains */ |
|
2260 | (objobjproc)index_contains, /* sq_contains */ | |
2250 | }; |
|
2261 | }; | |
2251 |
|
2262 | |||
2252 | static PyMappingMethods index_mapping_methods = { |
|
2263 | static PyMappingMethods index_mapping_methods = { | |
2253 | (lenfunc)index_length, /* mp_length */ |
|
2264 | (lenfunc)index_length, /* mp_length */ | |
2254 | (binaryfunc)index_getitem, /* mp_subscript */ |
|
2265 | (binaryfunc)index_getitem, /* mp_subscript */ | |
2255 | (objobjargproc)index_assign_subscript, /* mp_ass_subscript */ |
|
2266 | (objobjargproc)index_assign_subscript, /* mp_ass_subscript */ | |
2256 | }; |
|
2267 | }; | |
2257 |
|
2268 | |||
2258 | static PyMethodDef index_methods[] = { |
|
2269 | static PyMethodDef index_methods[] = { | |
2259 | {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS, |
|
2270 | {"ancestors", (PyCFunction)index_ancestors, METH_VARARGS, | |
2260 | "return the gca set of the given revs"}, |
|
2271 | "return the gca set of the given revs"}, | |
2261 | {"commonancestorsheads", (PyCFunction)index_commonancestorsheads, |
|
2272 | {"commonancestorsheads", (PyCFunction)index_commonancestorsheads, | |
2262 | METH_VARARGS, |
|
2273 | METH_VARARGS, | |
2263 | "return the heads of the common ancestors of the given revs"}, |
|
2274 | "return the heads of the common ancestors of the given revs"}, | |
2264 | {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS, |
|
2275 | {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS, | |
2265 | "clear the index caches"}, |
|
2276 | "clear the index caches"}, | |
2266 | {"get", (PyCFunction)index_m_get, METH_VARARGS, |
|
2277 | {"get", (PyCFunction)index_m_get, METH_VARARGS, | |
2267 | "get an index entry"}, |
|
2278 | "get an index entry"}, | |
2268 | {"computephasesmapsets", (PyCFunction)compute_phases_map_sets, |
|
2279 | {"computephasesmapsets", (PyCFunction)compute_phases_map_sets, | |
2269 | METH_VARARGS, "compute phases"}, |
|
2280 | METH_VARARGS, "compute phases"}, | |
2270 | {"headrevs", (PyCFunction)index_headrevs, METH_VARARGS, |
|
2281 | {"headrevs", (PyCFunction)index_headrevs, METH_VARARGS, | |
2271 | "get head revisions"}, /* Can do filtering since 3.2 */ |
|
2282 | "get head revisions"}, /* Can do filtering since 3.2 */ | |
2272 | {"headrevsfiltered", (PyCFunction)index_headrevs, METH_VARARGS, |
|
2283 | {"headrevsfiltered", (PyCFunction)index_headrevs, METH_VARARGS, | |
2273 | "get filtered head revisions"}, /* Can always do filtering */ |
|
2284 | "get filtered head revisions"}, /* Can always do filtering */ | |
2274 | {"insert", (PyCFunction)index_insert, METH_VARARGS, |
|
2285 | {"insert", (PyCFunction)index_insert, METH_VARARGS, | |
2275 | "insert an index entry"}, |
|
2286 | "insert an index entry"}, | |
2276 | {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS, |
|
2287 | {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS, | |
2277 | "match a potentially ambiguous node ID"}, |
|
2288 | "match a potentially ambiguous node ID"}, | |
2278 | {"stats", (PyCFunction)index_stats, METH_NOARGS, |
|
2289 | {"stats", (PyCFunction)index_stats, METH_NOARGS, | |
2279 | "stats for the index"}, |
|
2290 | "stats for the index"}, | |
2280 | {NULL} /* Sentinel */ |
|
2291 | {NULL} /* Sentinel */ | |
2281 | }; |
|
2292 | }; | |
2282 |
|
2293 | |||
2283 | static PyGetSetDef index_getset[] = { |
|
2294 | static PyGetSetDef index_getset[] = { | |
2284 | {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL}, |
|
2295 | {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL}, | |
2285 | {NULL} /* Sentinel */ |
|
2296 | {NULL} /* Sentinel */ | |
2286 | }; |
|
2297 | }; | |
2287 |
|
2298 | |||
2288 | static PyTypeObject indexType = { |
|
2299 | static PyTypeObject indexType = { | |
2289 | PyObject_HEAD_INIT(NULL) |
|
2300 | PyObject_HEAD_INIT(NULL) | |
2290 | 0, /* ob_size */ |
|
2301 | 0, /* ob_size */ | |
2291 | "parsers.index", /* tp_name */ |
|
2302 | "parsers.index", /* tp_name */ | |
2292 | sizeof(indexObject), /* tp_basicsize */ |
|
2303 | sizeof(indexObject), /* tp_basicsize */ | |
2293 | 0, /* tp_itemsize */ |
|
2304 | 0, /* tp_itemsize */ | |
2294 | (destructor)index_dealloc, /* tp_dealloc */ |
|
2305 | (destructor)index_dealloc, /* tp_dealloc */ | |
2295 | 0, /* tp_print */ |
|
2306 | 0, /* tp_print */ | |
2296 | 0, /* tp_getattr */ |
|
2307 | 0, /* tp_getattr */ | |
2297 | 0, /* tp_setattr */ |
|
2308 | 0, /* tp_setattr */ | |
2298 | 0, /* tp_compare */ |
|
2309 | 0, /* tp_compare */ | |
2299 | 0, /* tp_repr */ |
|
2310 | 0, /* tp_repr */ | |
2300 | 0, /* tp_as_number */ |
|
2311 | 0, /* tp_as_number */ | |
2301 | &index_sequence_methods, /* tp_as_sequence */ |
|
2312 | &index_sequence_methods, /* tp_as_sequence */ | |
2302 | &index_mapping_methods, /* tp_as_mapping */ |
|
2313 | &index_mapping_methods, /* tp_as_mapping */ | |
2303 | 0, /* tp_hash */ |
|
2314 | 0, /* tp_hash */ | |
2304 | 0, /* tp_call */ |
|
2315 | 0, /* tp_call */ | |
2305 | 0, /* tp_str */ |
|
2316 | 0, /* tp_str */ | |
2306 | 0, /* tp_getattro */ |
|
2317 | 0, /* tp_getattro */ | |
2307 | 0, /* tp_setattro */ |
|
2318 | 0, /* tp_setattro */ | |
2308 | 0, /* tp_as_buffer */ |
|
2319 | 0, /* tp_as_buffer */ | |
2309 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
2320 | Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
2310 | "revlog index", /* tp_doc */ |
|
2321 | "revlog index", /* tp_doc */ | |
2311 | 0, /* tp_traverse */ |
|
2322 | 0, /* tp_traverse */ | |
2312 | 0, /* tp_clear */ |
|
2323 | 0, /* tp_clear */ | |
2313 | 0, /* tp_richcompare */ |
|
2324 | 0, /* tp_richcompare */ | |
2314 | 0, /* tp_weaklistoffset */ |
|
2325 | 0, /* tp_weaklistoffset */ | |
2315 | 0, /* tp_iter */ |
|
2326 | 0, /* tp_iter */ | |
2316 | 0, /* tp_iternext */ |
|
2327 | 0, /* tp_iternext */ | |
2317 | index_methods, /* tp_methods */ |
|
2328 | index_methods, /* tp_methods */ | |
2318 | 0, /* tp_members */ |
|
2329 | 0, /* tp_members */ | |
2319 | index_getset, /* tp_getset */ |
|
2330 | index_getset, /* tp_getset */ | |
2320 | 0, /* tp_base */ |
|
2331 | 0, /* tp_base */ | |
2321 | 0, /* tp_dict */ |
|
2332 | 0, /* tp_dict */ | |
2322 | 0, /* tp_descr_get */ |
|
2333 | 0, /* tp_descr_get */ | |
2323 | 0, /* tp_descr_set */ |
|
2334 | 0, /* tp_descr_set */ | |
2324 | 0, /* tp_dictoffset */ |
|
2335 | 0, /* tp_dictoffset */ | |
2325 | (initproc)index_init, /* tp_init */ |
|
2336 | (initproc)index_init, /* tp_init */ | |
2326 | 0, /* tp_alloc */ |
|
2337 | 0, /* tp_alloc */ | |
2327 | }; |
|
2338 | }; | |
2328 |
|
2339 | |||
2329 | /* |
|
2340 | /* | |
2330 | * returns a tuple of the form (index, index, cache) with elements as |
|
2341 | * returns a tuple of the form (index, index, cache) with elements as | |
2331 | * follows: |
|
2342 | * follows: | |
2332 | * |
|
2343 | * | |
2333 | * index: an index object that lazily parses RevlogNG records |
|
2344 | * index: an index object that lazily parses RevlogNG records | |
2334 | * cache: if data is inlined, a tuple (index_file_content, 0), else None |
|
2345 | * cache: if data is inlined, a tuple (index_file_content, 0), else None | |
2335 | * |
|
2346 | * | |
2336 | * added complications are for backwards compatibility |
|
2347 | * added complications are for backwards compatibility | |
2337 | */ |
|
2348 | */ | |
2338 | static PyObject *parse_index2(PyObject *self, PyObject *args) |
|
2349 | static PyObject *parse_index2(PyObject *self, PyObject *args) | |
2339 | { |
|
2350 | { | |
2340 | PyObject *tuple = NULL, *cache = NULL; |
|
2351 | PyObject *tuple = NULL, *cache = NULL; | |
2341 | indexObject *idx; |
|
2352 | indexObject *idx; | |
2342 | int ret; |
|
2353 | int ret; | |
2343 |
|
2354 | |||
2344 | idx = PyObject_New(indexObject, &indexType); |
|
2355 | idx = PyObject_New(indexObject, &indexType); | |
2345 | if (idx == NULL) |
|
2356 | if (idx == NULL) | |
2346 | goto bail; |
|
2357 | goto bail; | |
2347 |
|
2358 | |||
2348 | ret = index_init(idx, args); |
|
2359 | ret = index_init(idx, args); | |
2349 | if (ret == -1) |
|
2360 | if (ret == -1) | |
2350 | goto bail; |
|
2361 | goto bail; | |
2351 |
|
2362 | |||
2352 | if (idx->inlined) { |
|
2363 | if (idx->inlined) { | |
2353 | cache = Py_BuildValue("iO", 0, idx->data); |
|
2364 | cache = Py_BuildValue("iO", 0, idx->data); | |
2354 | if (cache == NULL) |
|
2365 | if (cache == NULL) | |
2355 | goto bail; |
|
2366 | goto bail; | |
2356 | } else { |
|
2367 | } else { | |
2357 | cache = Py_None; |
|
2368 | cache = Py_None; | |
2358 | Py_INCREF(cache); |
|
2369 | Py_INCREF(cache); | |
2359 | } |
|
2370 | } | |
2360 |
|
2371 | |||
2361 | tuple = Py_BuildValue("NN", idx, cache); |
|
2372 | tuple = Py_BuildValue("NN", idx, cache); | |
2362 | if (!tuple) |
|
2373 | if (!tuple) | |
2363 | goto bail; |
|
2374 | goto bail; | |
2364 | return tuple; |
|
2375 | return tuple; | |
2365 |
|
2376 | |||
2366 | bail: |
|
2377 | bail: | |
2367 | Py_XDECREF(idx); |
|
2378 | Py_XDECREF(idx); | |
2368 | Py_XDECREF(cache); |
|
2379 | Py_XDECREF(cache); | |
2369 | Py_XDECREF(tuple); |
|
2380 | Py_XDECREF(tuple); | |
2370 | return NULL; |
|
2381 | return NULL; | |
2371 | } |
|
2382 | } | |
2372 |
|
2383 | |||
2373 | #define BUMPED_FIX 1 |
|
2384 | #define BUMPED_FIX 1 | |
2374 | #define USING_SHA_256 2 |
|
2385 | #define USING_SHA_256 2 | |
2375 |
|
2386 | |||
2376 | static PyObject *readshas( |
|
2387 | static PyObject *readshas( | |
2377 | const char *source, unsigned char num, Py_ssize_t hashwidth) |
|
2388 | const char *source, unsigned char num, Py_ssize_t hashwidth) | |
2378 | { |
|
2389 | { | |
2379 | int i; |
|
2390 | int i; | |
2380 | PyObject *list = PyTuple_New(num); |
|
2391 | PyObject *list = PyTuple_New(num); | |
2381 | if (list == NULL) { |
|
2392 | if (list == NULL) { | |
2382 | return NULL; |
|
2393 | return NULL; | |
2383 | } |
|
2394 | } | |
2384 | for (i = 0; i < num; i++) { |
|
2395 | for (i = 0; i < num; i++) { | |
2385 | PyObject *hash = PyString_FromStringAndSize(source, hashwidth); |
|
2396 | PyObject *hash = PyString_FromStringAndSize(source, hashwidth); | |
2386 | if (hash == NULL) { |
|
2397 | if (hash == NULL) { | |
2387 | Py_DECREF(list); |
|
2398 | Py_DECREF(list); | |
2388 | return NULL; |
|
2399 | return NULL; | |
2389 | } |
|
2400 | } | |
2390 | PyTuple_SetItem(list, i, hash); |
|
2401 | PyTuple_SetItem(list, i, hash); | |
2391 | source += hashwidth; |
|
2402 | source += hashwidth; | |
2392 | } |
|
2403 | } | |
2393 | return list; |
|
2404 | return list; | |
2394 | } |
|
2405 | } | |
2395 |
|
2406 | |||
2396 | static PyObject *fm1readmarker(const char *data, uint32_t *msize) |
|
2407 | static PyObject *fm1readmarker(const char *data, uint32_t *msize) | |
2397 | { |
|
2408 | { | |
2398 | const char *meta; |
|
2409 | const char *meta; | |
2399 |
|
2410 | |||
2400 | double mtime; |
|
2411 | double mtime; | |
2401 | int16_t tz; |
|
2412 | int16_t tz; | |
2402 | uint16_t flags; |
|
2413 | uint16_t flags; | |
2403 | unsigned char nsuccs, nparents, nmetadata; |
|
2414 | unsigned char nsuccs, nparents, nmetadata; | |
2404 | Py_ssize_t hashwidth = 20; |
|
2415 | Py_ssize_t hashwidth = 20; | |
2405 |
|
2416 | |||
2406 | PyObject *prec = NULL, *parents = NULL, *succs = NULL; |
|
2417 | PyObject *prec = NULL, *parents = NULL, *succs = NULL; | |
2407 | PyObject *metadata = NULL, *ret = NULL; |
|
2418 | PyObject *metadata = NULL, *ret = NULL; | |
2408 | int i; |
|
2419 | int i; | |
2409 |
|
2420 | |||
2410 | *msize = getbe32(data); |
|
2421 | *msize = getbe32(data); | |
2411 | data += 4; |
|
2422 | data += 4; | |
2412 | mtime = getbefloat64(data); |
|
2423 | mtime = getbefloat64(data); | |
2413 | data += 8; |
|
2424 | data += 8; | |
2414 | tz = getbeint16(data); |
|
2425 | tz = getbeint16(data); | |
2415 | data += 2; |
|
2426 | data += 2; | |
2416 | flags = getbeuint16(data); |
|
2427 | flags = getbeuint16(data); | |
2417 | data += 2; |
|
2428 | data += 2; | |
2418 |
|
2429 | |||
2419 | if (flags & USING_SHA_256) { |
|
2430 | if (flags & USING_SHA_256) { | |
2420 | hashwidth = 32; |
|
2431 | hashwidth = 32; | |
2421 | } |
|
2432 | } | |
2422 |
|
2433 | |||
2423 | nsuccs = (unsigned char)(*data++); |
|
2434 | nsuccs = (unsigned char)(*data++); | |
2424 | nparents = (unsigned char)(*data++); |
|
2435 | nparents = (unsigned char)(*data++); | |
2425 | nmetadata = (unsigned char)(*data++); |
|
2436 | nmetadata = (unsigned char)(*data++); | |
2426 |
|
2437 | |||
2427 | prec = PyString_FromStringAndSize(data, hashwidth); |
|
2438 | prec = PyString_FromStringAndSize(data, hashwidth); | |
2428 | data += hashwidth; |
|
2439 | data += hashwidth; | |
2429 | if (prec == NULL) { |
|
2440 | if (prec == NULL) { | |
2430 | goto bail; |
|
2441 | goto bail; | |
2431 | } |
|
2442 | } | |
2432 |
|
2443 | |||
2433 | succs = readshas(data, nsuccs, hashwidth); |
|
2444 | succs = readshas(data, nsuccs, hashwidth); | |
2434 | if (succs == NULL) { |
|
2445 | if (succs == NULL) { | |
2435 | goto bail; |
|
2446 | goto bail; | |
2436 | } |
|
2447 | } | |
2437 | data += nsuccs * hashwidth; |
|
2448 | data += nsuccs * hashwidth; | |
2438 |
|
2449 | |||
2439 | if (nparents == 1 || nparents == 2) { |
|
2450 | if (nparents == 1 || nparents == 2) { | |
2440 | parents = readshas(data, nparents, hashwidth); |
|
2451 | parents = readshas(data, nparents, hashwidth); | |
2441 | if (parents == NULL) { |
|
2452 | if (parents == NULL) { | |
2442 | goto bail; |
|
2453 | goto bail; | |
2443 | } |
|
2454 | } | |
2444 | data += nparents * hashwidth; |
|
2455 | data += nparents * hashwidth; | |
2445 | } else { |
|
2456 | } else { | |
2446 | parents = Py_None; |
|
2457 | parents = Py_None; | |
2447 | } |
|
2458 | } | |
2448 |
|
2459 | |||
2449 | meta = data + (2 * nmetadata); |
|
2460 | meta = data + (2 * nmetadata); | |
2450 | metadata = PyTuple_New(nmetadata); |
|
2461 | metadata = PyTuple_New(nmetadata); | |
2451 | if (metadata == NULL) { |
|
2462 | if (metadata == NULL) { | |
2452 | goto bail; |
|
2463 | goto bail; | |
2453 | } |
|
2464 | } | |
2454 | for (i = 0; i < nmetadata; i++) { |
|
2465 | for (i = 0; i < nmetadata; i++) { | |
2455 | PyObject *tmp, *left = NULL, *right = NULL; |
|
2466 | PyObject *tmp, *left = NULL, *right = NULL; | |
2456 | Py_ssize_t metasize = (unsigned char)(*data++); |
|
2467 | Py_ssize_t metasize = (unsigned char)(*data++); | |
2457 | left = PyString_FromStringAndSize(meta, metasize); |
|
2468 | left = PyString_FromStringAndSize(meta, metasize); | |
2458 | meta += metasize; |
|
2469 | meta += metasize; | |
2459 | metasize = (unsigned char)(*data++); |
|
2470 | metasize = (unsigned char)(*data++); | |
2460 | right = PyString_FromStringAndSize(meta, metasize); |
|
2471 | right = PyString_FromStringAndSize(meta, metasize); | |
2461 | meta += metasize; |
|
2472 | meta += metasize; | |
2462 | if (!left || !right) { |
|
2473 | if (!left || !right) { | |
2463 | Py_XDECREF(left); |
|
2474 | Py_XDECREF(left); | |
2464 | Py_XDECREF(right); |
|
2475 | Py_XDECREF(right); | |
2465 | goto bail; |
|
2476 | goto bail; | |
2466 | } |
|
2477 | } | |
2467 | tmp = PyTuple_Pack(2, left, right); |
|
2478 | tmp = PyTuple_Pack(2, left, right); | |
2468 | Py_DECREF(left); |
|
2479 | Py_DECREF(left); | |
2469 | Py_DECREF(right); |
|
2480 | Py_DECREF(right); | |
2470 | if (!tmp) { |
|
2481 | if (!tmp) { | |
2471 | goto bail; |
|
2482 | goto bail; | |
2472 | } |
|
2483 | } | |
2473 | PyTuple_SetItem(metadata, i, tmp); |
|
2484 | PyTuple_SetItem(metadata, i, tmp); | |
2474 | } |
|
2485 | } | |
2475 | ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags, |
|
2486 | ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags, | |
2476 | metadata, mtime, (int)tz * 60, parents); |
|
2487 | metadata, mtime, (int)tz * 60, parents); | |
2477 | bail: |
|
2488 | bail: | |
2478 | Py_XDECREF(prec); |
|
2489 | Py_XDECREF(prec); | |
2479 | Py_XDECREF(succs); |
|
2490 | Py_XDECREF(succs); | |
2480 | Py_XDECREF(metadata); |
|
2491 | Py_XDECREF(metadata); | |
2481 | if (parents != Py_None) |
|
2492 | if (parents != Py_None) | |
2482 | Py_XDECREF(parents); |
|
2493 | Py_XDECREF(parents); | |
2483 | return ret; |
|
2494 | return ret; | |
2484 | } |
|
2495 | } | |
2485 |
|
2496 | |||
2486 |
|
2497 | |||
2487 | static PyObject *fm1readmarkers(PyObject *self, PyObject *args) { |
|
2498 | static PyObject *fm1readmarkers(PyObject *self, PyObject *args) { | |
2488 | const char *data; |
|
2499 | const char *data; | |
2489 | Py_ssize_t datalen; |
|
2500 | Py_ssize_t datalen; | |
2490 | /* only unsigned long because python 2.4, should be Py_ssize_t */ |
|
2501 | /* only unsigned long because python 2.4, should be Py_ssize_t */ | |
2491 | unsigned long offset, stop; |
|
2502 | unsigned long offset, stop; | |
2492 | PyObject *markers = NULL; |
|
2503 | PyObject *markers = NULL; | |
2493 |
|
2504 | |||
2494 | /* replace kk with nn when we drop Python 2.4 */ |
|
2505 | /* replace kk with nn when we drop Python 2.4 */ | |
2495 | if (!PyArg_ParseTuple(args, "s#kk", &data, &datalen, &offset, &stop)) { |
|
2506 | if (!PyArg_ParseTuple(args, "s#kk", &data, &datalen, &offset, &stop)) { | |
2496 | return NULL; |
|
2507 | return NULL; | |
2497 | } |
|
2508 | } | |
2498 | data += offset; |
|
2509 | data += offset; | |
2499 | markers = PyList_New(0); |
|
2510 | markers = PyList_New(0); | |
2500 | if (!markers) { |
|
2511 | if (!markers) { | |
2501 | return NULL; |
|
2512 | return NULL; | |
2502 | } |
|
2513 | } | |
2503 | while (offset < stop) { |
|
2514 | while (offset < stop) { | |
2504 | uint32_t msize; |
|
2515 | uint32_t msize; | |
2505 | int error; |
|
2516 | int error; | |
2506 | PyObject *record = fm1readmarker(data, &msize); |
|
2517 | PyObject *record = fm1readmarker(data, &msize); | |
2507 | if (!record) { |
|
2518 | if (!record) { | |
2508 | goto bail; |
|
2519 | goto bail; | |
2509 | } |
|
2520 | } | |
2510 | error = PyList_Append(markers, record); |
|
2521 | error = PyList_Append(markers, record); | |
2511 | Py_DECREF(record); |
|
2522 | Py_DECREF(record); | |
2512 | if (error) { |
|
2523 | if (error) { | |
2513 | goto bail; |
|
2524 | goto bail; | |
2514 | } |
|
2525 | } | |
2515 | data += msize; |
|
2526 | data += msize; | |
2516 | offset += msize; |
|
2527 | offset += msize; | |
2517 | } |
|
2528 | } | |
2518 | return markers; |
|
2529 | return markers; | |
2519 | bail: |
|
2530 | bail: | |
2520 | Py_DECREF(markers); |
|
2531 | Py_DECREF(markers); | |
2521 | return NULL; |
|
2532 | return NULL; | |
2522 | } |
|
2533 | } | |
2523 |
|
2534 | |||
2524 | static char parsers_doc[] = "Efficient content parsing."; |
|
2535 | static char parsers_doc[] = "Efficient content parsing."; | |
2525 |
|
2536 | |||
2526 | PyObject *encodedir(PyObject *self, PyObject *args); |
|
2537 | PyObject *encodedir(PyObject *self, PyObject *args); | |
2527 | PyObject *pathencode(PyObject *self, PyObject *args); |
|
2538 | PyObject *pathencode(PyObject *self, PyObject *args); | |
2528 | PyObject *lowerencode(PyObject *self, PyObject *args); |
|
2539 | PyObject *lowerencode(PyObject *self, PyObject *args); | |
2529 |
|
2540 | |||
2530 | static PyMethodDef methods[] = { |
|
2541 | static PyMethodDef methods[] = { | |
2531 | {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"}, |
|
2542 | {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"}, | |
2532 | {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"}, |
|
2543 | {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"}, | |
2533 | {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"}, |
|
2544 | {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"}, | |
2534 | {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"}, |
|
2545 | {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"}, | |
2535 | {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"}, |
|
2546 | {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"}, | |
2536 | {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"}, |
|
2547 | {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"}, | |
2537 | {"dict_new_presized", dict_new_presized, METH_VARARGS, |
|
2548 | {"dict_new_presized", dict_new_presized, METH_VARARGS, | |
2538 | "construct a dict with an expected size\n"}, |
|
2549 | "construct a dict with an expected size\n"}, | |
2539 | {"make_file_foldmap", make_file_foldmap, METH_VARARGS, |
|
2550 | {"make_file_foldmap", make_file_foldmap, METH_VARARGS, | |
2540 | "make file foldmap\n"}, |
|
2551 | "make file foldmap\n"}, | |
2541 | {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"}, |
|
2552 | {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"}, | |
2542 | {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"}, |
|
2553 | {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"}, | |
2543 | {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"}, |
|
2554 | {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"}, | |
2544 | {"fm1readmarkers", fm1readmarkers, METH_VARARGS, |
|
2555 | {"fm1readmarkers", fm1readmarkers, METH_VARARGS, | |
2545 | "parse v1 obsolete markers\n"}, |
|
2556 | "parse v1 obsolete markers\n"}, | |
2546 | {NULL, NULL} |
|
2557 | {NULL, NULL} | |
2547 | }; |
|
2558 | }; | |
2548 |
|
2559 | |||
2549 | void dirs_module_init(PyObject *mod); |
|
2560 | void dirs_module_init(PyObject *mod); | |
2550 | void manifest_module_init(PyObject *mod); |
|
2561 | void manifest_module_init(PyObject *mod); | |
2551 |
|
2562 | |||
2552 | static void module_init(PyObject *mod) |
|
2563 | static void module_init(PyObject *mod) | |
2553 | { |
|
2564 | { | |
2554 | /* This module constant has two purposes. First, it lets us unit test |
|
2565 | /* This module constant has two purposes. First, it lets us unit test | |
2555 | * the ImportError raised without hard-coding any error text. This |
|
2566 | * the ImportError raised without hard-coding any error text. This | |
2556 | * means we can change the text in the future without breaking tests, |
|
2567 | * means we can change the text in the future without breaking tests, | |
2557 | * even across changesets without a recompile. Second, its presence |
|
2568 | * even across changesets without a recompile. Second, its presence | |
2558 | * can be used to determine whether the version-checking logic is |
|
2569 | * can be used to determine whether the version-checking logic is | |
2559 | * present, which also helps in testing across changesets without a |
|
2570 | * present, which also helps in testing across changesets without a | |
2560 | * recompile. Note that this means the pure-Python version of parsers |
|
2571 | * recompile. Note that this means the pure-Python version of parsers | |
2561 | * should not have this module constant. */ |
|
2572 | * should not have this module constant. */ | |
2562 | PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext); |
|
2573 | PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext); | |
2563 |
|
2574 | |||
2564 | dirs_module_init(mod); |
|
2575 | dirs_module_init(mod); | |
2565 | manifest_module_init(mod); |
|
2576 | manifest_module_init(mod); | |
2566 |
|
2577 | |||
2567 | indexType.tp_new = PyType_GenericNew; |
|
2578 | indexType.tp_new = PyType_GenericNew; | |
2568 | if (PyType_Ready(&indexType) < 0 || |
|
2579 | if (PyType_Ready(&indexType) < 0 || | |
2569 | PyType_Ready(&dirstateTupleType) < 0) |
|
2580 | PyType_Ready(&dirstateTupleType) < 0) | |
2570 | return; |
|
2581 | return; | |
2571 | Py_INCREF(&indexType); |
|
2582 | Py_INCREF(&indexType); | |
2572 | PyModule_AddObject(mod, "index", (PyObject *)&indexType); |
|
2583 | PyModule_AddObject(mod, "index", (PyObject *)&indexType); | |
2573 | Py_INCREF(&dirstateTupleType); |
|
2584 | Py_INCREF(&dirstateTupleType); | |
2574 | PyModule_AddObject(mod, "dirstatetuple", |
|
2585 | PyModule_AddObject(mod, "dirstatetuple", | |
2575 | (PyObject *)&dirstateTupleType); |
|
2586 | (PyObject *)&dirstateTupleType); | |
2576 |
|
2587 | |||
2577 | nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0, |
|
2588 | nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0, | |
2578 | -1, -1, -1, -1, nullid, 20); |
|
2589 | -1, -1, -1, -1, nullid, 20); | |
2579 | if (nullentry) |
|
2590 | if (nullentry) | |
2580 | PyObject_GC_UnTrack(nullentry); |
|
2591 | PyObject_GC_UnTrack(nullentry); | |
2581 | } |
|
2592 | } | |
2582 |
|
2593 | |||
2583 | static int check_python_version(void) |
|
2594 | static int check_python_version(void) | |
2584 | { |
|
2595 | { | |
2585 | PyObject *sys = PyImport_ImportModule("sys"), *ver; |
|
2596 | PyObject *sys = PyImport_ImportModule("sys"), *ver; | |
2586 | long hexversion; |
|
2597 | long hexversion; | |
2587 | if (!sys) |
|
2598 | if (!sys) | |
2588 | return -1; |
|
2599 | return -1; | |
2589 | ver = PyObject_GetAttrString(sys, "hexversion"); |
|
2600 | ver = PyObject_GetAttrString(sys, "hexversion"); | |
2590 | Py_DECREF(sys); |
|
2601 | Py_DECREF(sys); | |
2591 | if (!ver) |
|
2602 | if (!ver) | |
2592 | return -1; |
|
2603 | return -1; | |
2593 | hexversion = PyInt_AsLong(ver); |
|
2604 | hexversion = PyInt_AsLong(ver); | |
2594 | Py_DECREF(ver); |
|
2605 | Py_DECREF(ver); | |
2595 | /* sys.hexversion is a 32-bit number by default, so the -1 case |
|
2606 | /* sys.hexversion is a 32-bit number by default, so the -1 case | |
2596 | * should only occur in unusual circumstances (e.g. if sys.hexversion |
|
2607 | * should only occur in unusual circumstances (e.g. if sys.hexversion | |
2597 | * is manually set to an invalid value). */ |
|
2608 | * is manually set to an invalid value). */ | |
2598 | if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) { |
|
2609 | if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) { | |
2599 | PyErr_Format(PyExc_ImportError, "%s: The Mercurial extension " |
|
2610 | PyErr_Format(PyExc_ImportError, "%s: The Mercurial extension " | |
2600 | "modules were compiled with Python " PY_VERSION ", but " |
|
2611 | "modules were compiled with Python " PY_VERSION ", but " | |
2601 | "Mercurial is currently using Python with sys.hexversion=%ld: " |
|
2612 | "Mercurial is currently using Python with sys.hexversion=%ld: " | |
2602 | "Python %s\n at: %s", versionerrortext, hexversion, |
|
2613 | "Python %s\n at: %s", versionerrortext, hexversion, | |
2603 | Py_GetVersion(), Py_GetProgramFullPath()); |
|
2614 | Py_GetVersion(), Py_GetProgramFullPath()); | |
2604 | return -1; |
|
2615 | return -1; | |
2605 | } |
|
2616 | } | |
2606 | return 0; |
|
2617 | return 0; | |
2607 | } |
|
2618 | } | |
2608 |
|
2619 | |||
2609 | #ifdef IS_PY3K |
|
2620 | #ifdef IS_PY3K | |
2610 | static struct PyModuleDef parsers_module = { |
|
2621 | static struct PyModuleDef parsers_module = { | |
2611 | PyModuleDef_HEAD_INIT, |
|
2622 | PyModuleDef_HEAD_INIT, | |
2612 | "parsers", |
|
2623 | "parsers", | |
2613 | parsers_doc, |
|
2624 | parsers_doc, | |
2614 | -1, |
|
2625 | -1, | |
2615 | methods |
|
2626 | methods | |
2616 | }; |
|
2627 | }; | |
2617 |
|
2628 | |||
2618 | PyMODINIT_FUNC PyInit_parsers(void) |
|
2629 | PyMODINIT_FUNC PyInit_parsers(void) | |
2619 | { |
|
2630 | { | |
2620 | PyObject *mod; |
|
2631 | PyObject *mod; | |
2621 |
|
2632 | |||
2622 | if (check_python_version() == -1) |
|
2633 | if (check_python_version() == -1) | |
2623 | return; |
|
2634 | return; | |
2624 | mod = PyModule_Create(&parsers_module); |
|
2635 | mod = PyModule_Create(&parsers_module); | |
2625 | module_init(mod); |
|
2636 | module_init(mod); | |
2626 | return mod; |
|
2637 | return mod; | |
2627 | } |
|
2638 | } | |
2628 | #else |
|
2639 | #else | |
2629 | PyMODINIT_FUNC initparsers(void) |
|
2640 | PyMODINIT_FUNC initparsers(void) | |
2630 | { |
|
2641 | { | |
2631 | PyObject *mod; |
|
2642 | PyObject *mod; | |
2632 |
|
2643 | |||
2633 | if (check_python_version() == -1) |
|
2644 | if (check_python_version() == -1) | |
2634 | return; |
|
2645 | return; | |
2635 | mod = Py_InitModule3("parsers", methods, parsers_doc); |
|
2646 | mod = Py_InitModule3("parsers", methods, parsers_doc); | |
2636 | module_init(mod); |
|
2647 | module_init(mod); | |
2637 | } |
|
2648 | } | |
2638 | #endif |
|
2649 | #endif |
@@ -1,61 +1,120 | |||||
1 | revlog.parseindex must be able to parse the index file even if |
|
1 | revlog.parseindex must be able to parse the index file even if | |
2 | an index entry is split between two 64k blocks. The ideal test |
|
2 | an index entry is split between two 64k blocks. The ideal test | |
3 | would be to create an index file with inline data where |
|
3 | would be to create an index file with inline data where | |
4 | 64k < size < 64k + 64 (64k is the size of the read buffer, 64 is |
|
4 | 64k < size < 64k + 64 (64k is the size of the read buffer, 64 is | |
5 | the size of an index entry) and with an index entry starting right |
|
5 | the size of an index entry) and with an index entry starting right | |
6 | before the 64k block boundary, and try to read it. |
|
6 | before the 64k block boundary, and try to read it. | |
7 | We approximate that by reducing the read buffer to 1 byte. |
|
7 | We approximate that by reducing the read buffer to 1 byte. | |
8 |
|
8 | |||
9 | $ hg init a |
|
9 | $ hg init a | |
10 | $ cd a |
|
10 | $ cd a | |
11 | $ echo abc > foo |
|
11 | $ echo abc > foo | |
12 | $ hg add foo |
|
12 | $ hg add foo | |
13 | $ hg commit -m 'add foo' |
|
13 | $ hg commit -m 'add foo' | |
14 | $ echo >> foo |
|
14 | $ echo >> foo | |
15 | $ hg commit -m 'change foo' |
|
15 | $ hg commit -m 'change foo' | |
16 | $ hg log -r 0: |
|
16 | $ hg log -r 0: | |
17 | changeset: 0:7c31755bf9b5 |
|
17 | changeset: 0:7c31755bf9b5 | |
18 | user: test |
|
18 | user: test | |
19 | date: Thu Jan 01 00:00:00 1970 +0000 |
|
19 | date: Thu Jan 01 00:00:00 1970 +0000 | |
20 | summary: add foo |
|
20 | summary: add foo | |
21 |
|
21 | |||
22 | changeset: 1:26333235a41c |
|
22 | changeset: 1:26333235a41c | |
23 | tag: tip |
|
23 | tag: tip | |
24 | user: test |
|
24 | user: test | |
25 | date: Thu Jan 01 00:00:00 1970 +0000 |
|
25 | date: Thu Jan 01 00:00:00 1970 +0000 | |
26 | summary: change foo |
|
26 | summary: change foo | |
27 |
|
27 | |||
28 | $ cat >> test.py << EOF |
|
28 | $ cat >> test.py << EOF | |
29 | > from mercurial import changelog, scmutil |
|
29 | > from mercurial import changelog, scmutil | |
30 | > from mercurial.node import * |
|
30 | > from mercurial.node import * | |
31 | > |
|
31 | > | |
32 | > class singlebyteread(object): |
|
32 | > class singlebyteread(object): | |
33 | > def __init__(self, real): |
|
33 | > def __init__(self, real): | |
34 | > self.real = real |
|
34 | > self.real = real | |
35 | > |
|
35 | > | |
36 | > def read(self, size=-1): |
|
36 | > def read(self, size=-1): | |
37 | > if size == 65536: |
|
37 | > if size == 65536: | |
38 | > size = 1 |
|
38 | > size = 1 | |
39 | > return self.real.read(size) |
|
39 | > return self.real.read(size) | |
40 | > |
|
40 | > | |
41 | > def __getattr__(self, key): |
|
41 | > def __getattr__(self, key): | |
42 | > return getattr(self.real, key) |
|
42 | > return getattr(self.real, key) | |
43 | > |
|
43 | > | |
44 | > def opener(*args): |
|
44 | > def opener(*args): | |
45 | > o = scmutil.opener(*args) |
|
45 | > o = scmutil.opener(*args) | |
46 | > def wrapper(*a): |
|
46 | > def wrapper(*a): | |
47 | > f = o(*a) |
|
47 | > f = o(*a) | |
48 | > return singlebyteread(f) |
|
48 | > return singlebyteread(f) | |
49 | > return wrapper |
|
49 | > return wrapper | |
50 | > |
|
50 | > | |
51 | > cl = changelog.changelog(opener('.hg/store')) |
|
51 | > cl = changelog.changelog(opener('.hg/store')) | |
52 | > print len(cl), 'revisions:' |
|
52 | > print len(cl), 'revisions:' | |
53 | > for r in cl: |
|
53 | > for r in cl: | |
54 | > print short(cl.node(r)) |
|
54 | > print short(cl.node(r)) | |
55 | > EOF |
|
55 | > EOF | |
56 | $ python test.py |
|
56 | $ python test.py | |
57 | 2 revisions: |
|
57 | 2 revisions: | |
58 | 7c31755bf9b5 |
|
58 | 7c31755bf9b5 | |
59 | 26333235a41c |
|
59 | 26333235a41c | |
60 |
|
60 | |||
61 | $ cd .. |
|
61 | $ cd .. | |
|
62 | ||||
|
63 | Test corrupted p1/p2 fields that could cause SEGV at parsers.c: | |||
|
64 | ||||
|
65 | $ mkdir invalidparent | |||
|
66 | $ cd invalidparent | |||
|
67 | ||||
|
68 | $ hg clone --pull -q --config phases.publish=False ../a limit | |||
|
69 | $ hg clone --pull -q --config phases.publish=False ../a segv | |||
|
70 | $ rm -R limit/.hg/cache segv/.hg/cache | |||
|
71 | ||||
|
72 | $ python <<EOF | |||
|
73 | > data = open("limit/.hg/store/00changelog.i", "rb").read() | |||
|
74 | > for n, p in [('limit', '\0\0\0\x02'), ('segv', '\0\x01\0\0')]: | |||
|
75 | > # corrupt p1 at rev0 and p2 at rev1 | |||
|
76 | > d = data[:24] + p + data[28:127 + 28] + p + data[127 + 32:] | |||
|
77 | > open(n + "/.hg/store/00changelog.i", "wb").write(d) | |||
|
78 | > EOF | |||
|
79 | ||||
|
80 | $ hg debugindex -f1 limit/.hg/store/00changelog.i | |||
|
81 | rev flag offset length size base link p1 p2 nodeid | |||
|
82 | 0 0000 0 63 62 0 0 2 -1 7c31755bf9b5 | |||
|
83 | 1 0000 63 66 65 1 1 0 2 26333235a41c | |||
|
84 | $ hg debugindex -f1 segv/.hg/store/00changelog.i | |||
|
85 | rev flag offset length size base link p1 p2 nodeid | |||
|
86 | 0 0000 0 63 62 0 0 65536 -1 7c31755bf9b5 | |||
|
87 | 1 0000 63 66 65 1 1 0 65536 26333235a41c | |||
|
88 | ||||
|
89 | $ cat <<EOF > test.py | |||
|
90 | > import sys | |||
|
91 | > from mercurial import changelog, scmutil | |||
|
92 | > cl = changelog.changelog(scmutil.vfs(sys.argv[1])) | |||
|
93 | > n0, n1 = cl.node(0), cl.node(1) | |||
|
94 | > ops = [ | |||
|
95 | > ('compute_phases_map_sets', lambda: cl.computephases([[0], []])), | |||
|
96 | > ('index_headrevs', lambda: cl.headrevs()), | |||
|
97 | > ('find_gca_candidates', lambda: cl.commonancestorsheads(n0, n1)), | |||
|
98 | > ('find_deepest', lambda: cl.ancestor(n0, n1)), | |||
|
99 | > ] | |||
|
100 | > for l, f in ops: | |||
|
101 | > print l + ':', | |||
|
102 | > try: | |||
|
103 | > f() | |||
|
104 | > print 'uncaught buffer overflow?' | |||
|
105 | > except ValueError, inst: | |||
|
106 | > print inst | |||
|
107 | > EOF | |||
|
108 | ||||
|
109 | $ python test.py limit/.hg/store | |||
|
110 | compute_phases_map_sets: parent out of range | |||
|
111 | index_headrevs: parent out of range | |||
|
112 | find_gca_candidates: parent out of range | |||
|
113 | find_deepest: parent out of range | |||
|
114 | $ python test.py segv/.hg/store | |||
|
115 | compute_phases_map_sets: parent out of range | |||
|
116 | index_headrevs: parent out of range | |||
|
117 | find_gca_candidates: parent out of range | |||
|
118 | find_deepest: parent out of range | |||
|
119 | ||||
|
120 | $ cd .. |
General Comments 0
You need to be logged in to leave comments.
Login now