Maintaining high code quality is essential for any software project. Pre-commit hooks provide an automated way to enforce coding standards, run linters, and execute tests before changes are committed to the repository. This guide will walk you through setting up pre-commit hooks using the pre-commit
framework.
What are pre-commit hooks?
Pre-commit hooks are scripts that run automatically before a commit is finalized. They are commonly used to perform checks such as linting, formatting, and running tests to ensure that only high-quality code is committed. By catching issues early, pre-commit hooks help maintain code consistency and reduce the likelihood of bugs.
Setting up pre-commit hooks with the pre-commit
framework
Install pre-commit
First, install the pre-commit
package:
pip install pre-commit
Alternatively, you can install it using Conda:
conda install -c conda-forge pre-commit
Create a .pre-commit-config.yaml
file
In the root directory of your project, create a .pre-commit-config.yaml
file to define the hooks you want to run. Here's an example configuration:
repos:- repo: https://github.com/pre-commit/pre-commit-hooksrev: v4.3.0hooks:- id: trailing-whitespace- id: end-of-file-fixer- id: check-yaml- repo: https://github.com/psf/blackrev: 22.10.0hooks:- id: black- repo: https://github.com/pre-commit/mirrors-mypyrev: v1.0.1hooks:- id: mypy- repo: https://github.com/pre-commit/mirrors-eslintrev: v9.13.0hooks:- id: eslintentry: npx eslintlanguage: nodetypes: [javascript, typescript]
This configuration sets up hooks for trimming trailing whitespace, fixing end-of-file issues, checking YAML files, formatting Python code with Black, type-checking with Mypy, and linting JavaScript/TypeScript files with ESLint.
Install the hooks
After creating the configuration file, install the hooks into your Git repository:
pre-commit install
This command sets up the Git hooks to run automatically on git commit
.
Run hooks manually
To run all configured hooks against all files in the repository, use:
pre-commit run --all-files
This is useful for checking the entire codebase or after adding new hooks.
Integrating Graphite for enhanced Git workflows
Graphite simplifies Git workflows, especially when working with stacked pull requests. While Graphite doesn't directly manage pre-commit hooks, it complements them by providing a streamlined experience for code reviews and merges. By integrating Graphite into your development process, you can ensure that code quality checks are consistently applied across all changes.
Best practices for pre-commit hooks
- Keep hooks fast: Ensure that hooks execute quickly to avoid slowing down the commit process.
- Use language-specific tools: Utilize linters and formatters that are tailored to the languages used in your project.
- Share hook configurations: Include the
.pre-commit-config.yaml
file in your repository so that all team members use the same hooks. - Document hook usage: Provide documentation on how to install and use the pre-commit hooks for new contributors.
Skipping pre-commit hooks
In certain situations, you may want to bypass pre-commit hooks (e.g., for emergency fixes). You can do this by adding the --no-verify
flag to your commit command:
git commit --no-verify -m "Your commit message"
Use this option sparingly, as it skips all configured hooks and may allow code that doesn't meet quality standards to be committed.
Conclusion
Implementing pre-commit hooks is a proactive approach to maintaining code quality. By automating checks for formatting, linting, and testing, you can catch issues early and enforce consistent coding standards across your team. For more information on setting up and using pre-commit hooks, visit the official pre-commit documentation.