match: make glob '**/' match the empty string stable
authorSiddharth Agarwal <sid0@fb.com>
Wed, 25 Jun 2014 14:50:48 -0700
branchstable
changeset 21815 a4b67bf1f0a5
parent 21761 b2dc026a9bd2
child 21816 212955411633
child 21823 925d1bb9a971
child 21827 57012df4f423
match: make glob '**/' match the empty string Previously, a glob pattern of the form 'foo/**/bar' would match 'foo/a/bar' but not 'foo/bar'. That was because the '**' in 'foo/**/bar' would be translated to '.*', making the final regex pattern 'foo/.*/bar'. That pattern doesn't match the string 'foo/bar'. This is a bug because the '**/' glob matches the empty string in standard Unix shells like bash and zsh. Fix that by making the ending '/' optional if an empty string can be matched.
mercurial/match.py
tests/test-hgignore.t
--- a/mercurial/match.py	Wed Jun 18 19:46:18 2014 -0500
+++ b/mercurial/match.py	Wed Jun 25 14:50:48 2014 -0700
@@ -233,6 +233,10 @@
     [^/]*
     >>> print _globre(r'**')
     .*
+    >>> print _globre(r'**/a')
+    (?:.*/)?a
+    >>> print _globre(r'a/**/b')
+    a\/(?:.*/)?b
     >>> print _globre(r'[a*?!^][^b][!c]')
     [a*?!^][\^b][^c]
     >>> print _globre(r'{a,b}')
@@ -254,7 +258,11 @@
         elif c == '*':
             if peek() == '*':
                 i += 1
-                res += '.*'
+                if peek() == '/':
+                    i += 1
+                    res += '(?:.*/)?'
+                else:
+                    res += '.*'
             else:
                 res += '[^/]*'
         elif c == '?':
--- a/tests/test-hgignore.t	Wed Jun 18 19:46:18 2014 -0500
+++ b/tests/test-hgignore.t	Wed Jun 25 14:50:48 2014 -0700
@@ -134,3 +134,17 @@
   ? a.c
   ? a.o
   ? syntax
+
+Check recursive glob pattern matches no directories (dir/**/c.o matches dir/c.o)
+
+  $ echo "syntax: glob" > .hgignore
+  $ echo "dir/**/c.o" >> .hgignore
+  $ touch dir/c.o
+  $ mkdir dir/subdir
+  $ touch dir/subdir/c.o
+  $ hg status
+  A dir/b.o
+  ? .hgignore
+  ? a.c
+  ? a.o
+  ? syntax