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