Patchwork [3,of,7] match: add basic wrapper for boolean function

login
register
mail settings
Submitter Yuya Nishihara
Date July 5, 2018, 2:14 p.m.
Message ID <0b3716e343fed24d4d92.1530800068@mimosa>
Download mbox | patch
Permalink /patch/32643/
State New
Headers show

Comments

Yuya Nishihara - July 5, 2018, 2:14 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1528546404 -32400
#      Sat Jun 09 21:13:24 2018 +0900
# Node ID 0b3716e343fed24d4d921f5936e9db2837e9ca33
# Parent  6d9c3b65ffa5a397dfb163a392104349e97a22fe
match: add basic wrapper for boolean function

This serves as a generic wrapper for fileset predicates. In future patches,
a fileset expression will be mapped to a tree of matchers for a better support
of match attributes such as visitdir(). For example,

  $ hg debugwalk -v 'set:contrib/** and binary()'
  * matcher:
  <intersectionmatcher
    m1=<patternmatcher patterns='(?:contrib/.*$)'>,
    m2=<predicatenmatcher pred=binary>>
  ...

Patch

diff --git a/mercurial/match.py b/mercurial/match.py
--- a/mercurial/match.py
+++ b/mercurial/match.py
@@ -375,6 +375,20 @@  class nevermatcher(basematcher):
     def __repr__(self):
         return r'<nevermatcher>'
 
+class predicatematcher(basematcher):
+    """A matcher adapter for a simple boolean function"""
+
+    def __init__(self, root, cwd, predfn, predrepr=None, badfn=None):
+        super(predicatematcher, self).__init__(root, cwd, badfn)
+        self.matchfn = predfn
+        self._predrepr = predrepr
+
+    @encoding.strmethod
+    def __repr__(self):
+        s = (stringutil.buildrepr(self._predrepr)
+             or pycompat.byterepr(self.matchfn))
+        return '<predicatenmatcher pred=%s>' % s
+
 class patternmatcher(basematcher):
 
     def __init__(self, root, cwd, kindpats, ctx=None, listsubrepos=False,