Skip to content

Instantly share code, notes, and snippets.

@aiKrice
Last active February 25, 2024 20:57
Show Gist options
  • Select an option

  • Save aiKrice/6a1a3e0a7f401076585ebb2132f1d916 to your computer and use it in GitHub Desktop.

Select an option

Save aiKrice/6a1a3e0a7f401076585ebb2132f1d916 to your computer and use it in GitHub Desktop.
Detekt: pre-commit
#!/bin/sh
# Get kotlin files that are staged
staged_files=$(git diff --cached --name-only --diff-filter=d | grep ".kt$")
# Check if we have any Kotlin files to lint
if [[ "$staged_files" = "" ]]; then
echo "Detekt skipped: No source files to lint."
exit 0
fi
# Convert newline-separated list to comma-separated list
working_files=$(echo "$staged_files" | tr '\n' ',')
# Remove trailing comma
working_files=${working_files%,}
echo "$staged_files" | xargs detekt -b baseline.xml -c detekt.yml -i $working_files -ex "**/generated/**"
if [ $? -ne 0 ]; then
echo "Detekt failed, commit denied"
exit 1
fi
# Add changes to git
echo "$staged_files" | xargs git add
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment