From fb27784faaab8fb690c8165741ab3ddc690ca5ee Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Mon, 22 Aug 2022 10:04:09 +0300 Subject: [PATCH 1/2] fix: Quote path to script --- internal/lefthook/runner/runner.go | 31 +++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/internal/lefthook/runner/runner.go b/internal/lefthook/runner/runner.go index e7de2510..296a3ba5 100644 --- a/internal/lefthook/runner/runner.go +++ b/internal/lefthook/runner/runner.go @@ -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) } } @@ -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 @@ -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 @@ -278,18 +285,24 @@ 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 + var gitErr error + if filesType == config.SubFiles { + gitErr = fmt.Errorf("error running '%s': %s", filesCommand, err) + } else { + gitErr = fmt.Errorf("error replacing %s: %s", filesType, err) + } + return nil, gitErr } 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) @@ -303,7 +316,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 { From 9914c98e8e178b6aa8fe677a0fae7a9c20ce6f1e Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Mon, 22 Aug 2022 10:13:33 +0300 Subject: [PATCH 2/2] fix: Fix linting --- internal/lefthook/runner/runner.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/internal/lefthook/runner/runner.go b/internal/lefthook/runner/runner.go index 296a3ba5..bf4c588d 100644 --- a/internal/lefthook/runner/runner.go +++ b/internal/lefthook/runner/runner.go @@ -288,13 +288,7 @@ func (r *Runner) buildCommandArgs(command *config.Command) ([]string, error) { filesCommand != "" && filesType == config.SubFiles { files, err := filesFn() if err != nil { - var gitErr error - if filesType == config.SubFiles { - gitErr = fmt.Errorf("error running '%s': %s", filesCommand, err) - } else { - gitErr = fmt.Errorf("error replacing %s: %s", filesType, err) - } - return nil, gitErr + return nil, fmt.Errorf("error replacing %s: %s", filesType, err) } if len(files) == 0 { return nil, nil