Skip to content

Commit 33cce2b

Browse files
committed
add pre-commit configuration
This enables use of the `pre-commit` framework to run quick pre-commit checks. In particular this allows to automatically fix: * trailing white spaces * absence or multiple newlines at the end of files * QL code formatting * file sync More could be added in the future: anything that can be checked fast can be added in the configuration (for example well-formedness of `qldoc` files). This is a purely opt-in feature. Instructions for enabling it and possibly configuring its behaviour are in `pre-commit-hook-setup.md`.
1 parent aecc17c commit 33cce2b

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

.pre-commit-config.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
- repo: local
11+
hooks:
12+
- id: codeql-format
13+
name: Fix QL file formatting
14+
files: \.qll?$
15+
language: system
16+
entry: codeql query format --in-place
17+
- id: sync-files
18+
name: Fix files required to be identical
19+
language: system
20+
entry: python3 config/sync-files.py --latest
21+
pass_filenames: false

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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,21 @@ ql/cpp/ql/src/printAst.ql would change by autoformatting.
1414
```
1515

1616
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`.
21+
22+
By default, pre-commit will check and fix
23+
* trailing whitespaces;
24+
* absence of double newlines at end of files;
25+
* QL formatting;
26+
* files out of sync (see [`config/sync-files.py`](../config/sync-files.py)).
27+
28+
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+
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
31+
```
32+
git update-index --assume-unchanged .pre-commit-config.yaml
33+
```
34+
and you can then modify the configuration at your will.

0 commit comments

Comments
 (0)