Skip to content

Commit 2483b09

Browse files
authored
Merge pull request #4913 from MathiasVP/pre-hook-autoformat-check
Add pre-commit hook to scripts folder and document it
2 parents 5a9e098 + f18486a commit 2483b09

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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/hooks/pre-commit` and make sure that:
4+
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`.
7+
8+
The script will abort a commit that contains incorrectly formatted code in .ql or .qll files and print an error message like:
9+
10+
```
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.
14+
```
15+
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`).

misc/scripts/pre-commit

Lines changed: 18 additions & 0 deletions
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)