hgext/eol.py
changeset 13649 328ce8a405ac
parent 13625 8aec2516602b
child 13650 56e71e7d2ba2
equal deleted inserted replaced
13639:595dba23d337 13649:328ce8a405ac
   167             except KeyError:
   167             except KeyError:
   168                 ui.warn(_("ignoring unknown EOL style '%s' from %s\n")
   168                 ui.warn(_("ignoring unknown EOL style '%s' from %s\n")
   169                         % (style, self.cfg.source('patterns', pattern)))
   169                         % (style, self.cfg.source('patterns', pattern)))
   170 
   170 
   171     def checkrev(self, repo, ctx, files):
   171     def checkrev(self, repo, ctx, files):
       
   172         failed = []
   172         for f in files:
   173         for f in files:
   173             if f not in ctx:
   174             if f not in ctx:
   174                 continue
   175                 continue
   175             for pattern, style in self.cfg.items('patterns'):
   176             for pattern, style in self.cfg.items('patterns'):
   176                 if not match.match(repo.root, '', [pattern])(f):
   177                 if not match.match(repo.root, '', [pattern])(f):
   177                     continue
   178                     continue
   178                 target = self._encode[style.upper()]
   179                 target = self._encode[style.upper()]
   179                 data = ctx[f].data()
   180                 data = ctx[f].data()
   180                 if target == "to-lf" and "\r\n" in data:
   181                 if (target == "to-lf" and "\r\n" in data
   181                     raise util.Abort(_("%s should not have CRLF line endings")
   182                     or target == "to-crlf" and singlelf.search(data)):
   182                                      % f)
   183                     failed.append((str(ctx), target, f))
   183                 elif target == "to-crlf" and singlelf.search(data):
       
   184                     raise util.Abort(_("%s should not have LF line endings")
       
   185                                      % f)
       
   186                 # Ignore other rules for this file
       
   187                 break
   184                 break
       
   185         return failed
   188 
   186 
   189 def parseeol(ui, repo, nodes):
   187 def parseeol(ui, repo, nodes):
   190     try:
   188     try:
   191         for node in nodes:
   189         for node in nodes:
   192             try:
   190             try:
   213         files.update(ctx.files())
   211         files.update(ctx.files())
   214         revs.add(rev)
   212         revs.add(rev)
   215         if headsonly:
   213         if headsonly:
   216             for pctx in ctx.parents():
   214             for pctx in ctx.parents():
   217                 revs.discard(pctx.rev())
   215                 revs.discard(pctx.rev())
       
   216     failed = []
   218     for rev in revs:
   217     for rev in revs:
   219         ctx = repo[rev]
   218         ctx = repo[rev]
   220         eol = parseeol(ui, repo, [ctx.node()])
   219         eol = parseeol(ui, repo, [ctx.node()])
   221         if eol:
   220         if eol:
   222             eol.checkrev(repo, ctx, files)
   221             failed.extend(eol.checkrev(repo, ctx, files))
       
   222 
       
   223     if failed:
       
   224         eols = {'to-lf': 'CRLF', 'to-crlf': 'LF'}
       
   225         msgs = []
       
   226         for node, target, f in failed:
       
   227             msgs.append(_("  %s in %s should not have %s line endings") %
       
   228                         (f, node, eols[target]))
       
   229         raise util.Abort(_("end-of-line check failed:\n") + "\n".join(msgs))
   223 
   230 
   224 def checkallhook(ui, repo, node, hooktype, **kwargs):
   231 def checkallhook(ui, repo, node, hooktype, **kwargs):
   225     """verify that files have expected EOLs"""
   232     """verify that files have expected EOLs"""
   226     _checkhook(ui, repo, node, False)
   233     _checkhook(ui, repo, node, False)
   227 
   234