Search code examples
gitgithookshusky

How husky works?


https://github.com/typicode/husky has the ability to run git hooks automatically in a way that they can be shared between teams in the repository it self.

How can this even work? Since the hooks need to be in .git/hooks which is not added to repository.

Does it wraps git command and intercept commands, running hooks when they happen?

I want to reproduce this behavior for python and php projects without the need to depend on npm or node.


Solution

  • While the husky dependency is installed (through npm install, npm add husky, yarn install, ...) git hooks are created/updated in the .git/hooks directory. If the hook is triggered through a git command a script from husky is triggered that will execute a command based on the package manager you used for the installation. If you use npm npx --no-install husky-run $hookName "$gitParams" is executed. That command looks into your configuration and executes the command defined for the hook there.

    It's like a proxy for git hooks. The proxy is installed once and executed every time by a normal git hook. If it is executed it looks into the configuration and executes the commands defined there.