Show More
@@ -1,181 +1,179 b'' | |||||
1 | # parsers.py - Python implementation of parsers.c |
|
1 | # parsers.py - Python implementation of parsers.c | |
2 | # |
|
2 | # | |
3 | # Copyright 2009 Matt Mackall <mpm@selenic.com> and others |
|
3 | # Copyright 2009 Matt Mackall <mpm@selenic.com> and others | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | from __future__ import absolute_import |
|
8 | from __future__ import absolute_import | |
9 |
|
9 | |||
10 | import struct |
|
10 | import struct | |
11 | import zlib |
|
11 | import zlib | |
12 |
|
12 | |||
13 | from .node import nullid |
|
13 | from .node import nullid | |
14 | from . import pycompat |
|
14 | from . import pycompat | |
15 | stringio = pycompat.stringio |
|
15 | stringio = pycompat.stringio | |
16 |
|
16 | |||
17 | if pycompat.ispy3: |
|
|||
18 | long = int |
|
|||
19 |
|
17 | |||
20 | _pack = struct.pack |
|
18 | _pack = struct.pack | |
21 | _unpack = struct.unpack |
|
19 | _unpack = struct.unpack | |
22 | _compress = zlib.compress |
|
20 | _compress = zlib.compress | |
23 | _decompress = zlib.decompress |
|
21 | _decompress = zlib.decompress | |
24 |
|
22 | |||
25 | # Some code below makes tuples directly because it's more convenient. However, |
|
23 | # Some code below makes tuples directly because it's more convenient. However, | |
26 | # code outside this module should always use dirstatetuple. |
|
24 | # code outside this module should always use dirstatetuple. | |
27 | def dirstatetuple(*x): |
|
25 | def dirstatetuple(*x): | |
28 | # x is a tuple |
|
26 | # x is a tuple | |
29 | return x |
|
27 | return x | |
30 |
|
28 | |||
31 | indexformatng = ">Qiiiiii20s12x" |
|
29 | indexformatng = ">Qiiiiii20s12x" | |
32 | indexfirst = struct.calcsize('Q') |
|
30 | indexfirst = struct.calcsize('Q') | |
33 | sizeint = struct.calcsize('i') |
|
31 | sizeint = struct.calcsize('i') | |
34 | indexsize = struct.calcsize(indexformatng) |
|
32 | indexsize = struct.calcsize(indexformatng) | |
35 |
|
33 | |||
36 | def gettype(q): |
|
34 | def gettype(q): | |
37 | return int(q & 0xFFFF) |
|
35 | return int(q & 0xFFFF) | |
38 |
|
36 | |||
39 | def offset_type(offset, type): |
|
37 | def offset_type(offset, type): | |
40 |
return |
|
38 | return int(int(offset) << 16 | type) | |
41 |
|
39 | |||
42 | class BaseIndexObject(object): |
|
40 | class BaseIndexObject(object): | |
43 | def __len__(self): |
|
41 | def __len__(self): | |
44 | return self._lgt + len(self._extra) + 1 |
|
42 | return self._lgt + len(self._extra) + 1 | |
45 |
|
43 | |||
46 | def insert(self, i, tup): |
|
44 | def insert(self, i, tup): | |
47 | assert i == -1 |
|
45 | assert i == -1 | |
48 | self._extra.append(tup) |
|
46 | self._extra.append(tup) | |
49 |
|
47 | |||
50 | def _fix_index(self, i): |
|
48 | def _fix_index(self, i): | |
51 | if not isinstance(i, int): |
|
49 | if not isinstance(i, int): | |
52 | raise TypeError("expecting int indexes") |
|
50 | raise TypeError("expecting int indexes") | |
53 | if i < 0: |
|
51 | if i < 0: | |
54 | i = len(self) + i |
|
52 | i = len(self) + i | |
55 | if i < 0 or i >= len(self): |
|
53 | if i < 0 or i >= len(self): | |
56 | raise IndexError |
|
54 | raise IndexError | |
57 | return i |
|
55 | return i | |
58 |
|
56 | |||
59 | def __getitem__(self, i): |
|
57 | def __getitem__(self, i): | |
60 | i = self._fix_index(i) |
|
58 | i = self._fix_index(i) | |
61 | if i == len(self) - 1: |
|
59 | if i == len(self) - 1: | |
62 | return (0, 0, 0, -1, -1, -1, -1, nullid) |
|
60 | return (0, 0, 0, -1, -1, -1, -1, nullid) | |
63 | if i >= self._lgt: |
|
61 | if i >= self._lgt: | |
64 | return self._extra[i - self._lgt] |
|
62 | return self._extra[i - self._lgt] | |
65 | index = self._calculate_index(i) |
|
63 | index = self._calculate_index(i) | |
66 | r = struct.unpack(indexformatng, self._data[index:index + indexsize]) |
|
64 | r = struct.unpack(indexformatng, self._data[index:index + indexsize]) | |
67 | if i == 0: |
|
65 | if i == 0: | |
68 | e = list(r) |
|
66 | e = list(r) | |
69 | type = gettype(e[0]) |
|
67 | type = gettype(e[0]) | |
70 | e[0] = offset_type(0, type) |
|
68 | e[0] = offset_type(0, type) | |
71 | return tuple(e) |
|
69 | return tuple(e) | |
72 | return r |
|
70 | return r | |
73 |
|
71 | |||
74 | class IndexObject(BaseIndexObject): |
|
72 | class IndexObject(BaseIndexObject): | |
75 | def __init__(self, data): |
|
73 | def __init__(self, data): | |
76 | assert len(data) % indexsize == 0 |
|
74 | assert len(data) % indexsize == 0 | |
77 | self._data = data |
|
75 | self._data = data | |
78 | self._lgt = len(data) // indexsize |
|
76 | self._lgt = len(data) // indexsize | |
79 | self._extra = [] |
|
77 | self._extra = [] | |
80 |
|
78 | |||
81 | def _calculate_index(self, i): |
|
79 | def _calculate_index(self, i): | |
82 | return i * indexsize |
|
80 | return i * indexsize | |
83 |
|
81 | |||
84 | def __delitem__(self, i): |
|
82 | def __delitem__(self, i): | |
85 | if not isinstance(i, slice) or not i.stop == -1 or not i.step is None: |
|
83 | if not isinstance(i, slice) or not i.stop == -1 or not i.step is None: | |
86 | raise ValueError("deleting slices only supports a:-1 with step 1") |
|
84 | raise ValueError("deleting slices only supports a:-1 with step 1") | |
87 | i = self._fix_index(i.start) |
|
85 | i = self._fix_index(i.start) | |
88 | if i < self._lgt: |
|
86 | if i < self._lgt: | |
89 | self._data = self._data[:i * indexsize] |
|
87 | self._data = self._data[:i * indexsize] | |
90 | self._lgt = i |
|
88 | self._lgt = i | |
91 | self._extra = [] |
|
89 | self._extra = [] | |
92 | else: |
|
90 | else: | |
93 | self._extra = self._extra[:i - self._lgt] |
|
91 | self._extra = self._extra[:i - self._lgt] | |
94 |
|
92 | |||
95 | class InlinedIndexObject(BaseIndexObject): |
|
93 | class InlinedIndexObject(BaseIndexObject): | |
96 | def __init__(self, data, inline=0): |
|
94 | def __init__(self, data, inline=0): | |
97 | self._data = data |
|
95 | self._data = data | |
98 | self._lgt = self._inline_scan(None) |
|
96 | self._lgt = self._inline_scan(None) | |
99 | self._inline_scan(self._lgt) |
|
97 | self._inline_scan(self._lgt) | |
100 | self._extra = [] |
|
98 | self._extra = [] | |
101 |
|
99 | |||
102 | def _inline_scan(self, lgt): |
|
100 | def _inline_scan(self, lgt): | |
103 | off = 0 |
|
101 | off = 0 | |
104 | if lgt is not None: |
|
102 | if lgt is not None: | |
105 | self._offsets = [0] * lgt |
|
103 | self._offsets = [0] * lgt | |
106 | count = 0 |
|
104 | count = 0 | |
107 | while off <= len(self._data) - indexsize: |
|
105 | while off <= len(self._data) - indexsize: | |
108 | s, = struct.unpack('>i', |
|
106 | s, = struct.unpack('>i', | |
109 | self._data[off + indexfirst:off + sizeint + indexfirst]) |
|
107 | self._data[off + indexfirst:off + sizeint + indexfirst]) | |
110 | if lgt is not None: |
|
108 | if lgt is not None: | |
111 | self._offsets[count] = off |
|
109 | self._offsets[count] = off | |
112 | count += 1 |
|
110 | count += 1 | |
113 | off += indexsize + s |
|
111 | off += indexsize + s | |
114 | if off != len(self._data): |
|
112 | if off != len(self._data): | |
115 | raise ValueError("corrupted data") |
|
113 | raise ValueError("corrupted data") | |
116 | return count |
|
114 | return count | |
117 |
|
115 | |||
118 | def __delitem__(self, i): |
|
116 | def __delitem__(self, i): | |
119 | if not isinstance(i, slice) or not i.stop == -1 or not i.step is None: |
|
117 | if not isinstance(i, slice) or not i.stop == -1 or not i.step is None: | |
120 | raise ValueError("deleting slices only supports a:-1 with step 1") |
|
118 | raise ValueError("deleting slices only supports a:-1 with step 1") | |
121 | i = self._fix_index(i.start) |
|
119 | i = self._fix_index(i.start) | |
122 | if i < self._lgt: |
|
120 | if i < self._lgt: | |
123 | self._offsets = self._offsets[:i] |
|
121 | self._offsets = self._offsets[:i] | |
124 | self._lgt = i |
|
122 | self._lgt = i | |
125 | self._extra = [] |
|
123 | self._extra = [] | |
126 | else: |
|
124 | else: | |
127 | self._extra = self._extra[:i - self._lgt] |
|
125 | self._extra = self._extra[:i - self._lgt] | |
128 |
|
126 | |||
129 | def _calculate_index(self, i): |
|
127 | def _calculate_index(self, i): | |
130 | return self._offsets[i] |
|
128 | return self._offsets[i] | |
131 |
|
129 | |||
132 | def parse_index2(data, inline): |
|
130 | def parse_index2(data, inline): | |
133 | if not inline: |
|
131 | if not inline: | |
134 | return IndexObject(data), None |
|
132 | return IndexObject(data), None | |
135 | return InlinedIndexObject(data, inline), (0, data) |
|
133 | return InlinedIndexObject(data, inline), (0, data) | |
136 |
|
134 | |||
137 | def parse_dirstate(dmap, copymap, st): |
|
135 | def parse_dirstate(dmap, copymap, st): | |
138 | parents = [st[:20], st[20: 40]] |
|
136 | parents = [st[:20], st[20: 40]] | |
139 | # dereference fields so they will be local in loop |
|
137 | # dereference fields so they will be local in loop | |
140 | format = ">cllll" |
|
138 | format = ">cllll" | |
141 | e_size = struct.calcsize(format) |
|
139 | e_size = struct.calcsize(format) | |
142 | pos1 = 40 |
|
140 | pos1 = 40 | |
143 | l = len(st) |
|
141 | l = len(st) | |
144 |
|
142 | |||
145 | # the inner loop |
|
143 | # the inner loop | |
146 | while pos1 < l: |
|
144 | while pos1 < l: | |
147 | pos2 = pos1 + e_size |
|
145 | pos2 = pos1 + e_size | |
148 | e = _unpack(">cllll", st[pos1:pos2]) # a literal here is faster |
|
146 | e = _unpack(">cllll", st[pos1:pos2]) # a literal here is faster | |
149 | pos1 = pos2 + e[4] |
|
147 | pos1 = pos2 + e[4] | |
150 | f = st[pos2:pos1] |
|
148 | f = st[pos2:pos1] | |
151 | if '\0' in f: |
|
149 | if '\0' in f: | |
152 | f, c = f.split('\0') |
|
150 | f, c = f.split('\0') | |
153 | copymap[f] = c |
|
151 | copymap[f] = c | |
154 | dmap[f] = e[:4] |
|
152 | dmap[f] = e[:4] | |
155 | return parents |
|
153 | return parents | |
156 |
|
154 | |||
157 | def pack_dirstate(dmap, copymap, pl, now): |
|
155 | def pack_dirstate(dmap, copymap, pl, now): | |
158 | now = int(now) |
|
156 | now = int(now) | |
159 | cs = stringio() |
|
157 | cs = stringio() | |
160 | write = cs.write |
|
158 | write = cs.write | |
161 | write("".join(pl)) |
|
159 | write("".join(pl)) | |
162 | for f, e in dmap.iteritems(): |
|
160 | for f, e in dmap.iteritems(): | |
163 | if e[0] == 'n' and e[3] == now: |
|
161 | if e[0] == 'n' and e[3] == now: | |
164 | # The file was last modified "simultaneously" with the current |
|
162 | # The file was last modified "simultaneously" with the current | |
165 | # write to dirstate (i.e. within the same second for file- |
|
163 | # write to dirstate (i.e. within the same second for file- | |
166 | # systems with a granularity of 1 sec). This commonly happens |
|
164 | # systems with a granularity of 1 sec). This commonly happens | |
167 | # for at least a couple of files on 'update'. |
|
165 | # for at least a couple of files on 'update'. | |
168 | # The user could change the file without changing its size |
|
166 | # The user could change the file without changing its size | |
169 | # within the same second. Invalidate the file's mtime in |
|
167 | # within the same second. Invalidate the file's mtime in | |
170 | # dirstate, forcing future 'status' calls to compare the |
|
168 | # dirstate, forcing future 'status' calls to compare the | |
171 | # contents of the file if the size is the same. This prevents |
|
169 | # contents of the file if the size is the same. This prevents | |
172 | # mistakenly treating such files as clean. |
|
170 | # mistakenly treating such files as clean. | |
173 | e = dirstatetuple(e[0], e[1], e[2], -1) |
|
171 | e = dirstatetuple(e[0], e[1], e[2], -1) | |
174 | dmap[f] = e |
|
172 | dmap[f] = e | |
175 |
|
173 | |||
176 | if f in copymap: |
|
174 | if f in copymap: | |
177 | f = "%s\0%s" % (f, copymap[f]) |
|
175 | f = "%s\0%s" % (f, copymap[f]) | |
178 | e = _pack(">cllll", e[0], e[1], e[2], e[3], len(f)) |
|
176 | e = _pack(">cllll", e[0], e[1], e[2], e[3], len(f)) | |
179 | write(e) |
|
177 | write(e) | |
180 | write(f) |
|
178 | write(f) | |
181 | return cs.getvalue() |
|
179 | return cs.getvalue() |
General Comments 0
You need to be logged in to leave comments.
Login now