Skip to content

Commit

Permalink
expand: simplify globbing code
Browse files Browse the repository at this point in the history
Fields doesn't need to do that much work. We can also move an absolute
path check to globDir.
  • Loading branch information
mvdan committed Dec 16, 2018
1 parent 5774069 commit 2c24ce2
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions expand/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,27 +328,17 @@ func Fields(cfg *Config, words ...*syntax.Word) ([]string, error) {
for _, field := range wfields {
path, doGlob := cfg.escapedGlobField(field)
var matches []string
abs := filepath.IsAbs(path)
if doGlob && !cfg.NoGlob {
base := ""
if !abs {
base = dir
}
matches, err = cfg.glob(base, path)
matches, err = cfg.glob(dir, path)
if err != nil {
return nil, err
}
}
if len(matches) == 0 {
fields = append(fields, cfg.fieldJoin(field))
continue
}
for _, match := range matches {
if !abs {
match = strings.TrimPrefix(match, dir)
if len(matches) > 0 {
fields = append(fields, matches...)
continue
}
fields = append(fields, match)
}
fields = append(fields, cfg.fieldJoin(field))
}
}
}
Expand Down Expand Up @@ -701,7 +691,11 @@ func (cfg *Config) globDir(base, dir string, rx *regexp.Regexp, matches []string
// TODO(mvdan): check this at the beginning of a glob?
return nil, nil
}
infos, err := cfg.ReadDir(filepath.Join(base, dir))
fullDir := dir
if !filepath.IsAbs(dir) {
fullDir = filepath.Join(base, dir)
}
infos, err := cfg.ReadDir(fullDir)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2c24ce2

Please sign in to comment.