Skip to content

Commit

Permalink
fix: Quote path to script (#321)
Browse files Browse the repository at this point in the history
* fix: Quote path to script
  • Loading branch information
mrexox authored Aug 22, 2022
1 parent ad99947 commit 164aca6
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,16 @@ func (r *Runner) runScripts(dir string) {
continue
}

scriptPath := shellescape.Quote(filepath.Join(dir, file.Name()))

if r.hook.Parallel {
wg.Add(1)
go func(script *config.Script, path string, file os.FileInfo) {
defer wg.Done()
r.runScript(script, path, file)
}(script, filepath.Join(dir, file.Name()), file)
}(script, scriptPath, file)
} else {
r.runScript(script, filepath.Join(dir, file.Name()), file)
r.runScript(script, scriptPath, file)
}
}

Expand Down Expand Up @@ -246,7 +248,12 @@ func (r *Runner) runCommand(name string, command *config.Command) {
return
}

args := r.buildCommandArgs(command)
args, err := r.buildCommandArgs(command)
if err != nil {
log.Error(err)
logSkip(name, "(SKIP. ERROR)")
return
}
if len(args) == 0 {
logSkip(name, "(SKIP. NO FILES FOR INSPECTION)")
return
Expand All @@ -256,7 +263,7 @@ func (r *Runner) runCommand(name string, command *config.Command) {
r.run(name, root, command.FailText, args)
}

func (r *Runner) buildCommandArgs(command *config.Command) []string {
func (r *Runner) buildCommandArgs(command *config.Command) ([]string, error) {
filesCommand := r.hook.Files
if command.Files != "" {
filesCommand = command.Files
Expand All @@ -278,18 +285,18 @@ func (r *Runner) buildCommandArgs(command *config.Command) []string {
// Special case - `files` option: return if the result of files
// command is empty.
if strings.Contains(runString, filesType) ||
command.Files != "" && filesType == config.SubFiles {
filesCommand != "" && filesType == config.SubFiles {
files, err := filesFn()
if err != nil {
continue
return nil, fmt.Errorf("error replacing %s: %s", filesType, err)
}
if len(files) == 0 {
return nil
return nil, nil
}

filesPrepared := prepareFiles(command, files)
if len(filesPrepared) == 0 {
return nil
return nil, nil
}

runString = replaceQuoted(runString, filesType, filesPrepared)
Expand All @@ -303,7 +310,7 @@ func (r *Runner) buildCommandArgs(command *config.Command) []string {

log.Debug("Executing command is: ", runString)

return strings.Split(runString, " ")
return strings.Split(runString, " "), nil
}

func prepareFiles(command *config.Command, files []string) []string {
Expand Down

0 comments on commit 164aca6

Please sign in to comment.