From 376bb84c5c8f6183dbb88dae705e1de4182da4c9 Mon Sep 17 00:00:00 2001 From: Doug Davis Date: Thu, 1 Dec 2016 15:03:57 -0800 Subject: [PATCH] Fix use of **/ in .dockerignore .dockerignore pattern of **/.foo incorrectly matched **/bar.foo because **/.foo was getting converted into a .*\.foo regex instead of (.*/)*\.foo Closes #29014 Signed-off-by: Doug Davis --- pkg/fileutils/fileutils.go | 14 ++++++++------ pkg/fileutils/fileutils_test.go | 4 +++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pkg/fileutils/fileutils.go b/pkg/fileutils/fileutils.go index c63ae75ce8..aeb9908a3c 100644 --- a/pkg/fileutils/fileutils.go +++ b/pkg/fileutils/fileutils.go @@ -161,17 +161,19 @@ func regexpMatch(pattern, path string) (bool, error) { // is some flavor of "**" scan.Next() + // Treat **/ as ** so eat the "/" + if string(scan.Peek()) == sl { + scan.Next() + } + if scan.Peek() == scanner.EOF { // is "**EOF" - to align with .gitignore just accept all regStr += ".*" } else { // is "**" - regStr += "((.*" + escSL + ")|([^" + escSL + "]*))" - } - - // Treat **/ as ** so eat the "/" - if string(scan.Peek()) == sl { - scan.Next() + // Note that this allows for any # of /'s (even 0) because + // the .* will eat everything, even /'s + regStr += "(.*" + escSL + ")?" } } else { // is "*" so map it to anything but "/" diff --git a/pkg/fileutils/fileutils_test.go b/pkg/fileutils/fileutils_test.go index 6df1be89bb..23c48a4a82 100644 --- a/pkg/fileutils/fileutils_test.go +++ b/pkg/fileutils/fileutils_test.go @@ -325,7 +325,7 @@ func TestMatches(t *testing.T) { {"**", "/", true}, {"**/", "/", true}, {"**", "dir/file", true}, - {"**/", "dir/file", false}, + {"**/", "dir/file", true}, {"**", "dir/file/", true}, {"**/", "dir/file/", true}, {"**/**", "dir/file", true}, @@ -379,6 +379,8 @@ func TestMatches(t *testing.T) { {"abc/**", "abc", false}, {"abc/**", "abc/def", true}, {"abc/**", "abc/def/ghi", true}, + {"**/.foo", ".foo", true}, + {"**/.foo", "bar.foo", false}, } for _, test := range tests {