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