From c07eb4ccd58cfe0cf647c2f073a32f474182e53f Mon Sep 17 00:00:00 2001 From: Yuriy Losev Date: Tue, 25 Jun 2024 21:12:51 +0400 Subject: [PATCH] Optimize executable hook files detection (#622) --- pkg/utils/file/file.go | 45 ++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/pkg/utils/file/file.go b/pkg/utils/file/file.go index 29315e09..e6d7f581 100644 --- a/pkg/utils/file/file.go +++ b/pkg/utils/file/file.go @@ -42,20 +42,8 @@ func RecursiveGetExecutablePaths(dir string) ([]string, error) { return nil } - // ignore hidden files - if strings.HasPrefix(f.Name(), ".") { - return nil - } - - // ignore .yaml, .json, .txt, .md files - switch filepath.Ext(f.Name()) { - case ".yaml", ".json", ".md", ".txt": - return nil - } - - if !IsFileExecutable(f) { + if !isExecutableHookFile(f) { log.Warnf("File '%s' is skipped: no executable permissions, chmod +x is required to run this hook", path) - return nil } @@ -90,22 +78,8 @@ func RecursiveCheckLibDirectory(dir string) error { return nil } - - // ignore hidden files - if strings.HasPrefix(f.Name(), ".") { - return nil - } - - // ignore .yaml, .json, .txt, .md files - switch filepath.Ext(f.Name()) { - case ".yaml", ".json", ".md", ".txt": - return nil - } - - if IsFileExecutable(f) { + if isExecutableHookFile(f) { log.Warnf("File '%s' has executable permissions and is located in the ignored 'lib' directory", strings.TrimPrefix(path, dir)) - - return nil } return nil @@ -117,3 +91,18 @@ func RecursiveCheckLibDirectory(dir string) error { return nil } + +func isExecutableHookFile(f os.FileInfo) bool { + // ignore hidden files + if strings.HasPrefix(f.Name(), ".") { + return false + } + + // ignore .yaml, .json, .txt, .md files + switch filepath.Ext(f.Name()) { + case ".yaml", ".json", ".md", ".txt": + return false + } + + return IsFileExecutable(f) +}