Skip to content

Commit 9667315

Browse files
committed
pre-commit: add qhelp check
Also the instructions on customizing `pre-commit`'s behaviour have been updated to use the `--config` option.
1 parent e15c1f7 commit 9667315

File tree

3 files changed

+72
-16
lines changed

3 files changed

+72
-16
lines changed

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@ repos:
77
hooks:
88
- id: trailing-whitespace
99
- id: end-of-file-fixer
10+
1011
- repo: local
1112
hooks:
1213
- id: codeql-format
1314
name: Fix QL file formatting
1415
files: \.qll?$
1516
language: system
1617
entry: codeql query format --in-place
18+
1719
- id: sync-files
1820
name: Fix files required to be identical
1921
language: system
2022
entry: python3 config/sync-files.py --latest
2123
pass_filenames: false
24+
25+
- id: qlhelp
26+
name: Check query help generation
27+
files: \.qhelp$
28+
language: system
29+
entry: python3 misc/scripts/check-qhelp.py

docs/pre-commit-hook-setup.md

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,54 @@
11
# CodeQL pre-commit-hook setup
22

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:
43

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.
75

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
97

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):
109
```
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
1412
```
1513

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`.
2115

22-
By default, pre-commit will check and fix
16+
By default, pre-commit will check and fix:
2317
* trailing whitespaces;
2418
* absence of or duplicate newlines at end of files;
2519
* QL formatting;
2620
* files out of sync (see [`config/sync-files.py`](../config/sync-files.py)).
2721

22+
It will additionally check:
23+
* `.qhelp` files for query help generation.
24+
2825
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.
2926

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+
3148
```
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.
3352
```
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`).

misc/scripts/check-qhelp.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/env python3
2+
3+
"""cross platform wrapper around codeql generate query-help to check .qhelp files
4+
5+
This takes care of:
6+
* providing a temporary directory to --output
7+
* turning .inc.qhelp arguments into their containing directory
8+
"""
9+
10+
import pathlib
11+
import tempfile
12+
import sys
13+
import subprocess
14+
15+
def transform_input(arg):
16+
arg = pathlib.Path(arg)
17+
if arg.suffixes == ['.inc', '.qhelp']:
18+
return str(arg.parent)
19+
return str(arg)
20+
21+
cmd = ["codeql", "generate", "query-help", "--format=markdown"]
22+
23+
with tempfile.TemporaryDirectory() as tmp:
24+
cmd += [f"--output={tmp}", "--"]
25+
cmd.extend(transform_input(x) for x in sys.argv[1:])
26+
res = subprocess.run(cmd)
27+
28+
sys.exit(res.returncode)

0 commit comments

Comments
 (0)