Welcome back dear reader. My name is Alex M. Schapelle,AKA Silent-Mobius, your dedicated container whisperer. On today's escapade, we'll dwell on case, where we need to have client and server rules on our code version control and how to solve it.
Essentially, despite us being mon-keig with great potential, we still find ourselves handling great deal of work manually. In special cases of code control, we need, someone or something to check existing or possible errors. As it happened to be, there git version control, was developed to have hooks, to automate those code checks, yet those hooks are still are a code, that need to be written and maintained... Enter pre-commit
pre-commit is a framework that manages and maintains multi-language pre-commit hooks. It helps catch common issues—like syntax errors, style violations, and forgotten debug statements—before you commit code. This tutorial will guide you through installing, configuring, and using pre-commit in your projects.
Introduction
pre-commit automates the running of scripts (hooks) before a commit is finalized, ensuring that code adheres to your standards. By catching errors early, it can save you time and maintain code quality throughout your project lifecycle.
Installation
Using pip
Install pre-commit via pipx:
sudo apt-get update && sudo apt-get install python3-pip3 pipx -y
pipx install pre-commit
Verify Installation
Confirm the installation with:
pre-commit --version
Configuration
pre-commit uses a configuration file named .pre-commit-config.yaml in the root of your project, notice the . dot in the name, which is required. This file specifies which repositories of hooks to use and which hooks to run.
Example Configuration
Create a file named .pre-commit-config.yaml with the following content:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0 # Use the desired revision
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/psf/black
rev: 23.1.0 # Use the appropriate version for your project
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
The example above:
- Uses a set of common hooks from pre-commit-hooks for formatting fixes.
- Integrates Black for code formatting.
- Integrates Flake8 for code linting.
Usage
Installing Hooks
To set up the hooks defined in your configuration file, run:
pre-commit install
This command configures your Git repository to run the pre-commit hooks automatically on git commit.
Running Hooks Manually
You can run all configured hooks on all files at any time with:
pre-commit run --all-files
Bypassing Hooks
If necessary, you can bypass pre-commit hooks using the --no-verify flag when committing:
git commit -m "Your commit message" --no-verify
[!]Note: Bypassing hooks should be done sparingly, as it may allow code with issues to be committed.
Advanced Topics
Updating Hooks
Keep your hooks up-to-date by running:
pre-commit autoupdate
After updating, review and commit the changes in .pre-commit-config.yaml.
Customizing Hooks
Some hooks allow for custom arguments. For example, configuring isort to work with Black:
repos:
- repo: https://github.com/pre-commit/mirrors-isort
rev: v5.10.1
hooks:
- id: isort
args: ['--profile', 'black']
Using Pre-commit in CI/CD
To integrate pre-commit in your CI/CD pipeline, run:
pre-commit run --all-files --verbose
This ensures that all code is checked for quality standards as part of your automated builds.
Troubleshooting
- Hooks Not Running: Verify that
pre-commitis installed and that the.pre-commit-config.yamlfile is located in the root directory of your repository. - Hook Failures: Review the error messages provided by the hook. Often, they will indicate how to fix issues such as formatting or linting errors.
- Skipping Hooks: Remember that while you can bypass hooks with
--no-verify, doing so may allow problematic code to be committed.
Resources
Conclusion
Hope that you found this segment some what informative and gained the skill to elevate yourselves, remember that pre-commit is a powerful tool for maintaining code quality. Start with the basic setup and explore what suits your project based on documentation and other resources.
Hope that this article was committed useful information to you. Like, comment and share, and also : Do Try To Have Fun.
Thank you
Top comments (1)
Looks interesting might check it out in the future 😃