[PATCH 2 of 2] match: implement __repr__() and update users (API)

Martin von Zweigbergk martinvonz at google.com
Mon May 22 14:22:33 EDT 2017


# HG changeset patch
# User Martin von Zweigbergk <martinvonz at google.com>
# Date 1495476498 25200
#      Mon May 22 11:08:18 2017 -0700
# Node ID fa82a6f7adb3deef43dacf5059e906eed9a1beba
# Parent  bdc4861ffe597d6dc0c19b57dcb98edaf5aaa89f
match: implement __repr__() and update users (API)

fsmonitor and debugignore currently access matcher fields that I would
consider implementation details, namely patternspat, includepat, and
excludepat. Let' instead implement __repr__() and have the few users
use that instead.

Marked (API) because the fields can now be None.

diff --git a/hgext/fsmonitor/__init__.py b/hgext/fsmonitor/__init__.py
--- a/hgext/fsmonitor/__init__.py
+++ b/hgext/fsmonitor/__init__.py
@@ -148,19 +148,7 @@
 
     """
     sha1 = hashlib.sha1()
-    if util.safehasattr(ignore, 'includepat'):
-        sha1.update(ignore.includepat)
-    sha1.update('\0\0')
-    if util.safehasattr(ignore, 'excludepat'):
-        sha1.update(ignore.excludepat)
-    sha1.update('\0\0')
-    if util.safehasattr(ignore, 'patternspat'):
-        sha1.update(ignore.patternspat)
-    sha1.update('\0\0')
-    if util.safehasattr(ignore, '_files'):
-        for f in ignore._files:
-            sha1.update(f)
-    sha1.update('\0')
+    sha1.update(repr(ignore))
     return sha1.hexdigest()
 
 _watchmanencoding = pywatchman.encoding.get_local_encoding()
diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -810,11 +810,7 @@
     ignore = repo.dirstate._ignore
     if not files:
         # Show all the patterns
-        includepat = getattr(ignore, 'includepat', None)
-        if includepat is not None:
-            ui.write("%s\n" % includepat)
-        else:
-            raise error.Abort(_("no ignore patterns found"))
+        ui.write("%s\n" % repr(ignore))
     else:
         for f in files:
             nf = util.normpath(f)
diff --git a/mercurial/match.py b/mercurial/match.py
--- a/mercurial/match.py
+++ b/mercurial/match.py
@@ -214,6 +214,9 @@
         self._anypats = bool(include or exclude)
         self._always = False
         self._pathrestricted = bool(include or exclude or patterns)
+        self.patternspat = None
+        self.includepat = None
+        self.excludepat = None
 
         # roots are directories which are recursively included/excluded.
         self._includeroots = set()
@@ -375,6 +378,11 @@
     def prefix(self):
         return not self.always() and not self.isexact() and not self.anypats()
 
+    def __repr__(self):
+        return ('<matcher files=%r, patterns=%r, includes=%r, excludes=%r>' %
+                (self._files, self.patternspat, self.includepat,
+                 self.excludepat))
+
 class subdirmatcher(matcher):
     """Adapt a matcher to work on a subdirectory only.
 
diff --git a/tests/test-hgignore.t b/tests/test-hgignore.t
--- a/tests/test-hgignore.t
+++ b/tests/test-hgignore.t
@@ -164,7 +164,7 @@
   A b.o
 
   $ hg debugignore
-  (?:(?:|.*/)[^/]*(?:/|$))
+  <matcher files=[], patterns=None, includes=(?:(?:|.*/)[^/]*(?:/|$)), excludes=None>
 
   $ hg debugignore b.o
   b.o is ignored


More information about the Mercurial-devel mailing list