Skip to content

Commit 3719353

Browse files
authored
Merge pull request github#8172 from github/redsun82/pre-commit
add pre-commit configuration
2 parents 5ce6b84 + d2ed5c4 commit 3719353

File tree

4 files changed

+144
-3
lines changed

4 files changed

+144
-3
lines changed

.pre-commit-config.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# See https://pre-commit.com for more information
2+
# See https://pre-commit.com/hooks.html for more hooks
3+
exclude: /test/.*$(?<!\.ql)(?<!\.qll)(?<!\.qlref)
4+
repos:
5+
- repo: https://github.com/pre-commit/pre-commit-hooks
6+
rev: v3.2.0
7+
hooks:
8+
- id: trailing-whitespace
9+
- id: end-of-file-fixer
10+
11+
- repo: local
12+
hooks:
13+
- id: codeql-format
14+
name: Fix QL file formatting
15+
files: \.qll?$
16+
language: system
17+
entry: codeql query format --in-place
18+
19+
- id: sync-files
20+
name: Fix files required to be identical
21+
language: system
22+
entry: python3 config/sync-files.py --latest
23+
pass_filenames: false
24+
25+
- id: qhelp
26+
name: Check query help generation
27+
files: \.qhelp$
28+
language: system
29+
entry: python3 misc/scripts/check-qhelp.py

CONTRIBUTING.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ If you have an idea for a query that you would like to share with other CodeQL u
4242

4343
- The queries and libraries must be autoformatted, for example using the "Format Document" command in [CodeQL for Visual Studio Code](https://help.semmle.com/codeql/codeql-for-vscode/procedures/about-codeql-for-vscode.html).
4444

45-
If you prefer, you can use this [pre-commit hook](misc/scripts/pre-commit) that automatically checks whether your files are correctly formatted. See the [pre-commit hook installation guide](docs/pre-commit-hook-setup.md) for instructions on how to install the hook.
45+
If you prefer, you can either:
46+
1. install the [pre-commit framework](https://pre-commit.com/) and install the configured hooks on this repo via `pre-commit install`, or
47+
2. use this [pre-commit hook](misc/scripts/pre-commit) that automatically checks whether your files are correctly formatted.
48+
49+
See the [pre-commit hook installation guide](docs/pre-commit-hook-setup.md) for instructions on the two approaches.
4650

4751
4. **Compilation**
4852

@@ -63,6 +67,6 @@ After the experimental query is merged, we welcome pull requests to improve it.
6367

6468
## Using your personal data
6569

66-
If you contribute to this project, we will record your name and email address (as provided by you with your contributions) as part of the code repositories, which are public. We might also use this information to contact you in relation to your contributions, as well as in the normal course of software development. We also store records of CLA agreements signed in the past, but no longer require contributors to sign a CLA. Under GDPR legislation, we do this on the basis of our legitimate interest in creating the CodeQL product.
70+
If you contribute to this project, we will record your name and email address (as provided by you with your contributions) as part of the code repositories, which are public. We might also use this information to contact you in relation to your contributions, as well as in the normal course of software development. We also store records of CLA agreements signed in the past, but no longer require contributors to sign a CLA. Under GDPR legislation, we do this on the basis of our legitimate interest in creating the CodeQL product.
6771

6872
Please do get in touch ([email protected]) if you have any questions about this or our data protection policies.

docs/pre-commit-hook-setup.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,44 @@
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:
3+
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.
5+
6+
## Using the `pre-commit` framework
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):
9+
```
10+
python3 -m pip install pre-commit
11+
pre-commit install
12+
```
13+
14+
Also, make sure that the CodeQL CLI has been added to your `PATH`.
15+
16+
By default, pre-commit will check and fix:
17+
* trailing whitespaces;
18+
* absence of or duplicate newlines at end of files;
19+
* QL formatting;
20+
* files out of sync (see [`config/sync-files.py`](../config/sync-files.py)).
21+
22+
It will additionally check:
23+
* `.qhelp` files for query help generation.
24+
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.
26+
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 liking
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:
442

543
- The script is executable. On Linux and macOS this can be done using `chmod +x`.
644
- The CodeQL CLI has been added to your `PATH`.

misc/scripts/check-qhelp.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
* finding usages of .inc.qhelp arguments
8+
"""
9+
10+
import pathlib
11+
import tempfile
12+
import sys
13+
import subprocess
14+
import xml.sax
15+
16+
17+
include_cache = {}
18+
19+
class IncludeHandler(xml.sax.ContentHandler):
20+
def __init__(self, xml):
21+
self.__xml = xml
22+
23+
def startElement(self, name, attrs):
24+
if name == "include":
25+
src = (self.__xml.parent / attrs["src"]).resolve()
26+
include_cache.setdefault(src, set()).add(self.__xml)
27+
28+
class IgnoreErrorsHandler(xml.sax.ErrorHandler):
29+
def error(self, exc):
30+
pass
31+
32+
def fatalError(self, exc):
33+
pass
34+
35+
def warning(self, exc):
36+
pass
37+
38+
def init_include_cache():
39+
if not include_cache:
40+
for qhelp in pathlib.Path().rglob("*.qhelp"):
41+
xml.sax.parse(qhelp, IncludeHandler(qhelp), IgnoreErrorsHandler())
42+
43+
44+
def find_inc_qhelp_usages(arg):
45+
init_include_cache()
46+
return include_cache.get(arg.resolve(), ())
47+
48+
def transform_inputs(args):
49+
for arg in args:
50+
arg = pathlib.Path(arg)
51+
if arg.suffixes == ['.inc', '.qhelp']:
52+
for qhelp in find_inc_qhelp_usages(arg):
53+
yield str(qhelp)
54+
else:
55+
yield str(arg)
56+
57+
affected_qhelp_files = list(transform_inputs(sys.argv[1:]))
58+
if not affected_qhelp_files:
59+
# can happen with changes on an unused .inc.qhelp file
60+
print("nothing to do!")
61+
sys.exit(0)
62+
63+
cmd = ["codeql", "generate", "query-help", "--format=markdown"]
64+
65+
with tempfile.TemporaryDirectory() as tmp:
66+
cmd += [f"--output={tmp}", "--"]
67+
cmd += affected_qhelp_files
68+
res = subprocess.run(cmd)
69+
70+
sys.exit(res.returncode)

0 commit comments

Comments
 (0)