Git-hook for test and format before committing and pushing.

2 minute read

Check your code quality before Pull Request

When a project already has many developers. The project should think about how to maintain the quality. Quality can be gotten from many improvements on ship the code or coverage unit test, etc. The article talks about tools to run formatting and testing before commit or push. The tool is Git Hook. Git Hook have local hooks and server hooks. Local Hook :

  • pre-commit
  • prepare-commit-msg
  • commit-msg
  • post-commit
  • post-checkout
  • pre-rebase

Let us setup Git-hooks, as the default configuration git hook inside folder .git/hooks. The folder ignores as default. Share the config can make standardized to all developers. Developers can add another config if needed.

Change HookPath

  1. Create folder .githook on your root project.
1mkdir .githooks
  1. Run command terminal
1git config core.hooksPath .githooks

Pre-commit

The pre-commit script is executed every time you run Git commit before Git asks the developer for a commit message or generates a commit object. The script will validate the format. I use spotless as a tool. The first setup and validate code use Spotless. If you don’t know, you can read Android code style using Spotless and Ktlint. Following this command:

1> .githooks/pre-commit (Ctrl + D to save blank file pre-commit)
2chmod +x .githooks/pre-commit

After create the file, please edit into this code :

1#!/usr/bin/env bash
2printf "\e[33;1m%s\e[0m\n" 'Running the check formatter'
3./gradlew spotlessCheck
4if [ $? -ne 0 ]; then
5  printf "\e[31;1m%s\e[0m\n" 'Running the check format error'
6exit 1 fi
7printf "\e[33;1m%s\e[0m\n" 'Finished running the check formatter'

Pre-push

The pre-push script is execute every time you run Git push. This script will run static code analysis (Detekt), checking format code, and unit test. Detekt is faster than the linter android and Sonarqube plugin. So that can improve the speed. But if you don’t know about Detekt, please see Kotlin and static code analysis using detekt.

Following this command to create new file pre-push:

1> .githooks/pre-push (Ctrl + D to save blank file pre-push)
2chmod +x .githooks/pre-push

After create the file, please edit pre-push:

 1#!/usr/bin/env bash
 2printf "\e[33;1m%s\e[0m\n" 'Running the static code analysis'
 3./gradlew detekt
 4if [ $? -ne 0 ]; then
 5  printf "\e[31;1m%s\e[0m\n" 'Static code analysis error'
 6exit 1 fi
 7printf "\e[33;1m%s\e[0m\n" 'Finished running the static code analysis'
 8printf "\e[33;1m%s\e[0m\n" 'Running unit tests'
 9./gradlew testDebugUnitTest
10if [ $? -ne 0 ]; then
11  printf "\e[31;1m%s\e[0m\n" 'Unit tests error'
12exit 1 fi
13printf "\e[33;1m%s\e[0m\n" 'Finished running unit tests'
14printf "\e[33;1m%s\e[0m\n" 'Running the check formatter'
15./gradlew spotlessCheck
16if [ $? -ne 0 ]; then
17  printf "\e[31;1m%s\e[0m\n" 'Format error'
18exit 1 fi
19printf "\e[33;1m%s\e[0m\n" 'Finished running the check formatter'

note: Run unit test depends how many your flavor in the project example (test[flavor][release/debug]UnitTest -> testDevDebugUnitTest). This example hasn’t flavor.

Conclusion

1project
23└───.githooks
4│   │   pre-commit
5│   │   pre-push
6│   │   ....
7

That is a structured folder, and you can add another Git-hooks config. Run git commits and git push to test the script.