Patchwork sparse: override __repr__ in matchers

login
register
mail settings
Submitter via Mercurial-devel
Date July 7, 2017, 12:16 a.m.
Message ID <2fb74a0768a6364c5cf2.1499386593@martinvonz.svl.corp.google.com>
Download mbox | patch
Permalink /patch/22072/
State Accepted
Headers show

Comments

via Mercurial-devel - July 7, 2017, 12:16 a.m.
# HG changeset patch
# User Martin von Zweigbergk <martinvonz@google.com>
# Date 1499384256 25200
#      Thu Jul 06 16:37:36 2017 -0700
# Node ID 2fb74a0768a6364c5cf23eb34423c93d08278c07
# Parent  3e1accab7447b67ebb58d5b5da341e553a4cc564
sparse: override __repr__ in matchers

sparse.py in FB's hg-experimental repo switched to using __repr__ for
non-sparse matchers soon after hg core started overriding __repr__ in
the matchers in match.py (because the core matchers also stopped
having "includepat" and other attributes that sparse used to depend
on). Let's finish that migration by implementing __repr__ in the
sparse matchers as well. That also lets us remove the special handling
of them in _hashmatcher().
Gregory Szorc - July 7, 2017, 12:26 a.m.
On Thu, Jul 6, 2017 at 5:16 PM, Martin von Zweigbergk <martinvonz@google.com
> wrote:

> # HG changeset patch
> # User Martin von Zweigbergk <martinvonz@google.com>
> # Date 1499384256 25200
> #      Thu Jul 06 16:37:36 2017 -0700
> # Node ID 2fb74a0768a6364c5cf23eb34423c93d08278c07
> # Parent  3e1accab7447b67ebb58d5b5da341e553a4cc564
> sparse: override __repr__ in matchers
>
> sparse.py in FB's hg-experimental repo switched to using __repr__ for
> non-sparse matchers soon after hg core started overriding __repr__ in
> the matchers in match.py (because the core matchers also stopped
> having "includepat" and other attributes that sparse used to depend
> on). Let's finish that migration by implementing __repr__ in the
> sparse matchers as well. That also lets us remove the special handling
> of them in _hashmatcher().
>

Heh - I wrote this patch locally. So it seems reasonable to me.

The one thing this patch didn't do is remove the code to change the hash
matcher for fsmonitor, as sparse's and fsmonitor's implementations are
identical after this patch. But that's trivial enough to do in a follow-up.

I guess I'll queue this then rebase my patches. That means we can move the
sparse matchers directly to match.py. Huzzah.

Queued, thanks.


>
> diff --git a/hgext/sparse.py b/hgext/sparse.py
> --- a/hgext/sparse.py
> +++ b/hgext/sparse.py
> @@ -928,12 +928,9 @@
>      def prefix(self):
>          return False
>
> -    def hash(self):
> -        sha1 = hashlib.sha1()
> -        sha1.update(_hashmatcher(self._matcher))
> -        for include in sorted(self._includes):
> -            sha1.update(include + '\0')
> -        return sha1.hexdigest()
> +    def __repr__(self):
> +        return ('<forceincludematcher matcher=%r, includes=%r>' %
> +                (self._matcher, sorted(self._includes)))
>
>  class unionmatcher(object):
>      """A matcher that is the union of several matchers."""
> @@ -961,11 +958,8 @@
>      def prefix(self):
>          return False
>
> -    def hash(self):
> -        sha1 = hashlib.sha1()
> -        for m in self._matchers:
> -            sha1.update(_hashmatcher(m))
> -        return sha1.hexdigest()
> +    def __repr__(self):
> +        return ('<unionmatcher matchers=%r>' % self._matchers)
>
>  class negatematcher(object):
>      def __init__(self, matcher):
> @@ -986,16 +980,10 @@
>      def anypats(self):
>          return True
>
> -    def hash(self):
> -        sha1 = hashlib.sha1()
> -        sha1.update('negate')
> -        sha1.update(_hashmatcher(self._matcher))
> -        return sha1.hexdigest()
> +    def __repr__(self):
> +        return ('<negatematcher matcher=%r>' % self._matcher)
>
>  def _hashmatcher(matcher):
> -    if util.safehasattr(matcher, 'hash'):
> -        return matcher.hash()
> -
>      sha1 = hashlib.sha1()
>      sha1.update(repr(matcher))
>      return sha1.hexdigest()
>

Patch

diff --git a/hgext/sparse.py b/hgext/sparse.py
--- a/hgext/sparse.py
+++ b/hgext/sparse.py
@@ -928,12 +928,9 @@ 
     def prefix(self):
         return False
 
-    def hash(self):
-        sha1 = hashlib.sha1()
-        sha1.update(_hashmatcher(self._matcher))
-        for include in sorted(self._includes):
-            sha1.update(include + '\0')
-        return sha1.hexdigest()
+    def __repr__(self):
+        return ('<forceincludematcher matcher=%r, includes=%r>' %
+                (self._matcher, sorted(self._includes)))
 
 class unionmatcher(object):
     """A matcher that is the union of several matchers."""
@@ -961,11 +958,8 @@ 
     def prefix(self):
         return False
 
-    def hash(self):
-        sha1 = hashlib.sha1()
-        for m in self._matchers:
-            sha1.update(_hashmatcher(m))
-        return sha1.hexdigest()
+    def __repr__(self):
+        return ('<unionmatcher matchers=%r>' % self._matchers)
 
 class negatematcher(object):
     def __init__(self, matcher):
@@ -986,16 +980,10 @@ 
     def anypats(self):
         return True
 
-    def hash(self):
-        sha1 = hashlib.sha1()
-        sha1.update('negate')
-        sha1.update(_hashmatcher(self._matcher))
-        return sha1.hexdigest()
+    def __repr__(self):
+        return ('<negatematcher matcher=%r>' % self._matcher)
 
 def _hashmatcher(matcher):
-    if util.safehasattr(matcher, 'hash'):
-        return matcher.hash()
-
     sha1 = hashlib.sha1()
     sha1.update(repr(matcher))
     return sha1.hexdigest()