- Add pre-commit hook script to misc/scripts

- Refer to it in CONTRIBUTING.md
- Add setup note in docs folder
This commit is contained in:
Mathias Vorreiter Pedersen
2021-01-05 13:38:45 +01:00
parent e87fd86e63
commit 229ab7623e
3 changed files with 33 additions and 0 deletions

View File

@@ -38,6 +38,8 @@ If you have an idea for a query that you would like to share with other CodeQL u
- 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).
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.
4. **Compilation**
- 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`.

View File

@@ -0,0 +1,13 @@
# CodeQL pre-commit-hook setup
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.
The script will abort a commit that contains incorrectly formatted code in .ql or .qll files and print an error message like:
```
> git commit -m "My commit."
code/ql/cpp/ql/src/Options.qll would change by autoformatting.
code/ql/cpp/ql/src/printAst.ql would change by autoformatting.
```
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`).

18
misc/scripts/pre-commit Normal file
View File

@@ -0,0 +1,18 @@
#!/bin/bash
exec 1>&2
exitVal=0
while read -r f
do
filename="${f##*/}"
extension="${filename##*.}"
p="$PWD/$f";
if [[ -f "$p" ]] && { [ "$extension" == "ql" ] || [ "$extension" == "qll" ]; }
then
if ! codeql query format --check-only "$p"
then
exitVal=1
fi
fi
done <<<"$(git diff --cached --relative --name-only)"
exit $exitVal