I have a project with a backend and a frontend, but since the git pre-commit hook gets executed for every change I need to check if the changes were made in the frontend.
I tried this pre-commit hook:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
git diff --cached --name-only --quiet frontend
if [ $? -eq 1 ]; then
cd frontend && npm run lint
fi
But for some reason it fails when running the git command -- which works fine in my terminal. The error I get is:
husky - pre-commit hook exited with code 1 (error)
That does not really help. My guess is that the git command returns an error code and the script thus ends.
Any idea how to fix this?
CodePudding user response:
A shell script always exits with the exit code of the command which was executed last or with the code specified with the exit
builtin.
If you need your script to always exit with a specific exit code (e.g. always success, 0
), make sure you explicitly specify the exit code:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
git diff --cached --name-only --quiet frontend
if [ $? -eq 1 ]; then
cd frontend && npm run lint
fi
exit 0