comparison hgext/eol.py @ 13613:85b80261ca10

eol: separate .hgeol parsing from merge in ui This will help refactoring the hook.
author Patrick Mezard <pmezard@gmail.com>
date Sun, 13 Mar 2011 15:07:44 +0100
parents 21367c3da8aa
children 40d0cf79cb2c
comparison
equal deleted inserted replaced
13612:21367c3da8aa 13613:85b80261ca10
125 # The following provide backwards compatibility with win32text 125 # The following provide backwards compatibility with win32text
126 'cleverencode:': tolf, 126 'cleverencode:': tolf,
127 'cleverdecode:': tocrlf 127 'cleverdecode:': tocrlf
128 } 128 }
129 129
130 class eolfile(object):
131 def __init__(self, ui, root, data):
132 self._decode = {'LF': 'to-lf', 'CRLF': 'to-crlf', 'BIN': 'is-binary'}
133 self._encode = {'LF': 'to-lf', 'CRLF': 'to-crlf', 'BIN': 'is-binary'}
134
135 self.cfg = config.config()
136 # Our files should not be touched. The pattern must be
137 # inserted first override a '** = native' pattern.
138 self.cfg.set('patterns', '.hg*', 'BIN')
139 # We can then parse the user's patterns.
140 self.cfg.parse('.hgeol', data)
141
142 isrepolf = self.cfg.get('repository', 'native') != 'CRLF'
143 self._encode['NATIVE'] = isrepolf and 'to-lf' or 'to-crlf'
144 iswdlf = ui.config('eol', 'native', os.linesep) in ('LF', '\n')
145 self._decode['NATIVE'] = iswdlf and 'to-lf' or 'to-crlf'
146
147 include = []
148 exclude = []
149 for pattern, style in self.cfg.items('patterns'):
150 key = style.upper()
151 if key == 'BIN':
152 exclude.append(pattern)
153 else:
154 include.append(pattern)
155 # This will match the files for which we need to care
156 # about inconsistent newlines.
157 self.match = match.match(root, '', [], include, exclude)
158
159 def setfilters(self, ui):
160 for pattern, style in self.cfg.items('patterns'):
161 key = style.upper()
162 try:
163 ui.setconfig('decode', pattern, self._decode[key])
164 ui.setconfig('encode', pattern, self._encode[key])
165 except KeyError:
166 ui.warn(_("ignoring unknown EOL style '%s' from %s\n")
167 % (style, self.cfg.source('patterns', pattern)))
168
169 def parseeol(ui, repo, node=None):
170 try:
171 if node is None:
172 # Cannot use workingctx.data() since it would load
173 # and cache the filters before we configure them.
174 data = repo.wfile('.hgeol').read()
175 else:
176 data = repo[node]['.hgeol'].data()
177 return eolfile(ui, repo.root, data)
178 except (IOError, LookupError):
179 return None
130 180
131 def hook(ui, repo, node, hooktype, **kwargs): 181 def hook(ui, repo, node, hooktype, **kwargs):
132 """verify that files have expected EOLs""" 182 """verify that files have expected EOLs"""
133 files = set() 183 files = set()
134 for rev in xrange(repo[node].rev(), len(repo)): 184 for rev in xrange(repo[node].rev(), len(repo)):
151 201
152 202
153 def preupdate(ui, repo, hooktype, parent1, parent2): 203 def preupdate(ui, repo, hooktype, parent1, parent2):
154 #print "preupdate for %s: %s -> %s" % (repo.root, parent1, parent2) 204 #print "preupdate for %s: %s -> %s" % (repo.root, parent1, parent2)
155 try: 205 try:
156 repo.readhgeol(parent1) 206 repo.loadeol(parent1)
157 except error.ParseError, inst: 207 except error.ParseError, inst:
158 ui.warn(_("warning: ignoring .hgeol file due to parse error " 208 ui.warn(_("warning: ignoring .hgeol file due to parse error "
159 "at %s: %s\n") % (inst.args[1], inst.args[0])) 209 "at %s: %s\n") % (inst.args[1], inst.args[0]))
160 return False 210 return False
161 211
182 232
183 ui.setconfig('patch', 'eol', 'auto') 233 ui.setconfig('patch', 'eol', 'auto')
184 234
185 class eolrepo(repo.__class__): 235 class eolrepo(repo.__class__):
186 236
187 _decode = {'LF': 'to-lf', 'CRLF': 'to-crlf', 'BIN': 'is-binary'} 237 def loadeol(self, node=None):
188 _encode = {'LF': 'to-lf', 'CRLF': 'to-crlf', 'BIN': 'is-binary'} 238 eol = parseeol(self.ui, self, node)
189 239 if eol is None:
190 def readhgeol(self, node=None):
191 try:
192 if node is None:
193 # Cannot use workingctx.data() since it would load
194 # and cache the filters before we configure them.
195 data = self.wfile('.hgeol').read()
196 else:
197 data = self[node]['.hgeol'].data()
198 except (IOError, LookupError):
199 return None 240 return None
200 241 eol.setfilters(self.ui)
201 if self.ui.config('eol', 'native', os.linesep) in ('LF', '\n'): 242 return eol.match
202 self._decode['NATIVE'] = 'to-lf'
203 else:
204 self._decode['NATIVE'] = 'to-crlf'
205
206 eol = config.config()
207 # Our files should not be touched. The pattern must be
208 # inserted first override a '** = native' pattern.
209 eol.set('patterns', '.hg*', 'BIN')
210 # We can then parse the user's patterns.
211 eol.parse('.hgeol', data)
212
213 if eol.get('repository', 'native') == 'CRLF':
214 self._encode['NATIVE'] = 'to-crlf'
215 else:
216 self._encode['NATIVE'] = 'to-lf'
217
218 for pattern, style in eol.items('patterns'):
219 key = style.upper()
220 try:
221 self.ui.setconfig('decode', pattern, self._decode[key])
222 self.ui.setconfig('encode', pattern, self._encode[key])
223 except KeyError:
224 self.ui.warn(_("ignoring unknown EOL style '%s' from %s\n")
225 % (style, eol.source('patterns', pattern)))
226
227 include = []
228 exclude = []
229 for pattern, style in eol.items('patterns'):
230 key = style.upper()
231 if key == 'BIN':
232 exclude.append(pattern)
233 else:
234 include.append(pattern)
235
236 # This will match the files for which we need to care
237 # about inconsistent newlines.
238 return match.match(self.root, '', [], include, exclude)
239 243
240 def _hgcleardirstate(self): 244 def _hgcleardirstate(self):
241 try: 245 try:
242 self._eolfile = self.readhgeol() or self.readhgeol('tip') 246 self._eolfile = (self.loadeol() or self.loadeol('tip'))
243 except error.ParseError, inst: 247 except error.ParseError, inst:
244 ui.warn(_("warning: ignoring .hgeol file due to parse error " 248 ui.warn(_("warning: ignoring .hgeol file due to parse error "
245 "at %s: %s\n") % (inst.args[1], inst.args[0])) 249 "at %s: %s\n") % (inst.args[1], inst.args[0]))
246 self._eolfile = None 250 self._eolfile = None
247 251