fix: fix test and linting fails

Signed-off-by: ronantakizawa <ronantakizawa@gmail.com>
pull/13293/head
ronantakizawa 1 year ago
parent 9b478d5ac2
commit 85403563b3

@ -25,7 +25,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
) )
// Walk walks the file tree rooted at root, calling walkFn for each file or directory // Walk walks the file tree rooted at root, calling walkFn for each file or directory
@ -64,53 +63,56 @@ func readDirNames(dirname string) ([]string, error) {
// symwalk recursively descends path, calling walkFn. // symwalk recursively descends path, calling walkFn.
func symwalk(path string, info os.FileInfo, walkFn filepath.WalkFunc) error { func symwalk(path string, info os.FileInfo, walkFn filepath.WalkFunc) error {
// Recursively walk symlinked directories. // Recursively walk symlinked directories.
if IsSymlink(info) { if IsSymlink(info) {
resolved, err := filepath.EvalSymlinks(path) resolved, err := filepath.EvalSymlinks(path)
if err != nil { if err != nil {
log.Printf("Skipping broken symlink: %s", path) // Log broken symlink log.Printf("Skipping broken symlink: %s", path) // Log broken symlink
return nil // Skip this symlink and continue return nil // Skip this symlink and continue
} }
log.Printf("Found symbolic link in path: %s resolves to %s. Contents of linked file included and used", path, resolved) log.Printf("Found symbolic link in path: %s resolves to %s. Contents of linked file included and used", path, resolved)
if info, err = os.Lstat(resolved); err != nil { if info, err = os.Lstat(resolved); err != nil {
return err return err
} }
return symwalk(resolved, info, walkFn) if err := symwalk(path, info, walkFn); err != nil && err != filepath.SkipDir { // Notice we pass the original `path` here
} return err
}
return nil
}
if err := walkFn(path, info, nil); err != nil { if err := walkFn(path, info, nil); err != nil {
return err return err
} }
if !info.IsDir() { if !info.IsDir() {
return nil return nil
} }
names, err := readDirNames(path) names, err := readDirNames(path)
if err != nil { if err != nil {
return walkFn(path, info, err) return walkFn(path, info, err)
} }
for _, name := range names { for _, name := range names {
filename := filepath.Join(path, name) filename := filepath.Join(path, name)
fileInfo, err := os.Lstat(filename) fileInfo, err := os.Lstat(filename)
if err != nil { if err != nil {
if err := walkFn(filename, fileInfo, err); err != nil && err != filepath.SkipDir { if err := walkFn(filename, fileInfo, err); err != nil && err != filepath.SkipDir {
return err return err
} }
} else { } else {
err = symwalk(filename, fileInfo, walkFn) err = symwalk(filename, fileInfo, walkFn)
if err != nil { if err != nil {
if (!fileInfo.IsDir() && !IsSymlink(fileInfo)) || err != filepath.SkipDir { if (!fileInfo.IsDir() && !IsSymlink(fileInfo)) || err != filepath.SkipDir {
return err return err
} }
} }
} }
} }
return nil return nil
} }
// IsSymlink is used to determine if the fileinfo is a symbolic link. // IsSymlink is used to determine if the fileinfo is a symbolic link.
func IsSymlink(fi os.FileInfo) bool { func IsSymlink(fi os.FileInfo) bool {
return fi.Mode()&os.ModeSymlink != 0 return fi.Mode()&os.ModeSymlink != 0
} }

@ -90,57 +90,46 @@ func Parse(file io.Reader) (*Rules, error) {
// is found. Matching a negative rule will stop evaluation. // is found. Matching a negative rule will stop evaluation.
// Ignore evaluates the file at the given path, and returns true if it should be ignored. // Ignore evaluates the file at the given path, and returns true if it should be ignored.
func (r *Rules) Ignore(path string, fi os.FileInfo) bool { func (r *Rules) Ignore(path string, fi os.FileInfo) bool {
// Don't match on empty dirs. // Don't match on empty dirs.
if path == "" { if path == "" {
return false return false
} }
// Disallow ignoring the current working directory. // Disallow ignoring the current working directory.
// See issue: // See issue:
// 1776 (New York City) Hamilton: "Pardon me, are you Aaron Burr, sir?" // 1776 (New York City) Hamilton: "Pardon me, are you Aaron Burr, sir?"
if path == "." || path == "./" { if path == "." || path == "./" {
return false return false
} }
for _, p := range r.patterns {
// Check for symlink and ignore based on pattern if p.match == nil {
if fi.Mode()&os.ModeSymlink != 0 { log.Printf("ignore: no matcher supplied for %q", p.raw)
resolvedPath, err := filepath.EvalSymlinks(path) return false
if err != nil { }
log.Printf("Ignoring broken symlink: %s -> %s", path, resolvedPath) // Log and ignore broken symlink
return false // Skip this symlink and continue // For negative rules, we need to capture and return non-matches,
} // and continue for matches.
for _, p := range r.patterns { if p.negate {
if p.match == nil { if p.mustDir && !fi.IsDir() {
log.Printf("ignore: no matcher supplied for %q", p.raw) return true
return false }
} if !p.match(path, fi) {
if p.mustDir && !fi.IsDir() { return true
continue }
} continue
if p.match(resolvedPath, fi) { }
log.Printf("Ignoring symlink path: %s", path)
return true // If the rule is looking for directories, and this is not a directory,
} // skip it.
} if p.mustDir && !fi.IsDir() {
return false // Skip further evaluation for this symlink continue
} }
if p.match(path, fi) {
for _, p := range r.patterns { return true
if p.match == nil { }
log.Printf("ignore: no matcher supplied for %q", p.raw) }
return false return false
}
// If the rule is looking for directories, and this is not a directory,
// skip it.
if p.mustDir && !fi.IsDir() {
continue
}
if p.match(path, fi) {
return true
}
}
return false
} }
// parseRule parses a rule string and creates a pattern, which is then stored in the Rules object. // parseRule parses a rule string and creates a pattern, which is then stored in the Rules object.

Loading…
Cancel
Save