-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto-exclude gitignore patterns (#109)
- Loading branch information
1 parent
22fd462
commit e815ad3
Showing
12 changed files
with
260 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
excludes: | ||
- dist/** | ||
- .cspellcache | ||
- coverage.out | ||
output: | ||
show_context: true | ||
show_url: true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ words: | |
- gopls | ||
- goreleaser | ||
- gosec | ||
- gostub | ||
- hadolint | ||
- infof | ||
- iostreams | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package stylist | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/bmatcuk/doublestar/v4" | ||
"github.com/denormal/go-gitignore" | ||
|
||
"github.com/twelvelabs/stylist/internal/fsutils" | ||
) | ||
|
||
var ( | ||
newGitIgnoreParser = gitignore.NewFromFile | ||
) | ||
|
||
// NewPathIgnorer returns a new path ignorer. | ||
func NewPathIgnorer(gitIgnorePath string, patterns []string) (*PathIgnorer, error) { | ||
cleanPatterns := []string{} | ||
for _, pattern := range patterns { | ||
p := strings.ReplaceAll(pattern, "\\", "/") | ||
if !doublestar.ValidatePattern(p) { | ||
return nil, doublestar.ErrBadPattern | ||
} | ||
cleanPatterns = append(cleanPatterns, p) | ||
} | ||
|
||
pi := &PathIgnorer{ | ||
patterns: cleanPatterns, | ||
} | ||
if gitIgnorePath != "" && fsutils.PathExists(gitIgnorePath) { | ||
gitIgnore, err := newGitIgnoreParser(gitIgnorePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("gitignore parse: %w", err) | ||
} | ||
pi.gitIgnore = gitIgnore | ||
pi.gitIgnorePath = gitIgnorePath | ||
} | ||
return pi, nil | ||
} | ||
|
||
// PathIgnorer is responsible for ignoring paths during the index process. | ||
type PathIgnorer struct { | ||
gitIgnore gitignore.GitIgnore | ||
gitIgnorePath string | ||
patterns []string | ||
} | ||
|
||
// ShouldIgnore returns true if path should be ignored. | ||
func (pi *PathIgnorer) ShouldIgnore(path string, isDir bool) bool { | ||
if pi.gitIgnore != nil { | ||
// Note: Specifically using `.Relative` for speed. | ||
// The higher level GitIgnore methods (Ignore, Match) | ||
// stat the path to figure out isDir. | ||
if match := pi.gitIgnore.Relative(path, isDir); match != nil { | ||
if match.Ignore() { | ||
return true | ||
} | ||
} | ||
} | ||
for _, pattern := range pi.patterns { | ||
if ok, _ := doublestar.Match(pattern, filepath.ToSlash(path)); ok { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package stylist | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/prashantv/gostub" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewPathIgnorer(t *testing.T) { | ||
require := require.New(t) | ||
|
||
ignorer, err := NewPathIgnorer("", nil) | ||
require.NoError(err) | ||
require.Empty(ignorer.gitIgnore) | ||
require.Empty(ignorer.gitIgnorePath) | ||
require.Empty(ignorer.patterns) | ||
|
||
ignorer, err = NewPathIgnorer("testdata/.gitignore", []string{ | ||
`\Documents\**\*.bat`, | ||
}) | ||
require.NoError(err) | ||
require.NotNil(ignorer.gitIgnore) | ||
require.Equal("testdata/.gitignore", ignorer.gitIgnorePath) | ||
require.Equal([]string{ | ||
"/Documents/**/*.bat", | ||
}, ignorer.patterns) | ||
} | ||
|
||
func TestNewPathIgnorer_WhenGitIgnoreParseError(t *testing.T) { | ||
stubs := gostub.StubFunc(&newGitIgnoreParser, nil, errors.New("boom")) | ||
defer stubs.Reset() | ||
|
||
require := require.New(t) | ||
|
||
ignorer, err := NewPathIgnorer("testdata/.gitignore", nil) | ||
require.ErrorContains(err, "boom") | ||
require.Nil(ignorer) | ||
} | ||
|
||
func TestNewPathIgnorer_WhenPatternValidateError(t *testing.T) { | ||
require := require.New(t) | ||
|
||
ignorer, err := NewPathIgnorer("", []string{ | ||
`*{{*}`, | ||
}) | ||
require.ErrorContains(err, "error in pattern") | ||
require.Nil(ignorer) | ||
} | ||
|
||
func TestPathIgnorer_ShouldIgnore(t *testing.T) { | ||
tests := []struct { | ||
path string | ||
dir bool | ||
expected bool | ||
}{ | ||
{ | ||
path: "example.json", | ||
expected: false, | ||
}, | ||
{ | ||
path: "example.yaml", | ||
expected: true, | ||
}, | ||
{ | ||
path: "ignored_file.txt", | ||
expected: true, | ||
}, | ||
{ | ||
path: "ignored_dir", | ||
dir: true, | ||
expected: true, | ||
}, | ||
{ | ||
path: "ignored_dir", | ||
dir: false, | ||
expected: false, | ||
}, | ||
{ | ||
path: "ignored_dir/allowed.txt", | ||
expected: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.path, func(t *testing.T) { | ||
require := require.New(t) | ||
|
||
ignorer, err := NewPathIgnorer("testdata/.gitignore", []string{ | ||
"**/*.{yaml,yml}", | ||
}) | ||
require.NoError(err) | ||
|
||
actual := ignorer.ShouldIgnore(tt.path, tt.dir) | ||
require.Equal(tt.expected, actual) | ||
}) | ||
} | ||
} |
Oops, something went wrong.