Skip to content

Commit

Permalink
expand: support globbing directories only
Browse files Browse the repository at this point in the history
That is, "foo*/" and "foo*/." should match a "foobar" directory, but not
a "foobar" file.

Fixes mvdan#337.
  • Loading branch information
mvdan committed Dec 16, 2018
1 parent 2c24ce2 commit 41839c7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
19 changes: 16 additions & 3 deletions expand/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,23 @@ func (cfg *Config) glob(base, pattern string) ([]string, error) {
}
for _, part := range parts {
switch {
case part == ".", part == "..":
for i, dir := range matches {
matches[i] = pathJoin2(dir, part)
case part == "", part == ".", part == "..":
var newMatches []string
for _, dir := range matches {
// TODO(mvdan): reuse the previous ReadDir call
if cfg.ReadDir == nil {
continue // no globbing
}
fullDir := dir
if !filepath.IsAbs(dir) {
fullDir = filepath.Join(base, dir)
}
if _, err := cfg.ReadDir(fullDir); err != nil {
continue // not actually a dir
}
newMatches = append(newMatches, pathJoin2(dir, part))
}
matches = newMatches
continue
case part == "**" && cfg.GlobStar:
for i, match := range matches {
Expand Down
12 changes: 12 additions & 0 deletions interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,18 @@ set +o pipefail
"mkdir -p a/b a/c d; cd d; echo ../a/* | sed 's@\\\\@/@g'",
"../a/b ../a/c\n",
},
{
"mkdir x-d1 x-d2; touch x-f; echo x-*/ | sed -e 's@\\\\@/@g'",
"x-d1/ x-d2/\n",
},
{
"mkdir x-d1 x-d2; touch x-f; echo ././x-*/// | sed -e 's@\\\\@/@g'",
"././x-d1/ ././x-d2/\n",
},
{
"mkdir x-d; touch x-f; test -d $PWD/x-*/",
"",
},

// brace expansion; more exhaustive tests in the syntax package
{"echo a}b", "a}b\n"},
Expand Down

0 comments on commit 41839c7

Please sign in to comment.