Pre-commit
Pre-commit is a framework for managing and maintaining multi-language pre-commit hooks. These hooks are scripts that run automatically before a commit is finalized, helping to ensure code quality and adherence to coding standards.
When to Use Pre-commit
Pre-commit is useful in a variety of scenarios, particularly when you want to automate code quality checks and ensure consistency across a project. Here are some common use cases:
- Code Quality Assurance: Automatically run linters and formatters to ensure that code adheres to style guidelines before it’s committed.
- Security Checks: Use hooks to scan for common security issues, such as sensitive data leaks or known vulnerabilities.
- Error Prevention: Catch issues early, such as syntax errors or missing files, that might otherwise slip through manual checks.
- Consistency Across Teams: Ensure that all team members adhere to the same coding standards and practices, reducing discrepancies and integration issues.
- Automating Tasks: Automate repetitive tasks, like fixing minor formatting issues or updating documentation, before commits are finalized.
How to Use Pre-commit
-
Install Pre-commit:
-
First, you need to install the pre-commit package. You can do this using pip
-
-
Create a Configuration File:
-
In the root of your repository, create a
.pre-commit-config.yaml
file. This file will list the hooks you want to use. For example:
-
-
Install the Git Hooks:
-
Run the following command to set up the Git hooks based on the configuration file.
-
This command installs the hooks in your
.git/hooks
directory, which will run automatically before each commit.
-
-
Run Hooks Manually (Optional):
-
You can manually run hooks against all files or specific files if you want to test them without making a commit:
-
Or run a specific hook:
-
-
Update Hooks (Optional):
-
To update the hooks to their latest versions as specified in the configuration file, use:
-
-
Customize Hooks:
Some hooks can be configured with additional options. For example, you can set specific arguments or configurations directly in the
.pre-commit-config.yaml
file.
By following these steps, you can integrate pre-commit hooks into your development workflow, automating checks and improving code quality.
difference between
Pre-commit Hooks vs
GitHub Actions
Whether to use pre-commit hooks or GitHub Actions for code formatting and testing largely depends on your workflow and needs. Both have their advantages and can even complement each other. Here's a breakdown to help you decide which to use or how to use both effectively:
Advantages:
- Immediate Feedback: Pre-commit hooks run locally on the developer's machine before the commit is made, providing immediate feedback and ensuring that code formatting and linting issues are caught before they reach the repository.
- Consistency: By catching issues locally, pre-commit hooks help maintain code consistency across all contributors, reducing the likelihood of formatting issues slipping through.
- Lower CI Costs: Running checks locally can reduce the load on CI systems and potentially lower associated costs.
Use Cases:
- Ensuring that all code adheres to formatting and linting rules before committing.
- Automating repetitive tasks, like fixing code style issues or applying certain modifications automatically.
Advantages:
- Centralized Checks: GitHub Actions run in a centralized CI/CD environment, ensuring that all code changes are validated in the same environment, regardless of where they were committed from.
- Integration with Workflow: Actions can be integrated into a broader CI/CD pipeline, including building, testing, and deploying code, not just formatting and linting.
- Flexibility: Actions offer a wide range of capabilities, from running complex workflows to deploying applications, and can be customized with various workflows and environment variables.
Use Cases:
- Running code formatting and linting checks as part of a continuous integration pipeline.
- Performing additional tasks like building, testing, and deploying code.
- Enforcing checks that should be validated on every pull request or push to the repository.
name: Code Quality
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install black flake8
- name: Run Black
run: black --check .
- name: Run Flake8
run: flake8 .
Using Both Together
You can use both pre-commit hooks and GitHub Actions in your workflow for optimal results:
- Pre-commit Hooks: Set up pre-commit hooks to catch issues locally before commits are made, ensuring immediate feedback and consistent formatting.
- GitHub Actions: Set up GitHub Actions to run code quality checks on every push and pull request, ensuring that all code is validated in a consistent CI environment before being merged.
This combination helps to catch issues early, maintain code quality, and integrate smoothly with your development workflow and CI/CD pipeline.