From e4f6b07a34f18bdf5381014ecf1ca125d4536243 Mon Sep 17 00:00:00 2001 From: Alyssa Date: Tue, 12 Apr 2022 14:17:31 +0100 Subject: [PATCH 1/4] Document pre-commit hook for staged files Relates to: #1057, #1792, #2175, #2320, #3078 --- .../gettingstarted/git-pre-commit-hook.md | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/docs/pages/gettingstarted/git-pre-commit-hook.md b/docs/pages/gettingstarted/git-pre-commit-hook.md index 977f388f879..f7cb267e29d 100644 --- a/docs/pages/gettingstarted/git-pre-commit-hook.md +++ b/docs/pages/gettingstarted/git-pre-commit-hook.md @@ -10,7 +10,7 @@ summary: Detekt can be integrated into your development workflow by using a Git pre-commit hook. For that reason Git supports to run custom scripts automatically, when a specific action occurs. The mentioned pre-commit hook can be setup locally on your dev-machine. -The following client-side detekt hook is triggered by a commit operation. +The following client-side detekt hook is triggered by a commit operation, and checks all files via the gradle task. ```bash #!/usr/bin/env bash @@ -38,3 +38,45 @@ More information about Git hooks and how to install them can be found in A special thanks goes to Mohit Sarveiya for providing this shell script. You can watch his excellent talk about **Static Code Analysis For Kotlin** on [YouTube](https://www.youtube.com/watch?v=LT6m5_LO2DQ). + +## Only run on staged files + +It is also possible to use [the CLI](cli.md) to create a hook that only runs on staged files. This has the advantage of speedier execution, by running on fewer files and avoiding the warm-up time of the gradle daemon. + +Please note, however, that a handful of checks will not work correctly with this approach. This is because they require full type resolution - see for example [ElseCaseInsteadOfExhaustiveWhen](https://detekt.dev/potential-bugs.html#elsecaseinsteadofexhaustivewhen). If you do adopt a partial hook, it is recommended that you still implement a full `detekt` check as part of your CI pipeline. + +Hook definition in pre-commit: + +```yml +- id: detekt + name: detekt check + description: Runs `detekt` on modified .kt files. + language: script + entry: detekt.sh + files: \.kt +``` + +Script `detekt.sh`: + +```bash +#!/bin/bash + +echo "Running detekt check..." +fileArray=($@) +detektInput=$(IFS=,;printf "%s" "${fileArray[*]}") +echo "Input files: $detektInput" + +OUTPUT="/tmp/detekt-$(date +%s)" +detekt --input "$detektInput" > $OUTPUT +EXIT_CODE=$? +if [ $EXIT_CODE -ne 0 ]; then + cat $OUTPUT + rm $OUTPUT + echo "***********************************************" + echo " Detekt failed " + echo " Please fix the above issues before committing " + echo "***********************************************" + exit $EXIT_CODE +fi +rm $OUTPUT +``` From a79143e95c43a892d6db8b4f0fe1e15523e1bb16 Mon Sep 17 00:00:00 2001 From: Alyssa Date: Wed, 13 Apr 2022 13:12:29 +0100 Subject: [PATCH 2/4] Documentation improvements - Link to pre-commit.com - Link to general type resolution page --- docs/pages/gettingstarted/git-pre-commit-hook.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/pages/gettingstarted/git-pre-commit-hook.md b/docs/pages/gettingstarted/git-pre-commit-hook.md index f7cb267e29d..3294f016fb8 100644 --- a/docs/pages/gettingstarted/git-pre-commit-hook.md +++ b/docs/pages/gettingstarted/git-pre-commit-hook.md @@ -43,7 +43,9 @@ You can watch his excellent talk about **Static Code Analysis For Kotlin** on It is also possible to use [the CLI](cli.md) to create a hook that only runs on staged files. This has the advantage of speedier execution, by running on fewer files and avoiding the warm-up time of the gradle daemon. -Please note, however, that a handful of checks will not work correctly with this approach. This is because they require full type resolution - see for example [ElseCaseInsteadOfExhaustiveWhen](https://detekt.dev/potential-bugs.html#elsecaseinsteadofexhaustivewhen). If you do adopt a partial hook, it is recommended that you still implement a full `detekt` check as part of your CI pipeline. +Please note, however, that a handful of checks will not work correctly with this approach - those that require [type resolution](type-resolution.md). If you do adopt a partial hook, it is recommended that you still implement a full `detekt` check as part of your CI pipeline. + +This example has been put together using [pre-commit](https://pre-commit.com/), but the same principle can be applied to any kind of hook. Hook definition in pre-commit: From d022fc24e78218de33738ad31f94d6867d2c0db1 Mon Sep 17 00:00:00 2001 From: Alyssa <57534485+alyssa-glean@users.noreply.github.com> Date: Thu, 14 Apr 2022 09:08:09 +0100 Subject: [PATCH 3/4] Simplify type resolution wording Co-authored-by: M Schalk <30376729+schalkms@users.noreply.github.com> --- docs/pages/gettingstarted/git-pre-commit-hook.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/pages/gettingstarted/git-pre-commit-hook.md b/docs/pages/gettingstarted/git-pre-commit-hook.md index 3294f016fb8..794b42e18b9 100644 --- a/docs/pages/gettingstarted/git-pre-commit-hook.md +++ b/docs/pages/gettingstarted/git-pre-commit-hook.md @@ -43,7 +43,7 @@ You can watch his excellent talk about **Static Code Analysis For Kotlin** on It is also possible to use [the CLI](cli.md) to create a hook that only runs on staged files. This has the advantage of speedier execution, by running on fewer files and avoiding the warm-up time of the gradle daemon. -Please note, however, that a handful of checks will not work correctly with this approach - those that require [type resolution](type-resolution.md). If you do adopt a partial hook, it is recommended that you still implement a full `detekt` check as part of your CI pipeline. +Please note, however, that a handful of checks requiring [type resolution](type-resolution.md) will not work correctly with this approach. If you do adopt a partial hook, it is recommended that you still implement a full `detekt` check as part of your CI pipeline. This example has been put together using [pre-commit](https://pre-commit.com/), but the same principle can be applied to any kind of hook. From da071952b04c098e5bcce22d13c21714336612f9 Mon Sep 17 00:00:00 2001 From: Alyssa Date: Wed, 20 Apr 2022 11:21:21 +0100 Subject: [PATCH 4/4] Simplify hook to not use a temporary file --- docs/pages/gettingstarted/git-pre-commit-hook.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/docs/pages/gettingstarted/git-pre-commit-hook.md b/docs/pages/gettingstarted/git-pre-commit-hook.md index 3294f016fb8..ae2fbc10a73 100644 --- a/docs/pages/gettingstarted/git-pre-commit-hook.md +++ b/docs/pages/gettingstarted/git-pre-commit-hook.md @@ -68,17 +68,14 @@ fileArray=($@) detektInput=$(IFS=,;printf "%s" "${fileArray[*]}") echo "Input files: $detektInput" -OUTPUT="/tmp/detekt-$(date +%s)" -detekt --input "$detektInput" > $OUTPUT +OUTPUT=$(detekt --input "$detektInput" 2>&1) EXIT_CODE=$? if [ $EXIT_CODE -ne 0 ]; then - cat $OUTPUT - rm $OUTPUT + echo $OUTPUT echo "***********************************************" echo " Detekt failed " echo " Please fix the above issues before committing " echo "***********************************************" exit $EXIT_CODE fi -rm $OUTPUT ```