Browse code

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 <dug@us.ibm.com>

Doug Davis authored on 2016/12/02 08:03:57
Showing 2 changed files
... ...
@@ -161,17 +161,19 @@ func regexpMatch(pattern, path string) (bool, error) {
161 161
 				// is some flavor of "**"
162 162
 				scan.Next()
163 163
 
164
+				// Treat **/ as ** so eat the "/"
165
+				if string(scan.Peek()) == sl {
166
+					scan.Next()
167
+				}
168
+
164 169
 				if scan.Peek() == scanner.EOF {
165 170
 					// is "**EOF" - to align with .gitignore just accept all
166 171
 					regStr += ".*"
167 172
 				} else {
168 173
 					// is "**"
169
-					regStr += "((.*" + escSL + ")|([^" + escSL + "]*))"
170
-				}
171
-
172
-				// Treat **/ as ** so eat the "/"
173
-				if string(scan.Peek()) == sl {
174
-					scan.Next()
174
+					// Note that this allows for any # of /'s (even 0) because
175
+					// the .* will eat everything, even /'s
176
+					regStr += "(.*" + escSL + ")?"
175 177
 				}
176 178
 			} else {
177 179
 				// is "*" so map it to anything but "/"
... ...
@@ -325,7 +325,7 @@ func TestMatches(t *testing.T) {
325 325
 		{"**", "/", true},
326 326
 		{"**/", "/", true},
327 327
 		{"**", "dir/file", true},
328
-		{"**/", "dir/file", false},
328
+		{"**/", "dir/file", true},
329 329
 		{"**", "dir/file/", true},
330 330
 		{"**/", "dir/file/", true},
331 331
 		{"**/**", "dir/file", true},
... ...
@@ -379,6 +379,8 @@ func TestMatches(t *testing.T) {
379 379
 		{"abc/**", "abc", false},
380 380
 		{"abc/**", "abc/def", true},
381 381
 		{"abc/**", "abc/def/ghi", true},
382
+		{"**/.foo", ".foo", true},
383
+		{"**/.foo", "bar.foo", false},
382 384
 	}
383 385
 
384 386
 	for _, test := range tests {