sparse: start moving away from the global variable for detection of usage
authorPierre-Yves David <pierre-yves.david@octobus.net>
Wed, 08 Jun 2022 09:31:01 +0200
changeset 49354 216f273b6b30
parent 49353 fa8d974284f8
child 49355 0540c1628fd4
sparse: start moving away from the global variable for detection of usage Code is now checking if the repository using the sparse feature and that's it. Some code in `debugsparse` still rely on "global" state, as it apply sparse logic before updating the requirement. Cleaning that up is more work that we signed up for, but we could narrow the hack to that specific command.
hgext/sparse.py
mercurial/sparse.py
--- a/hgext/sparse.py	Fri Jun 10 19:54:08 2022 +0200
+++ b/hgext/sparse.py	Wed Jun 08 09:31:01 2022 +0200
@@ -397,6 +397,9 @@
     if count > 1:
         raise error.Abort(_(b"too many flags specified"))
 
+    # enable sparse on repo even if the requirements is missing.
+    repo._has_sparse = True
+
     if count == 0:
         if repo.vfs.exists(b'sparse'):
             ui.status(repo.vfs.read(b"sparse") + b"\n")
@@ -452,3 +455,5 @@
             )
         finally:
             wlock.release()
+
+    del repo._has_sparse
--- a/mercurial/sparse.py	Fri Jun 10 19:54:08 2022 +0200
+++ b/mercurial/sparse.py	Wed Jun 08 09:31:01 2022 +0200
@@ -30,6 +30,16 @@
 enabled = False
 
 
+def use_sparse(repo):
+    if getattr(repo, "_has_sparse", False):
+        # When enabling sparse the first time we need it to be enabled before
+        # actually enabling it.  This hack could be avoided if the code was
+        # improved further, however this is an improvement over the previously
+        # existing global variable.
+        return True
+    return requirements.SPARSE_REQUIREMENT in repo.requirements
+
+
 def parseconfig(ui, raw, action):
     """Parse sparse config file content.
 
@@ -114,7 +124,7 @@
     patterns.
     """
     # Feature isn't enabled. No-op.
-    if not enabled:
+    if not use_sparse(repo):
         return set(), set(), set()
 
     raw = repo.vfs.tryread(b'sparse')
@@ -260,7 +270,7 @@
 
 
 def prunetemporaryincludes(repo):
-    if not enabled or not repo.vfs.exists(b'tempsparse'):
+    if not use_sparse(repo) or not repo.vfs.exists(b'tempsparse'):
         return
 
     s = repo.status()
@@ -313,7 +323,7 @@
     ``includetemp`` indicates whether to use the temporary sparse profile.
     """
     # If sparse isn't enabled, sparse matcher matches everything.
-    if not enabled:
+    if not use_sparse(repo):
         return matchmod.always()
 
     if not revs or revs == [None]:
@@ -367,7 +377,7 @@
 
 def filterupdatesactions(repo, wctx, mctx, branchmerge, mresult):
     """Filter updates to only lay out files that match the sparse rules."""
-    if not enabled:
+    if not use_sparse(repo):
         return
 
     oldrevs = [pctx.rev() for pctx in wctx.parents()]