139 def _shrinktext(text, subfunc): |
139 def _shrinktext(text, subfunc): |
140 '''Helper for keyword expansion removal in text. |
140 '''Helper for keyword expansion removal in text. |
141 Depending on subfunc also returns number of substitutions.''' |
141 Depending on subfunc also returns number of substitutions.''' |
142 return subfunc(r'$\1$', text) |
142 return subfunc(r'$\1$', text) |
143 |
143 |
|
144 def _preselect(wstatus, changed): |
|
145 '''Retrieves modfied and added files from a working directory state |
|
146 and returns the subset of each contained in given changed files |
|
147 retrieved from a change context.''' |
|
148 modified, added = wstatus[:2] |
|
149 modified = [f for f in modified if f in changed] |
|
150 added = [f for f in added if f in changed] |
|
151 return modified, added |
|
152 |
144 |
153 |
145 class kwtemplater(object): |
154 class kwtemplater(object): |
146 ''' |
155 ''' |
147 Sets up keyword templates, corresponding keyword regex, and |
156 Sets up keyword templates, corresponding keyword regex, and |
148 provides keyword substitution functions. |
157 provides keyword substitution functions. |
192 def iskwfile(self, cand, ctx): |
201 def iskwfile(self, cand, ctx): |
193 '''Returns subset of candidates which are configured for keyword |
202 '''Returns subset of candidates which are configured for keyword |
194 expansion are not symbolic links.''' |
203 expansion are not symbolic links.''' |
195 return [f for f in cand if self.match(f) and not 'l' in ctx.flags(f)] |
204 return [f for f in cand if self.match(f) and not 'l' in ctx.flags(f)] |
196 |
205 |
197 def overwrite(self, ctx, candidates, lookup, expand, recsubn=None): |
206 def overwrite(self, ctx, candidates, lookup, expand, rekw=False): |
198 '''Overwrites selected files expanding/shrinking keywords.''' |
207 '''Overwrites selected files expanding/shrinking keywords.''' |
199 if self.restrict or lookup: # exclude kw_copy |
208 if self.restrict or lookup: # exclude kw_copy |
200 candidates = self.iskwfile(candidates, ctx) |
209 candidates = self.iskwfile(candidates, ctx) |
201 if not candidates: |
210 if not candidates: |
202 return |
211 return |
203 commit = self.restrict and not lookup |
212 commit = self.restrict and not lookup |
204 if self.restrict or expand and lookup: |
213 if self.restrict or expand and lookup: |
205 mf = ctx.manifest() |
214 mf = ctx.manifest() |
206 fctx = ctx |
215 fctx = ctx |
207 subn = (self.restrict and self.re_kw.subn or |
216 subn = (self.restrict or rekw) and self.re_kw.subn or self.re_kwexp.subn |
208 recsubn or self.re_kwexp.subn) |
|
209 msg = (expand and _('overwriting %s expanding keywords\n') |
217 msg = (expand and _('overwriting %s expanding keywords\n') |
210 or _('overwriting %s shrinking keywords\n')) |
218 or _('overwriting %s shrinking keywords\n')) |
211 for f in candidates: |
219 for f in candidates: |
212 if self.restrict: |
220 if self.restrict: |
213 data = self.repo.file(f).read(mf[f]) |
221 data = self.repo.file(f).read(mf[f]) |
508 False, True) |
516 False, True) |
509 kwt.restrict = restrict |
517 kwt.restrict = restrict |
510 return n |
518 return n |
511 |
519 |
512 def rollback(self, dryrun=False): |
520 def rollback(self, dryrun=False): |
513 wlock = repo.wlock() |
521 wlock = self.wlock() |
514 try: |
522 try: |
515 if not dryrun: |
523 if not dryrun: |
516 changed = self['.'].files() |
524 changed = self['.'].files() |
517 ret = super(kwrepo, self).rollback(dryrun) |
525 ret = super(kwrepo, self).rollback(dryrun) |
518 if not dryrun: |
526 if not dryrun: |
519 ctx = self['.'] |
527 ctx = self['.'] |
520 modified, added = self[None].status()[:2] |
528 modified, added = _preselect(self[None].status(), changed) |
521 modified = [f for f in modified if f in changed] |
529 kwt.overwrite(ctx, modified, True, True) |
522 added = [f for f in added if f in changed] |
|
523 kwt.overwrite(ctx, added, True, False) |
530 kwt.overwrite(ctx, added, True, False) |
524 kwt.overwrite(ctx, modified, True, True) |
|
525 return ret |
531 return ret |
526 finally: |
532 finally: |
527 wlock.release() |
533 wlock.release() |
528 |
534 |
529 # monkeypatches |
535 # monkeypatches |
568 try: |
574 try: |
569 # record returns 0 even when nothing has changed |
575 # record returns 0 even when nothing has changed |
570 # therefore compare nodes before and after |
576 # therefore compare nodes before and after |
571 kwt.record = True |
577 kwt.record = True |
572 ctx = repo['.'] |
578 ctx = repo['.'] |
573 modified, added = repo[None].status()[:2] |
579 wstatus = repo[None].status() |
574 ret = orig(ui, repo, commitfunc, *pats, **opts) |
580 ret = orig(ui, repo, commitfunc, *pats, **opts) |
575 recctx = repo['.'] |
581 recctx = repo['.'] |
576 if ctx != recctx: |
582 if ctx != recctx: |
577 modified = [f for f in modified if f in recctx] |
583 modified, added = _preselect(wstatus, recctx.files()) |
578 added = [f for f in added if f in recctx] |
|
579 kwt.restrict = False |
584 kwt.restrict = False |
580 kwt.overwrite(recctx, modified, False, True, kwt.re_kwexp.subn) |
585 kwt.overwrite(recctx, modified, False, True) |
581 kwt.overwrite(recctx, added, False, True, kwt.re_kw.subn) |
586 kwt.overwrite(recctx, added, False, True, True) |
582 kwt.restrict = True |
587 kwt.restrict = True |
583 return ret |
588 return ret |
584 finally: |
589 finally: |
585 wlock.release() |
590 wlock.release() |
586 |
591 |