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

Sign up to request clarification or add additional context in comments.

5 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.