-
Notifications
You must be signed in to change notification settings - Fork 9
[WIP] feat: filter files on mount #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aagumin
wants to merge
3
commits into
modelpack:main
Choose a base branch
from
aagumin:feature/super-exclude
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,167 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| gitignore "github.com/go-git/go-git/v5/plumbing/format/gitignore" | ||
| "github.com/modelpack/model-csi-driver/pkg/logger" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| // FilePatternMatcher wraps gitignore pattern matching functionality | ||
| type FilePatternMatcher struct { | ||
| matcher gitignore.Matcher | ||
| patterns []string | ||
| } | ||
|
|
||
| // NewFilePatternMatcher creates a new pattern matcher from a list of gitignore-compatible patterns | ||
| func NewFilePatternMatcher(patterns []string) (*FilePatternMatcher, error) { | ||
| // Create gitignore matcher from patterns | ||
| // Parse each string pattern into gitignore.Pattern | ||
| var gitPatterns []gitignore.Pattern | ||
| for _, p := range patterns { | ||
| gitPatterns = append(gitPatterns, gitignore.ParsePattern(p, nil)) | ||
| } | ||
| matcher := gitignore.NewMatcher(gitPatterns) | ||
|
|
||
| return &FilePatternMatcher{ | ||
| matcher: matcher, | ||
| patterns: patterns, | ||
| }, nil | ||
| } | ||
|
|
||
| // Match returns true if the given path matches any of the exclusion patterns | ||
| func (m *FilePatternMatcher) Match(path string) bool { | ||
| // gitignore matcher expects paths in forward-slash format | ||
| // and uses a slice of strings for path components | ||
| path = filepath.ToSlash(path) | ||
| pathParts := strings.Split(path, "/") | ||
| isDir := strings.HasSuffix(path, "/") | ||
| return m.matcher.Match(pathParts, isDir) | ||
| } | ||
|
|
||
| // Excludes returns true if any exclusion patterns are defined | ||
| func (m *FilePatternMatcher) Excludes() bool { | ||
| return len(m.patterns) > 0 | ||
| } | ||
|
|
||
| // filterFilesByPatterns walks the target directory and removes files matching the exclusion patterns | ||
| // Returns a list of excluded file paths (relative to targetDir) | ||
| func filterFilesByPatterns(targetDir string, matcher *FilePatternMatcher) ([]string, error) { | ||
| excludedFiles := []string{} | ||
|
|
||
| // First pass: identify and remove matched files | ||
| err := filepath.Walk(targetDir, func(path string, info os.FileInfo, err error) error { | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Skip the target directory itself | ||
| if path == targetDir { | ||
| return nil | ||
| } | ||
|
|
||
| // Get relative path for pattern matching | ||
| relPath, err := filepath.Rel(targetDir, path) | ||
| if err != nil { | ||
| return errors.Wrap(err, "get relative path") | ||
| } | ||
|
|
||
| // Check if file/directory matches exclusion pattern | ||
| if matcher.Match(relPath) { | ||
| if !info.IsDir() { | ||
| logger.Logger().Infof("Excluding file: %s", relPath) | ||
| excludedFiles = append(excludedFiles, relPath) | ||
|
|
||
| // Remove the file | ||
| if err := os.Remove(path); err != nil { | ||
| return errors.Wrapf(err, "remove excluded file: %s", relPath) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
|
|
||
| if err != nil { | ||
| return nil, errors.Wrap(err, "walk directory for pattern matching") | ||
| } | ||
|
|
||
| // Second pass: remove empty directories | ||
| removeEmptyDirectories(targetDir, matcher) | ||
|
|
||
| // Sort excluded files for consistent logging | ||
| sort.Strings(excludedFiles) | ||
|
|
||
| logger.Logger().Infof("Excluded %d file(s) matching patterns", len(excludedFiles)) | ||
|
|
||
| return excludedFiles, nil | ||
| } | ||
|
|
||
| // removeEmptyDirectories removes empty directories that were created after file removal | ||
| func removeEmptyDirectories(targetDir string, matcher *FilePatternMatcher) { | ||
| dirsToRemove := []string{} | ||
|
|
||
| // First, find all empty directories | ||
| err := filepath.Walk(targetDir, func(path string, info os.FileInfo, err error) error { | ||
| if err != nil { | ||
| return nil // Continue on error | ||
| } | ||
|
|
||
| if info.IsDir() && path != targetDir { | ||
| isEmpty, err := isDirEmpty(path) | ||
| if err != nil { | ||
| logger.Logger().WithError(err).Warnf("Failed to check if directory is empty: %s", path) | ||
| return nil | ||
| } | ||
| if isEmpty { | ||
| dirsToRemove = append(dirsToRemove, path) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
|
|
||
| if err != nil { | ||
| logger.Logger().WithError(err).Warn("Failed to walk directories for cleanup") | ||
| return | ||
| } | ||
|
|
||
| // Remove empty directories in reverse order (deepest first) | ||
| for i := len(dirsToRemove) - 1; i >= 0; i-- { | ||
| dir := dirsToRemove[i] | ||
| if err := os.Remove(dir); err != nil { | ||
| logger.Logger().WithError(err).Warnf("Failed to remove empty directory: %s", dir) | ||
| } else { | ||
| relPath, _ := filepath.Rel(targetDir, dir) | ||
| logger.Logger().Infof("Removed empty directory: %s", relPath) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // isDirEmpty checks if a directory is empty | ||
| func isDirEmpty(dir string) (bool, error) { | ||
| f, err := os.Open(dir) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| defer func(f *os.File) { | ||
| err = f.Close() | ||
| if err != nil { | ||
| return | ||
| } | ||
| }(f) | ||
|
|
||
| _, err = f.Readdirnames(1) | ||
| if err == nil { | ||
| return false, nil // Directory is not empty | ||
| } | ||
| if err == io.EOF { | ||
| return true, nil // Directory is empty | ||
| } | ||
| return false, err // Error reading directory | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.