Mercurial > hg
comparison hgext/keyword.py @ 6506:850071c017fa
keyword: check for '\0' in entire data before acting
util.binary might not be safe enough, as it fails eg. on certain
pdf files (issue1066).
author | Christian Ebert <blacktrash@gmx.net> |
---|---|
date | Wed, 09 Apr 2008 14:12:32 +0200 |
parents | 7ae26bb2d5cc |
children | 4b2c266bf059 |
comparison
equal
deleted
inserted
replaced
6505:7ae26bb2d5cc | 6506:850071c017fa |
---|---|
97 restricted = 'record qfold qimport qnew qpush qrefresh qrecord' | 97 restricted = 'record qfold qimport qnew qpush qrefresh qrecord' |
98 | 98 |
99 def utcdate(date): | 99 def utcdate(date): |
100 '''Returns hgdate in cvs-like UTC format.''' | 100 '''Returns hgdate in cvs-like UTC format.''' |
101 return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0])) | 101 return time.strftime('%Y/%m/%d %H:%M:%S', time.gmtime(date[0])) |
102 | |
103 def textsafe(s): | |
104 '''Safe version of util.binary with reversed logic. | |
105 Note: argument may not be None, which is allowed for util.binary.''' | |
106 return '\0' not in s | |
102 | 107 |
103 # make keyword tools accessible | 108 # make keyword tools accessible |
104 kwtools = {'templater': None, 'hgcmd': '', 'inc': [], 'exc': ['.hg*']} | 109 kwtools = {'templater': None, 'hgcmd': '', 'inc': [], 'exc': ['.hg*']} |
105 | 110 |
106 | 111 |
156 return '$%s: %s $' % (kw, ekw) | 161 return '$%s: %s $' % (kw, ekw) |
157 return subfunc(kwsub, data) | 162 return subfunc(kwsub, data) |
158 | 163 |
159 def expand(self, path, node, data): | 164 def expand(self, path, node, data): |
160 '''Returns data with keywords expanded.''' | 165 '''Returns data with keywords expanded.''' |
161 if not self.restrict and self.matcher(path) and not util.binary(data): | 166 if not self.restrict and self.matcher(path) and textsafe(data): |
162 changenode = self.getnode(path, node) | 167 changenode = self.getnode(path, node) |
163 return self.substitute(data, path, changenode, self.re_kw.sub) | 168 return self.substitute(data, path, changenode, self.re_kw.sub) |
164 return data | 169 return data |
165 | 170 |
166 def iskwfile(self, path, islink): | 171 def iskwfile(self, path, islink): |
184 candidates.sort() | 189 candidates.sort() |
185 action = expand and 'expanding' or 'shrinking' | 190 action = expand and 'expanding' or 'shrinking' |
186 for f in candidates: | 191 for f in candidates: |
187 fp = self.repo.file(f) | 192 fp = self.repo.file(f) |
188 data = fp.read(mf[f]) | 193 data = fp.read(mf[f]) |
189 if util.binary(data): | 194 if not textsafe(data): |
190 continue | 195 continue |
191 if expand: | 196 if expand: |
192 changenode = node or self.getnode(f, mf[f]) | 197 changenode = node or self.getnode(f, mf[f]) |
193 data, found = self.substitute(data, f, changenode, | 198 data, found = self.substitute(data, f, changenode, |
194 self.re_kw.subn) | 199 self.re_kw.subn) |
204 '''Unconditionally removes all keyword substitutions from text.''' | 209 '''Unconditionally removes all keyword substitutions from text.''' |
205 return self.re_kw.sub(r'$\1$', text) | 210 return self.re_kw.sub(r'$\1$', text) |
206 | 211 |
207 def shrink(self, fname, text): | 212 def shrink(self, fname, text): |
208 '''Returns text with all keyword substitutions removed.''' | 213 '''Returns text with all keyword substitutions removed.''' |
209 if self.matcher(fname) and not util.binary(text): | 214 if self.matcher(fname) and textsafe(text): |
210 return self.shrinktext(text) | 215 return self.shrinktext(text) |
211 return text | 216 return text |
212 | 217 |
213 def shrinklines(self, fname, lines): | 218 def shrinklines(self, fname, lines): |
214 '''Returns lines with keyword substitutions removed.''' | 219 '''Returns lines with keyword substitutions removed.''' |
215 if self.matcher(fname): | 220 if self.matcher(fname): |
216 text = ''.join(lines) | 221 text = ''.join(lines) |
217 if not util.binary(text): | 222 if textsafe(text): |
218 return self.shrinktext(text).splitlines(True) | 223 return self.shrinktext(text).splitlines(True) |
219 return lines | 224 return lines |
220 | 225 |
221 def wread(self, fname, data): | 226 def wread(self, fname, data): |
222 '''If in restricted mode returns data read from wdir with | 227 '''If in restricted mode returns data read from wdir with |