Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quote path to script #321

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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