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