2024-01-15 17:25:06 +01:00
## 'pre-commit' ("replaces" precommit)
### Introduction
[`pre-commit` ](https://pre-commit.org ) is a framework for managing and
maintaining multi-language pre-commit hooks.
"pre-commit hooks" integrate with `git` and are run when you perform a
`git commit` for instance.
Historically there was `precommit` for Dolibarr which you can find in this
directory. That script runs `phplint` , `phpcs` and `phpcbf` upon commit.
`pre-commit` is not specific to Dolibarr and is deployed on many projects -
mostly Python projects, but it is applicable to most (or *all* ) code and
documentation development.
You can find documentation at https://pre-commit.com/ .
Now you can use `pre-commit` which uses the configuration found at the root of
the project: `pre-commit-config.yaml` .
2024-02-18 13:39:25 +01:00
### Installation in your git project
2024-01-15 17:25:06 +01:00
2024-03-07 18:44:55 +01:00
1. Install pre-commit tool.\
2024-02-18 13:39:25 +01:00
If you do not have python installed, install [python ](https://www.python.org ) first.\
2024-07-21 00:38:45 +02:00
`sudo apt install python3`
2024-11-27 17:09:17 +01:00
If you do not have [`pip` ](https://pypi.org/project/pip ), install that as well.\
`sudo apt install pip`
2024-02-18 13:39:25 +01:00
2024-11-27 17:09:33 +01:00
Then you can install pre-commit tool:\
2024-03-07 18:44:55 +01:00
`python3 -m pip install pre-commit`
2025-02-14 23:21:32 +01:00
or
`python3 -m pip install pre-commit --break-system-packages`
2024-02-18 13:39:25 +01:00
2025-02-14 23:21:32 +01:00
Then install phpcbf and phpcs:\
`sudo apt install php-codesniffer`
3. In your local git clone of the project, run `pre-commit install` to add the hooks
2024-03-07 18:44:55 +01:00
or copy the file *dev/setup/git/hooks/pre-commit* manually into *.git/hooks/pre-commit*
(recommended because this file may differ from the file installed with the pre-commit install).
The good file redirects output to the error channel so your IDE will be able to catch the error.
2024-01-15 17:25:06 +01:00
2025-02-16 14:00:52 +01:00
### Troubleshooting
2025-02-16 14:17:21 +01:00
* If you get error "ModuleNotFoundError: No module named 'platformdirs'"
2025-02-16 14:00:52 +01:00
Install the python package with
`pip3 install platformdirs` or `pip3 install platformdirs --break-system-packages`
2025-02-16 14:17:21 +01:00
* If you get error "ERROR: PHP_CodeSniffer requires the tokenizer, xmlwriter and SimpleXML extensions to be enabled. Please enable xmlwriter and SimpleXML."
2025-02-16 14:00:52 +01:00
Install the PHP package xml
`sudo apt install php-simplexml`
2024-01-15 17:25:06 +01:00
### Tips
2025-02-16 14:00:52 +01:00
2024-01-15 17:25:06 +01:00
After installing `pre-commit` onto your local git clone, pre-commit will run
2024-03-01 12:39:23 +01:00
on every commit. The first time, all tools required by pre-commit will be installed
into ~/.cache/pre-commit
2024-01-15 17:25:06 +01:00
When it finds some issue, the git commit will be aborted, so you can fix it,
or verify it.
The tools run by `pre-commit` may modify your code: format PHP code
2024-02-18 13:39:25 +01:00
(`phpcbf`), fix line endings, end of files, etc. If code is modified, the commit
is canceled. Try another commit to do it.
2024-01-15 17:25:06 +01:00
They may also alert about potential issues: syntax errors, spelling errors,
code quality, an execute bit that was (not) set in the git repository, etc.
One way to use it is this:
```bash
cd PROJECT_DIR
pre-commit install # Only needed once
# Repeat until success.
git commit -a -m "My message"
# `pre-commit` is run and reports
# Check the results, make fixes, and re-commit (=repeat above line).
```
In some cases you may want to commit despite the changes.\
You can just add
`--no-verify` to your git command:
```bash
git commit -a -m "My message" --no-verify
```
If you want to skip certain checks for whatever reason, you can set the SKIP
2024-10-27 23:02:45 +01:00
environment variable into the .git/hooks/pre-commit file:
2024-01-15 17:25:06 +01:00
```bash
2024-10-27 23:02:45 +01:00
export SKIP=no-commit-to-branch
```
2024-01-15 17:25:06 +01:00
2024-10-27 23:02:45 +01:00
You can also switch output to text only, by setting the PRE_COMMIT_COLOR in .git/hooks/pre-commit file:
2024-01-15 17:25:06 +01:00
2024-10-27 23:02:45 +01:00
```bash
export PRE_COMMIT_COLOR=never
2024-01-15 17:25:06 +01:00
```
There is much more you can do with pre-commit, check out its
[documentation ](https://pre-commit.com ).
2024-03-20 00:35:01 +01:00
Now your commit is less likely to fail in the Continuous Integration (CI) run
2024-01-15 17:25:06 +01:00
on github.\
CI also runs pre-commit to help maintain code quality.
2024-02-24 19:28:14 +01:00
Note:
2024-03-01 12:39:23 +01:00
Code for precommits are saved into:
2024-11-28 11:36:13 +01:00
.cache/pre-commit/repo.../pre_commit_hooks/php-....sh
2024-11-27 21:00:49 +01:00
and
2024-11-28 11:36:13 +01:00
.cache/pre-commit/repo.../py_env-python3/lib/python.../site-packages/pre_commit_hooks/no_commit_to_branch.py