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