[PATCH 1 of 4] match: introduce an anyfiles method

Martin Geisler mg at aragost.com
Thu Sep 23 10:03:19 CDT 2010


# HG changeset patch
# User Martin Geisler <mg at aragost.com>
# Date 1285253484 -7200
# Node ID fe6086f0af535e5ce27c15ab25b91c630b448339
# Parent  aff4afdcfd2ba82b7c3ad1066336e1b0494b4b89
match: introduce an anyfiles method

With the introduction of narrowed matchers, we face a problem when we
want to distinguish between a matcher that is built from no explicit
files and a matcher that just happens to match no files because it is
narrowed. We need to distinguish them to tell the difference between

  hg log

and

  hg log a.txt

when we enter a subrepository and thus end up with a narrow matcher m
where m.files() == []. In that case, log would wrongly default to show
all changesets in the subrepository.

Adding the anyfiles method lets the narrow matcher signal "yes, files
were entered on the command line", while still returning no files when
asked via the files method.

diff --git a/mercurial/match.py b/mercurial/match.py
--- a/mercurial/match.py
+++ b/mercurial/match.py
@@ -78,6 +78,7 @@
 
         self.matchfn = m
         self._fmap = set(self._files)
+        self._anyfiles = bool(self._files)
 
     def __call__(self, fn):
         return self.matchfn(fn)
@@ -99,6 +100,8 @@
         return util.pathto(self._root, self._cwd, f)
     def files(self):
         return self._files
+    def anyfiles(self):
+        return self._anyfiles
     def anypats(self):
         return self._anypats
 
@@ -136,6 +139,11 @@
     >>> m1.bad = bad
     >>> m2.bad('x.txt', 'No such file')
     sub/x.txt: No such file
+    >>> m3 = narrowmatcher('sub/sub', m1)
+    >>> m3.files()
+    []
+    >>> m3.anyfiles()
+    True
     """
 
     def __init__(self, path, matcher):
@@ -146,6 +154,7 @@
 
         self._files = [f[len(path) + 1:] for f in matcher._files
                        if f.startswith(path + "/")]
+        self._anyfiles = matcher._anyfiles
         self._anypats = matcher._anypats
         self.matchfn = lambda fn: matcher.matchfn(self._path + "/" + fn)
         self._fmap = set(self._files)


More information about the Mercurial-devel mailing list