comparison hgext/eol.py @ 30114:ad43458d3529

eol: store and reuse pattern matchers instead of creating in tight loop More "right" and more efficient.
author Mads Kiilerich <madski@unity3d.com>
date Sun, 09 Oct 2016 15:54:42 +0200
parents ffb682412b98
children 747e546c561f
comparison
equal deleted inserted replaced
30113:ffb682412b98 30114:ad43458d3529
173 iswdlf = ui.config('eol', 'native', os.linesep) in ('LF', '\n') 173 iswdlf = ui.config('eol', 'native', os.linesep) in ('LF', '\n')
174 self._decode['NATIVE'] = iswdlf and 'to-lf' or 'to-crlf' 174 self._decode['NATIVE'] = iswdlf and 'to-lf' or 'to-crlf'
175 175
176 include = [] 176 include = []
177 exclude = [] 177 exclude = []
178 self.patterns = []
178 for pattern, style in self.cfg.items('patterns'): 179 for pattern, style in self.cfg.items('patterns'):
179 key = style.upper() 180 key = style.upper()
180 if key == 'BIN': 181 if key == 'BIN':
181 exclude.append(pattern) 182 exclude.append(pattern)
182 else: 183 else:
183 include.append(pattern) 184 include.append(pattern)
185 m = match.match(root, '', [pattern])
186 self.patterns.append((pattern, key, m))
184 # This will match the files for which we need to care 187 # This will match the files for which we need to care
185 # about inconsistent newlines. 188 # about inconsistent newlines.
186 self.match = match.match(root, '', [], include, exclude) 189 self.match = match.match(root, '', [], include, exclude)
187 190
188 def copytoui(self, ui): 191 def copytoui(self, ui):
189 for pattern, style in self.cfg.items('patterns'): 192 for pattern, key, m in self.patterns:
190 key = style.upper()
191 try: 193 try:
192 ui.setconfig('decode', pattern, self._decode[key], 'eol') 194 ui.setconfig('decode', pattern, self._decode[key], 'eol')
193 ui.setconfig('encode', pattern, self._encode[key], 'eol') 195 ui.setconfig('encode', pattern, self._encode[key], 'eol')
194 except KeyError: 196 except KeyError:
195 ui.warn(_("ignoring unknown EOL style '%s' from %s\n") 197 ui.warn(_("ignoring unknown EOL style '%s' from %s\n")
196 % (style, self.cfg.source('patterns', pattern))) 198 % (key, self.cfg.source('patterns', pattern)))
197 # eol.only-consistent can be specified in ~/.hgrc or .hgeol 199 # eol.only-consistent can be specified in ~/.hgrc or .hgeol
198 for k, v in self.cfg.items('eol'): 200 for k, v in self.cfg.items('eol'):
199 ui.setconfig('eol', k, v, 'eol') 201 ui.setconfig('eol', k, v, 'eol')
200 202
201 def checkrev(self, repo, ctx, files): 203 def checkrev(self, repo, ctx, files):
202 failed = [] 204 failed = []
203 for f in (files or ctx.files()): 205 for f in (files or ctx.files()):
204 if f not in ctx: 206 if f not in ctx:
205 continue 207 continue
206 for pattern, style in self.cfg.items('patterns'): 208 for pattern, key, m in self.patterns:
207 if not match.match(repo.root, '', [pattern])(f): 209 if not m(f):
208 continue 210 continue
209 target = self._encode[style.upper()] 211 target = self._encode[key]
210 data = ctx[f].data() 212 data = ctx[f].data()
211 if (target == "to-lf" and "\r\n" in data 213 if (target == "to-lf" and "\r\n" in data
212 or target == "to-crlf" and singlelf.search(data)): 214 or target == "to-crlf" and singlelf.search(data)):
213 failed.append((f, target, str(ctx))) 215 failed.append((f, target, str(ctx)))
214 break 216 break