[PATCH 1 of 2] match: introduce boolean prefix() method

Martin von Zweigbergk martinvonz at google.com
Wed May 20 18:29:26 CDT 2015

# HG changeset patch
# User Martin von Zweigbergk <martinvonz at google.com>
# Date 1414561642 25200
#      Tue Oct 28 22:47:22 2014 -0700
# Node ID 1ca06dc8213e456def24b52e1307efd50b4dd997
# Parent  451df92cec4912aefac57a4cf82e9268192c867b
match: introduce boolean prefix() method

tl;dr: This is another step towards a (previously unstated) goal of
eliminating match.files() in conditions.

There are four types of matchers:

 * always: Matches everything, checked with always(), files() is empty

 * exact: Matches exact set of files, checked with isexact(), files()
   contains the files to match

 * patterns: Matches more complex patterns, checked with anypats(),
   files() contains roots of the matched patterns

 * prefix: Matches simple 'path:' patterns as prefixes ('foo' matches
   both 'foo' and 'foo/bar'), no single method to check, files()
   contains the prefixes to match

For completeness, it would be nice to have a method for checking for
the "prefix" type of matcher as well, so let's add that, making it
return True simply when none of the others do.

The larger goal here is to eliminate uses of match.files() in
conditions (i.e. bool(match.files())). The reason for this is that
there are scenarios when you would like to create a "prefix" matcher
that happens to match no files. One example is for 'hg files -I foo
bar'. The narrowmatcher also restricts the set of files given and it
would not surprise me if have bugs caused by that already. Note that
'if m.files() and not m.anypats()' and similar is sometimes used to
catch the "exact" and "prefix" cases above.

diff --git a/mercurial/match.py b/mercurial/match.py
--- a/mercurial/match.py
+++ b/mercurial/match.py
@@ -206,6 +206,9 @@
     def isexact(self):
         return self.matchfn == self.exact
+    def prefix(self):
+        return not self.always() and not self.isexact() and not self.anypats()
     def _normalize(self, patterns, default, root, cwd, auditor):
         '''Convert 'kind:pat' from the patterns list to tuples with kind and
         normalized and rooted patterns and with listfiles expanded.'''

More information about the Mercurial-devel mailing list