changeset 38869:ad24b581e4d9

narrow: call narrowspec.{save,restore,clear}backup directly I want to move .hg/narrowspec to .hg/store/narrowspec and we need to decouple the narrowspec update from the dirstate update for that. This patch lets the callers call the narrowspec backup functions directly, in addition to the dirstate backup functions. The narrowspec methods are made to check if narrowing is enabled. For that, a repo instance was needed, which all the callers luckily already had available. Differential Revision: https://phab.mercurial-scm.org/D4096
author Martin von Zweigbergk <martinvonz@google.com>
date Fri, 03 Aug 2018 11:09:41 -0700
parents 0db50770f388
children ae2962bb56a3
files hgext/narrow/narrowdirstate.py hgext/shelve.py mercurial/dirstateguard.py mercurial/localrepo.py mercurial/narrowspec.py
diffstat 5 files changed, 29 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/narrow/narrowdirstate.py	Sat Aug 04 23:15:06 2018 -0700
+++ b/hgext/narrow/narrowdirstate.py	Fri Aug 03 11:09:41 2018 -0700
@@ -11,7 +11,6 @@
 from mercurial import (
     error,
     match as matchmod,
-    narrowspec,
 )
 
 def wrapdirstate(repo, dirstate):
@@ -28,10 +27,6 @@
             return fn(self, *args)
         return _wrapper
 
-    def _narrowbackupname(backupname):
-        assert 'dirstate' in backupname
-        return backupname.replace('dirstate', narrowspec.FILENAME)
-
     class narrowdirstate(dirstate.__class__):
         def walk(self, match, subrepos, unknown, ignored, full=True,
                  narrowonly=True):
@@ -77,18 +72,5 @@
                 allfiles = [f for f in allfiles if repo.narrowmatch()(f)]
             super(narrowdirstate, self).rebuild(parent, allfiles, changedfiles)
 
-        def restorebackup(self, tr, backupname):
-            narrowspec.restorebackup(self._opener,
-                                     _narrowbackupname(backupname))
-            super(narrowdirstate, self).restorebackup(tr, backupname)
-
-        def savebackup(self, tr, backupname):
-            super(narrowdirstate, self).savebackup(tr, backupname)
-            narrowspec.savebackup(self._opener, _narrowbackupname(backupname))
-
-        def clearbackup(self, tr, backupname):
-            super(narrowdirstate, self).clearbackup(tr, backupname)
-            narrowspec.clearbackup(self._opener, _narrowbackupname(backupname))
-
     dirstate.__class__ = narrowdirstate
     return dirstate
--- a/hgext/shelve.py	Sat Aug 04 23:15:06 2018 -0700
+++ b/hgext/shelve.py	Fri Aug 03 11:09:41 2018 -0700
@@ -41,6 +41,7 @@
     lock as lockmod,
     mdiff,
     merge,
+    narrowspec,
     node as nodemod,
     patch,
     phases,
@@ -314,10 +315,13 @@
     '''Abort current transaction for shelve/unshelve, but keep dirstate
     '''
     tr = repo.currenttransaction()
-    backupname = 'dirstate.shelve'
-    repo.dirstate.savebackup(tr, backupname)
+    dirstatebackupname = 'dirstate.shelve'
+    narrowspecbackupname = 'narrowspec.shelve'
+    repo.dirstate.savebackup(tr, dirstatebackupname)
+    narrowspec.savebackup(repo, narrowspecbackupname)
     tr.abort()
-    repo.dirstate.restorebackup(None, backupname)
+    narrowspec.restorebackup(repo, narrowspecbackupname)
+    repo.dirstate.restorebackup(None, dirstatebackupname)
 
 def createcmd(ui, repo, pats, opts):
     """subcommand that creates a new shelve"""
--- a/mercurial/dirstateguard.py	Sat Aug 04 23:15:06 2018 -0700
+++ b/mercurial/dirstateguard.py	Fri Aug 03 11:09:41 2018 -0700
@@ -11,6 +11,7 @@
 
 from . import (
     error,
+    narrowspec,
     util,
 )
 
@@ -33,7 +34,10 @@
         self._active = False
         self._closed = False
         self._backupname = 'dirstate.backup.%s.%d' % (name, id(self))
+        self._narrowspecbackupname = ('narrowspec.backup.%s.%d' %
+                                      (name, id(self)))
         repo.dirstate.savebackup(repo.currenttransaction(), self._backupname)
+        narrowspec.savebackup(repo, self._narrowspecbackupname)
         self._active = True
 
     def __del__(self):
@@ -52,10 +56,12 @@
 
         self._repo.dirstate.clearbackup(self._repo.currenttransaction(),
                                          self._backupname)
+        narrowspec.clearbackup(self._repo, self._narrowspecbackupname)
         self._active = False
         self._closed = True
 
     def _abort(self):
+        narrowspec.restorebackup(self._repo, self._narrowspecbackupname)
         self._repo.dirstate.restorebackup(self._repo.currenttransaction(),
                                            self._backupname)
         self._active = False
--- a/mercurial/localrepo.py	Sat Aug 04 23:15:06 2018 -0700
+++ b/mercurial/localrepo.py	Fri Aug 03 11:09:41 2018 -0700
@@ -1373,6 +1373,7 @@
             else:
                 # discard all changes (including ones already written
                 # out) in this transaction
+                narrowspec.restorebackup(self, 'journal.narrowspec')
                 repo.dirstate.restorebackup(None, 'journal.dirstate')
 
                 repo.invalidate(clearfilecache=True)
@@ -1461,6 +1462,7 @@
     @unfilteredmethod
     def _writejournal(self, desc):
         self.dirstate.savebackup(None, 'journal.dirstate')
+        narrowspec.savebackup(self, 'journal.narrowspec')
         self.vfs.write("journal.branch",
                           encoding.fromlocal(self.dirstate.branch()))
         self.vfs.write("journal.desc",
@@ -1548,6 +1550,7 @@
             # prevent dirstateguard from overwriting already restored one
             dsguard.close()
 
+            narrowspec.restorebackup(self, 'undo.narrowspec')
             self.dirstate.restorebackup(None, 'undo.dirstate')
             try:
                 branch = self.vfs.read('undo.branch')
--- a/mercurial/narrowspec.py	Sat Aug 04 23:15:06 2018 -0700
+++ b/mercurial/narrowspec.py	Fri Aug 03 11:09:41 2018 -0700
@@ -13,6 +13,7 @@
 from . import (
     error,
     match as matchmod,
+    repository,
     sparse,
     util,
 )
@@ -129,15 +130,22 @@
     spec = format(includepats, excludepats)
     repo.vfs.write(FILENAME, spec)
 
-def savebackup(vfs, backupname):
+def savebackup(repo, backupname):
+    if repository.NARROW_REQUIREMENT not in repo.requirements:
+        return
+    vfs = repo.vfs
     vfs.tryunlink(backupname)
     util.copyfile(vfs.join(FILENAME), vfs.join(backupname), hardlink=True)
 
-def restorebackup(vfs, backupname):
-    vfs.rename(backupname, FILENAME, checkambig=True)
+def restorebackup(repo, backupname):
+    if repository.NARROW_REQUIREMENT not in repo.requirements:
+        return
+    repo.vfs.rename(backupname, FILENAME, checkambig=True)
 
-def clearbackup(vfs, backupname):
-    vfs.unlink(backupname)
+def clearbackup(repo, backupname):
+    if repository.NARROW_REQUIREMENT not in repo.requirements:
+        return
+    repo.vfs.unlink(backupname)
 
 def restrictpatterns(req_includes, req_excludes, repo_includes, repo_excludes):
     r""" Restricts the patterns according to repo settings,