What could be more annoying than committing code changes to a repository and noticing afterwards that the formatting isn’t right or tests are failing? Your automated tests on Continuous Integration shows rain clouds and you need to get back to the code and fix minor issues with extra commits polluting the git history? Fortunately, with small enhancements to your development workflow, you can automatically prevent all the hassle and check your changes before committing them. The answer is to use Git hooks for example on pre-commit for running linters and tests.
Git Hooks
Git hooks are scripts that Git executes before or after events such as commit, push, and receive. They’re a built-in feature and run locally. Hook scripts are only limited by a developer’s imagination. Some example hook scripts include:
- pre-commit: Check the commit for linting errors.
- pre-receive: Enforce project coding standards.
- post-commit: Email team members of a new commit.
- post-receive: Push the code to production.
Every Git repository has a .git/hooks
folder with a script for each hook you can bind to. You’re free to change or update these scripts as necessary, and Git will execute them when those events occur.
Git hooks can greatly increase your productivity as a developer as you can automate tasks and ensure that your code is ready to commit or pushing to a remote repository.
For more reading about Git hooks, you can check missing Git hooks documentation, read the basics and check tutorial how to use Git hooks on local Git clients and Git servers.
Pre-commit
One productive way to use Git hooks is a pre-commit framework for managing and maintaining multi-language pre-commit hooks. Read tips for using a pre-commit hook.
Pre-commit is nice for example running linters to ensure that your changes conform to coding standards. All you need is to install pre-commit and then add hooks.
Installing pre-commit, ktlint and pre-commit-hook on MacOS with Homebrew:
$ brew install pre-commit
$ brew install ktlint
$ ktlint --install-git-pre-commit-hook
For example, the pre-commit hook to run ktlint with auto-correct option looks like the following in projects .git/hooks/pre-commit. The ”export PATH=/usr/local/bin:$PATH” is for SourceTree to find git on MacOS.
#!/bin/sh
export PATH=/usr/local/bin:$PATH
# https://github.com/shyiko/ktlint pre-commit hook
git diff --name-only --cached --relative | grep '.kt[s"]?$' | xargs ktlint -F --relative .
if [ $? -ne 0 ]; then exit 1; else git add .; fi
The main disadvantage is using pre-commit and local git hooks is that hooks are kept within .git directory and it never comes to the remote repository. Each contributor will have to install them manually in his local repository which may be overlooked.
Maven projects
Githook Maven plugin deals with the problem of providing hook configuration to the repository and automates their installation. It binds to Maven projects build process and configures and installs local git hooks.
It keeps a mapping between the hook name and the script by creating a respective file in .git/hooks for each hook containing given script in Maven project’s initial lifecycle phase. It’s good to notice that the plugin rewrites hooks.
Usage Example:
<build>
<plugins>
<plugin>
<groupId>org.sandbox</groupId>
<artifactId>githook-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<goals>
<goal>install</goal>
</goals>
<configuration>
<hooks>
<pre-commit>
echo running validation build
exec mvn clean install
</pre-commit>
</hooks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Git hooks for Node.js projects
On Node.js projects, you can define scripts in package.json and run them with npm which enables another approach to run Git hooks.
? Husky is Git hooks made easy for Node.js projects. It keeps existing user hooks, supports GUI Git clients and all Git hooks.
Installing Husky is like any other npm library
npm install husky --save-dev
The following configuration on your package.json runs lint (e.g. eslint with –fix) command when you try to commit and runs lint and tests (e.g. mocha, jest) when you try to push to a remote repository.
"husky": { "hooks": { "pre-commit": "npm run lint", "pre-push": "npm run lint && npm run test" } }
Another useful tool is lint-staged which utilizes husky and runs linters against staged git files.
Summary
Make your development workflow easier by automating all the things. Check your changes before committing them with pre-commit, husky or Githook Maven plugin. You get better code and commit quality for free and your team is happier.