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"
}
2
  • 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
    Commented May 25, 2020 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 stackoverflow.com/a/42666651/6309)
    – VonC
    Commented May 25, 2020 at 14:01

1 Answer 1

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?".

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.