Ensuring Code Quality with Pre-commit.com
When it comes to coding, ensuring that your source code is of high quality from the get-go saves time, reduces bugs, and fosters better collaboration. One effective way to achieve this is by using pre-commit hooks, and that’s where pre-commit.com shines. Let’s dive into what it offers and how you can integrate it into your workflow.
What is Pre-commit?
pre-commit.com is a tool that manages and maintains pre-commit hooks. Pre-commit hooks are scripts which automatically run when you try to commit changes in your version control system. This helps to ensure that you’re only committing files that meet your linting criteria and other set guidelines.
Why Use Pre-commit?
1. Consistency
Ensure that all contributors are adhering to the same coding guidelines without manual enforcement.
2. Automatic Fixes
Some hooks can automatically fix minor issues (e.g., whitespace removal, code formatting).
3. Reduction in Code Review Cycle
By catching common issues before code review, you can reduce the number of iterations and speed up the merge process.
4. Integration with Popular Tools
Pre-commit supports various well-known tools out of the box, such as flake8, black, isort, and many more.
Setting Up Pre-commit
1. Installation
You can install pre-commit using pip:
pip install pre-commit
2. Configuring Pre-commit
You’ll need a .pre-commit-config.yaml
file in the root of your project. Here’s a basic example:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: trailing-whitespace
- id: check-json
This configuration sets up two hooks: one that checks for trailing whitespaces and another that checks for valid JSON.
3. Installing Git Hook Scripts
Now, you’ll want to set up the git hooks in your repo:
pre-commit install
This will install the pre-commit hook in the .git/hooks/ directory.
4. Committing Code
From this point on, every time you try to commit code, the hooks specified in .pre-commit-config.yaml
will run. If they find issues, the commit will fail, allowing you to fix problems before trying again.
Advanced Features
Custom Hooks: Apart from using the provided hooks, you can write custom hooks tailored to your project’s needs.
Auto-update: You can keep your hooks updated automatically using the command:
pre-commit autoupdate
Run Against All Files: Want to check your entire project, even without a commit? Use:
pre-commit run --all-files
Pre-Commit Hooks (plugins)
The pre-commit ecosystem is quite rich and contains a myriad of useful plugins and hooks. Here’s a list of some popular and useful ones:
pre-commit/pre-commit-hooks: This is the official
set of hooks for pre-commit, and it contains a variety of common checks:
- trailing-whitespace: Trims trailing whitespace.
- end-of-file-fixer: Ensures files end in a newline and only a newline.
- check-yaml: Checks YAML files for parseable syntax.
- check-json: Checks JSON files for parseable syntax.
- check-merge-conflict: Checks for files that contain merge conflict strings. And many more…
psf/black: The uncompromising Python code formatter, black, can be used as a pre-commit hook to automatically format your Python code.
pre-commit/mirrors-isort: isort is a Python utility to sort imports alphabetically and automatically separate them into sections. This is its pre-commit hook.
asottile/reorder_python_imports: Another import order checker and fixer for Python but with a different approach than isort.
golang/pre-commit: If you’re developing in Go, this set of hooks checks for formatting issues, lints the code, and more.
prettier/pre-commit: For web developers, Prettier is a widely-used code formatter. This hook will automatically format your JavaScript, CSS, HTML, and other files.
commitizen-tools/commitizen: This hook ensures that your commit messages adhere to the Commitizen format, which aligns with the Conventional Commits specification.
hadolint/hadolint: A linter for Dockerfiles, hadolint, provides this pre-commit hook to ensure your Dockerfiles are up to standard.
Lucas-C/pre-commit-hooks-safety: This hook checks your Python dependencies for known security vulnerabilities using the safety package.
detailyang/pre-commit-shell: If you have shell scripts in your repository, this hook will help lint them using shellcheck.
These are just a few examples from a vast ecosystem of pre-commit hooks. When selecting hooks, always consider the needs and tech stack of your specific project. The beauty of pre-commit is that it’s language-agnostic, so you can usually find a hook (or make one!) for almost any language or tool you’re using.
You can find another hooks here: https://pre-commit.com/hooks.html
Conclusion
Pre-commit.com provides a robust and flexible way to improve your code quality right from the moment changes are committed. Integrating it into your workflow might require some initial setup and familiarization, but the long-term benefits in terms of code consistency and quality are undeniable. Happy coding!