hgext/eol.py
changeset 31779 fe9b33bcec6a
parent 31778 8181f378b073
child 32146 74930cf4a10e
equal deleted inserted replaced
31778:8181f378b073 31779:fe9b33bcec6a
   111 # leave the attribute unspecified.
   111 # leave the attribute unspecified.
   112 testedwith = 'ships-with-hg-core'
   112 testedwith = 'ships-with-hg-core'
   113 
   113 
   114 # Matches a lone LF, i.e., one that is not part of CRLF.
   114 # Matches a lone LF, i.e., one that is not part of CRLF.
   115 singlelf = re.compile('(^|[^\r])\n')
   115 singlelf = re.compile('(^|[^\r])\n')
   116 # Matches a single EOL which can either be a CRLF where repeated CR
       
   117 # are removed or a LF. We do not care about old Macintosh files, so a
       
   118 # stray CR is an error.
       
   119 eolre = re.compile('\r*\n')
       
   120 
       
   121 
   116 
   122 def inconsistenteol(data):
   117 def inconsistenteol(data):
   123     return '\r\n' in data and singlelf.search(data)
   118     return '\r\n' in data and singlelf.search(data)
   124 
   119 
   125 def tolf(s, params, ui, **kwargs):
   120 def tolf(s, params, ui, **kwargs):
   129     if ui.configbool('eol', 'only-consistent', True) and inconsistenteol(s):
   124     if ui.configbool('eol', 'only-consistent', True) and inconsistenteol(s):
   130         return s
   125         return s
   131     if (ui.configbool('eol', 'fix-trailing-newline', False)
   126     if (ui.configbool('eol', 'fix-trailing-newline', False)
   132         and s and s[-1] != '\n'):
   127         and s and s[-1] != '\n'):
   133         s = s + '\n'
   128         s = s + '\n'
   134     return eolre.sub('\n', s)
   129     return util.tolf(s)
   135 
   130 
   136 def tocrlf(s, params, ui, **kwargs):
   131 def tocrlf(s, params, ui, **kwargs):
   137     """Filter to convert to CRLF EOLs."""
   132     """Filter to convert to CRLF EOLs."""
   138     if util.binary(s):
   133     if util.binary(s):
   139         return s
   134         return s
   140     if ui.configbool('eol', 'only-consistent', True) and inconsistenteol(s):
   135     if ui.configbool('eol', 'only-consistent', True) and inconsistenteol(s):
   141         return s
   136         return s
   142     if (ui.configbool('eol', 'fix-trailing-newline', False)
   137     if (ui.configbool('eol', 'fix-trailing-newline', False)
   143         and s and s[-1] != '\n'):
   138         and s and s[-1] != '\n'):
   144         s = s + '\n'
   139         s = s + '\n'
   145     return eolre.sub('\r\n', s)
   140     return util.tocrlf(s)
   146 
   141 
   147 def isbinary(s, params):
   142 def isbinary(s, params):
   148     """Filter to do nothing with the file."""
   143     """Filter to do nothing with the file."""
   149     return s
   144     return s
   150 
   145