##// END OF EJS Templates
keyword: refactor kwtemplater.overwrite()...
Christian Ebert -
r12625:d87f3ff9 default
parent child Browse files
Show More
@@ -96,7 +96,7 b" nokwcommands = ('add addremove annotate "
96
96
97 # hg commands that trigger expansion only when writing to working dir,
97 # hg commands that trigger expansion only when writing to working dir,
98 # not when reading filelog, and unexpand when reading from working dir
98 # not when reading filelog, and unexpand when reading from working dir
99 restricted = 'merge record qrecord resolve transplant'
99 restricted = 'merge kwexpand kwshrink record qrecord resolve transplant'
100
100
101 # commands using dorecord
101 # commands using dorecord
102 recordcommands = 'record qrecord'
102 recordcommands = 'record qrecord'
@@ -138,6 +138,12 b' def _defaultkwmaps(ui):'
138 templates.update(kwsets[ui.configbool('keywordset', 'svn')])
138 templates.update(kwsets[ui.configbool('keywordset', 'svn')])
139 return templates
139 return templates
140
140
141 def _shrinktext(text, subfunc):
142 '''Helper for keyword expansion removal in text.
143 Depending on subfunc also returns number of substitutions.'''
144 return subfunc(r'$\1$', text)
145
146
141 class kwtemplater(object):
147 class kwtemplater(object):
142 '''
148 '''
143 Sets up keyword templates, corresponding keyword regex, and
149 Sets up keyword templates, corresponding keyword regex, and
@@ -191,49 +197,44 b' class kwtemplater(object):'
191 Caveat: localrepository._link fails on Windows.'''
197 Caveat: localrepository._link fails on Windows.'''
192 return self.match(path) and not 'l' in flagfunc(path)
198 return self.match(path) and not 'l' in flagfunc(path)
193
199
194 def overwrite(self, ctx, candidates, iswctx, expand, changed):
200 def overwrite(self, ctx, candidates, lookup, expand):
195 '''Overwrites selected files expanding/shrinking keywords.'''
201 '''Overwrites selected files expanding/shrinking keywords.'''
196 if changed is not None:
197 candidates = [f for f in candidates if f in changed]
198 candidates = [f for f in candidates if self.iskwfile(f, ctx.flags)]
202 candidates = [f for f in candidates if self.iskwfile(f, ctx.flags)]
199 if candidates:
203 if not candidates:
200 restrict = self.restrict
204 return
201 self.restrict = True # do not expand when reading
205 commit = self.restrict and not lookup
202 rollback = kwtools['hgcmd'] == 'rollback'
206 if self.restrict or expand and lookup:
203 mf = ctx.manifest()
207 mf = ctx.manifest()
208 fctx = ctx
204 msg = (expand and _('overwriting %s expanding keywords\n')
209 msg = (expand and _('overwriting %s expanding keywords\n')
205 or _('overwriting %s shrinking keywords\n'))
210 or _('overwriting %s shrinking keywords\n'))
206 for f in candidates:
211 for f in candidates:
207 if not self.record and not rollback:
212 if self.restrict:
208 data = self.repo.file(f).read(mf[f])
213 data = self.repo.file(f).read(mf[f])
209 else:
214 else:
210 data = self.repo.wread(f)
215 data = self.repo.wread(f)
211 if util.binary(data):
216 if util.binary(data):
212 continue
217 continue
213 if expand:
218 if expand:
214 if iswctx:
219 if lookup:
215 ctx = self.repo.filectx(f, fileid=mf[f]).changectx()
220 fctx = self.repo.filectx(f, fileid=mf[f]).changectx()
216 data, found = self.substitute(data, f, ctx,
221 data, found = self.substitute(data, f, fctx, self.re_kw.subn)
217 self.re_kw.subn)
222 elif self.restrict:
223 found = self.re_kw.search(data)
218 else:
224 else:
219 found = self.re_kw.search(data)
225 data, found = _shrinktext(data, self.re_kw.subn)
220 if found:
226 if found:
221 self.ui.note(msg % f)
227 self.ui.note(msg % f)
222 self.repo.wwrite(f, data, mf.flags(f))
228 self.repo.wwrite(f, data, ctx.flags(f))
223 if iswctx and not rollback:
229 if commit:
224 self.repo.dirstate.normal(f)
230 self.repo.dirstate.normal(f)
225 elif self.record:
231 elif self.record:
226 self.repo.dirstate.normallookup(f)
232 self.repo.dirstate.normallookup(f)
227 self.restrict = restrict
228
229 def shrinktext(self, text):
230 '''Unconditionally removes all keyword substitutions from text.'''
231 return self.re_kw.sub(r'$\1$', text)
232
233
233 def shrink(self, fname, text):
234 def shrink(self, fname, text):
234 '''Returns text with all keyword substitutions removed.'''
235 '''Returns text with all keyword substitutions removed.'''
235 if self.match(fname) and not util.binary(text):
236 if self.match(fname) and not util.binary(text):
236 return self.shrinktext(text)
237 return _shrinktext(text, self.re_kw.sub)
237 return text
238 return text
238
239
239 def shrinklines(self, fname, lines):
240 def shrinklines(self, fname, lines):
@@ -241,7 +242,7 b' class kwtemplater(object):'
241 if self.match(fname):
242 if self.match(fname):
242 text = ''.join(lines)
243 text = ''.join(lines)
243 if not util.binary(text):
244 if not util.binary(text):
244 return self.shrinktext(text).splitlines(True)
245 return _shrinktext(text, self.re_kw.sub).splitlines(True)
245 return lines
246 return lines
246
247
247 def wread(self, fname, data):
248 def wread(self, fname, data):
@@ -299,7 +300,7 b' def _kwfwrite(ui, repo, expand, *pats, *'
299 modified, added, removed, deleted, unknown, ignored, clean = status
300 modified, added, removed, deleted, unknown, ignored, clean = status
300 if modified or added or removed or deleted:
301 if modified or added or removed or deleted:
301 raise util.Abort(_('outstanding uncommitted changes'))
302 raise util.Abort(_('outstanding uncommitted changes'))
302 kwt.overwrite(wctx, clean, True, expand, None)
303 kwt.overwrite(wctx, clean, True, expand)
303 finally:
304 finally:
304 wlock.release()
305 wlock.release()
305
306
@@ -502,8 +503,11 b' def reposetup(ui, repo):'
502 n = super(kwrepo, self).commitctx(ctx, error)
503 n = super(kwrepo, self).commitctx(ctx, error)
503 # no lock needed, only called from repo.commit() which already locks
504 # no lock needed, only called from repo.commit() which already locks
504 if not kwt.record:
505 if not kwt.record:
506 restrict = kwt.restrict
507 kwt.restrict = True
505 kwt.overwrite(self[n], sorted(ctx.added() + ctx.modified()),
508 kwt.overwrite(self[n], sorted(ctx.added() + ctx.modified()),
506 False, True, None)
509 False, True)
510 kwt.restrict = restrict
507 return n
511 return n
508
512
509 def rollback(self, dryrun=False):
513 def rollback(self, dryrun=False):
@@ -515,8 +519,10 b' def reposetup(ui, repo):'
515 if not dryrun:
519 if not dryrun:
516 ctx = self['.']
520 ctx = self['.']
517 modified, added = self[None].status()[:2]
521 modified, added = self[None].status()[:2]
518 kwt.overwrite(ctx, added, True, False, changed)
522 modified = [f for f in modified if f in changed]
519 kwt.overwrite(ctx, modified, True, True, changed)
523 added = [f for f in added if f in changed]
524 kwt.overwrite(ctx, added, True, False)
525 kwt.overwrite(ctx, modified, True, True)
520 return ret
526 return ret
521 finally:
527 finally:
522 wlock.release()
528 wlock.release()
@@ -551,8 +557,10 b' def reposetup(ui, repo):'
551 ret = orig(ui, repo, commitfunc, *pats, **opts)
557 ret = orig(ui, repo, commitfunc, *pats, **opts)
552 recordctx = repo['.']
558 recordctx = repo['.']
553 if ctx != recordctx:
559 if ctx != recordctx:
554 kwt.overwrite(recordctx, recordctx.files(),
560 candidates = [f for f in recordctx.files() if f in recordctx]
555 False, True, recordctx)
561 kwt.restrict = False
562 kwt.overwrite(recordctx, candidates, False, True)
563 kwt.restrict = True
556 return ret
564 return ret
557 finally:
565 finally:
558 wlock.release()
566 wlock.release()
General Comments 0
You need to be logged in to leave comments. Login now