lefthook
lefthook copied to clipboard
Fail if no lefthook was found
Currently, if lefthook was not found, the user will see the message "Can't find lefthook in PATH" but commit still will go through.
IMHO it is not how it should work. The fix can be as simple as adding exit 1
in the else clause.
call_lefthook()
{
if lefthook -h >/dev/null 2>&1
then
eval lefthook $1
elif test -f "$dir/node_modules/@arkweid/lefthook/bin/lefthook"
then
eval "$dir/node_modules/@arkweid/lefthook/bin/lefthook $1"
elif bundle exec lefthook -h >/dev/null 2>&1
then
bundle exec lefthook $1
elif npx lefthook -h >/dev/null 2>&1
then
npx lefthook $1
elif yarn lefthook -h >/dev/null 2>&1
then
yarn lefthook $1
else
echo "Can't find lefthook in PATH"
fi
}
call_lefthook "run pre-commit $@"
Well, I believe that by default it shouldn't fail if binary wasn't found. It is better to miss something than completely break workflow for some people, IMO.
I don't see a case why we should always abort commit/push/whatever in case we can't perform git hook checks (because hooks are easily skippable by --no-verify
option to git commit
and git push
commands).
If you want to enforce some rules (code linting, commit messages, whatever), you'd better to do this on CI (git hooks just helps to shorten feedback loop).
It can break someone with a modified PATH also https://github.com/evilmartians/lefthook/issues/178
There are several escape paths already if things goes wrong
- git commit --no-verify
- LEFTHOOK=0 git commit
And similar as for @skryukov it broke for me twice already.
The use case - I pull latest changes which introduce a new gem in Gemfile, then make some changes myself and commit them without doing bundle install
.
People add lefthook for a reason even if it is easy to skip. I think it should break and print a reasonable message what to do if one still wants to proceed.
Opened PR - https://github.com/evilmartians/lefthook/pull/213
ooh, LEFTHOOK=0
is gold --no-verify
isn't working for me on a branch which doesn't yet have lefthook installed. I just get Error: Config File "lefthook" Not Found in...
and lose the commit message
I've just installed lefthook using snap and by default git actions fail if no lefthook config file is found. Is there any way to change this behavior without the need to add LEFTHOOK=0
all the time?
Is there any way to change this behavior without the need to add LEFTHOOK=0 all the time?
Could you describe what happened? You have git hooks using lefthook, but no lefthook.yml in your git project, right? And you'd like to see only a warning and not to stop git from creating a commit, right?
@mrexox yes, exactly
I'll try to prepare a fix by next week :+1:
Hey @roman-ih! Sorry for the long delay. It feels like changing current behavior is a big change for lefthook. So, I'd like to suggest an option.
What do you think of having a global config option for that, like ensure: true
or mandatory: true
? Git hooks will have exit 1
if this option is set to true
. So, current lefthook users won't have problems when upgrading, but you can make your configuration stricter and require lefthook to be available (through PATH or other supported options).
I'm interested in this feature as well, and such a global configuration sounds very useful. The next-best option if the default cannot be changed for everyone.
@hyperupcall, cool! Do you like mandatory: true
or any other naming for this option?
For me mandatory
seems a bit vague to me - I like ~~assert_lefthook_exists
~~ assert_lefthook_installed
, or maybe fail_if_absent
or abort_if_not_installed