diff mercurial/sparse.py @ 33317:df1287268cc0

sparse: move config signature logic into core This is a pretty straightforward port. It will be cleaned up in a subsequent commit.
author Gregory Szorc <gregory.szorc@gmail.com>
date Thu, 06 Jul 2017 16:11:56 -0700
parents 3e1accab7447
children 526255fe7899
line wrap: on
line diff
--- a/mercurial/sparse.py	Thu Jul 06 17:31:33 2017 -0700
+++ b/mercurial/sparse.py	Thu Jul 06 16:11:56 2017 -0700
@@ -7,6 +7,8 @@
 
 from __future__ import absolute_import
 
+import hashlib
+
 from .i18n import _
 from .node import nullid
 from . import (
@@ -130,6 +132,42 @@
 def invalidatesignaturecache(repo):
     repo._sparsesignaturecache.clear()
 
+def _checksum(repo, path):
+    data = repo.vfs.read(path)
+    return hashlib.sha1(data).hexdigest()
+
+def configsignature(repo, includetemp=True):
+    """Obtain the signature string for the current sparse configuration.
+
+    This is used to construct a cache key for matchers.
+    """
+    cache = repo._sparsesignaturecache
+
+    signature = cache.get('signature')
+
+    if includetemp:
+        tempsignature = cache.get('tempsignature')
+    else:
+        tempsignature = 0
+
+    if signature is None or (includetemp and tempsignature is None):
+        signature = 0
+        try:
+            signature = _checksum(repo, 'sparse')
+        except (OSError, IOError):
+            pass
+        cache['signature'] = signature
+
+        tempsignature = 0
+        if includetemp:
+            try:
+                tempsignature = _checksum(repo, 'tempsparse')
+            except (OSError, IOError):
+                pass
+            cache['tempsignature'] = tempsignature
+
+    return '%s %s' % (str(signature), str(tempsignature))
+
 def writeconfig(repo, includes, excludes, profiles):
     """Write the sparse config file given a sparse configuration."""
     with repo.vfs('sparse', 'wb') as fh: