[PATCH] keyword: code cleanup

Christian Ebert blacktrash at gmx.net
Sun Oct 10 13:53:18 CDT 2010


# HG changeset patch
# User Christian Ebert <blacktrash at gmx.net>
# Date 1286736772 -3600
# Node ID dc096f38bfa55efa3139e9c7082316a8ced2cf07
# Parent  05077896ffe233bbaf9e1c180f18dd85fd45a8e4
keyword: code cleanup

- move preselection of expansion candidates for rollback
  and record into helper function
- same overwrite order in rollback and record:
  1. modified, 2. added
- self.wlock() inside kwrepo class instead of repo.wlock()

diff --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -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):
     '''
@@ -509,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()
@@ -569,13 +576,11 @@
             # 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:
-                changed = recctx.files()
-                modified = [f for f in modified if f in changed]
-                added = [f for f in added if f in changed]
+                modified, added = _preselect(wstatus, recctx.files())
                 kwt.restrict = False
                 kwt.overwrite(recctx, modified, False, True)
                 kwt.overwrite(recctx, added, False, True, True)


More information about the Mercurial-devel mailing list