Skip to content

Commit 229ab76

Browse files
committed
- Add pre-commit hook script to misc/scripts
- Refer to it in CONTRIBUTING.md - Add setup note in docs folder
1 parent e87fd86 commit 229ab76

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CONTRIBUTING.md

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ If you have an idea for a query that you would like to share with other CodeQL u
3838

3939
- 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).
4040

41+
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/install-pre-commit-hook.md) for instructions on how to install the hook.
42+
4143
4. **Compilation**
4244

4345
- Compilation of the query and any associated libraries and tests must be resilient to future development of the [supported](docs/supported-queries.md) libraries. This means that the functionality cannot use internal libraries, cannot depend on the output of `getAQlClass`, and cannot make use of regexp matching on `toString`.

docs/pre-commit-hook-setup.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# CodeQL pre-commit-hook setup
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/modules/ql/hooks/pre-commit` and make sure that it is executable.
4+
5+
The script will abort a commit that contains incorrectly formatted code in .ql or .qll files and print an error message like:
6+
7+
```
8+
> git commit -m "My commit."
9+
code/ql/cpp/ql/src/Options.qll would change by autoformatting.
10+
code/ql/cpp/ql/src/printAst.ql would change by autoformatting.
11+
```
12+
13+
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/pre-commit

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
exec 1>&2
4+
exitVal=0
5+
while read -r f
6+
do
7+
filename="${f##*/}"
8+
extension="${filename##*.}"
9+
p="$PWD/$f";
10+
if [[ -f "$p" ]] && { [ "$extension" == "ql" ] || [ "$extension" == "qll" ]; }
11+
then
12+
if ! codeql query format --check-only "$p"
13+
then
14+
exitVal=1
15+
fi
16+
fi
17+
done <<<"$(git diff --cached --relative --name-only)"
18+
exit $exitVal

0 commit comments

Comments
 (0)