hgkw/keyword.py
branchstable
changeset 839 908f0742e280
parent 838 c793c1a87f1c
child 840 df93d61a7790
--- a/hgkw/keyword.py	Sat Oct 09 16:27:10 2010 -0500
+++ b/hgkw/keyword.py	Wed Oct 13 09:18:46 2010 +0100
@@ -141,6 +141,15 @@
     Depending on subfunc also returns number of substitutions.'''
     return subfunc(r'$\1$', text)
 
+def _preselect(wstatus, changed):
+    '''Retrieves modfied and added files from a working directory state
+    and returns the subset of each contained in given changed files
+    retrieved from a change context.'''
+    modified, added = wstatus[:2]
+    modified = [f for f in modified if f in changed]
+    added = [f for f in added if f in changed]
+    return modified, added
+
 
 class kwtemplater(object):
     '''
@@ -194,7 +203,7 @@
         expansion are not symbolic links.'''
         return [f for f in cand if self.match(f) and not 'l' in ctx.flags(f)]
 
-    def overwrite(self, ctx, candidates, lookup, expand, recsubn=None):
+    def overwrite(self, ctx, candidates, lookup, expand, rekw=False):
         '''Overwrites selected files expanding/shrinking keywords.'''
         if self.restrict or lookup: # exclude kw_copy
             candidates = self.iskwfile(candidates, ctx)
@@ -204,8 +213,7 @@
         if self.restrict or expand and lookup:
             mf = ctx.manifest()
         fctx = ctx
-        subn = (self.restrict and self.re_kw.subn or
-                recsubn or self.re_kwexp.subn)
+        subn = (self.restrict or rekw) and self.re_kw.subn or self.re_kwexp.subn
         msg = (expand and _('overwriting %s expanding keywords\n')
                or _('overwriting %s shrinking keywords\n'))
         for f in candidates:
@@ -510,18 +518,16 @@
             return n
 
         def rollback(self, dryrun=False):
-            wlock = repo.wlock()
+            wlock = self.wlock()
             try:
                 if not dryrun:
                     changed = self['.'].files()
                 ret = super(kwrepo, self).rollback(dryrun)
                 if not dryrun:
                     ctx = self['.']
-                    modified, added = self[None].status()[:2]
-                    modified = [f for f in modified if f in changed]
-                    added = [f for f in added if f in changed]
+                    modified, added = _preselect(self[None].status(), changed)
+                    kwt.overwrite(ctx, modified, True, True)
                     kwt.overwrite(ctx, added, True, False)
-                    kwt.overwrite(ctx, modified, True, True)
                 return ret
             finally:
                 wlock.release()
@@ -570,15 +576,14 @@
             # therefore compare nodes before and after
             kwt.record = True
             ctx = repo['.']
-            modified, added = repo[None].status()[:2]
+            wstatus = repo[None].status()
             ret = orig(ui, repo, commitfunc, *pats, **opts)
             recctx = repo['.']
             if ctx != recctx:
-                modified = [f for f in modified if f in recctx]
-                added = [f for f in added if f in recctx]
+                modified, added = _preselect(wstatus, recctx.files())
                 kwt.restrict = False
-                kwt.overwrite(recctx, modified, False, True, kwt.re_kwexp.subn)
-                kwt.overwrite(recctx, added, False, True, kwt.re_kw.subn)
+                kwt.overwrite(recctx, modified, False, True)
+                kwt.overwrite(recctx, added, False, True, True)
                 kwt.restrict = True
             return ret
         finally: