Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 357
geckos Avatar asked Jul 31 '19 19:07

geckos


People also ask

How do Husky hooks work?

Husky is a tool that allows us to easily wrangle Git hooks and run the scripts we want at those stages. It works by including an object right within our package. json file that configures Husky to run the scripts we specify. After that, Husky handles managing at which point in the Git lifecycle our scripts will run.

What is husky used for?

Husky is a general term for a dog used in the polar regions, primarily and specifically for work as sled dogs. It refers to a traditional northern type, notable for its cold-weather tolerance and overall hardiness.

What is the use of Husky in react?

Husky is a very popular (1 million downloads a month) npm package that allows custom scripts to be ran against your repository. Husky works with any project that uses a package. json file.


2 Answers

As of version 5 husky uses git's core.hooksPath git config core.hooksPath .husky to be exact. This is done in husky install step.

Since .husky folder created by husky install contains pre-commit script, it will be run before commit. By default it will have npm test in it but you can edit it to your wish.

You can do something similar in any project. Just add .githooks folder and hook files inside it(check .git/hooks for sample files). But you have to run git config core.hooksPath .githooks while cloning the repo(or setting up hooks for the first time). You can have some init script to do that.
npm has scripts.prepare which can run commands on npm install which is husky install in this case.

like image 101
succerror Avatar answered Oct 03 '22 08:10

succerror


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.

like image 41
Nico Avatar answered Oct 03 '22 08:10

Nico