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