Files
detekt/docs/pages/gettingstarted/git-pre-commit-hook.md
Artur Bosch ded9617c8d Remove tags from common documentation sites - Closes #2309 (#2466)
Tags for the news/guides are still useful
2020-03-19 17:59:59 +01:00

1.5 KiB

title, keywords, sidebar, permalink, folder, summary
title keywords sidebar permalink folder summary
Run detekt using a Git pre-commit hook detekt static analysis code kotlin git-pre-commit-hook.html gettingstarted

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.

#!/usr/bin/env bash
echo "Running detekt check..."
OUTPUT="/tmp/detekt-$(date +%s)"
./gradlew detekt > $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

The shell script can be installed by copying the content over to <<your-repo>>/.git/hooks/pre-commit. This pre-commit hook needs to be executable, so you may need to change the permission (chmod +x pre-commit). More information about Git hooks and how to install them can be found in Atlassian's tutorial.

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.