evilmartians / lefthook

Fast and powerful Git hooks manager for any type of projects.
MIT License
4.73k stars 211 forks source link

Executable file not found #237

Open bbodenmiller opened 2 years ago

bbodenmiller commented 2 years ago

Trying to run this simple command on Windows but getting the error exec: "if": executable file not found in %PATH%

pre-commit:
  commands:
    passwords:
      glob: "*.{config}"
      run: if grep -c 'password' {staged_files}; then exit 1; fi
Envek commented 2 years ago

Most probably command is run in Windows shell (cmd?) but your syntax is for Unix sh-compatible ones.

Try to wrap your command into shell like this:

run: bash -c "if grep -c 'password' {staged_files}; then exit 1; fi"

Haven't worked in Windows for years, so can't really suggest a specific solution.

Or try to move commands to scripts.

bbodenmiller commented 2 years ago

Most probably command is run in Windows shell (cmd?) but your syntax is for Unix sh-compatible ones.

I thought the same but run command of unix2dos {staged_files} works fine in config (and does not work in cmd).

bbodenmiller commented 2 years ago

I'm getting this error more when I try to run with GitHub Desktop. I think it's a GitHub Desktop problem in those cases but interested in others opinion: https://github.com/desktop/desktop/issues/13052

Envek commented 2 years ago

Oh, yes, GUI tools have problems with PATH. See https://github.com/evilmartians/lefthook/issues/178 for example (especially https://github.com/evilmartians/lefthook/issues/178#issuecomment-833305335)

bbodenmiller commented 2 years ago

@Envek can you pass {staged_files} to scripts? I couldn't figure out how to based on docs and experimentation.

bbodenmiller commented 2 years ago

FYI

run: bash -c "if grep -c 'password' {staged_files}; then exit 1; fi"

Does not work. It throws

⠹ waiting bash: -c: line 0: syntax error near unexpected token `then'
bash: -c: line 0: `\if grep -c 'password' myfile.config; then exit 1; fi"'
mrexox commented 1 month ago

Lefthook on Windows is not supposed to use bash. BTW you can just use grep here, it will exit 1 when no match found. So, no need for if statement.