2

I have a pre-commit hook that when I run returns this error:

error: cannot spawn .git/hooks/pre-commit: No error

I have a #!/bin/sh at the top and have definitely used chmod +x on it. However, those appear to be fixes for when there is a no such file or directory error. My error simply says No error and I cannot work out why.

Code in the hook:

#!/bin/sh

changes() {
  git diff --name-only --diff-filter=AMDR --cached @~..@
}

if changes | grep -q dirname {
  echo "Test"
}
compsciman
  • 349
  • 3
  • 17
  • Can you try to target `bash` (`#!/bin/bash`) ? I'm not sure `sh` is set up as you would expect when running `git-bash` on Windows. – LeGEC May 25 '20 at 11:32
  • 1
    @LeGEC There is an `sh.exe` alongside `bash.exe` in Git for Windows, but... a `--version` on both return the same `GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)`. I mean `sh -c "if [[ "aa" == "aa" ]]; then echo "ok"; fi"` will work (print "`ok`"), even though `sh` is not supposed to support `[[ ... ]]` (as explained in https://stackoverflow.com/a/42666651/6309) – VonC May 25 '20 at 14:01

1 Answers1

2

Check first, as in here, if your script does have a final newline.
Its absence would trigger a "no error" message.

Check also the eol style (end of line): LF is prefered for those bash script.

The OP compsciman confirms in the comments that switching to Git For Windows 2.26 from 2.21 solved the issue.

The only recent modification to pre-commit involves the removal of git config --bool option (commit 81e3db4) that I mentioned in "How to color the Git console?".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I put a new line at the end as well as between every line just in case but still receive the same error. – compsciman May 25 '20 at 07:27
  • @compsciman What is your version of Git and your local platform where the pre-commit hook is executed (Windows? Linux? Mac?) – VonC May 25 '20 at 07:42
  • Windows 10, git version 2.21.0.windows.1 – compsciman May 25 '20 at 07:59
  • @compsciman Does the issue persist with 2.26? (Git For Windows: https://github.com/git-for-windows/git/releases) – VonC May 25 '20 at 08:21
  • @compsciman Great. I have included your comment in the answer for more visibility, with some additional details. – VonC May 26 '20 at 05:22