Patchwork [02,of,11,sparse,V3] sparse: move config signature logic into core

login
register
mail settings
Submitter Gregory Szorc
Date July 7, 2017, 1:18 a.m.
Message ID <01f89a68e9269eec2492.1499390303@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/22073/
State Accepted
Headers show

Comments

Gregory Szorc - July 7, 2017, 1:18 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1499382716 25200
#      Thu Jul 06 16:11:56 2017 -0700
# Node ID 01f89a68e9269eec249207478e1e8344e90b7496
# Parent  310f7bcab50bc92d6ff93ee1c6b055a636925123
sparse: move config signature logic into core

This is a pretty straightforward port. It will be cleaned up in
a subsequent commit.
via Mercurial-devel - July 7, 2017, 5 a.m.
On Thu, Jul 6, 2017 at 6:18 PM, Gregory Szorc <gregory.szorc@gmail.com> wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc@gmail.com>
> # Date 1499382716 25200
> #      Thu Jul 06 16:11:56 2017 -0700
> # Node ID 01f89a68e9269eec249207478e1e8344e90b7496
> # Parent  310f7bcab50bc92d6ff93ee1c6b055a636925123
> sparse: move config signature logic into core
>
> This is a pretty straightforward port. It will be cleaned up in
> a subsequent commit.
>
> diff --git a/hgext/sparse.py b/hgext/sparse.py
> --- a/hgext/sparse.py
> +++ b/hgext/sparse.py
> @@ -405,38 +405,6 @@ def _setupdirstate(ui):
>
>  def _wraprepo(ui, repo):
>      class SparseRepo(repo.__class__):
> -        def _sparsechecksum(self, path):
> -            data = self.vfs.read(path)
> -            return hashlib.sha1(data).hexdigest()

With patch 1 added to the series, this was the last hashlib use, so
check-imports was unhappy. Fixing in flight.

Patch

diff --git a/hgext/sparse.py b/hgext/sparse.py
--- a/hgext/sparse.py
+++ b/hgext/sparse.py
@@ -405,38 +405,6 @@  def _setupdirstate(ui):
 
 def _wraprepo(ui, repo):
     class SparseRepo(repo.__class__):
-        def _sparsechecksum(self, path):
-            data = self.vfs.read(path)
-            return hashlib.sha1(data).hexdigest()
-
-        def _sparsesignature(self, includetemp=True):
-            """Returns the signature string representing the contents of the
-            current project sparse configuration. This can be used to cache the
-            sparse matcher for a given set of revs."""
-            signaturecache = self._sparsesignaturecache
-            signature = signaturecache.get('signature')
-            if includetemp:
-                tempsignature = signaturecache.get('tempsignature')
-            else:
-                tempsignature = 0
-
-            if signature is None or (includetemp and tempsignature is None):
-                signature = 0
-                try:
-                    signature = self._sparsechecksum('sparse')
-                except (OSError, IOError):
-                    pass
-                signaturecache['signature'] = signature
-
-                tempsignature = 0
-                if includetemp:
-                    try:
-                        tempsignature = self._sparsechecksum('tempsparse')
-                    except (OSError, IOError):
-                        pass
-                    signaturecache['tempsignature'] = tempsignature
-            return '%s %s' % (str(signature), str(tempsignature))
-
         def sparsematch(self, *revs, **kwargs):
             """Returns the sparse match function for the given revs.
 
@@ -451,7 +419,7 @@  def _wraprepo(ui, repo):
                     self.dirstate.parents() if node != nullid]
 
             includetemp = kwargs.get('includetemp', True)
-            signature = self._sparsesignature(includetemp=includetemp)
+            signature = sparse.configsignature(self, includetemp=includetemp)
 
             key = '%s %s' % (str(signature), ' '.join([str(r) for r in revs]))
 
diff --git a/mercurial/sparse.py b/mercurial/sparse.py
--- a/mercurial/sparse.py
+++ b/mercurial/sparse.py
@@ -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 activeprofiles(repo):
 def invalidatesignaturecache(repo):
     repo._sparsesignaturecache.clear()
 
+def _checksum(self, path):
+    data = self.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('sparse')
+        except (OSError, IOError):
+            pass
+        cache['signature'] = signature
+
+        tempsignature = 0
+        if includetemp:
+            try:
+                tempsignature = _checksum('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: