narrow: delegate the narrow spec writing to the transaction
authorPierre-Yves David <pierre-yves.david@octobus.net>
Thu, 23 Feb 2023 04:15:16 +0100
changeset 50225 8bc14ac53a41
parent 50224 99296ca9f29e
child 50226 f18e4608bb61
narrow: delegate the narrow spec writing to the transaction This make it more transactional and will help us to simplify their backup. The implementation is not great, but it keep the patch simple as this is not the time for a larger refactoring yet.
mercurial/localrepo.py
mercurial/narrowspec.py
--- a/mercurial/localrepo.py	Thu Feb 23 04:02:38 2023 +0100
+++ b/mercurial/localrepo.py	Thu Feb 23 04:15:16 2023 +0100
@@ -1469,6 +1469,8 @@
         # post-dirstate-status hooks
         self._postdsstatus = []
 
+        self._pending_narrow_pats = None
+
         # generic mapping between names and nodes
         self.names = namespaces.namespaces()
 
@@ -1799,7 +1801,11 @@
 
         A tuple of (includes, excludes).
         """
-        return narrowspec.load(self)
+        # the narrow management should probably move into its own object
+        val = self._pending_narrow_pats
+        if val is None:
+            val = narrowspec.load(self)
+        return val
 
     @storecache(narrowspec.FILENAME)
     def _storenarrowmatch(self):
--- a/mercurial/narrowspec.py	Thu Feb 23 04:02:38 2023 +0100
+++ b/mercurial/narrowspec.py	Thu Feb 23 04:15:16 2023 +0100
@@ -5,6 +5,7 @@
 # This software may be used and distributed according to the terms of the
 # GNU General Public License version 2 or any later version.
 
+import weakref
 
 from .i18n import _
 from .pycompat import getattr
@@ -174,10 +175,40 @@
 
 
 def save(repo, includepats, excludepats):
+    repo = repo.unfiltered()
+
     validatepatterns(includepats)
     validatepatterns(excludepats)
     spec = format(includepats, excludepats)
-    repo.svfs.write(FILENAME, spec)
+
+    tr = repo.currenttransaction()
+    if tr is None:
+        repo.svfs.write(FILENAME, spec)
+    else:
+        # the roundtrip is sometime different
+        # not taking any chance for now
+        value = parseconfig(repo.ui, spec)
+        reporef = weakref.ref(repo)
+
+        def clean_pending(tr):
+            r = reporef()
+            if r is not None:
+                r._pending_narrow_pats = None
+
+        tr.addpostclose(b'narrow-spec', clean_pending)
+        tr.addabort(b'narrow-spec', clean_pending)
+        repo._pending_narrow_pats = value
+
+        def write_spec(f):
+            f.write(spec)
+
+        tr.addfilegenerator(
+            # XXX think about order at some point
+            b"narrow-spec",
+            (FILENAME,),
+            write_spec,
+            location=b'store',
+        )
 
 
 def copytoworkingcopy(repo):