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