##// END OF EJS Templates
cext: remove Python 2 support...
Gregory Szorc -
r49671:824b2082 default
parent child Browse files
Show More
@@ -1,1322 +1,1314 b''
1 /*
1 /*
2 parsers.c - efficient content parsing
2 parsers.c - efficient content parsing
3
3
4 Copyright 2008 Olivia Mackall <olivia@selenic.com> and others
4 Copyright 2008 Olivia Mackall <olivia@selenic.com> and others
5
5
6 This software may be used and distributed according to the terms of
6 This software may be used and distributed according to the terms of
7 the GNU General Public License, incorporated herein by reference.
7 the GNU General Public License, incorporated herein by reference.
8 */
8 */
9
9
10 #define PY_SSIZE_T_CLEAN
10 #define PY_SSIZE_T_CLEAN
11 #include <Python.h>
11 #include <Python.h>
12 #include <ctype.h>
12 #include <ctype.h>
13 #include <stddef.h>
13 #include <stddef.h>
14 #include <string.h>
14 #include <string.h>
15
15
16 #include "bitmanipulation.h"
16 #include "bitmanipulation.h"
17 #include "charencode.h"
17 #include "charencode.h"
18 #include "util.h"
18 #include "util.h"
19
19
20 #ifdef IS_PY3K
21 /* The mapping of Python types is meant to be temporary to get Python
20 /* The mapping of Python types is meant to be temporary to get Python
22 * 3 to compile. We should remove this once Python 3 support is fully
21 * 3 to compile. We should remove this once Python 3 support is fully
23 * supported and proper types are used in the extensions themselves. */
22 * supported and proper types are used in the extensions themselves. */
24 #define PyInt_Check PyLong_Check
23 #define PyInt_Check PyLong_Check
25 #define PyInt_FromLong PyLong_FromLong
24 #define PyInt_FromLong PyLong_FromLong
26 #define PyInt_FromSsize_t PyLong_FromSsize_t
25 #define PyInt_FromSsize_t PyLong_FromSsize_t
27 #define PyInt_AsLong PyLong_AsLong
26 #define PyInt_AsLong PyLong_AsLong
28 #else
29 /* Windows on Python 2.7 doesn't define S_IFLNK. Python 3+ defines via
30 * pyport.h. */
31 #ifndef S_IFLNK
32 #define S_IFLNK 0120000
33 #endif
34 #endif
35
27
36 static const char *const versionerrortext = "Python minor version mismatch";
28 static const char *const versionerrortext = "Python minor version mismatch";
37
29
38 static const int dirstate_v1_from_p2 = -2;
30 static const int dirstate_v1_from_p2 = -2;
39 static const int dirstate_v1_nonnormal = -1;
31 static const int dirstate_v1_nonnormal = -1;
40 static const int ambiguous_time = -1;
32 static const int ambiguous_time = -1;
41
33
42 static PyObject *dict_new_presized(PyObject *self, PyObject *args)
34 static PyObject *dict_new_presized(PyObject *self, PyObject *args)
43 {
35 {
44 Py_ssize_t expected_size;
36 Py_ssize_t expected_size;
45
37
46 if (!PyArg_ParseTuple(args, "n:make_presized_dict", &expected_size)) {
38 if (!PyArg_ParseTuple(args, "n:make_presized_dict", &expected_size)) {
47 return NULL;
39 return NULL;
48 }
40 }
49
41
50 return _dict_new_presized(expected_size);
42 return _dict_new_presized(expected_size);
51 }
43 }
52
44
53 static PyObject *dirstate_item_new(PyTypeObject *subtype, PyObject *args,
45 static PyObject *dirstate_item_new(PyTypeObject *subtype, PyObject *args,
54 PyObject *kwds)
46 PyObject *kwds)
55 {
47 {
56 /* We do all the initialization here and not a tp_init function because
48 /* We do all the initialization here and not a tp_init function because
57 * dirstate_item is immutable. */
49 * dirstate_item is immutable. */
58 dirstateItemObject *t;
50 dirstateItemObject *t;
59 int wc_tracked;
51 int wc_tracked;
60 int p1_tracked;
52 int p1_tracked;
61 int p2_info;
53 int p2_info;
62 int has_meaningful_data;
54 int has_meaningful_data;
63 int has_meaningful_mtime;
55 int has_meaningful_mtime;
64 int mtime_second_ambiguous;
56 int mtime_second_ambiguous;
65 int mode;
57 int mode;
66 int size;
58 int size;
67 int mtime_s;
59 int mtime_s;
68 int mtime_ns;
60 int mtime_ns;
69 PyObject *parentfiledata;
61 PyObject *parentfiledata;
70 PyObject *mtime;
62 PyObject *mtime;
71 PyObject *fallback_exec;
63 PyObject *fallback_exec;
72 PyObject *fallback_symlink;
64 PyObject *fallback_symlink;
73 static char *keywords_name[] = {
65 static char *keywords_name[] = {
74 "wc_tracked", "p1_tracked", "p2_info",
66 "wc_tracked", "p1_tracked", "p2_info",
75 "has_meaningful_data", "has_meaningful_mtime", "parentfiledata",
67 "has_meaningful_data", "has_meaningful_mtime", "parentfiledata",
76 "fallback_exec", "fallback_symlink", NULL,
68 "fallback_exec", "fallback_symlink", NULL,
77 };
69 };
78 wc_tracked = 0;
70 wc_tracked = 0;
79 p1_tracked = 0;
71 p1_tracked = 0;
80 p2_info = 0;
72 p2_info = 0;
81 has_meaningful_mtime = 1;
73 has_meaningful_mtime = 1;
82 has_meaningful_data = 1;
74 has_meaningful_data = 1;
83 mtime_second_ambiguous = 0;
75 mtime_second_ambiguous = 0;
84 parentfiledata = Py_None;
76 parentfiledata = Py_None;
85 fallback_exec = Py_None;
77 fallback_exec = Py_None;
86 fallback_symlink = Py_None;
78 fallback_symlink = Py_None;
87 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iiiiiOOO", keywords_name,
79 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iiiiiOOO", keywords_name,
88 &wc_tracked, &p1_tracked, &p2_info,
80 &wc_tracked, &p1_tracked, &p2_info,
89 &has_meaningful_data,
81 &has_meaningful_data,
90 &has_meaningful_mtime, &parentfiledata,
82 &has_meaningful_mtime, &parentfiledata,
91 &fallback_exec, &fallback_symlink)) {
83 &fallback_exec, &fallback_symlink)) {
92 return NULL;
84 return NULL;
93 }
85 }
94 t = (dirstateItemObject *)subtype->tp_alloc(subtype, 1);
86 t = (dirstateItemObject *)subtype->tp_alloc(subtype, 1);
95 if (!t) {
87 if (!t) {
96 return NULL;
88 return NULL;
97 }
89 }
98
90
99 t->flags = 0;
91 t->flags = 0;
100 if (wc_tracked) {
92 if (wc_tracked) {
101 t->flags |= dirstate_flag_wc_tracked;
93 t->flags |= dirstate_flag_wc_tracked;
102 }
94 }
103 if (p1_tracked) {
95 if (p1_tracked) {
104 t->flags |= dirstate_flag_p1_tracked;
96 t->flags |= dirstate_flag_p1_tracked;
105 }
97 }
106 if (p2_info) {
98 if (p2_info) {
107 t->flags |= dirstate_flag_p2_info;
99 t->flags |= dirstate_flag_p2_info;
108 }
100 }
109
101
110 if (fallback_exec != Py_None) {
102 if (fallback_exec != Py_None) {
111 t->flags |= dirstate_flag_has_fallback_exec;
103 t->flags |= dirstate_flag_has_fallback_exec;
112 if (PyObject_IsTrue(fallback_exec)) {
104 if (PyObject_IsTrue(fallback_exec)) {
113 t->flags |= dirstate_flag_fallback_exec;
105 t->flags |= dirstate_flag_fallback_exec;
114 }
106 }
115 }
107 }
116 if (fallback_symlink != Py_None) {
108 if (fallback_symlink != Py_None) {
117 t->flags |= dirstate_flag_has_fallback_symlink;
109 t->flags |= dirstate_flag_has_fallback_symlink;
118 if (PyObject_IsTrue(fallback_symlink)) {
110 if (PyObject_IsTrue(fallback_symlink)) {
119 t->flags |= dirstate_flag_fallback_symlink;
111 t->flags |= dirstate_flag_fallback_symlink;
120 }
112 }
121 }
113 }
122
114
123 if (parentfiledata != Py_None) {
115 if (parentfiledata != Py_None) {
124 if (!PyArg_ParseTuple(parentfiledata, "iiO", &mode, &size,
116 if (!PyArg_ParseTuple(parentfiledata, "iiO", &mode, &size,
125 &mtime)) {
117 &mtime)) {
126 return NULL;
118 return NULL;
127 }
119 }
128 if (mtime != Py_None) {
120 if (mtime != Py_None) {
129 if (!PyArg_ParseTuple(mtime, "iii", &mtime_s, &mtime_ns,
121 if (!PyArg_ParseTuple(mtime, "iii", &mtime_s, &mtime_ns,
130 &mtime_second_ambiguous)) {
122 &mtime_second_ambiguous)) {
131 return NULL;
123 return NULL;
132 }
124 }
133 } else {
125 } else {
134 has_meaningful_mtime = 0;
126 has_meaningful_mtime = 0;
135 }
127 }
136 } else {
128 } else {
137 has_meaningful_data = 0;
129 has_meaningful_data = 0;
138 has_meaningful_mtime = 0;
130 has_meaningful_mtime = 0;
139 }
131 }
140 if (has_meaningful_data) {
132 if (has_meaningful_data) {
141 t->flags |= dirstate_flag_has_meaningful_data;
133 t->flags |= dirstate_flag_has_meaningful_data;
142 t->mode = mode;
134 t->mode = mode;
143 t->size = size;
135 t->size = size;
144 if (mtime_second_ambiguous) {
136 if (mtime_second_ambiguous) {
145 t->flags |= dirstate_flag_mtime_second_ambiguous;
137 t->flags |= dirstate_flag_mtime_second_ambiguous;
146 }
138 }
147 } else {
139 } else {
148 t->mode = 0;
140 t->mode = 0;
149 t->size = 0;
141 t->size = 0;
150 }
142 }
151 if (has_meaningful_mtime) {
143 if (has_meaningful_mtime) {
152 t->flags |= dirstate_flag_has_mtime;
144 t->flags |= dirstate_flag_has_mtime;
153 t->mtime_s = mtime_s;
145 t->mtime_s = mtime_s;
154 t->mtime_ns = mtime_ns;
146 t->mtime_ns = mtime_ns;
155 } else {
147 } else {
156 t->mtime_s = 0;
148 t->mtime_s = 0;
157 t->mtime_ns = 0;
149 t->mtime_ns = 0;
158 }
150 }
159 return (PyObject *)t;
151 return (PyObject *)t;
160 }
152 }
161
153
162 static void dirstate_item_dealloc(PyObject *o)
154 static void dirstate_item_dealloc(PyObject *o)
163 {
155 {
164 PyObject_Del(o);
156 PyObject_Del(o);
165 }
157 }
166
158
167 static inline bool dirstate_item_c_tracked(dirstateItemObject *self)
159 static inline bool dirstate_item_c_tracked(dirstateItemObject *self)
168 {
160 {
169 return (self->flags & dirstate_flag_wc_tracked);
161 return (self->flags & dirstate_flag_wc_tracked);
170 }
162 }
171
163
172 static inline bool dirstate_item_c_any_tracked(dirstateItemObject *self)
164 static inline bool dirstate_item_c_any_tracked(dirstateItemObject *self)
173 {
165 {
174 const int mask = dirstate_flag_wc_tracked | dirstate_flag_p1_tracked |
166 const int mask = dirstate_flag_wc_tracked | dirstate_flag_p1_tracked |
175 dirstate_flag_p2_info;
167 dirstate_flag_p2_info;
176 return (self->flags & mask);
168 return (self->flags & mask);
177 }
169 }
178
170
179 static inline bool dirstate_item_c_added(dirstateItemObject *self)
171 static inline bool dirstate_item_c_added(dirstateItemObject *self)
180 {
172 {
181 const int mask = (dirstate_flag_wc_tracked | dirstate_flag_p1_tracked |
173 const int mask = (dirstate_flag_wc_tracked | dirstate_flag_p1_tracked |
182 dirstate_flag_p2_info);
174 dirstate_flag_p2_info);
183 const int target = dirstate_flag_wc_tracked;
175 const int target = dirstate_flag_wc_tracked;
184 return (self->flags & mask) == target;
176 return (self->flags & mask) == target;
185 }
177 }
186
178
187 static inline bool dirstate_item_c_removed(dirstateItemObject *self)
179 static inline bool dirstate_item_c_removed(dirstateItemObject *self)
188 {
180 {
189 if (self->flags & dirstate_flag_wc_tracked) {
181 if (self->flags & dirstate_flag_wc_tracked) {
190 return false;
182 return false;
191 }
183 }
192 return (self->flags &
184 return (self->flags &
193 (dirstate_flag_p1_tracked | dirstate_flag_p2_info));
185 (dirstate_flag_p1_tracked | dirstate_flag_p2_info));
194 }
186 }
195
187
196 static inline bool dirstate_item_c_merged(dirstateItemObject *self)
188 static inline bool dirstate_item_c_merged(dirstateItemObject *self)
197 {
189 {
198 return ((self->flags & dirstate_flag_wc_tracked) &&
190 return ((self->flags & dirstate_flag_wc_tracked) &&
199 (self->flags & dirstate_flag_p1_tracked) &&
191 (self->flags & dirstate_flag_p1_tracked) &&
200 (self->flags & dirstate_flag_p2_info));
192 (self->flags & dirstate_flag_p2_info));
201 }
193 }
202
194
203 static inline bool dirstate_item_c_from_p2(dirstateItemObject *self)
195 static inline bool dirstate_item_c_from_p2(dirstateItemObject *self)
204 {
196 {
205 return ((self->flags & dirstate_flag_wc_tracked) &&
197 return ((self->flags & dirstate_flag_wc_tracked) &&
206 !(self->flags & dirstate_flag_p1_tracked) &&
198 !(self->flags & dirstate_flag_p1_tracked) &&
207 (self->flags & dirstate_flag_p2_info));
199 (self->flags & dirstate_flag_p2_info));
208 }
200 }
209
201
210 static inline char dirstate_item_c_v1_state(dirstateItemObject *self)
202 static inline char dirstate_item_c_v1_state(dirstateItemObject *self)
211 {
203 {
212 if (dirstate_item_c_removed(self)) {
204 if (dirstate_item_c_removed(self)) {
213 return 'r';
205 return 'r';
214 } else if (dirstate_item_c_merged(self)) {
206 } else if (dirstate_item_c_merged(self)) {
215 return 'm';
207 return 'm';
216 } else if (dirstate_item_c_added(self)) {
208 } else if (dirstate_item_c_added(self)) {
217 return 'a';
209 return 'a';
218 } else {
210 } else {
219 return 'n';
211 return 'n';
220 }
212 }
221 }
213 }
222
214
223 static inline bool dirstate_item_c_has_fallback_exec(dirstateItemObject *self)
215 static inline bool dirstate_item_c_has_fallback_exec(dirstateItemObject *self)
224 {
216 {
225 return (bool)self->flags & dirstate_flag_has_fallback_exec;
217 return (bool)self->flags & dirstate_flag_has_fallback_exec;
226 }
218 }
227
219
228 static inline bool
220 static inline bool
229 dirstate_item_c_has_fallback_symlink(dirstateItemObject *self)
221 dirstate_item_c_has_fallback_symlink(dirstateItemObject *self)
230 {
222 {
231 return (bool)self->flags & dirstate_flag_has_fallback_symlink;
223 return (bool)self->flags & dirstate_flag_has_fallback_symlink;
232 }
224 }
233
225
234 static inline int dirstate_item_c_v1_mode(dirstateItemObject *self)
226 static inline int dirstate_item_c_v1_mode(dirstateItemObject *self)
235 {
227 {
236 if (self->flags & dirstate_flag_has_meaningful_data) {
228 if (self->flags & dirstate_flag_has_meaningful_data) {
237 return self->mode;
229 return self->mode;
238 } else {
230 } else {
239 return 0;
231 return 0;
240 }
232 }
241 }
233 }
242
234
243 static inline int dirstate_item_c_v1_size(dirstateItemObject *self)
235 static inline int dirstate_item_c_v1_size(dirstateItemObject *self)
244 {
236 {
245 if (!(self->flags & dirstate_flag_wc_tracked) &&
237 if (!(self->flags & dirstate_flag_wc_tracked) &&
246 (self->flags & dirstate_flag_p2_info)) {
238 (self->flags & dirstate_flag_p2_info)) {
247 if (self->flags & dirstate_flag_p1_tracked) {
239 if (self->flags & dirstate_flag_p1_tracked) {
248 return dirstate_v1_nonnormal;
240 return dirstate_v1_nonnormal;
249 } else {
241 } else {
250 return dirstate_v1_from_p2;
242 return dirstate_v1_from_p2;
251 }
243 }
252 } else if (dirstate_item_c_removed(self)) {
244 } else if (dirstate_item_c_removed(self)) {
253 return 0;
245 return 0;
254 } else if (self->flags & dirstate_flag_p2_info) {
246 } else if (self->flags & dirstate_flag_p2_info) {
255 return dirstate_v1_from_p2;
247 return dirstate_v1_from_p2;
256 } else if (dirstate_item_c_added(self)) {
248 } else if (dirstate_item_c_added(self)) {
257 return dirstate_v1_nonnormal;
249 return dirstate_v1_nonnormal;
258 } else if (self->flags & dirstate_flag_has_meaningful_data) {
250 } else if (self->flags & dirstate_flag_has_meaningful_data) {
259 return self->size;
251 return self->size;
260 } else {
252 } else {
261 return dirstate_v1_nonnormal;
253 return dirstate_v1_nonnormal;
262 }
254 }
263 }
255 }
264
256
265 static inline int dirstate_item_c_v1_mtime(dirstateItemObject *self)
257 static inline int dirstate_item_c_v1_mtime(dirstateItemObject *self)
266 {
258 {
267 if (dirstate_item_c_removed(self)) {
259 if (dirstate_item_c_removed(self)) {
268 return 0;
260 return 0;
269 } else if (!(self->flags & dirstate_flag_has_mtime) ||
261 } else if (!(self->flags & dirstate_flag_has_mtime) ||
270 !(self->flags & dirstate_flag_p1_tracked) ||
262 !(self->flags & dirstate_flag_p1_tracked) ||
271 !(self->flags & dirstate_flag_wc_tracked) ||
263 !(self->flags & dirstate_flag_wc_tracked) ||
272 (self->flags & dirstate_flag_p2_info) ||
264 (self->flags & dirstate_flag_p2_info) ||
273 (self->flags & dirstate_flag_mtime_second_ambiguous)) {
265 (self->flags & dirstate_flag_mtime_second_ambiguous)) {
274 return ambiguous_time;
266 return ambiguous_time;
275 } else {
267 } else {
276 return self->mtime_s;
268 return self->mtime_s;
277 }
269 }
278 }
270 }
279
271
280 static PyObject *dirstate_item_v2_data(dirstateItemObject *self)
272 static PyObject *dirstate_item_v2_data(dirstateItemObject *self)
281 {
273 {
282 int flags = self->flags;
274 int flags = self->flags;
283 int mode = dirstate_item_c_v1_mode(self);
275 int mode = dirstate_item_c_v1_mode(self);
284 #ifdef S_IXUSR
276 #ifdef S_IXUSR
285 /* This is for platforms with an exec bit */
277 /* This is for platforms with an exec bit */
286 if ((mode & S_IXUSR) != 0) {
278 if ((mode & S_IXUSR) != 0) {
287 flags |= dirstate_flag_mode_exec_perm;
279 flags |= dirstate_flag_mode_exec_perm;
288 } else {
280 } else {
289 flags &= ~dirstate_flag_mode_exec_perm;
281 flags &= ~dirstate_flag_mode_exec_perm;
290 }
282 }
291 #else
283 #else
292 flags &= ~dirstate_flag_mode_exec_perm;
284 flags &= ~dirstate_flag_mode_exec_perm;
293 #endif
285 #endif
294 #ifdef S_ISLNK
286 #ifdef S_ISLNK
295 /* This is for platforms with support for symlinks */
287 /* This is for platforms with support for symlinks */
296 if (S_ISLNK(mode)) {
288 if (S_ISLNK(mode)) {
297 flags |= dirstate_flag_mode_is_symlink;
289 flags |= dirstate_flag_mode_is_symlink;
298 } else {
290 } else {
299 flags &= ~dirstate_flag_mode_is_symlink;
291 flags &= ~dirstate_flag_mode_is_symlink;
300 }
292 }
301 #else
293 #else
302 flags &= ~dirstate_flag_mode_is_symlink;
294 flags &= ~dirstate_flag_mode_is_symlink;
303 #endif
295 #endif
304 return Py_BuildValue("iiii", flags, self->size, self->mtime_s,
296 return Py_BuildValue("iiii", flags, self->size, self->mtime_s,
305 self->mtime_ns);
297 self->mtime_ns);
306 };
298 };
307
299
308 static PyObject *dirstate_item_v1_state(dirstateItemObject *self)
300 static PyObject *dirstate_item_v1_state(dirstateItemObject *self)
309 {
301 {
310 char state = dirstate_item_c_v1_state(self);
302 char state = dirstate_item_c_v1_state(self);
311 return PyBytes_FromStringAndSize(&state, 1);
303 return PyBytes_FromStringAndSize(&state, 1);
312 };
304 };
313
305
314 static PyObject *dirstate_item_v1_mode(dirstateItemObject *self)
306 static PyObject *dirstate_item_v1_mode(dirstateItemObject *self)
315 {
307 {
316 return PyInt_FromLong(dirstate_item_c_v1_mode(self));
308 return PyInt_FromLong(dirstate_item_c_v1_mode(self));
317 };
309 };
318
310
319 static PyObject *dirstate_item_v1_size(dirstateItemObject *self)
311 static PyObject *dirstate_item_v1_size(dirstateItemObject *self)
320 {
312 {
321 return PyInt_FromLong(dirstate_item_c_v1_size(self));
313 return PyInt_FromLong(dirstate_item_c_v1_size(self));
322 };
314 };
323
315
324 static PyObject *dirstate_item_v1_mtime(dirstateItemObject *self)
316 static PyObject *dirstate_item_v1_mtime(dirstateItemObject *self)
325 {
317 {
326 return PyInt_FromLong(dirstate_item_c_v1_mtime(self));
318 return PyInt_FromLong(dirstate_item_c_v1_mtime(self));
327 };
319 };
328
320
329 static PyObject *dirstate_item_mtime_likely_equal_to(dirstateItemObject *self,
321 static PyObject *dirstate_item_mtime_likely_equal_to(dirstateItemObject *self,
330 PyObject *other)
322 PyObject *other)
331 {
323 {
332 int other_s;
324 int other_s;
333 int other_ns;
325 int other_ns;
334 int other_second_ambiguous;
326 int other_second_ambiguous;
335 if (!PyArg_ParseTuple(other, "iii", &other_s, &other_ns,
327 if (!PyArg_ParseTuple(other, "iii", &other_s, &other_ns,
336 &other_second_ambiguous)) {
328 &other_second_ambiguous)) {
337 return NULL;
329 return NULL;
338 }
330 }
339 if (!(self->flags & dirstate_flag_has_mtime)) {
331 if (!(self->flags & dirstate_flag_has_mtime)) {
340 Py_RETURN_FALSE;
332 Py_RETURN_FALSE;
341 }
333 }
342 if (self->mtime_s != other_s) {
334 if (self->mtime_s != other_s) {
343 Py_RETURN_FALSE;
335 Py_RETURN_FALSE;
344 }
336 }
345 if (self->mtime_ns == 0 || other_ns == 0) {
337 if (self->mtime_ns == 0 || other_ns == 0) {
346 if (self->flags & dirstate_flag_mtime_second_ambiguous) {
338 if (self->flags & dirstate_flag_mtime_second_ambiguous) {
347 Py_RETURN_FALSE;
339 Py_RETURN_FALSE;
348 } else {
340 } else {
349 Py_RETURN_TRUE;
341 Py_RETURN_TRUE;
350 }
342 }
351 }
343 }
352 if (self->mtime_ns == other_ns) {
344 if (self->mtime_ns == other_ns) {
353 Py_RETURN_TRUE;
345 Py_RETURN_TRUE;
354 } else {
346 } else {
355 Py_RETURN_FALSE;
347 Py_RETURN_FALSE;
356 }
348 }
357 };
349 };
358
350
359 /* This will never change since it's bound to V1
351 /* This will never change since it's bound to V1
360 */
352 */
361 static inline dirstateItemObject *
353 static inline dirstateItemObject *
362 dirstate_item_from_v1_data(char state, int mode, int size, int mtime)
354 dirstate_item_from_v1_data(char state, int mode, int size, int mtime)
363 {
355 {
364 dirstateItemObject *t =
356 dirstateItemObject *t =
365 PyObject_New(dirstateItemObject, &dirstateItemType);
357 PyObject_New(dirstateItemObject, &dirstateItemType);
366 if (!t) {
358 if (!t) {
367 return NULL;
359 return NULL;
368 }
360 }
369 t->flags = 0;
361 t->flags = 0;
370 t->mode = 0;
362 t->mode = 0;
371 t->size = 0;
363 t->size = 0;
372 t->mtime_s = 0;
364 t->mtime_s = 0;
373 t->mtime_ns = 0;
365 t->mtime_ns = 0;
374
366
375 if (state == 'm') {
367 if (state == 'm') {
376 t->flags = (dirstate_flag_wc_tracked |
368 t->flags = (dirstate_flag_wc_tracked |
377 dirstate_flag_p1_tracked | dirstate_flag_p2_info);
369 dirstate_flag_p1_tracked | dirstate_flag_p2_info);
378 } else if (state == 'a') {
370 } else if (state == 'a') {
379 t->flags = dirstate_flag_wc_tracked;
371 t->flags = dirstate_flag_wc_tracked;
380 } else if (state == 'r') {
372 } else if (state == 'r') {
381 if (size == dirstate_v1_nonnormal) {
373 if (size == dirstate_v1_nonnormal) {
382 t->flags =
374 t->flags =
383 dirstate_flag_p1_tracked | dirstate_flag_p2_info;
375 dirstate_flag_p1_tracked | dirstate_flag_p2_info;
384 } else if (size == dirstate_v1_from_p2) {
376 } else if (size == dirstate_v1_from_p2) {
385 t->flags = dirstate_flag_p2_info;
377 t->flags = dirstate_flag_p2_info;
386 } else {
378 } else {
387 t->flags = dirstate_flag_p1_tracked;
379 t->flags = dirstate_flag_p1_tracked;
388 }
380 }
389 } else if (state == 'n') {
381 } else if (state == 'n') {
390 if (size == dirstate_v1_from_p2) {
382 if (size == dirstate_v1_from_p2) {
391 t->flags =
383 t->flags =
392 dirstate_flag_wc_tracked | dirstate_flag_p2_info;
384 dirstate_flag_wc_tracked | dirstate_flag_p2_info;
393 } else if (size == dirstate_v1_nonnormal) {
385 } else if (size == dirstate_v1_nonnormal) {
394 t->flags =
386 t->flags =
395 dirstate_flag_wc_tracked | dirstate_flag_p1_tracked;
387 dirstate_flag_wc_tracked | dirstate_flag_p1_tracked;
396 } else if (mtime == ambiguous_time) {
388 } else if (mtime == ambiguous_time) {
397 t->flags = (dirstate_flag_wc_tracked |
389 t->flags = (dirstate_flag_wc_tracked |
398 dirstate_flag_p1_tracked |
390 dirstate_flag_p1_tracked |
399 dirstate_flag_has_meaningful_data);
391 dirstate_flag_has_meaningful_data);
400 t->mode = mode;
392 t->mode = mode;
401 t->size = size;
393 t->size = size;
402 } else {
394 } else {
403 t->flags = (dirstate_flag_wc_tracked |
395 t->flags = (dirstate_flag_wc_tracked |
404 dirstate_flag_p1_tracked |
396 dirstate_flag_p1_tracked |
405 dirstate_flag_has_meaningful_data |
397 dirstate_flag_has_meaningful_data |
406 dirstate_flag_has_mtime);
398 dirstate_flag_has_mtime);
407 t->mode = mode;
399 t->mode = mode;
408 t->size = size;
400 t->size = size;
409 t->mtime_s = mtime;
401 t->mtime_s = mtime;
410 }
402 }
411 } else {
403 } else {
412 PyErr_Format(PyExc_RuntimeError,
404 PyErr_Format(PyExc_RuntimeError,
413 "unknown state: `%c` (%d, %d, %d)", state, mode,
405 "unknown state: `%c` (%d, %d, %d)", state, mode,
414 size, mtime, NULL);
406 size, mtime, NULL);
415 Py_DECREF(t);
407 Py_DECREF(t);
416 return NULL;
408 return NULL;
417 }
409 }
418
410
419 return t;
411 return t;
420 }
412 }
421
413
422 /* This will never change since it's bound to V1, unlike `dirstate_item_new` */
414 /* This will never change since it's bound to V1, unlike `dirstate_item_new` */
423 static PyObject *dirstate_item_from_v1_meth(PyTypeObject *subtype,
415 static PyObject *dirstate_item_from_v1_meth(PyTypeObject *subtype,
424 PyObject *args)
416 PyObject *args)
425 {
417 {
426 /* We do all the initialization here and not a tp_init function because
418 /* We do all the initialization here and not a tp_init function because
427 * dirstate_item is immutable. */
419 * dirstate_item is immutable. */
428 char state;
420 char state;
429 int size, mode, mtime;
421 int size, mode, mtime;
430 if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime)) {
422 if (!PyArg_ParseTuple(args, "ciii", &state, &mode, &size, &mtime)) {
431 return NULL;
423 return NULL;
432 }
424 }
433 return (PyObject *)dirstate_item_from_v1_data(state, mode, size, mtime);
425 return (PyObject *)dirstate_item_from_v1_data(state, mode, size, mtime);
434 };
426 };
435
427
436 static PyObject *dirstate_item_from_v2_meth(PyTypeObject *subtype,
428 static PyObject *dirstate_item_from_v2_meth(PyTypeObject *subtype,
437 PyObject *args)
429 PyObject *args)
438 {
430 {
439 dirstateItemObject *t =
431 dirstateItemObject *t =
440 PyObject_New(dirstateItemObject, &dirstateItemType);
432 PyObject_New(dirstateItemObject, &dirstateItemType);
441 if (!t) {
433 if (!t) {
442 return NULL;
434 return NULL;
443 }
435 }
444 if (!PyArg_ParseTuple(args, "iiii", &t->flags, &t->size, &t->mtime_s,
436 if (!PyArg_ParseTuple(args, "iiii", &t->flags, &t->size, &t->mtime_s,
445 &t->mtime_ns)) {
437 &t->mtime_ns)) {
446 return NULL;
438 return NULL;
447 }
439 }
448 if (t->flags & dirstate_flag_expected_state_is_modified) {
440 if (t->flags & dirstate_flag_expected_state_is_modified) {
449 t->flags &= ~(dirstate_flag_expected_state_is_modified |
441 t->flags &= ~(dirstate_flag_expected_state_is_modified |
450 dirstate_flag_has_meaningful_data |
442 dirstate_flag_has_meaningful_data |
451 dirstate_flag_has_mtime);
443 dirstate_flag_has_mtime);
452 }
444 }
453 t->mode = 0;
445 t->mode = 0;
454 if (t->flags & dirstate_flag_has_meaningful_data) {
446 if (t->flags & dirstate_flag_has_meaningful_data) {
455 if (t->flags & dirstate_flag_mode_exec_perm) {
447 if (t->flags & dirstate_flag_mode_exec_perm) {
456 t->mode = 0755;
448 t->mode = 0755;
457 } else {
449 } else {
458 t->mode = 0644;
450 t->mode = 0644;
459 }
451 }
460 if (t->flags & dirstate_flag_mode_is_symlink) {
452 if (t->flags & dirstate_flag_mode_is_symlink) {
461 t->mode |= S_IFLNK;
453 t->mode |= S_IFLNK;
462 } else {
454 } else {
463 t->mode |= S_IFREG;
455 t->mode |= S_IFREG;
464 }
456 }
465 }
457 }
466 return (PyObject *)t;
458 return (PyObject *)t;
467 };
459 };
468
460
469 /* This means the next status call will have to actually check its content
461 /* This means the next status call will have to actually check its content
470 to make sure it is correct. */
462 to make sure it is correct. */
471 static PyObject *dirstate_item_set_possibly_dirty(dirstateItemObject *self)
463 static PyObject *dirstate_item_set_possibly_dirty(dirstateItemObject *self)
472 {
464 {
473 self->flags &= ~dirstate_flag_has_mtime;
465 self->flags &= ~dirstate_flag_has_mtime;
474 Py_RETURN_NONE;
466 Py_RETURN_NONE;
475 }
467 }
476
468
477 /* See docstring of the python implementation for details */
469 /* See docstring of the python implementation for details */
478 static PyObject *dirstate_item_set_clean(dirstateItemObject *self,
470 static PyObject *dirstate_item_set_clean(dirstateItemObject *self,
479 PyObject *args)
471 PyObject *args)
480 {
472 {
481 int size, mode, mtime_s, mtime_ns, mtime_second_ambiguous;
473 int size, mode, mtime_s, mtime_ns, mtime_second_ambiguous;
482 PyObject *mtime;
474 PyObject *mtime;
483 mtime_s = 0;
475 mtime_s = 0;
484 mtime_ns = 0;
476 mtime_ns = 0;
485 mtime_second_ambiguous = 0;
477 mtime_second_ambiguous = 0;
486 if (!PyArg_ParseTuple(args, "iiO", &mode, &size, &mtime)) {
478 if (!PyArg_ParseTuple(args, "iiO", &mode, &size, &mtime)) {
487 return NULL;
479 return NULL;
488 }
480 }
489 if (mtime != Py_None) {
481 if (mtime != Py_None) {
490 if (!PyArg_ParseTuple(mtime, "iii", &mtime_s, &mtime_ns,
482 if (!PyArg_ParseTuple(mtime, "iii", &mtime_s, &mtime_ns,
491 &mtime_second_ambiguous)) {
483 &mtime_second_ambiguous)) {
492 return NULL;
484 return NULL;
493 }
485 }
494 } else {
486 } else {
495 self->flags &= ~dirstate_flag_has_mtime;
487 self->flags &= ~dirstate_flag_has_mtime;
496 }
488 }
497 self->flags = dirstate_flag_wc_tracked | dirstate_flag_p1_tracked |
489 self->flags = dirstate_flag_wc_tracked | dirstate_flag_p1_tracked |
498 dirstate_flag_has_meaningful_data |
490 dirstate_flag_has_meaningful_data |
499 dirstate_flag_has_mtime;
491 dirstate_flag_has_mtime;
500 if (mtime_second_ambiguous) {
492 if (mtime_second_ambiguous) {
501 self->flags |= dirstate_flag_mtime_second_ambiguous;
493 self->flags |= dirstate_flag_mtime_second_ambiguous;
502 }
494 }
503 self->mode = mode;
495 self->mode = mode;
504 self->size = size;
496 self->size = size;
505 self->mtime_s = mtime_s;
497 self->mtime_s = mtime_s;
506 self->mtime_ns = mtime_ns;
498 self->mtime_ns = mtime_ns;
507 Py_RETURN_NONE;
499 Py_RETURN_NONE;
508 }
500 }
509
501
510 static PyObject *dirstate_item_set_tracked(dirstateItemObject *self)
502 static PyObject *dirstate_item_set_tracked(dirstateItemObject *self)
511 {
503 {
512 self->flags |= dirstate_flag_wc_tracked;
504 self->flags |= dirstate_flag_wc_tracked;
513 self->flags &= ~dirstate_flag_has_mtime;
505 self->flags &= ~dirstate_flag_has_mtime;
514 Py_RETURN_NONE;
506 Py_RETURN_NONE;
515 }
507 }
516
508
517 static PyObject *dirstate_item_set_untracked(dirstateItemObject *self)
509 static PyObject *dirstate_item_set_untracked(dirstateItemObject *self)
518 {
510 {
519 self->flags &= ~dirstate_flag_wc_tracked;
511 self->flags &= ~dirstate_flag_wc_tracked;
520 self->mode = 0;
512 self->mode = 0;
521 self->size = 0;
513 self->size = 0;
522 self->mtime_s = 0;
514 self->mtime_s = 0;
523 self->mtime_ns = 0;
515 self->mtime_ns = 0;
524 Py_RETURN_NONE;
516 Py_RETURN_NONE;
525 }
517 }
526
518
527 static PyObject *dirstate_item_drop_merge_data(dirstateItemObject *self)
519 static PyObject *dirstate_item_drop_merge_data(dirstateItemObject *self)
528 {
520 {
529 if (self->flags & dirstate_flag_p2_info) {
521 if (self->flags & dirstate_flag_p2_info) {
530 self->flags &= ~(dirstate_flag_p2_info |
522 self->flags &= ~(dirstate_flag_p2_info |
531 dirstate_flag_has_meaningful_data |
523 dirstate_flag_has_meaningful_data |
532 dirstate_flag_has_mtime);
524 dirstate_flag_has_mtime);
533 self->mode = 0;
525 self->mode = 0;
534 self->size = 0;
526 self->size = 0;
535 self->mtime_s = 0;
527 self->mtime_s = 0;
536 self->mtime_ns = 0;
528 self->mtime_ns = 0;
537 }
529 }
538 Py_RETURN_NONE;
530 Py_RETURN_NONE;
539 }
531 }
540 static PyMethodDef dirstate_item_methods[] = {
532 static PyMethodDef dirstate_item_methods[] = {
541 {"v2_data", (PyCFunction)dirstate_item_v2_data, METH_NOARGS,
533 {"v2_data", (PyCFunction)dirstate_item_v2_data, METH_NOARGS,
542 "return data suitable for v2 serialization"},
534 "return data suitable for v2 serialization"},
543 {"v1_state", (PyCFunction)dirstate_item_v1_state, METH_NOARGS,
535 {"v1_state", (PyCFunction)dirstate_item_v1_state, METH_NOARGS,
544 "return a \"state\" suitable for v1 serialization"},
536 "return a \"state\" suitable for v1 serialization"},
545 {"v1_mode", (PyCFunction)dirstate_item_v1_mode, METH_NOARGS,
537 {"v1_mode", (PyCFunction)dirstate_item_v1_mode, METH_NOARGS,
546 "return a \"mode\" suitable for v1 serialization"},
538 "return a \"mode\" suitable for v1 serialization"},
547 {"v1_size", (PyCFunction)dirstate_item_v1_size, METH_NOARGS,
539 {"v1_size", (PyCFunction)dirstate_item_v1_size, METH_NOARGS,
548 "return a \"size\" suitable for v1 serialization"},
540 "return a \"size\" suitable for v1 serialization"},
549 {"v1_mtime", (PyCFunction)dirstate_item_v1_mtime, METH_NOARGS,
541 {"v1_mtime", (PyCFunction)dirstate_item_v1_mtime, METH_NOARGS,
550 "return a \"mtime\" suitable for v1 serialization"},
542 "return a \"mtime\" suitable for v1 serialization"},
551 {"mtime_likely_equal_to", (PyCFunction)dirstate_item_mtime_likely_equal_to,
543 {"mtime_likely_equal_to", (PyCFunction)dirstate_item_mtime_likely_equal_to,
552 METH_O, "True if the stored mtime is likely equal to the given mtime"},
544 METH_O, "True if the stored mtime is likely equal to the given mtime"},
553 {"from_v1_data", (PyCFunction)dirstate_item_from_v1_meth,
545 {"from_v1_data", (PyCFunction)dirstate_item_from_v1_meth,
554 METH_VARARGS | METH_CLASS, "build a new DirstateItem object from V1 data"},
546 METH_VARARGS | METH_CLASS, "build a new DirstateItem object from V1 data"},
555 {"from_v2_data", (PyCFunction)dirstate_item_from_v2_meth,
547 {"from_v2_data", (PyCFunction)dirstate_item_from_v2_meth,
556 METH_VARARGS | METH_CLASS, "build a new DirstateItem object from V2 data"},
548 METH_VARARGS | METH_CLASS, "build a new DirstateItem object from V2 data"},
557 {"set_possibly_dirty", (PyCFunction)dirstate_item_set_possibly_dirty,
549 {"set_possibly_dirty", (PyCFunction)dirstate_item_set_possibly_dirty,
558 METH_NOARGS, "mark a file as \"possibly dirty\""},
550 METH_NOARGS, "mark a file as \"possibly dirty\""},
559 {"set_clean", (PyCFunction)dirstate_item_set_clean, METH_VARARGS,
551 {"set_clean", (PyCFunction)dirstate_item_set_clean, METH_VARARGS,
560 "mark a file as \"clean\""},
552 "mark a file as \"clean\""},
561 {"set_tracked", (PyCFunction)dirstate_item_set_tracked, METH_NOARGS,
553 {"set_tracked", (PyCFunction)dirstate_item_set_tracked, METH_NOARGS,
562 "mark a file as \"tracked\""},
554 "mark a file as \"tracked\""},
563 {"set_untracked", (PyCFunction)dirstate_item_set_untracked, METH_NOARGS,
555 {"set_untracked", (PyCFunction)dirstate_item_set_untracked, METH_NOARGS,
564 "mark a file as \"untracked\""},
556 "mark a file as \"untracked\""},
565 {"drop_merge_data", (PyCFunction)dirstate_item_drop_merge_data, METH_NOARGS,
557 {"drop_merge_data", (PyCFunction)dirstate_item_drop_merge_data, METH_NOARGS,
566 "remove all \"merge-only\" from a DirstateItem"},
558 "remove all \"merge-only\" from a DirstateItem"},
567 {NULL} /* Sentinel */
559 {NULL} /* Sentinel */
568 };
560 };
569
561
570 static PyObject *dirstate_item_get_mode(dirstateItemObject *self)
562 static PyObject *dirstate_item_get_mode(dirstateItemObject *self)
571 {
563 {
572 return PyInt_FromLong(dirstate_item_c_v1_mode(self));
564 return PyInt_FromLong(dirstate_item_c_v1_mode(self));
573 };
565 };
574
566
575 static PyObject *dirstate_item_get_size(dirstateItemObject *self)
567 static PyObject *dirstate_item_get_size(dirstateItemObject *self)
576 {
568 {
577 return PyInt_FromLong(dirstate_item_c_v1_size(self));
569 return PyInt_FromLong(dirstate_item_c_v1_size(self));
578 };
570 };
579
571
580 static PyObject *dirstate_item_get_mtime(dirstateItemObject *self)
572 static PyObject *dirstate_item_get_mtime(dirstateItemObject *self)
581 {
573 {
582 return PyInt_FromLong(dirstate_item_c_v1_mtime(self));
574 return PyInt_FromLong(dirstate_item_c_v1_mtime(self));
583 };
575 };
584
576
585 static PyObject *dirstate_item_get_state(dirstateItemObject *self)
577 static PyObject *dirstate_item_get_state(dirstateItemObject *self)
586 {
578 {
587 char state = dirstate_item_c_v1_state(self);
579 char state = dirstate_item_c_v1_state(self);
588 return PyBytes_FromStringAndSize(&state, 1);
580 return PyBytes_FromStringAndSize(&state, 1);
589 };
581 };
590
582
591 static PyObject *dirstate_item_get_has_fallback_exec(dirstateItemObject *self)
583 static PyObject *dirstate_item_get_has_fallback_exec(dirstateItemObject *self)
592 {
584 {
593 if (dirstate_item_c_has_fallback_exec(self)) {
585 if (dirstate_item_c_has_fallback_exec(self)) {
594 Py_RETURN_TRUE;
586 Py_RETURN_TRUE;
595 } else {
587 } else {
596 Py_RETURN_FALSE;
588 Py_RETURN_FALSE;
597 }
589 }
598 };
590 };
599
591
600 static PyObject *dirstate_item_get_fallback_exec(dirstateItemObject *self)
592 static PyObject *dirstate_item_get_fallback_exec(dirstateItemObject *self)
601 {
593 {
602 if (dirstate_item_c_has_fallback_exec(self)) {
594 if (dirstate_item_c_has_fallback_exec(self)) {
603 if (self->flags & dirstate_flag_fallback_exec) {
595 if (self->flags & dirstate_flag_fallback_exec) {
604 Py_RETURN_TRUE;
596 Py_RETURN_TRUE;
605 } else {
597 } else {
606 Py_RETURN_FALSE;
598 Py_RETURN_FALSE;
607 }
599 }
608 } else {
600 } else {
609 Py_RETURN_NONE;
601 Py_RETURN_NONE;
610 }
602 }
611 };
603 };
612
604
613 static int dirstate_item_set_fallback_exec(dirstateItemObject *self,
605 static int dirstate_item_set_fallback_exec(dirstateItemObject *self,
614 PyObject *value)
606 PyObject *value)
615 {
607 {
616 if ((value == Py_None) || (value == NULL)) {
608 if ((value == Py_None) || (value == NULL)) {
617 self->flags &= ~dirstate_flag_has_fallback_exec;
609 self->flags &= ~dirstate_flag_has_fallback_exec;
618 } else {
610 } else {
619 self->flags |= dirstate_flag_has_fallback_exec;
611 self->flags |= dirstate_flag_has_fallback_exec;
620 if (PyObject_IsTrue(value)) {
612 if (PyObject_IsTrue(value)) {
621 self->flags |= dirstate_flag_fallback_exec;
613 self->flags |= dirstate_flag_fallback_exec;
622 } else {
614 } else {
623 self->flags &= ~dirstate_flag_fallback_exec;
615 self->flags &= ~dirstate_flag_fallback_exec;
624 }
616 }
625 }
617 }
626 return 0;
618 return 0;
627 };
619 };
628
620
629 static PyObject *
621 static PyObject *
630 dirstate_item_get_has_fallback_symlink(dirstateItemObject *self)
622 dirstate_item_get_has_fallback_symlink(dirstateItemObject *self)
631 {
623 {
632 if (dirstate_item_c_has_fallback_symlink(self)) {
624 if (dirstate_item_c_has_fallback_symlink(self)) {
633 Py_RETURN_TRUE;
625 Py_RETURN_TRUE;
634 } else {
626 } else {
635 Py_RETURN_FALSE;
627 Py_RETURN_FALSE;
636 }
628 }
637 };
629 };
638
630
639 static PyObject *dirstate_item_get_fallback_symlink(dirstateItemObject *self)
631 static PyObject *dirstate_item_get_fallback_symlink(dirstateItemObject *self)
640 {
632 {
641 if (dirstate_item_c_has_fallback_symlink(self)) {
633 if (dirstate_item_c_has_fallback_symlink(self)) {
642 if (self->flags & dirstate_flag_fallback_symlink) {
634 if (self->flags & dirstate_flag_fallback_symlink) {
643 Py_RETURN_TRUE;
635 Py_RETURN_TRUE;
644 } else {
636 } else {
645 Py_RETURN_FALSE;
637 Py_RETURN_FALSE;
646 }
638 }
647 } else {
639 } else {
648 Py_RETURN_NONE;
640 Py_RETURN_NONE;
649 }
641 }
650 };
642 };
651
643
652 static int dirstate_item_set_fallback_symlink(dirstateItemObject *self,
644 static int dirstate_item_set_fallback_symlink(dirstateItemObject *self,
653 PyObject *value)
645 PyObject *value)
654 {
646 {
655 if ((value == Py_None) || (value == NULL)) {
647 if ((value == Py_None) || (value == NULL)) {
656 self->flags &= ~dirstate_flag_has_fallback_symlink;
648 self->flags &= ~dirstate_flag_has_fallback_symlink;
657 } else {
649 } else {
658 self->flags |= dirstate_flag_has_fallback_symlink;
650 self->flags |= dirstate_flag_has_fallback_symlink;
659 if (PyObject_IsTrue(value)) {
651 if (PyObject_IsTrue(value)) {
660 self->flags |= dirstate_flag_fallback_symlink;
652 self->flags |= dirstate_flag_fallback_symlink;
661 } else {
653 } else {
662 self->flags &= ~dirstate_flag_fallback_symlink;
654 self->flags &= ~dirstate_flag_fallback_symlink;
663 }
655 }
664 }
656 }
665 return 0;
657 return 0;
666 };
658 };
667
659
668 static PyObject *dirstate_item_get_tracked(dirstateItemObject *self)
660 static PyObject *dirstate_item_get_tracked(dirstateItemObject *self)
669 {
661 {
670 if (dirstate_item_c_tracked(self)) {
662 if (dirstate_item_c_tracked(self)) {
671 Py_RETURN_TRUE;
663 Py_RETURN_TRUE;
672 } else {
664 } else {
673 Py_RETURN_FALSE;
665 Py_RETURN_FALSE;
674 }
666 }
675 };
667 };
676 static PyObject *dirstate_item_get_p1_tracked(dirstateItemObject *self)
668 static PyObject *dirstate_item_get_p1_tracked(dirstateItemObject *self)
677 {
669 {
678 if (self->flags & dirstate_flag_p1_tracked) {
670 if (self->flags & dirstate_flag_p1_tracked) {
679 Py_RETURN_TRUE;
671 Py_RETURN_TRUE;
680 } else {
672 } else {
681 Py_RETURN_FALSE;
673 Py_RETURN_FALSE;
682 }
674 }
683 };
675 };
684
676
685 static PyObject *dirstate_item_get_added(dirstateItemObject *self)
677 static PyObject *dirstate_item_get_added(dirstateItemObject *self)
686 {
678 {
687 if (dirstate_item_c_added(self)) {
679 if (dirstate_item_c_added(self)) {
688 Py_RETURN_TRUE;
680 Py_RETURN_TRUE;
689 } else {
681 } else {
690 Py_RETURN_FALSE;
682 Py_RETURN_FALSE;
691 }
683 }
692 };
684 };
693
685
694 static PyObject *dirstate_item_get_p2_info(dirstateItemObject *self)
686 static PyObject *dirstate_item_get_p2_info(dirstateItemObject *self)
695 {
687 {
696 if (self->flags & dirstate_flag_wc_tracked &&
688 if (self->flags & dirstate_flag_wc_tracked &&
697 self->flags & dirstate_flag_p2_info) {
689 self->flags & dirstate_flag_p2_info) {
698 Py_RETURN_TRUE;
690 Py_RETURN_TRUE;
699 } else {
691 } else {
700 Py_RETURN_FALSE;
692 Py_RETURN_FALSE;
701 }
693 }
702 };
694 };
703
695
704 static PyObject *dirstate_item_get_merged(dirstateItemObject *self)
696 static PyObject *dirstate_item_get_merged(dirstateItemObject *self)
705 {
697 {
706 if (dirstate_item_c_merged(self)) {
698 if (dirstate_item_c_merged(self)) {
707 Py_RETURN_TRUE;
699 Py_RETURN_TRUE;
708 } else {
700 } else {
709 Py_RETURN_FALSE;
701 Py_RETURN_FALSE;
710 }
702 }
711 };
703 };
712
704
713 static PyObject *dirstate_item_get_from_p2(dirstateItemObject *self)
705 static PyObject *dirstate_item_get_from_p2(dirstateItemObject *self)
714 {
706 {
715 if (dirstate_item_c_from_p2(self)) {
707 if (dirstate_item_c_from_p2(self)) {
716 Py_RETURN_TRUE;
708 Py_RETURN_TRUE;
717 } else {
709 } else {
718 Py_RETURN_FALSE;
710 Py_RETURN_FALSE;
719 }
711 }
720 };
712 };
721
713
722 static PyObject *dirstate_item_get_maybe_clean(dirstateItemObject *self)
714 static PyObject *dirstate_item_get_maybe_clean(dirstateItemObject *self)
723 {
715 {
724 if (!(self->flags & dirstate_flag_wc_tracked)) {
716 if (!(self->flags & dirstate_flag_wc_tracked)) {
725 Py_RETURN_FALSE;
717 Py_RETURN_FALSE;
726 } else if (!(self->flags & dirstate_flag_p1_tracked)) {
718 } else if (!(self->flags & dirstate_flag_p1_tracked)) {
727 Py_RETURN_FALSE;
719 Py_RETURN_FALSE;
728 } else if (self->flags & dirstate_flag_p2_info) {
720 } else if (self->flags & dirstate_flag_p2_info) {
729 Py_RETURN_FALSE;
721 Py_RETURN_FALSE;
730 } else {
722 } else {
731 Py_RETURN_TRUE;
723 Py_RETURN_TRUE;
732 }
724 }
733 };
725 };
734
726
735 static PyObject *dirstate_item_get_any_tracked(dirstateItemObject *self)
727 static PyObject *dirstate_item_get_any_tracked(dirstateItemObject *self)
736 {
728 {
737 if (dirstate_item_c_any_tracked(self)) {
729 if (dirstate_item_c_any_tracked(self)) {
738 Py_RETURN_TRUE;
730 Py_RETURN_TRUE;
739 } else {
731 } else {
740 Py_RETURN_FALSE;
732 Py_RETURN_FALSE;
741 }
733 }
742 };
734 };
743
735
744 static PyObject *dirstate_item_get_removed(dirstateItemObject *self)
736 static PyObject *dirstate_item_get_removed(dirstateItemObject *self)
745 {
737 {
746 if (dirstate_item_c_removed(self)) {
738 if (dirstate_item_c_removed(self)) {
747 Py_RETURN_TRUE;
739 Py_RETURN_TRUE;
748 } else {
740 } else {
749 Py_RETURN_FALSE;
741 Py_RETURN_FALSE;
750 }
742 }
751 };
743 };
752
744
753 static PyGetSetDef dirstate_item_getset[] = {
745 static PyGetSetDef dirstate_item_getset[] = {
754 {"mode", (getter)dirstate_item_get_mode, NULL, "mode", NULL},
746 {"mode", (getter)dirstate_item_get_mode, NULL, "mode", NULL},
755 {"size", (getter)dirstate_item_get_size, NULL, "size", NULL},
747 {"size", (getter)dirstate_item_get_size, NULL, "size", NULL},
756 {"mtime", (getter)dirstate_item_get_mtime, NULL, "mtime", NULL},
748 {"mtime", (getter)dirstate_item_get_mtime, NULL, "mtime", NULL},
757 {"state", (getter)dirstate_item_get_state, NULL, "state", NULL},
749 {"state", (getter)dirstate_item_get_state, NULL, "state", NULL},
758 {"has_fallback_exec", (getter)dirstate_item_get_has_fallback_exec, NULL,
750 {"has_fallback_exec", (getter)dirstate_item_get_has_fallback_exec, NULL,
759 "has_fallback_exec", NULL},
751 "has_fallback_exec", NULL},
760 {"fallback_exec", (getter)dirstate_item_get_fallback_exec,
752 {"fallback_exec", (getter)dirstate_item_get_fallback_exec,
761 (setter)dirstate_item_set_fallback_exec, "fallback_exec", NULL},
753 (setter)dirstate_item_set_fallback_exec, "fallback_exec", NULL},
762 {"has_fallback_symlink", (getter)dirstate_item_get_has_fallback_symlink,
754 {"has_fallback_symlink", (getter)dirstate_item_get_has_fallback_symlink,
763 NULL, "has_fallback_symlink", NULL},
755 NULL, "has_fallback_symlink", NULL},
764 {"fallback_symlink", (getter)dirstate_item_get_fallback_symlink,
756 {"fallback_symlink", (getter)dirstate_item_get_fallback_symlink,
765 (setter)dirstate_item_set_fallback_symlink, "fallback_symlink", NULL},
757 (setter)dirstate_item_set_fallback_symlink, "fallback_symlink", NULL},
766 {"tracked", (getter)dirstate_item_get_tracked, NULL, "tracked", NULL},
758 {"tracked", (getter)dirstate_item_get_tracked, NULL, "tracked", NULL},
767 {"p1_tracked", (getter)dirstate_item_get_p1_tracked, NULL, "p1_tracked",
759 {"p1_tracked", (getter)dirstate_item_get_p1_tracked, NULL, "p1_tracked",
768 NULL},
760 NULL},
769 {"added", (getter)dirstate_item_get_added, NULL, "added", NULL},
761 {"added", (getter)dirstate_item_get_added, NULL, "added", NULL},
770 {"p2_info", (getter)dirstate_item_get_p2_info, NULL, "p2_info", NULL},
762 {"p2_info", (getter)dirstate_item_get_p2_info, NULL, "p2_info", NULL},
771 {"merged", (getter)dirstate_item_get_merged, NULL, "merged", NULL},
763 {"merged", (getter)dirstate_item_get_merged, NULL, "merged", NULL},
772 {"from_p2", (getter)dirstate_item_get_from_p2, NULL, "from_p2", NULL},
764 {"from_p2", (getter)dirstate_item_get_from_p2, NULL, "from_p2", NULL},
773 {"maybe_clean", (getter)dirstate_item_get_maybe_clean, NULL, "maybe_clean",
765 {"maybe_clean", (getter)dirstate_item_get_maybe_clean, NULL, "maybe_clean",
774 NULL},
766 NULL},
775 {"any_tracked", (getter)dirstate_item_get_any_tracked, NULL, "any_tracked",
767 {"any_tracked", (getter)dirstate_item_get_any_tracked, NULL, "any_tracked",
776 NULL},
768 NULL},
777 {"removed", (getter)dirstate_item_get_removed, NULL, "removed", NULL},
769 {"removed", (getter)dirstate_item_get_removed, NULL, "removed", NULL},
778 {NULL} /* Sentinel */
770 {NULL} /* Sentinel */
779 };
771 };
780
772
781 PyTypeObject dirstateItemType = {
773 PyTypeObject dirstateItemType = {
782 PyVarObject_HEAD_INIT(NULL, 0) /* header */
774 PyVarObject_HEAD_INIT(NULL, 0) /* header */
783 "dirstate_tuple", /* tp_name */
775 "dirstate_tuple", /* tp_name */
784 sizeof(dirstateItemObject), /* tp_basicsize */
776 sizeof(dirstateItemObject), /* tp_basicsize */
785 0, /* tp_itemsize */
777 0, /* tp_itemsize */
786 (destructor)dirstate_item_dealloc, /* tp_dealloc */
778 (destructor)dirstate_item_dealloc, /* tp_dealloc */
787 0, /* tp_print */
779 0, /* tp_print */
788 0, /* tp_getattr */
780 0, /* tp_getattr */
789 0, /* tp_setattr */
781 0, /* tp_setattr */
790 0, /* tp_compare */
782 0, /* tp_compare */
791 0, /* tp_repr */
783 0, /* tp_repr */
792 0, /* tp_as_number */
784 0, /* tp_as_number */
793 0, /* tp_as_sequence */
785 0, /* tp_as_sequence */
794 0, /* tp_as_mapping */
786 0, /* tp_as_mapping */
795 0, /* tp_hash */
787 0, /* tp_hash */
796 0, /* tp_call */
788 0, /* tp_call */
797 0, /* tp_str */
789 0, /* tp_str */
798 0, /* tp_getattro */
790 0, /* tp_getattro */
799 0, /* tp_setattro */
791 0, /* tp_setattro */
800 0, /* tp_as_buffer */
792 0, /* tp_as_buffer */
801 Py_TPFLAGS_DEFAULT, /* tp_flags */
793 Py_TPFLAGS_DEFAULT, /* tp_flags */
802 "dirstate tuple", /* tp_doc */
794 "dirstate tuple", /* tp_doc */
803 0, /* tp_traverse */
795 0, /* tp_traverse */
804 0, /* tp_clear */
796 0, /* tp_clear */
805 0, /* tp_richcompare */
797 0, /* tp_richcompare */
806 0, /* tp_weaklistoffset */
798 0, /* tp_weaklistoffset */
807 0, /* tp_iter */
799 0, /* tp_iter */
808 0, /* tp_iternext */
800 0, /* tp_iternext */
809 dirstate_item_methods, /* tp_methods */
801 dirstate_item_methods, /* tp_methods */
810 0, /* tp_members */
802 0, /* tp_members */
811 dirstate_item_getset, /* tp_getset */
803 dirstate_item_getset, /* tp_getset */
812 0, /* tp_base */
804 0, /* tp_base */
813 0, /* tp_dict */
805 0, /* tp_dict */
814 0, /* tp_descr_get */
806 0, /* tp_descr_get */
815 0, /* tp_descr_set */
807 0, /* tp_descr_set */
816 0, /* tp_dictoffset */
808 0, /* tp_dictoffset */
817 0, /* tp_init */
809 0, /* tp_init */
818 0, /* tp_alloc */
810 0, /* tp_alloc */
819 dirstate_item_new, /* tp_new */
811 dirstate_item_new, /* tp_new */
820 };
812 };
821
813
822 static PyObject *parse_dirstate(PyObject *self, PyObject *args)
814 static PyObject *parse_dirstate(PyObject *self, PyObject *args)
823 {
815 {
824 PyObject *dmap, *cmap, *parents = NULL, *ret = NULL;
816 PyObject *dmap, *cmap, *parents = NULL, *ret = NULL;
825 PyObject *fname = NULL, *cname = NULL, *entry = NULL;
817 PyObject *fname = NULL, *cname = NULL, *entry = NULL;
826 char state, *cur, *str, *cpos;
818 char state, *cur, *str, *cpos;
827 int mode, size, mtime;
819 int mode, size, mtime;
828 unsigned int flen, pos = 40;
820 unsigned int flen, pos = 40;
829 Py_ssize_t len = 40;
821 Py_ssize_t len = 40;
830 Py_ssize_t readlen;
822 Py_ssize_t readlen;
831
823
832 if (!PyArg_ParseTuple(
824 if (!PyArg_ParseTuple(
833 args, PY23("O!O!s#:parse_dirstate", "O!O!y#:parse_dirstate"),
825 args, PY23("O!O!s#:parse_dirstate", "O!O!y#:parse_dirstate"),
834 &PyDict_Type, &dmap, &PyDict_Type, &cmap, &str, &readlen)) {
826 &PyDict_Type, &dmap, &PyDict_Type, &cmap, &str, &readlen)) {
835 goto quit;
827 goto quit;
836 }
828 }
837
829
838 len = readlen;
830 len = readlen;
839
831
840 /* read parents */
832 /* read parents */
841 if (len < 40) {
833 if (len < 40) {
842 PyErr_SetString(PyExc_ValueError,
834 PyErr_SetString(PyExc_ValueError,
843 "too little data for parents");
835 "too little data for parents");
844 goto quit;
836 goto quit;
845 }
837 }
846
838
847 parents = Py_BuildValue(PY23("s#s#", "y#y#"), str, (Py_ssize_t)20,
839 parents = Py_BuildValue(PY23("s#s#", "y#y#"), str, (Py_ssize_t)20,
848 str + 20, (Py_ssize_t)20);
840 str + 20, (Py_ssize_t)20);
849 if (!parents) {
841 if (!parents) {
850 goto quit;
842 goto quit;
851 }
843 }
852
844
853 /* read filenames */
845 /* read filenames */
854 while (pos >= 40 && pos < len) {
846 while (pos >= 40 && pos < len) {
855 if (pos + 17 > len) {
847 if (pos + 17 > len) {
856 PyErr_SetString(PyExc_ValueError,
848 PyErr_SetString(PyExc_ValueError,
857 "overflow in dirstate");
849 "overflow in dirstate");
858 goto quit;
850 goto quit;
859 }
851 }
860 cur = str + pos;
852 cur = str + pos;
861 /* unpack header */
853 /* unpack header */
862 state = *cur;
854 state = *cur;
863 mode = getbe32(cur + 1);
855 mode = getbe32(cur + 1);
864 size = getbe32(cur + 5);
856 size = getbe32(cur + 5);
865 mtime = getbe32(cur + 9);
857 mtime = getbe32(cur + 9);
866 flen = getbe32(cur + 13);
858 flen = getbe32(cur + 13);
867 pos += 17;
859 pos += 17;
868 cur += 17;
860 cur += 17;
869 if (flen > len - pos) {
861 if (flen > len - pos) {
870 PyErr_SetString(PyExc_ValueError,
862 PyErr_SetString(PyExc_ValueError,
871 "overflow in dirstate");
863 "overflow in dirstate");
872 goto quit;
864 goto quit;
873 }
865 }
874
866
875 entry = (PyObject *)dirstate_item_from_v1_data(state, mode,
867 entry = (PyObject *)dirstate_item_from_v1_data(state, mode,
876 size, mtime);
868 size, mtime);
877 if (!entry)
869 if (!entry)
878 goto quit;
870 goto quit;
879 cpos = memchr(cur, 0, flen);
871 cpos = memchr(cur, 0, flen);
880 if (cpos) {
872 if (cpos) {
881 fname = PyBytes_FromStringAndSize(cur, cpos - cur);
873 fname = PyBytes_FromStringAndSize(cur, cpos - cur);
882 cname = PyBytes_FromStringAndSize(
874 cname = PyBytes_FromStringAndSize(
883 cpos + 1, flen - (cpos - cur) - 1);
875 cpos + 1, flen - (cpos - cur) - 1);
884 if (!fname || !cname ||
876 if (!fname || !cname ||
885 PyDict_SetItem(cmap, fname, cname) == -1 ||
877 PyDict_SetItem(cmap, fname, cname) == -1 ||
886 PyDict_SetItem(dmap, fname, entry) == -1) {
878 PyDict_SetItem(dmap, fname, entry) == -1) {
887 goto quit;
879 goto quit;
888 }
880 }
889 Py_DECREF(cname);
881 Py_DECREF(cname);
890 } else {
882 } else {
891 fname = PyBytes_FromStringAndSize(cur, flen);
883 fname = PyBytes_FromStringAndSize(cur, flen);
892 if (!fname ||
884 if (!fname ||
893 PyDict_SetItem(dmap, fname, entry) == -1) {
885 PyDict_SetItem(dmap, fname, entry) == -1) {
894 goto quit;
886 goto quit;
895 }
887 }
896 }
888 }
897 Py_DECREF(fname);
889 Py_DECREF(fname);
898 Py_DECREF(entry);
890 Py_DECREF(entry);
899 fname = cname = entry = NULL;
891 fname = cname = entry = NULL;
900 pos += flen;
892 pos += flen;
901 }
893 }
902
894
903 ret = parents;
895 ret = parents;
904 Py_INCREF(ret);
896 Py_INCREF(ret);
905 quit:
897 quit:
906 Py_XDECREF(fname);
898 Py_XDECREF(fname);
907 Py_XDECREF(cname);
899 Py_XDECREF(cname);
908 Py_XDECREF(entry);
900 Py_XDECREF(entry);
909 Py_XDECREF(parents);
901 Py_XDECREF(parents);
910 return ret;
902 return ret;
911 }
903 }
912
904
913 /*
905 /*
914 * Efficiently pack a dirstate object into its on-disk format.
906 * Efficiently pack a dirstate object into its on-disk format.
915 */
907 */
916 static PyObject *pack_dirstate(PyObject *self, PyObject *args)
908 static PyObject *pack_dirstate(PyObject *self, PyObject *args)
917 {
909 {
918 PyObject *packobj = NULL;
910 PyObject *packobj = NULL;
919 PyObject *map, *copymap, *pl, *mtime_unset = NULL;
911 PyObject *map, *copymap, *pl, *mtime_unset = NULL;
920 Py_ssize_t nbytes, pos, l;
912 Py_ssize_t nbytes, pos, l;
921 PyObject *k, *v = NULL, *pn;
913 PyObject *k, *v = NULL, *pn;
922 char *p, *s;
914 char *p, *s;
923
915
924 if (!PyArg_ParseTuple(args, "O!O!O!:pack_dirstate", &PyDict_Type, &map,
916 if (!PyArg_ParseTuple(args, "O!O!O!:pack_dirstate", &PyDict_Type, &map,
925 &PyDict_Type, &copymap, &PyTuple_Type, &pl)) {
917 &PyDict_Type, &copymap, &PyTuple_Type, &pl)) {
926 return NULL;
918 return NULL;
927 }
919 }
928
920
929 if (PyTuple_Size(pl) != 2) {
921 if (PyTuple_Size(pl) != 2) {
930 PyErr_SetString(PyExc_TypeError, "expected 2-element tuple");
922 PyErr_SetString(PyExc_TypeError, "expected 2-element tuple");
931 return NULL;
923 return NULL;
932 }
924 }
933
925
934 /* Figure out how much we need to allocate. */
926 /* Figure out how much we need to allocate. */
935 for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) {
927 for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) {
936 PyObject *c;
928 PyObject *c;
937 if (!PyBytes_Check(k)) {
929 if (!PyBytes_Check(k)) {
938 PyErr_SetString(PyExc_TypeError, "expected string key");
930 PyErr_SetString(PyExc_TypeError, "expected string key");
939 goto bail;
931 goto bail;
940 }
932 }
941 nbytes += PyBytes_GET_SIZE(k) + 17;
933 nbytes += PyBytes_GET_SIZE(k) + 17;
942 c = PyDict_GetItem(copymap, k);
934 c = PyDict_GetItem(copymap, k);
943 if (c) {
935 if (c) {
944 if (!PyBytes_Check(c)) {
936 if (!PyBytes_Check(c)) {
945 PyErr_SetString(PyExc_TypeError,
937 PyErr_SetString(PyExc_TypeError,
946 "expected string key");
938 "expected string key");
947 goto bail;
939 goto bail;
948 }
940 }
949 nbytes += PyBytes_GET_SIZE(c) + 1;
941 nbytes += PyBytes_GET_SIZE(c) + 1;
950 }
942 }
951 }
943 }
952
944
953 packobj = PyBytes_FromStringAndSize(NULL, nbytes);
945 packobj = PyBytes_FromStringAndSize(NULL, nbytes);
954 if (packobj == NULL) {
946 if (packobj == NULL) {
955 goto bail;
947 goto bail;
956 }
948 }
957
949
958 p = PyBytes_AS_STRING(packobj);
950 p = PyBytes_AS_STRING(packobj);
959
951
960 pn = PyTuple_GET_ITEM(pl, 0);
952 pn = PyTuple_GET_ITEM(pl, 0);
961 if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
953 if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
962 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
954 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
963 goto bail;
955 goto bail;
964 }
956 }
965 memcpy(p, s, l);
957 memcpy(p, s, l);
966 p += 20;
958 p += 20;
967 pn = PyTuple_GET_ITEM(pl, 1);
959 pn = PyTuple_GET_ITEM(pl, 1);
968 if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
960 if (PyBytes_AsStringAndSize(pn, &s, &l) == -1 || l != 20) {
969 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
961 PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash");
970 goto bail;
962 goto bail;
971 }
963 }
972 memcpy(p, s, l);
964 memcpy(p, s, l);
973 p += 20;
965 p += 20;
974
966
975 for (pos = 0; PyDict_Next(map, &pos, &k, &v);) {
967 for (pos = 0; PyDict_Next(map, &pos, &k, &v);) {
976 dirstateItemObject *tuple;
968 dirstateItemObject *tuple;
977 char state;
969 char state;
978 int mode, size, mtime;
970 int mode, size, mtime;
979 Py_ssize_t len, l;
971 Py_ssize_t len, l;
980 PyObject *o;
972 PyObject *o;
981 char *t;
973 char *t;
982
974
983 if (!dirstate_tuple_check(v)) {
975 if (!dirstate_tuple_check(v)) {
984 PyErr_SetString(PyExc_TypeError,
976 PyErr_SetString(PyExc_TypeError,
985 "expected a dirstate tuple");
977 "expected a dirstate tuple");
986 goto bail;
978 goto bail;
987 }
979 }
988 tuple = (dirstateItemObject *)v;
980 tuple = (dirstateItemObject *)v;
989
981
990 state = dirstate_item_c_v1_state(tuple);
982 state = dirstate_item_c_v1_state(tuple);
991 mode = dirstate_item_c_v1_mode(tuple);
983 mode = dirstate_item_c_v1_mode(tuple);
992 size = dirstate_item_c_v1_size(tuple);
984 size = dirstate_item_c_v1_size(tuple);
993 mtime = dirstate_item_c_v1_mtime(tuple);
985 mtime = dirstate_item_c_v1_mtime(tuple);
994 *p++ = state;
986 *p++ = state;
995 putbe32((uint32_t)mode, p);
987 putbe32((uint32_t)mode, p);
996 putbe32((uint32_t)size, p + 4);
988 putbe32((uint32_t)size, p + 4);
997 putbe32((uint32_t)mtime, p + 8);
989 putbe32((uint32_t)mtime, p + 8);
998 t = p + 12;
990 t = p + 12;
999 p += 16;
991 p += 16;
1000 len = PyBytes_GET_SIZE(k);
992 len = PyBytes_GET_SIZE(k);
1001 memcpy(p, PyBytes_AS_STRING(k), len);
993 memcpy(p, PyBytes_AS_STRING(k), len);
1002 p += len;
994 p += len;
1003 o = PyDict_GetItem(copymap, k);
995 o = PyDict_GetItem(copymap, k);
1004 if (o) {
996 if (o) {
1005 *p++ = '\0';
997 *p++ = '\0';
1006 l = PyBytes_GET_SIZE(o);
998 l = PyBytes_GET_SIZE(o);
1007 memcpy(p, PyBytes_AS_STRING(o), l);
999 memcpy(p, PyBytes_AS_STRING(o), l);
1008 p += l;
1000 p += l;
1009 len += l + 1;
1001 len += l + 1;
1010 }
1002 }
1011 putbe32((uint32_t)len, t);
1003 putbe32((uint32_t)len, t);
1012 }
1004 }
1013
1005
1014 pos = p - PyBytes_AS_STRING(packobj);
1006 pos = p - PyBytes_AS_STRING(packobj);
1015 if (pos != nbytes) {
1007 if (pos != nbytes) {
1016 PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld",
1008 PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld",
1017 (long)pos, (long)nbytes);
1009 (long)pos, (long)nbytes);
1018 goto bail;
1010 goto bail;
1019 }
1011 }
1020
1012
1021 return packobj;
1013 return packobj;
1022 bail:
1014 bail:
1023 Py_XDECREF(mtime_unset);
1015 Py_XDECREF(mtime_unset);
1024 Py_XDECREF(packobj);
1016 Py_XDECREF(packobj);
1025 Py_XDECREF(v);
1017 Py_XDECREF(v);
1026 return NULL;
1018 return NULL;
1027 }
1019 }
1028
1020
1029 #define BUMPED_FIX 1
1021 #define BUMPED_FIX 1
1030 #define USING_SHA_256 2
1022 #define USING_SHA_256 2
1031 #define FM1_HEADER_SIZE (4 + 8 + 2 + 2 + 1 + 1 + 1)
1023 #define FM1_HEADER_SIZE (4 + 8 + 2 + 2 + 1 + 1 + 1)
1032
1024
1033 static PyObject *readshas(const char *source, unsigned char num,
1025 static PyObject *readshas(const char *source, unsigned char num,
1034 Py_ssize_t hashwidth)
1026 Py_ssize_t hashwidth)
1035 {
1027 {
1036 int i;
1028 int i;
1037 PyObject *list = PyTuple_New(num);
1029 PyObject *list = PyTuple_New(num);
1038 if (list == NULL) {
1030 if (list == NULL) {
1039 return NULL;
1031 return NULL;
1040 }
1032 }
1041 for (i = 0; i < num; i++) {
1033 for (i = 0; i < num; i++) {
1042 PyObject *hash = PyBytes_FromStringAndSize(source, hashwidth);
1034 PyObject *hash = PyBytes_FromStringAndSize(source, hashwidth);
1043 if (hash == NULL) {
1035 if (hash == NULL) {
1044 Py_DECREF(list);
1036 Py_DECREF(list);
1045 return NULL;
1037 return NULL;
1046 }
1038 }
1047 PyTuple_SET_ITEM(list, i, hash);
1039 PyTuple_SET_ITEM(list, i, hash);
1048 source += hashwidth;
1040 source += hashwidth;
1049 }
1041 }
1050 return list;
1042 return list;
1051 }
1043 }
1052
1044
1053 static PyObject *fm1readmarker(const char *databegin, const char *dataend,
1045 static PyObject *fm1readmarker(const char *databegin, const char *dataend,
1054 uint32_t *msize)
1046 uint32_t *msize)
1055 {
1047 {
1056 const char *data = databegin;
1048 const char *data = databegin;
1057 const char *meta;
1049 const char *meta;
1058
1050
1059 double mtime;
1051 double mtime;
1060 int16_t tz;
1052 int16_t tz;
1061 uint16_t flags;
1053 uint16_t flags;
1062 unsigned char nsuccs, nparents, nmetadata;
1054 unsigned char nsuccs, nparents, nmetadata;
1063 Py_ssize_t hashwidth = 20;
1055 Py_ssize_t hashwidth = 20;
1064
1056
1065 PyObject *prec = NULL, *parents = NULL, *succs = NULL;
1057 PyObject *prec = NULL, *parents = NULL, *succs = NULL;
1066 PyObject *metadata = NULL, *ret = NULL;
1058 PyObject *metadata = NULL, *ret = NULL;
1067 int i;
1059 int i;
1068
1060
1069 if (data + FM1_HEADER_SIZE > dataend) {
1061 if (data + FM1_HEADER_SIZE > dataend) {
1070 goto overflow;
1062 goto overflow;
1071 }
1063 }
1072
1064
1073 *msize = getbe32(data);
1065 *msize = getbe32(data);
1074 data += 4;
1066 data += 4;
1075 mtime = getbefloat64(data);
1067 mtime = getbefloat64(data);
1076 data += 8;
1068 data += 8;
1077 tz = getbeint16(data);
1069 tz = getbeint16(data);
1078 data += 2;
1070 data += 2;
1079 flags = getbeuint16(data);
1071 flags = getbeuint16(data);
1080 data += 2;
1072 data += 2;
1081
1073
1082 if (flags & USING_SHA_256) {
1074 if (flags & USING_SHA_256) {
1083 hashwidth = 32;
1075 hashwidth = 32;
1084 }
1076 }
1085
1077
1086 nsuccs = (unsigned char)(*data++);
1078 nsuccs = (unsigned char)(*data++);
1087 nparents = (unsigned char)(*data++);
1079 nparents = (unsigned char)(*data++);
1088 nmetadata = (unsigned char)(*data++);
1080 nmetadata = (unsigned char)(*data++);
1089
1081
1090 if (databegin + *msize > dataend) {
1082 if (databegin + *msize > dataend) {
1091 goto overflow;
1083 goto overflow;
1092 }
1084 }
1093 dataend = databegin + *msize; /* narrow down to marker size */
1085 dataend = databegin + *msize; /* narrow down to marker size */
1094
1086
1095 if (data + hashwidth > dataend) {
1087 if (data + hashwidth > dataend) {
1096 goto overflow;
1088 goto overflow;
1097 }
1089 }
1098 prec = PyBytes_FromStringAndSize(data, hashwidth);
1090 prec = PyBytes_FromStringAndSize(data, hashwidth);
1099 data += hashwidth;
1091 data += hashwidth;
1100 if (prec == NULL) {
1092 if (prec == NULL) {
1101 goto bail;
1093 goto bail;
1102 }
1094 }
1103
1095
1104 if (data + nsuccs * hashwidth > dataend) {
1096 if (data + nsuccs * hashwidth > dataend) {
1105 goto overflow;
1097 goto overflow;
1106 }
1098 }
1107 succs = readshas(data, nsuccs, hashwidth);
1099 succs = readshas(data, nsuccs, hashwidth);
1108 if (succs == NULL) {
1100 if (succs == NULL) {
1109 goto bail;
1101 goto bail;
1110 }
1102 }
1111 data += nsuccs * hashwidth;
1103 data += nsuccs * hashwidth;
1112
1104
1113 if (nparents == 1 || nparents == 2) {
1105 if (nparents == 1 || nparents == 2) {
1114 if (data + nparents * hashwidth > dataend) {
1106 if (data + nparents * hashwidth > dataend) {
1115 goto overflow;
1107 goto overflow;
1116 }
1108 }
1117 parents = readshas(data, nparents, hashwidth);
1109 parents = readshas(data, nparents, hashwidth);
1118 if (parents == NULL) {
1110 if (parents == NULL) {
1119 goto bail;
1111 goto bail;
1120 }
1112 }
1121 data += nparents * hashwidth;
1113 data += nparents * hashwidth;
1122 } else {
1114 } else {
1123 parents = Py_None;
1115 parents = Py_None;
1124 Py_INCREF(parents);
1116 Py_INCREF(parents);
1125 }
1117 }
1126
1118
1127 if (data + 2 * nmetadata > dataend) {
1119 if (data + 2 * nmetadata > dataend) {
1128 goto overflow;
1120 goto overflow;
1129 }
1121 }
1130 meta = data + (2 * nmetadata);
1122 meta = data + (2 * nmetadata);
1131 metadata = PyTuple_New(nmetadata);
1123 metadata = PyTuple_New(nmetadata);
1132 if (metadata == NULL) {
1124 if (metadata == NULL) {
1133 goto bail;
1125 goto bail;
1134 }
1126 }
1135 for (i = 0; i < nmetadata; i++) {
1127 for (i = 0; i < nmetadata; i++) {
1136 PyObject *tmp, *left = NULL, *right = NULL;
1128 PyObject *tmp, *left = NULL, *right = NULL;
1137 Py_ssize_t leftsize = (unsigned char)(*data++);
1129 Py_ssize_t leftsize = (unsigned char)(*data++);
1138 Py_ssize_t rightsize = (unsigned char)(*data++);
1130 Py_ssize_t rightsize = (unsigned char)(*data++);
1139 if (meta + leftsize + rightsize > dataend) {
1131 if (meta + leftsize + rightsize > dataend) {
1140 goto overflow;
1132 goto overflow;
1141 }
1133 }
1142 left = PyBytes_FromStringAndSize(meta, leftsize);
1134 left = PyBytes_FromStringAndSize(meta, leftsize);
1143 meta += leftsize;
1135 meta += leftsize;
1144 right = PyBytes_FromStringAndSize(meta, rightsize);
1136 right = PyBytes_FromStringAndSize(meta, rightsize);
1145 meta += rightsize;
1137 meta += rightsize;
1146 tmp = PyTuple_New(2);
1138 tmp = PyTuple_New(2);
1147 if (!left || !right || !tmp) {
1139 if (!left || !right || !tmp) {
1148 Py_XDECREF(left);
1140 Py_XDECREF(left);
1149 Py_XDECREF(right);
1141 Py_XDECREF(right);
1150 Py_XDECREF(tmp);
1142 Py_XDECREF(tmp);
1151 goto bail;
1143 goto bail;
1152 }
1144 }
1153 PyTuple_SET_ITEM(tmp, 0, left);
1145 PyTuple_SET_ITEM(tmp, 0, left);
1154 PyTuple_SET_ITEM(tmp, 1, right);
1146 PyTuple_SET_ITEM(tmp, 1, right);
1155 PyTuple_SET_ITEM(metadata, i, tmp);
1147 PyTuple_SET_ITEM(metadata, i, tmp);
1156 }
1148 }
1157 ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags, metadata, mtime,
1149 ret = Py_BuildValue("(OOHO(di)O)", prec, succs, flags, metadata, mtime,
1158 (int)tz * 60, parents);
1150 (int)tz * 60, parents);
1159 goto bail; /* return successfully */
1151 goto bail; /* return successfully */
1160
1152
1161 overflow:
1153 overflow:
1162 PyErr_SetString(PyExc_ValueError, "overflow in obsstore");
1154 PyErr_SetString(PyExc_ValueError, "overflow in obsstore");
1163 bail:
1155 bail:
1164 Py_XDECREF(prec);
1156 Py_XDECREF(prec);
1165 Py_XDECREF(succs);
1157 Py_XDECREF(succs);
1166 Py_XDECREF(metadata);
1158 Py_XDECREF(metadata);
1167 Py_XDECREF(parents);
1159 Py_XDECREF(parents);
1168 return ret;
1160 return ret;
1169 }
1161 }
1170
1162
1171 static PyObject *fm1readmarkers(PyObject *self, PyObject *args)
1163 static PyObject *fm1readmarkers(PyObject *self, PyObject *args)
1172 {
1164 {
1173 const char *data, *dataend;
1165 const char *data, *dataend;
1174 Py_ssize_t datalen, offset, stop;
1166 Py_ssize_t datalen, offset, stop;
1175 PyObject *markers = NULL;
1167 PyObject *markers = NULL;
1176
1168
1177 if (!PyArg_ParseTuple(args, PY23("s#nn", "y#nn"), &data, &datalen,
1169 if (!PyArg_ParseTuple(args, PY23("s#nn", "y#nn"), &data, &datalen,
1178 &offset, &stop)) {
1170 &offset, &stop)) {
1179 return NULL;
1171 return NULL;
1180 }
1172 }
1181 if (offset < 0) {
1173 if (offset < 0) {
1182 PyErr_SetString(PyExc_ValueError,
1174 PyErr_SetString(PyExc_ValueError,
1183 "invalid negative offset in fm1readmarkers");
1175 "invalid negative offset in fm1readmarkers");
1184 return NULL;
1176 return NULL;
1185 }
1177 }
1186 if (stop > datalen) {
1178 if (stop > datalen) {
1187 PyErr_SetString(
1179 PyErr_SetString(
1188 PyExc_ValueError,
1180 PyExc_ValueError,
1189 "stop longer than data length in fm1readmarkers");
1181 "stop longer than data length in fm1readmarkers");
1190 return NULL;
1182 return NULL;
1191 }
1183 }
1192 dataend = data + datalen;
1184 dataend = data + datalen;
1193 data += offset;
1185 data += offset;
1194 markers = PyList_New(0);
1186 markers = PyList_New(0);
1195 if (!markers) {
1187 if (!markers) {
1196 return NULL;
1188 return NULL;
1197 }
1189 }
1198 while (offset < stop) {
1190 while (offset < stop) {
1199 uint32_t msize;
1191 uint32_t msize;
1200 int error;
1192 int error;
1201 PyObject *record = fm1readmarker(data, dataend, &msize);
1193 PyObject *record = fm1readmarker(data, dataend, &msize);
1202 if (!record) {
1194 if (!record) {
1203 goto bail;
1195 goto bail;
1204 }
1196 }
1205 error = PyList_Append(markers, record);
1197 error = PyList_Append(markers, record);
1206 Py_DECREF(record);
1198 Py_DECREF(record);
1207 if (error) {
1199 if (error) {
1208 goto bail;
1200 goto bail;
1209 }
1201 }
1210 data += msize;
1202 data += msize;
1211 offset += msize;
1203 offset += msize;
1212 }
1204 }
1213 return markers;
1205 return markers;
1214 bail:
1206 bail:
1215 Py_DECREF(markers);
1207 Py_DECREF(markers);
1216 return NULL;
1208 return NULL;
1217 }
1209 }
1218
1210
1219 static char parsers_doc[] = "Efficient content parsing.";
1211 static char parsers_doc[] = "Efficient content parsing.";
1220
1212
1221 PyObject *encodedir(PyObject *self, PyObject *args);
1213 PyObject *encodedir(PyObject *self, PyObject *args);
1222 PyObject *pathencode(PyObject *self, PyObject *args);
1214 PyObject *pathencode(PyObject *self, PyObject *args);
1223 PyObject *lowerencode(PyObject *self, PyObject *args);
1215 PyObject *lowerencode(PyObject *self, PyObject *args);
1224 PyObject *parse_index2(PyObject *self, PyObject *args, PyObject *kwargs);
1216 PyObject *parse_index2(PyObject *self, PyObject *args, PyObject *kwargs);
1225
1217
1226 static PyMethodDef methods[] = {
1218 static PyMethodDef methods[] = {
1227 {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"},
1219 {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"},
1228 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
1220 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
1229 {"parse_index2", (PyCFunction)parse_index2, METH_VARARGS | METH_KEYWORDS,
1221 {"parse_index2", (PyCFunction)parse_index2, METH_VARARGS | METH_KEYWORDS,
1230 "parse a revlog index\n"},
1222 "parse a revlog index\n"},
1231 {"isasciistr", isasciistr, METH_VARARGS, "check if an ASCII string\n"},
1223 {"isasciistr", isasciistr, METH_VARARGS, "check if an ASCII string\n"},
1232 {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"},
1224 {"asciilower", asciilower, METH_VARARGS, "lowercase an ASCII string\n"},
1233 {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"},
1225 {"asciiupper", asciiupper, METH_VARARGS, "uppercase an ASCII string\n"},
1234 {"dict_new_presized", dict_new_presized, METH_VARARGS,
1226 {"dict_new_presized", dict_new_presized, METH_VARARGS,
1235 "construct a dict with an expected size\n"},
1227 "construct a dict with an expected size\n"},
1236 {"make_file_foldmap", make_file_foldmap, METH_VARARGS,
1228 {"make_file_foldmap", make_file_foldmap, METH_VARARGS,
1237 "make file foldmap\n"},
1229 "make file foldmap\n"},
1238 {"jsonescapeu8fast", jsonescapeu8fast, METH_VARARGS,
1230 {"jsonescapeu8fast", jsonescapeu8fast, METH_VARARGS,
1239 "escape a UTF-8 byte string to JSON (fast path)\n"},
1231 "escape a UTF-8 byte string to JSON (fast path)\n"},
1240 {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"},
1232 {"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"},
1241 {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"},
1233 {"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"},
1242 {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"},
1234 {"lowerencode", lowerencode, METH_VARARGS, "lower-encode a path\n"},
1243 {"fm1readmarkers", fm1readmarkers, METH_VARARGS,
1235 {"fm1readmarkers", fm1readmarkers, METH_VARARGS,
1244 "parse v1 obsolete markers\n"},
1236 "parse v1 obsolete markers\n"},
1245 {NULL, NULL}};
1237 {NULL, NULL}};
1246
1238
1247 void dirs_module_init(PyObject *mod);
1239 void dirs_module_init(PyObject *mod);
1248 void manifest_module_init(PyObject *mod);
1240 void manifest_module_init(PyObject *mod);
1249 void revlog_module_init(PyObject *mod);
1241 void revlog_module_init(PyObject *mod);
1250
1242
1251 static const int version = 20;
1243 static const int version = 20;
1252
1244
1253 static void module_init(PyObject *mod)
1245 static void module_init(PyObject *mod)
1254 {
1246 {
1255 PyModule_AddIntConstant(mod, "version", version);
1247 PyModule_AddIntConstant(mod, "version", version);
1256
1248
1257 /* This module constant has two purposes. First, it lets us unit test
1249 /* This module constant has two purposes. First, it lets us unit test
1258 * the ImportError raised without hard-coding any error text. This
1250 * the ImportError raised without hard-coding any error text. This
1259 * means we can change the text in the future without breaking tests,
1251 * means we can change the text in the future without breaking tests,
1260 * even across changesets without a recompile. Second, its presence
1252 * even across changesets without a recompile. Second, its presence
1261 * can be used to determine whether the version-checking logic is
1253 * can be used to determine whether the version-checking logic is
1262 * present, which also helps in testing across changesets without a
1254 * present, which also helps in testing across changesets without a
1263 * recompile. Note that this means the pure-Python version of parsers
1255 * recompile. Note that this means the pure-Python version of parsers
1264 * should not have this module constant. */
1256 * should not have this module constant. */
1265 PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext);
1257 PyModule_AddStringConstant(mod, "versionerrortext", versionerrortext);
1266
1258
1267 dirs_module_init(mod);
1259 dirs_module_init(mod);
1268 manifest_module_init(mod);
1260 manifest_module_init(mod);
1269 revlog_module_init(mod);
1261 revlog_module_init(mod);
1270
1262
1271 if (PyType_Ready(&dirstateItemType) < 0) {
1263 if (PyType_Ready(&dirstateItemType) < 0) {
1272 return;
1264 return;
1273 }
1265 }
1274 Py_INCREF(&dirstateItemType);
1266 Py_INCREF(&dirstateItemType);
1275 PyModule_AddObject(mod, "DirstateItem", (PyObject *)&dirstateItemType);
1267 PyModule_AddObject(mod, "DirstateItem", (PyObject *)&dirstateItemType);
1276 }
1268 }
1277
1269
1278 static int check_python_version(void)
1270 static int check_python_version(void)
1279 {
1271 {
1280 PyObject *sys = PyImport_ImportModule("sys"), *ver;
1272 PyObject *sys = PyImport_ImportModule("sys"), *ver;
1281 long hexversion;
1273 long hexversion;
1282 if (!sys) {
1274 if (!sys) {
1283 return -1;
1275 return -1;
1284 }
1276 }
1285 ver = PyObject_GetAttrString(sys, "hexversion");
1277 ver = PyObject_GetAttrString(sys, "hexversion");
1286 Py_DECREF(sys);
1278 Py_DECREF(sys);
1287 if (!ver) {
1279 if (!ver) {
1288 return -1;
1280 return -1;
1289 }
1281 }
1290 hexversion = PyInt_AsLong(ver);
1282 hexversion = PyInt_AsLong(ver);
1291 Py_DECREF(ver);
1283 Py_DECREF(ver);
1292 /* sys.hexversion is a 32-bit number by default, so the -1 case
1284 /* sys.hexversion is a 32-bit number by default, so the -1 case
1293 * should only occur in unusual circumstances (e.g. if sys.hexversion
1285 * should only occur in unusual circumstances (e.g. if sys.hexversion
1294 * is manually set to an invalid value). */
1286 * is manually set to an invalid value). */
1295 if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) {
1287 if ((hexversion == -1) || (hexversion >> 16 != PY_VERSION_HEX >> 16)) {
1296 PyErr_Format(PyExc_ImportError,
1288 PyErr_Format(PyExc_ImportError,
1297 "%s: The Mercurial extension "
1289 "%s: The Mercurial extension "
1298 "modules were compiled with Python " PY_VERSION
1290 "modules were compiled with Python " PY_VERSION
1299 ", but "
1291 ", but "
1300 "Mercurial is currently using Python with "
1292 "Mercurial is currently using Python with "
1301 "sys.hexversion=%ld: "
1293 "sys.hexversion=%ld: "
1302 "Python %s\n at: %s",
1294 "Python %s\n at: %s",
1303 versionerrortext, hexversion, Py_GetVersion(),
1295 versionerrortext, hexversion, Py_GetVersion(),
1304 Py_GetProgramFullPath());
1296 Py_GetProgramFullPath());
1305 return -1;
1297 return -1;
1306 }
1298 }
1307 return 0;
1299 return 0;
1308 }
1300 }
1309
1301
1310 static struct PyModuleDef parsers_module = {PyModuleDef_HEAD_INIT, "parsers",
1302 static struct PyModuleDef parsers_module = {PyModuleDef_HEAD_INIT, "parsers",
1311 parsers_doc, -1, methods};
1303 parsers_doc, -1, methods};
1312
1304
1313 PyMODINIT_FUNC PyInit_parsers(void)
1305 PyMODINIT_FUNC PyInit_parsers(void)
1314 {
1306 {
1315 PyObject *mod;
1307 PyObject *mod;
1316
1308
1317 if (check_python_version() == -1)
1309 if (check_python_version() == -1)
1318 return NULL;
1310 return NULL;
1319 mod = PyModule_Create(&parsers_module);
1311 mod = PyModule_Create(&parsers_module);
1320 module_init(mod);
1312 module_init(mod);
1321 return mod;
1313 return mod;
1322 }
1314 }
General Comments 0
You need to be logged in to leave comments. Login now