Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom hook folder #839

Closed
cgaube opened this issue Oct 11, 2024 · 9 comments
Closed

Custom hook folder #839

cgaube opened this issue Oct 11, 2024 · 9 comments
Labels
feature request A new lefthook feature description

Comments

@cgaube
Copy link

cgaube commented Oct 11, 2024

⚡ Summary

Right now lefthook automatically installs github hooks files in the git rev-parse --git-path hooks folder
This folder should be configurable.

Value

Every systems is configured differently,
A common scenario is to setup a global hook folder that contains all the github hooks that will delegate to other scripts stored in different directories.
A use case would be to have script executed on all your repositories installed on your computer but also add custom hooks at each repos level

e.g
~/.gitconfig

[core]
	hooksPath = ~/hooks

~/.hooks/pre-commit hooks that execute global scripts + delegate to individual repo pre-commit hooks if needed

~/repo1/.hooks/pre-commit
~/repo2/.hooks/pre-commit

Behavior and configuration changes

Add a new configuration at the root level

hooks_folder: '.hooks/'

use that directory when installing and synchronizing hooks scripts

e.g if you are in
~/repo1. lefthook install will add git hooks scripts to ~/repo1/.hooks

If hooks_folder is not provided fallback to git rev-parse --git-path hooks

@cgaube cgaube added the feature request A new lefthook feature description label Oct 11, 2024
@mrexox
Copy link
Member

mrexox commented Oct 14, 2024

Hey! Could you please provide more details about the suggested setting?

Git uses only one dir for hooks, so if you have ~/hooks configured and run git commit - a ~/hooks/pre-commit will be triggered. If this is your manual hook, then what's the purpose of having different hooks dir for lefthook? It looks like lefthook hooks won't be executed in this scenario.

And if you have lefthook hook installed in ~/hooks/pre-commit, then you'll have repo-specific commands anyway, because settings are parsed from your repo's lefthook.yml, they are not global.

@cgaube
Copy link
Author

cgaube commented Oct 14, 2024

Hello, thank you for your reply

Git uses only one dir for hooks, so if you have ~/hooks configured and run git commit - a ~/hooks/pre-commit will be triggered. If this is your manual hook, then what's the purpose of having different hooks dir for lefthook? It looks like lefthook hooks won't be executed in this scenario.

Here is the scenario

The global pre-commit actually looks into the current repo local .hooks/pre-commit and execute that script if it exits

So if i do a git commit from the repoA folder for example
~/hooks pre-commit is executed, looks for repoA/.hooks/pre-commit and execute that script

What i m trying to do is install lefthook just for repoA but I dont want other repos to use it
So i want to configure repoA/lefthook.yml to install the lefthook precommit script automatically in repoA/.hooks/pre-commit

That way I can still use global hooks that will be executed on ALL repos precommit and also execute lefthook on repoA

This is why i would like that have this option that way lefthook never override my global/pre-commit script.

Hope this make sense

@0xjac
Copy link

0xjac commented Jan 2, 2025

@cgaube isn't this similar to the issue I am having, or would it at least solve it: #832

@cgaube
Copy link
Author

cgaube commented Jan 2, 2025

Similar yes - but the total opposite.

If I understand correctly you want to execute common scripts in all your repos via lefthook.

I, on the other hand, want to only invoke lefthook in ONE repo without overriding my global git hook folder (which currently does a lot of other things that lefthook is totally overriding as it takes over the system)

@mrexox
Copy link
Member

mrexox commented Jan 15, 2025

Sorry for a delay. Let me clarify your use case.

That way I can still use global hooks that will be executed on ALL repos precommit and also execute lefthook on repoA

I guess you can't implicitly make Git execute two scripts on a git hook. You can only run one pre-commit script, either global (from global ~/.gitconfig setting) or local (.git/hooks/pre-commit).

If you want to use lefthook together with your hooks without lefthook overriding your hooks you can configure hooksPath for the repos you use lefthook (.git/config).

git config --local core.hooksPath .git/hooks/

And if you want to run both global and local hooks I can suggest you something like this:

# lefthook

pre-commit:
  jobs:
    - name: run global hook
      run: sh ~/hooks/pre-commit {0} # pass the same arguments
    - name: run local hook command
      run: yarn lint
    - name: run local hook command 2
      run: yarn test

@cgaube
Copy link
Author

cgaube commented Jan 15, 2025

Hi Valentin

Using git config --local core.hooksPath .hooks/ is definitely a great alternative


Unfortunately our company global ~/githooks/pre-commit does other things globally as well as calling each repo custom pre-commit hooks (like security scanning) etc
By using a local core.hooksPath folder in a repo will make it so those company global hooks are not executed. I would need to manually call them in each repo that i want to use lefthook manually which is a pain and prone to error

Furthermore lefthook only provides pre-commit and prepare-commit hooks (as it should ) - but my company has others preconfigured hooks like post-merge pre-push etc etc

This is why having that custom-hook folder install option would be greatly appreciated

Thank you

@mrexox
Copy link
Member

mrexox commented Jan 16, 2025

Oh, I guess I understand. You have global hooks configured and the local hooks also set up in a specific folder, and the global hook calls the local hook.

In this case I can suggest you the following. Manually create a hook script (e.g. .hook/pre-commit) and add a line:

#!/bin/sh

lefthook run pre-commit --no-auto-install "$@"

This will execute the pre-commit hook and avoid installing the scripts to hooksPath.

Also I'd like to mention that lefthook supports all hooks Git supports 😉

Please, share your thoughts on whether this workaround works for you or not. I'd like to avoid adding specific git hooks setting just because it's more about git's responsibility, not lefthook's.

@cgaube
Copy link
Author

cgaube commented Jan 16, 2025

Yes this is essentially what I ended up doing,
which I was hoping I could avoid as it does not take into consideration the hook template that checks that lefthook is installed

https://github.com/evilmartians/lefthook/blob/82f1c788e90411160e1b1e3aee312be802c87185/internal/templates/hook.tmpl

So instead i used lefthook install and copied the files generated back to my repo hook folder (then did lefthook uninstall to cleanup my global hook folder) - This is a manual effort that need to be done from time to time to avoid getting out of sync with most recent version of lefthook template

Works so far but wanted to simplify this and also enjoy the "auto update" of those template if necessary thus my ask for this lefthook configuration

Please, share your thoughts on whether this workaround works for you or not. I'd like to avoid adding specific git hooks setting just because it's more about git's responsibility, not lefthook's.

Anyway I appreciate your patience, please feel free to close this issue if you do not think it is a good candidate for lefthook (I did my best to try to prove otherwise :) )

Thank you

@mrexox
Copy link
Member

mrexox commented Jan 17, 2025

Thank you for providing all the context. I think if this feature finds more requests and more cases I'll add it to lefthook.

I'm closing the issue, but for anyone needing this feature: please, describe your case and if there's no workaround, I'll consider adding custom hooks path.

@mrexox mrexox closed this as completed Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request A new lefthook feature description
Projects
None yet
Development

No branches or pull requests

3 participants