Show More
@@ -1,234 +1,233 b'' | |||
|
1 | 1 | # manifest.py - manifest revision class for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from i18n import _ |
|
9 | 9 | import mdiff, parsers, error, revlog, util, dicthelpers |
|
10 | 10 | import array, struct |
|
11 | 11 | |
|
12 | 12 | class manifestdict(dict): |
|
13 | 13 | def __init__(self, mapping=None, flags=None): |
|
14 | 14 | if mapping is None: |
|
15 | 15 | mapping = {} |
|
16 | 16 | if flags is None: |
|
17 | 17 | flags = {} |
|
18 | 18 | dict.__init__(self, mapping) |
|
19 | 19 | self._flags = flags |
|
20 | 20 | def flags(self, f): |
|
21 | 21 | return self._flags.get(f, "") |
|
22 | 22 | def withflags(self): |
|
23 | 23 | return set(self._flags.keys()) |
|
24 | 24 | def set(self, f, flags): |
|
25 | 25 | self._flags[f] = flags |
|
26 | 26 | def copy(self): |
|
27 | 27 | return manifestdict(self, dict.copy(self._flags)) |
|
28 | 28 | def intersectfiles(self, files): |
|
29 | 29 | '''make a new manifestdict with the intersection of self with files |
|
30 | 30 | |
|
31 | 31 | The algorithm assumes that files is much smaller than self.''' |
|
32 | 32 | ret = manifestdict() |
|
33 | 33 | for fn in files: |
|
34 | 34 | if fn in self: |
|
35 | 35 | ret[fn] = self[fn] |
|
36 | 36 | flags = self._flags.get(fn, None) |
|
37 | 37 | if flags: |
|
38 | 38 | ret._flags[fn] = flags |
|
39 | 39 | return ret |
|
40 | 40 | def flagsdiff(self, d2): |
|
41 | 41 | return dicthelpers.diff(self._flags, d2._flags, "") |
|
42 | 42 | |
|
43 | 43 | |
|
44 | 44 | def _checkforbidden(l): |
|
45 | 45 | """Check filenames for illegal characters.""" |
|
46 | 46 | for f in l: |
|
47 | 47 | if '\n' in f or '\r' in f: |
|
48 | 48 | raise error.RevlogError( |
|
49 | 49 | _("'\\n' and '\\r' disallowed in filenames: %r") % f) |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | # apply the changes collected during the bisect loop to our addlist |
|
53 | 53 | # return a delta suitable for addrevision |
|
54 | 54 | def _addlistdelta(addlist, x): |
|
55 | 55 | # for large addlist arrays, building a new array is cheaper |
|
56 | 56 | # than repeatedly modifying the existing one |
|
57 | 57 | currentposition = 0 |
|
58 | 58 | newaddlist = array.array('c') |
|
59 | 59 | |
|
60 | 60 | for start, end, content in x: |
|
61 | 61 | newaddlist += addlist[currentposition:start] |
|
62 | 62 | if content: |
|
63 | 63 | newaddlist += array.array('c', content) |
|
64 | 64 | |
|
65 | 65 | currentposition = end |
|
66 | 66 | |
|
67 | 67 | newaddlist += addlist[currentposition:] |
|
68 | 68 | |
|
69 | 69 | deltatext = "".join(struct.pack(">lll", start, end, len(content)) |
|
70 | 70 | + content for start, end, content in x) |
|
71 | 71 | return deltatext, newaddlist |
|
72 | 72 | |
|
73 | def _parse(lines): | |
|
74 | mfdict = manifestdict() | |
|
75 | parsers.parse_manifest(mfdict, mfdict._flags, lines) | |
|
76 | return mfdict | |
|
73 | 77 | |
|
74 | 78 | class manifest(revlog.revlog): |
|
75 | 79 | def __init__(self, opener): |
|
76 | 80 | # we expect to deal with not more than four revs at a time, |
|
77 | 81 | # during a commit --amend |
|
78 | 82 | self._mancache = util.lrucachedict(4) |
|
79 | 83 | revlog.revlog.__init__(self, opener, "00manifest.i") |
|
80 | 84 | |
|
81 | def parse(self, lines): | |
|
82 | mfdict = manifestdict() | |
|
83 | parsers.parse_manifest(mfdict, mfdict._flags, lines) | |
|
84 | return mfdict | |
|
85 | ||
|
86 | 85 | def readdelta(self, node): |
|
87 | 86 | r = self.rev(node) |
|
88 |
return |
|
|
87 | return _parse(mdiff.patchtext(self.revdiff(self.deltaparent(r), r))) | |
|
89 | 88 | |
|
90 | 89 | def readfast(self, node): |
|
91 | 90 | '''use the faster of readdelta or read''' |
|
92 | 91 | r = self.rev(node) |
|
93 | 92 | deltaparent = self.deltaparent(r) |
|
94 | 93 | if deltaparent != revlog.nullrev and deltaparent in self.parentrevs(r): |
|
95 | 94 | return self.readdelta(node) |
|
96 | 95 | return self.read(node) |
|
97 | 96 | |
|
98 | 97 | def read(self, node): |
|
99 | 98 | if node == revlog.nullid: |
|
100 | 99 | return manifestdict() # don't upset local cache |
|
101 | 100 | if node in self._mancache: |
|
102 | 101 | return self._mancache[node][0] |
|
103 | 102 | text = self.revision(node) |
|
104 | 103 | arraytext = array.array('c', text) |
|
105 |
mapping = |
|
|
104 | mapping = _parse(text) | |
|
106 | 105 | self._mancache[node] = (mapping, arraytext) |
|
107 | 106 | return mapping |
|
108 | 107 | |
|
109 | 108 | def _search(self, m, s, lo=0, hi=None): |
|
110 | 109 | '''return a tuple (start, end) that says where to find s within m. |
|
111 | 110 | |
|
112 | 111 | If the string is found m[start:end] are the line containing |
|
113 | 112 | that string. If start == end the string was not found and |
|
114 | 113 | they indicate the proper sorted insertion point. |
|
115 | 114 | |
|
116 | 115 | m should be a buffer or a string |
|
117 | 116 | s is a string''' |
|
118 | 117 | def advance(i, c): |
|
119 | 118 | while i < lenm and m[i] != c: |
|
120 | 119 | i += 1 |
|
121 | 120 | return i |
|
122 | 121 | if not s: |
|
123 | 122 | return (lo, lo) |
|
124 | 123 | lenm = len(m) |
|
125 | 124 | if not hi: |
|
126 | 125 | hi = lenm |
|
127 | 126 | while lo < hi: |
|
128 | 127 | mid = (lo + hi) // 2 |
|
129 | 128 | start = mid |
|
130 | 129 | while start > 0 and m[start - 1] != '\n': |
|
131 | 130 | start -= 1 |
|
132 | 131 | end = advance(start, '\0') |
|
133 | 132 | if m[start:end] < s: |
|
134 | 133 | # we know that after the null there are 40 bytes of sha1 |
|
135 | 134 | # this translates to the bisect lo = mid + 1 |
|
136 | 135 | lo = advance(end + 40, '\n') + 1 |
|
137 | 136 | else: |
|
138 | 137 | # this translates to the bisect hi = mid |
|
139 | 138 | hi = start |
|
140 | 139 | end = advance(lo, '\0') |
|
141 | 140 | found = m[lo:end] |
|
142 | 141 | if s == found: |
|
143 | 142 | # we know that after the null there are 40 bytes of sha1 |
|
144 | 143 | end = advance(end + 40, '\n') |
|
145 | 144 | return (lo, end + 1) |
|
146 | 145 | else: |
|
147 | 146 | return (lo, lo) |
|
148 | 147 | |
|
149 | 148 | def find(self, node, f): |
|
150 | 149 | '''look up entry for a single file efficiently. |
|
151 | 150 | return (node, flags) pair if found, (None, None) if not.''' |
|
152 | 151 | if node in self._mancache: |
|
153 | 152 | mapping = self._mancache[node][0] |
|
154 | 153 | return mapping.get(f), mapping.flags(f) |
|
155 | 154 | text = self.revision(node) |
|
156 | 155 | start, end = self._search(text, f) |
|
157 | 156 | if start == end: |
|
158 | 157 | return None, None |
|
159 | 158 | l = text[start:end] |
|
160 | 159 | f, n = l.split('\0') |
|
161 | 160 | return revlog.bin(n[:40]), n[40:-1] |
|
162 | 161 | |
|
163 | 162 | def add(self, map, transaction, link, p1=None, p2=None, |
|
164 | 163 | changed=None): |
|
165 | 164 | # if we're using the cache, make sure it is valid and |
|
166 | 165 | # parented by the same node we're diffing against |
|
167 | 166 | if not (changed and p1 and (p1 in self._mancache)): |
|
168 | 167 | files = sorted(map) |
|
169 | 168 | _checkforbidden(files) |
|
170 | 169 | |
|
171 | 170 | # if this is changed to support newlines in filenames, |
|
172 | 171 | # be sure to check the templates/ dir again (especially *-raw.tmpl) |
|
173 | 172 | hex, flags = revlog.hex, map.flags |
|
174 | 173 | text = ''.join("%s\0%s%s\n" % (f, hex(map[f]), flags(f)) |
|
175 | 174 | for f in files) |
|
176 | 175 | arraytext = array.array('c', text) |
|
177 | 176 | cachedelta = None |
|
178 | 177 | else: |
|
179 | 178 | added, removed = changed |
|
180 | 179 | addlist = self._mancache[p1][1] |
|
181 | 180 | |
|
182 | 181 | _checkforbidden(added) |
|
183 | 182 | # combine the changed lists into one list for sorting |
|
184 | 183 | work = [(x, False) for x in added] |
|
185 | 184 | work.extend((x, True) for x in removed) |
|
186 | 185 | # this could use heapq.merge() (from Python 2.6+) or equivalent |
|
187 | 186 | # since the lists are already sorted |
|
188 | 187 | work.sort() |
|
189 | 188 | |
|
190 | 189 | delta = [] |
|
191 | 190 | dstart = None |
|
192 | 191 | dend = None |
|
193 | 192 | dline = [""] |
|
194 | 193 | start = 0 |
|
195 | 194 | # zero copy representation of addlist as a buffer |
|
196 | 195 | addbuf = util.buffer(addlist) |
|
197 | 196 | |
|
198 | 197 | # start with a readonly loop that finds the offset of |
|
199 | 198 | # each line and creates the deltas |
|
200 | 199 | for f, todelete in work: |
|
201 | 200 | # bs will either be the index of the item or the insert point |
|
202 | 201 | start, end = self._search(addbuf, f, start) |
|
203 | 202 | if not todelete: |
|
204 | 203 | l = "%s\0%s%s\n" % (f, revlog.hex(map[f]), map.flags(f)) |
|
205 | 204 | else: |
|
206 | 205 | if start == end: |
|
207 | 206 | # item we want to delete was not found, error out |
|
208 | 207 | raise AssertionError( |
|
209 | 208 | _("failed to remove %s from manifest") % f) |
|
210 | 209 | l = "" |
|
211 | 210 | if dstart is not None and dstart <= start and dend >= start: |
|
212 | 211 | if dend < end: |
|
213 | 212 | dend = end |
|
214 | 213 | if l: |
|
215 | 214 | dline.append(l) |
|
216 | 215 | else: |
|
217 | 216 | if dstart is not None: |
|
218 | 217 | delta.append([dstart, dend, "".join(dline)]) |
|
219 | 218 | dstart = start |
|
220 | 219 | dend = end |
|
221 | 220 | dline = [l] |
|
222 | 221 | |
|
223 | 222 | if dstart is not None: |
|
224 | 223 | delta.append([dstart, dend, "".join(dline)]) |
|
225 | 224 | # apply the delta to the addlist, and get a delta for addrevision |
|
226 | 225 | deltatext, addlist = _addlistdelta(addlist, delta) |
|
227 | 226 | cachedelta = (self.rev(p1), deltatext) |
|
228 | 227 | arraytext = addlist |
|
229 | 228 | text = util.buffer(arraytext) |
|
230 | 229 | |
|
231 | 230 | n = self.addrevision(text, transaction, link, p1, p2, cachedelta) |
|
232 | 231 | self._mancache[n] = (map, arraytext) |
|
233 | 232 | |
|
234 | 233 | return n |
General Comments 0
You need to be logged in to leave comments.
Login now