The quickest way to get feedback from a build server for small checks is to use pre-commit
hooks. Pre-commit
hooks
is a
project that sits above the Git hooks directory in your project. What are git hooks
,
they’re scripts that run during the lifecycle of a Git action. Typically the hooks are used prior to commiting. The
reference for this feature can be
found on the official documentation.
The pre-commit
project is a generic framework that allows for a repository based approach for brining in commit hooks
and enabling Git hooks based on configuration.
This tool can be especially helpful for applying style guides and verifying that files are compliant to your organization standard before making it to the repository and/or the build.
Installing Pre-commit
If you’re on Arch: pacman -S pre-commit
Mac: brew install pre-commit
Now the pre-commit project has been installed on your machine. The next step is to configure your project that it will be installed on.
Project Setup
- Install the git hooks in your project
- Configure your project
- Test the hooks
Navigate to the project repository in your shell and install the pre-commit
templated Githooks package.
Run the following command: pre-commit install
This will fill your project’s .git/hooks
folder with a few scripts that will run the pre-commit
hooks’ configuration
file.
Next, create a file named: .pre-commit-config.yaml
in the root of your project.
The following configuration is a good first start:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
This validates/lints Yaml files, adds new lines at the end of all new files, and it adds spaces to the end of every line. These kinds of changes make git differences a bit nicer. They’re not for everyone, so look into the hooks before using them.
The repo line tells the pre-commit
tool to pull down that repo and where to find the hooks that you’re expecting to
use. Other collections of hooks can be found on github.
To test the configured hooks, try running the tool in the command line with: pre-commit run --all-files
.
Warning Some hooks make modifications to the files. This will do that, so make sure you’ve commited or stashed all files that have changes you want before running this command.
What next?
Commit the files that were fixed by the hooks, and the pre-commit configuration file.
If the tool fixes any files, your commit will fail, and the changes will be previewable before commiting again incase you want to roll back the changes.
You should now be getting warning, style fixes, and feedback sooner rather than later.