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