Skip to content

Commit

Permalink
cmd/shfmt: add a way to ignore directory entries when walking
Browse files Browse the repository at this point in the history
This piggybacks on v3's EditorConfig feature, obeying the "ignore =
true" property.

The provided example uses a "/**" suffix, since that's the best way to
match a directory sub-tree right now. "third_party" alone could match a
non-directory with that name, and EditorConfig doesn't support patterns
like "third_party/". We could add support for that later if needed.

Once a directory is ignored, it's not walked at all. This way, huge
directories containing uninteresting files can be skipped entirely.

Fixes #288.
  • Loading branch information
mvdan committed Jan 21, 2020
1 parent b638b4b commit 2c15805
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ binary_next_line = true # like -bn
switch_case_indent = true # like -ci
space_redirects = true # like -sr
keep_padding = true # like -kp
# Ignore the entire "third_party" directory.
[third_party/**]
ignore = true
```

Packages are available on [Alpine], [Arch], [Docker], [FreeBSD], [Homebrew],
Expand Down
17 changes: 15 additions & 2 deletions cmd/shfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ func walk(path string, onError func(error)) {
if info.IsDir() && vcsDir.MatchString(info.Name()) {
return filepath.SkipDir
}
if useEditorConfig {
props, err := ecQuery.Find(path)
if err != nil {
return err
}
if props.Get("ignore") == "true" {
if info.IsDir() {
return filepath.SkipDir
} else {
return nil
}
}
}
conf := fileutil.CouldBeScript(info)
if conf == fileutil.ConfNotScript {
return nil
Expand All @@ -227,7 +240,7 @@ func walk(path string, onError func(error)) {
})
}

var query = editorconfig.Query{
var ecQuery = editorconfig.Query{
FileCache: make(map[string]*editorconfig.File),
RegexpCache: make(map[string]*regexp.Regexp),
}
Expand Down Expand Up @@ -287,7 +300,7 @@ func formatPath(path string, checkShebang bool) error {

func formatBytes(src []byte, path string) error {
if useEditorConfig {
props, err := query.Find(path)
props, err := ecQuery.Find(path)
if err != nil {
return err
}
Expand Down
26 changes: 26 additions & 0 deletions cmd/shfmt/testdata/scripts/editorconfig.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ shfmt -l otherknobs
! stdout .
! stderr .

# Ignore directories when walking, if they match ignore=true.
shfmt -l ignored
stdout 'regular\.sh'
! stdout 'ignored\.sh'
! stderr .

-- .editorconfig --
root = true

Expand Down Expand Up @@ -107,3 +113,23 @@ esac
echo foo > bar
-- otherknobs/keep_padding.sh --
echo foo bar
-- ignored/.editorconfig --
root = true

[third_party/**]
ignore = true

[1_lone_ignored.sh]
ignore = true

[2_dir_ignored]
ignore = true

-- ignored/third_party/ignored.sh --
bad (syntax
-- ignored/1_lone_ignored.sh
echo foo
-- ignored/2_dir_ignored/ignored.sh --
echo foo
-- ignored/3_regular/regular.sh --
echo foo

0 comments on commit 2c15805

Please sign in to comment.