Skip to content

Commit

Permalink
Optimize executable hook files detection (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
yalosev authored Jun 25, 2024
1 parent cb18052 commit c07eb4c
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions pkg/utils/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
}

0 comments on commit c07eb4c

Please sign in to comment.