match: implement __repr__() and update users (API)
authorMartin von Zweigbergk <martinvonz@google.com>
Mon, 22 May 2017 11:08:18 -0700
changeset 32406 952017471f93
parent 32405 bdc4861ffe59
child 32407 a7dce526c462
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.
hgext/fsmonitor/__init__.py
mercurial/debugcommands.py
mercurial/match.py
tests/test-hgignore.t
--- a/hgext/fsmonitor/__init__.py	Mon May 22 10:41:32 2017 -0700
+++ b/hgext/fsmonitor/__init__.py	Mon May 22 11:08:18 2017 -0700
@@ -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()
--- a/mercurial/debugcommands.py	Mon May 22 10:41:32 2017 -0700
+++ b/mercurial/debugcommands.py	Mon May 22 11:08:18 2017 -0700
@@ -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)
--- a/mercurial/match.py	Mon May 22 10:41:32 2017 -0700
+++ b/mercurial/match.py	Mon May 22 11:08:18 2017 -0700
@@ -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.
 
--- a/tests/test-hgignore.t	Mon May 22 10:41:32 2017 -0700
+++ b/tests/test-hgignore.t	Mon May 22 11:08:18 2017 -0700
@@ -164,7 +164,7 @@
   A b.o
 
   $ hg debugignore
-  (?:(?:|.*/)[^/]*(?:/|$))
+  <matcher files=[], patterns=None, includes='(?:(?:|.*/)[^/]*(?:/|$))', excludes=None>
 
   $ hg debugignore b.o
   b.o is ignored