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-commitOnce 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.yamlfile from thec-configdirectory to your project root.
For C++ projects, you'll need to integrate these hooks into your project:
-
Copy the
.pre-commit-config.yamlfile from thecpp-configdirectory to your project root. -
The
.pre-commit-config.yamlfile is preconfigured to utilize a personalized.clang-formatfile if there is one present in the root of the repository. Otherwise it will use the default Microsoft formatter. There is a.clang-formatfile in the folder.
For Python projects, you'll need to integrate these hooks into your project:
-
Copy the
.pre-commit-config.yamlfile from thepython-configdirectory to your project root. -
The
.pre-commit-config.yamlfile is preconfigured to utilize a personalized.ruff.tomlfile 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.tomlfile in the folder.
For Haskell projects, you'll need to integrate these hooks into your project:
-
Copy the
.pre-commit-config.yamlfile from thehaskell-configdirectory to your project root. -
Fourmolu is preconfigured to utilize a personalized
fourmolu.yamlfile if there is one present in the root of the repository. Otherwise it will use the default fourmolu formatting configuration. There is afourmolu.yamlfile 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.shThe 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-filesThis 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 autoupdateThis 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.