Skip to content

5. Pre-Commit Hooks

5.1 Overview

Pre-commit hooks are automated scripts configured to execute specific validations before a Git commit is finalized. In this project, the pre-commit system is defined in the .pre-commit-config.yaml file. Its primary purpose is to enforce code quality, detect potential errors early, and prevent non-compliant code from entering the repository. By integrating pre-commit hooks into the workflow, the project ensures that all committed changes meet established coding standards and pass essential checks automatically.

5.2 Functionality

When a developer attempts to commit changes, the pre-commit system automatically triggers the full development pipeline by executing:

make pipeline

This command performs a comprehensive sequence of actions, including:

  • Linting and formatting checks to ensure consistent code style.
  • Type validation using Mypy to detect mismatches and type-related errors.
  • Security analysis with Bandit to identify potential vulnerabilities in the code.
  • Execution of all unit tests to confirm that functionality remains correct.

If any of these checks fail, the commit is blocked, thereby preventing problematic code from being added to the repository.

5.3 Advantages

Integrating pre-commit hooks into the development workflow provides several key benefits:

  • Quality Enforcement: Only code that passes all validations can be committed, maintaining a high standard across the codebase.
  • Early Issue Detection: Errors are detected before code is pushed or reviewed, reducing downstream debugging and rework.
  • Clean Git History: By preventing commits that violate coding standards or fail tests, the repository maintains a cleaner and more reliable history.

While it is possible to bypass pre-commit validations using the --no-verify flag:

git commit --no-verify -m "commit message"

this approach is discouraged because it undermines the automated quality safeguards and may introduce errors or inconsistencies into the codebase. Pre-commit hooks are intended to be an integral part of the workflow, ensuring that each commit contributes to a robust, secure, and maintainable project.