From 229ab7623e000f85f79e928dbdba5d57c587ffb2 Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Tue, 5 Jan 2021 13:38:45 +0100 Subject: [PATCH] - Add pre-commit hook script to misc/scripts - Refer to it in CONTRIBUTING.md - Add setup note in docs folder --- CONTRIBUTING.md | 2 ++ docs/pre-commit-hook-setup.md | 13 +++++++++++++ misc/scripts/pre-commit | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 docs/pre-commit-hook-setup.md create mode 100644 misc/scripts/pre-commit diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fa88395e5d0..39552ad0bd9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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`. diff --git a/docs/pre-commit-hook-setup.md b/docs/pre-commit-hook-setup.md new file mode 100644 index 00000000000..d0c7fa5d1d3 --- /dev/null +++ b/docs/pre-commit-hook-setup.md @@ -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`). diff --git a/misc/scripts/pre-commit b/misc/scripts/pre-commit new file mode 100644 index 00000000000..e1492099519 --- /dev/null +++ b/misc/scripts/pre-commit @@ -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