Graphite Reviewer is now Diamond

Implementing pre-commit hooks to enforce code quality

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite

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.

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.

First, install the pre-commit package:

Terminal
pip install pre-commit

Alternatively, you can install it using Conda:

Terminal
conda install -c conda-forge pre-commit

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:

Terminal
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
- id: black
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.0.1
hooks:
- id: mypy
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v9.13.0
hooks:
- id: eslint
entry: npx eslint
language: node
types: [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.

After creating the configuration file, install the hooks into your Git repository:

Terminal
pre-commit install

This command sets up the Git hooks to run automatically on git commit.

To run all configured hooks against all files in the repository, use:

Terminal
pre-commit run --all-files

This is useful for checking the entire codebase or after adding new hooks.

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.

  • 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.

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:

Terminal
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.

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.

Built for the world's fastest engineering teams, now available for everyone