|
1 | 1 | # CodeQL pre-commit-hook setup
|
2 | 2 |
|
3 |
| -As stated in [CONTRIBUTING](../CONTRIBUTING.md) all CodeQL files must be formatted according to our [CodeQL style guide](ql-style-guide.md). You can use our pre-commit hook to avoid committing incorrectly formatted code. To use it, simply copy the [pre-commit](../misc/scripts/pre-commit) script to `.git/hooks/pre-commit` and make sure that: |
4 | 3 |
|
5 |
| -- The script is executable. On Linux and macOS this can be done using `chmod +x`. |
6 |
| -- The CodeQL CLI has been added to your `PATH`. |
| 4 | +As stated in [CONTRIBUTING](../CONTRIBUTING.md) all CodeQL files must be formatted according to our [CodeQL style guide](ql-style-guide.md). You can use a pre-commit hook to avoid committing incorrectly formatted code, as well as prevent some other easily checkable errors. |
7 | 5 |
|
8 |
| -The script will abort a commit that contains incorrectly formatted code in .ql or .qll files and print an error message like: |
| 6 | +## Using the `pre-commit` framework |
9 | 7 |
|
| 8 | +Preferably, you can use the [pre-commit framework](https://pre-commit.com/). There are some pre-commit hooks already configured on [`.pre-commit-config.yaml`](../.pre-commit-config.yaml). In order to install them you need to follow pre-commit's [installation instructions](https://pre-commit.com/#installation) and then run `pre-commit install`. Typically (assuming you have [`pip`](https://pip.pypa.io/en/stable/installation/) installed): |
10 | 9 | ```
|
11 |
| -> git commit -m "My commit." |
12 |
| -ql/cpp/ql/src/Options.qll would change by autoformatting. |
13 |
| -ql/cpp/ql/src/printAst.ql would change by autoformatting. |
| 10 | +python3 -m pip install pre-commit |
| 11 | +pre-commit install |
14 | 12 | ```
|
15 | 13 |
|
16 |
| -If you prefer to have the script automatically format the code (and not abort the commit), you can replace the line `codeql query format --check-only` with `codeql query format --in-place` (and `exit $exitVal` with `exit 0`). |
17 |
| - |
18 |
| -## Using the `pre-commit` framework |
19 |
| - |
20 |
| -Alternatively, you can use the [pre-commit framework](https://pre-commit.com/). There are some pre-commit hooks already configured on [`.pre-commit-config.yaml`](../.pre-commit-config.yaml). In order to install them you need to follow pre-commit's [installation instructions](https://pre-commit.com/#installation) and then run `pre-commit install`. |
| 14 | +Also, make sure that the CodeQL CLI has been added to your `PATH`. |
21 | 15 |
|
22 |
| -By default, pre-commit will check and fix |
| 16 | +By default, pre-commit will check and fix: |
23 | 17 | * trailing whitespaces;
|
24 | 18 | * absence of or duplicate newlines at end of files;
|
25 | 19 | * QL formatting;
|
26 | 20 | * files out of sync (see [`config/sync-files.py`](../config/sync-files.py)).
|
27 | 21 |
|
| 22 | +It will additionally check: |
| 23 | +* `.qhelp` files for query help generation. |
| 24 | + |
28 | 25 | It will run the checks only on files changed by the commit (except for the file sync check) and it will skip all files under `test` directories unless they are `.ql`, `.qll` or `.qlref` files.
|
29 | 26 |
|
30 |
| -If you want to change one of these default behaviours (for example, you want to skip the out-of-sync file check, or you prefer to pass `--check-only` instead of `--in-place` to the query formatter), run |
| 27 | +If you want to change any behaviour (for example, you want to skip the out-of-sync file check, or you want to avoid auto-fixing formatting or file syncing), you can copy the configuration file to a separate location, modify it and use that. For example |
| 28 | +``` |
| 29 | +cp .pre-commit-config.yaml ~/my-codeql-pre-commit-config.yaml |
| 30 | +pre-commit install --config ~/my-codeql-pre-commit-config.yaml |
| 31 | +# edit ~/my-codeql-pre-commit-config.yaml to your linking |
| 32 | +``` |
| 33 | + |
| 34 | +You can for example: |
| 35 | +* change `--in-place` to `--check-only` in the `codeql-format` hook to have it report formatting problems instead of auto-fixing them; |
| 36 | +* remove `--latest` in the `sync-files` hook to do the same; |
| 37 | +* remove any hook altogether. |
| 38 | + |
| 39 | +## Manual approach |
| 40 | + |
| 41 | +You can have the formatting check in place by copying the [pre-commit](../misc/scripts/pre-commit) script to `.git/hooks/pre-commit` and make sure that: |
| 42 | + |
| 43 | +- The script is executable. On Linux and macOS this can be done using `chmod +x`. |
| 44 | +- The CodeQL CLI has been added to your `PATH`. |
| 45 | + |
| 46 | +The script will abort a commit that contains incorrectly formatted code in .ql or .qll files and print an error message like: |
| 47 | + |
31 | 48 | ```
|
32 |
| -git update-index --assume-unchanged .pre-commit-config.yaml |
| 49 | +> git commit -m "My commit." |
| 50 | +ql/cpp/ql/src/Options.qll would change by autoformatting. |
| 51 | +ql/cpp/ql/src/printAst.ql would change by autoformatting. |
33 | 52 | ```
|
34 |
| -and you can then modify the configuration at your will. |
| 53 | + |
| 54 | +If you prefer to have the script automatically format the code (and not abort the commit), you can replace the line `codeql query format --check-only` with `codeql query format --in-place` (and `exit $exitVal` with `exit 0`). |
0 commit comments