Pre-commit-hooks is a project designed to enhance code quality and consistency by providing a collection of pre-commit hooks, those hooks can be run locally as well as with Docker.
There are two way to utilize the pre-commit hooks, locally, downloading necessary dependecies and with Docker, without downloading anything.
If you want to have the hooks run in every single commit, refere to running it locally, if you prefere to decide when the hooks will run, refere to running it via Docker.
For both options, you will have to copy some files/scripts present in this repository.
To run the pre-commit hooks via Docker, you only need to have Docker
installed on your computer. Refer to the official Docker documentation for installation instructions.
You can execute the hooks using a provided script whenever you deem it appropriate.
To utilize the pre-commit hooks locally, you'll need to install pre-commit
with the following command:
pip install pre-commit
Once the pre-commit hooks are set up, they will only run on the TRACKED FILES IN THE REPOSITORY. If a hook detects an error locally, the commit process will be stopped.
A general project usage .pre-commit-config.yaml
file is available at the root of the repository for reference and use if needed.
For C projects, you'll need to integrate these hooks into your project:
- Copy the
.pre-commit-config.yaml
file from thec-config
directory to your project root.
For C++ projects, you'll need to integrate these hooks into your project:
-
Copy the
.pre-commit-config.yaml
file from thecpp-config
directory to your project root. -
The
.pre-commit-config.yaml
file is preconfigured to utilize a personalized.clang-format
file if there is one present in the root of the repository. Otherwise it will use the default Microsoft formatter. There is a.clang-format
file in the folder.
For Python projects, you'll need to integrate these hooks into your project:
-
Copy the
.pre-commit-config.yaml
file from thepython-config
directory to your project root. -
The
.pre-commit-config.yaml
file is preconfigured to utilize a personalized.ruff.toml
file if there is one present in the root of the repository. Otherwise it will use the default ruff settings for formatting and linting. There is a.ruff.toml
file in the folder.
For Haskell projects, you'll need to integrate these hooks into your project:
-
Copy the
.pre-commit-config.yaml
file from thehaskell-config
directory to your project root. -
Fourmolu is preconfigured to utilize a personalized
fourmolu.yaml
file if there is one present in the root of the repository. Otherwise it will use the default fourmolu formatting configuration. There is afourmolu.yaml
file in the folder in case you want one.
Refer to this documentation if you want to run them with Docker
Refer to this documentation if you want to run them locally
To use it with Docker, you will additionally need to copy the script pre-commit.sh
into your root repository.
Whenever you wish to run the hooks, simply execute the script as follows:
./pre-commit.sh
The script will create a container with a published image, connected to your code via a volume, where the hooks will be executed. After execution, the script will display the output of the hooks and their status.
Please note that the script will create two temporary files, .pre-commit-keeper.log
and .pre-commit-output.log
. These files will be automatically deleted upon the completion of the script.
Once you've copied over the required files, the next step is to configure your Git repository to utilize the pre-commit hooks locally. Follow these steps:
- Run the command below to set up pre-commit in your local git repository:
pre-commit install
- After installing pre-commit, it's advisable to run the following command:
pre-commit run --all-files
This command ensures that all hooks are executed for all files previously added to the repository. If there is any error about not finding a binary, you will probably have to locally install that necessary application with apt, stack, pip... etc.
To ensure that you have the latest versions of the hooks, you can periodically run the following command:
pre-commit autoupdate
This command updates the hooks to their latest versions, keeping your pre-commit setup current with any improvements or bug fixes.
Contributions are welcome. Please follow the existing coding standards and add pre-commit hooks for any new files.