DEV Community

Remi Vledder
Remi Vledder

Posted on • Edited on

Using husky's pre-commit hook with a GUI

Husky is a tool which enables you to create git hooks easy.

With a hook you could for example make code linting and code formatting mandatory on the developers machine instead of doing that on the CI service.

I found that setting up husky following husky's documentation does not work out of the box for Git Graphical User Interfaces (GUI).


How to enable husky pre-commit hooks for Git GUI's

To enable this feature for a GUI such as Tower or sourcetree follow these steps:

Create a .huskyrc file in your OSX home directory (~/). Open your terminal and run the following commands:

$ cd ~/
$ touch .huskyrc
$ vi .huskyrc
Enter fullscreen mode Exit fullscreen mode

Press i to enter vim's instert mode. Copy paste the following line:

PATH="/usr/local/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

Press esc. Then : and wq. Finally press enter.

Now doing a commit in a GUI should show you the issues with the code when doing a commit:

git-commit-error

Download gist

Troubleshooting

How to solve dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.60.dylib

Adding the path to the husky file enables husky, but initially it gave the following error on my machine:

dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.64.dylib
  Referenced from: /usr/local/bin/node
  Reason: image not found
.git/hooks/husky.sh: line 15: 90006 Abort trap: 6           "$@" husky-run $hookName $gitParams
Enter fullscreen mode Exit fullscreen mode

You can upgrade node to resolve this issue:

$ brew upgrade node
Enter fullscreen mode Exit fullscreen mode

Bonus

When using a node version package manager make sure to also include the following lines to .huskyrc:

# this loads nvm.sh and sets the correct PATH before running hook
. ~/.nvm/nvm.sh
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
rekomat profile image
René Keller

Thanks Remi, you made my day.
BTW: The first code snippet in the article is missing a dot. Line 3 should read $ vi .huskyrc, not huskyrc).

Collapse
 
studiospindle profile image
Remi Vledder

Thank you! Changed it :)

Collapse
 
mananruck profile image
Manuel

Thanks! 🚀

I use this one-liner: echo "\nPATH="/usr/local/bin:\$PATH"" >> ~/.huskyrc

Collapse
 
mathieug profile image
Mathieu Ghennassia

Thanks for the tips.

I now have another error:

yarn run v1.22.17
error An unexpected error occurred: "Failed to replace env in config: ${NPM_TOKEN}".
info If you think this is a bug, please open a bug report with the information provided in "[...]/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
husky - pre-commit hook exited with code 1 (error)
Enter fullscreen mode Exit fullscreen mode

Here's my ~/.huskyrc:

PATH="/usr/local/bin:$PATH"

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
happy profile image
Matúš Šťastný

Thanks! You saved my nerves 🙂