diff --git a/docs/pages/gettingstarted/git-pre-commit-hook.md b/docs/pages/gettingstarted/git-pre-commit-hook.md index 977f388f879..5d8d5ade3d9 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,44 @@ 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 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. + +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=$(detekt --input "$detektInput" 2>&1) +EXIT_CODE=$? +if [ $EXIT_CODE -ne 0 ]; then + echo $OUTPUT + echo "***********************************************" + echo " Detekt failed " + echo " Please fix the above issues before committing " + echo "***********************************************" + exit $EXIT_CODE +fi +```