From f041ba228158efa8ca3e4d12ceeefb9742a7d08d Mon Sep 17 00:00:00 2001 From: Kai Krakow Date: Mon, 24 Sep 2018 20:21:55 +0200 Subject: [PATCH 1/2] format-check: Optimize format-check.sh This commit silences the standard output of wget to generate less noise when using `format-check.sh` as a pre-commit hook. It also doesn't redownload the file over and over again but only when changes were detected. Previous behavior was wget creating numbered versions by multiple downloads, thus it always used the first version which was download. That is obviously wrong. It errors out nicely now when the download failed. Signed-off-by: Kai Krakow --- scripts/format-check.sh | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/scripts/format-check.sh b/scripts/format-check.sh index a1ba3b9..6a9111f 100755 --- a/scripts/format-check.sh +++ b/scripts/format-check.sh @@ -1,14 +1,18 @@ #!/bin/bash # Simple script to check for clang-format compliance -wget https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/git-clang-format -chmod +x git-clang-format -CLANG_FORMAT_OUTPUT=$(./git-clang-format HEAD^ HEAD --diff) -if [[ ! ${CLANG_FORMAT_OUTPUT} == "no modified files to format" ]] && [[ ! -z ${CLANG_FORMAT_OUTPUT} ]]; then - echo "Failed clang format check:" - echo "${CLANG_FORMAT_OUTPUT}" - exit 1 +wget -Nq https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/git-clang-format + +if chmod +x git-clang-format; then + CLANG_FORMAT_OUTPUT=$(./git-clang-format HEAD^ HEAD --diff) + if [[ ! ${CLANG_FORMAT_OUTPUT} == "no modified files to format" ]] && [[ ! -z ${CLANG_FORMAT_OUTPUT} ]]; then + echo "Failed clang format check:" + echo "${CLANG_FORMAT_OUTPUT}" + exit 1 + else + echo "Passed clang format check" + fi else - echo "Passed clang format check" + echo "git-clang-format not downloaded" + exit 1 fi - From a8726e89595ffbadc6dc591ca3f87df37e4377d3 Mon Sep 17 00:00:00 2001 From: Kai Krakow Date: Mon, 24 Sep 2018 20:39:00 +0200 Subject: [PATCH 2/2] format-check: Enable usage as pre-commit hook This adds support for a switch `--pre-commit` to work in pre-commit-mode. It downloads the file and runs the real pre-commit-hook then. Signed-off-by: Kai Krakow --- scripts/format-check.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/scripts/format-check.sh b/scripts/format-check.sh index 6a9111f..a2384e0 100755 --- a/scripts/format-check.sh +++ b/scripts/format-check.sh @@ -1,9 +1,18 @@ #!/bin/bash # Simple script to check for clang-format compliance +# Ensure we are at the project root +cd "$(dirname $0)"/.. + wget -Nq https://llvm.org/svn/llvm-project/cfe/trunk/tools/clang-format/git-clang-format if chmod +x git-clang-format; then + if [[ "$1" == "--pre-commit" ]]; then + # used via .git/hooks/pre-commit: + # exec "$(dirname $0)"/../../scripts/format-check.sh --pre-commit + ./git-clang-format + exit + fi CLANG_FORMAT_OUTPUT=$(./git-clang-format HEAD^ HEAD --diff) if [[ ! ${CLANG_FORMAT_OUTPUT} == "no modified files to format" ]] && [[ ! -z ${CLANG_FORMAT_OUTPUT} ]]; then echo "Failed clang format check:"