You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR changes the behaviour of the file filter utility to not plainly filter files if they match patterns in gitignore, .snyk and dcignore, but to also consider if a file is already tracked in git. The change will make files be not filtered if they are already tracked.
In GetFilteredFiles, filepath.Abs(f) resolves the input file path relative to the process's current working directory (CWD). However, in getGitTrackedFiles, the map keys are built using filepath.Abs(filepath.Join(repoRoot, entry.Name)). If the tool is executed from a subdirectory of the repository and provided with relative paths, filepath.Abs(f) will result in a different string than the one stored in the gitTrackedFiles map, causing the 'tracked file' check to fail and files to be incorrectly filtered.
absPath, err:=filepath.Abs(f)
iferr!=nil {
absPath=filepath.Clean(f)
}
// files tracked in git should not be filtered, even if they match gitignore patternsifgitTrackedFiles[absPath] {
The getGitTrackedFiles function constructs a map[string]bool containing the absolute paths of every single tracked file in the repository. In large monorepos with hundreds of thousands of files, this will cause significant memory consumption and a latency spike at the start of every filtering operation, as the entire git index must be iterated and string-concatenated into the map.
func (fw*FileFilter) getGitTrackedFiles() map[string]bool {
trackedFiles:=make(map[string]bool)
// open the git repositoryrepo, err:=git.PlainOpenWithOptions(fw.path, &git.PlainOpenOptions{
DetectDotGit: true,
})
iferr!=nil {
fw.logger.Debug().Msgf("failed to open git repository: %v", err)
returntrackedFiles
}
// get the worktree to find the root path and access the indexworktree, err:=repo.Worktree()
iferr!=nil {
fw.logger.Debug().Msgf("failed to get worktree: %v", err)
returntrackedFiles
}
repoRoot:=worktree.Filesystem.Root()
// get the index (staging area) - this contains all tracked files// A file is tracked in git once it's added to the index, even before commitidx, err:=repo.Storer.Index()
iferr!=nil {
fw.logger.Debug().Msgf("failed to get git index: %v", err)
returntrackedFiles
}
// iterate through all entries in the indexfor_, entry:=rangeidx.Entries {
absolutePath:=filepath.Join(repoRoot, entry.Name)
// ensure the path is absolute and cleaned for consistent comparisonabsolutePath, err=filepath.Abs(absolutePath)
iferr!=nil {
absolutePath=filepath.Clean(filepath.Join(repoRoot, entry.Name))
}
trackedFiles[absolutePath] =true
}
📚 Repository Context Analyzed
This review considered 9 relevant code sections from 9 files (average relevance: 0.79)
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
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.
This PR changes the behaviour of the file filter utility to not plainly filter files if they match patterns in gitignore, .snyk and dcignore, but to also consider if a file is already tracked in git. The change will make files be not filtered if they are already tracked.