lefthook icon indicating copy to clipboard operation
lefthook copied to clipboard

Fail if no lefthook was found

Open roman-ih opened this issue 3 years ago • 5 comments

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 $@"

roman-ih avatar Jun 22 '21 00:06 roman-ih

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

Envek avatar Jun 22 '21 11:06 Envek

It can break someone with a modified PATH also https://github.com/evilmartians/lefthook/issues/178

skryukov avatar Jun 22 '21 11:06 skryukov

There are several escape paths already if things goes wrong

  1. git commit --no-verify
  2. 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.

roman-ih avatar Jun 22 '21 17:06 roman-ih

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.

roman-ih avatar Jun 22 '21 17:06 roman-ih

Opened PR - https://github.com/evilmartians/lefthook/pull/213

roman-ih avatar Jun 24 '21 01:06 roman-ih

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

bf4 avatar Feb 08 '23 19:02 bf4

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?

marat-y avatar Mar 16 '23 06:03 marat-y

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 avatar Mar 16 '23 08:03 mrexox

@mrexox yes, exactly

marat-y avatar Mar 16 '23 08:03 marat-y

I'll try to prepare a fix by next week :+1:

mrexox avatar Mar 16 '23 11:03 mrexox

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

mrexox avatar Mar 20 '23 07:03 mrexox

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 avatar Jul 18 '23 07:07 hyperupcall

@hyperupcall, cool! Do you like mandatory: true or any other naming for this option?

mrexox avatar Jul 18 '23 07:07 mrexox

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

hyperupcall avatar Jul 18 '23 08:07 hyperupcall