Skip to content

Instantly share code, notes, and snippets.

@k3muri84
Last active May 8, 2021 14:44
Show Gist options
  • Save k3muri84/4246cd4ec42098fce66c169d28aa2812 to your computer and use it in GitHub Desktop.
Save k3muri84/4246cd4ec42098fce66c169d28aa2812 to your computer and use it in GitHub Desktop.
ktlintFormat pre-commit hook
#!/bin/sh
# based on https://github.com/shyiko/ktlint pre-commit hook
# with further tweaks of @neugartf
set -e
CHANGED_FILES="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $2 ~ /\.kts|\.kt/ { print $2}')"
if [ -z "$CHANGED_FILES" ]; then
exit 0
fi;
echo "Running ktlint over these files:"
echo "$CHANGED_FILES"
# Change depending on the format script
ktlint -F $CHANGED_FILES
echo "$CHANGED_FILES" | while read -r file; do
if [ -f $file ]; then
git add $file
fi
done
@k3muri84
Copy link
Author

k3muri84 commented May 7, 2021

thx @neugartf, tweaked my file

@neugartf
Copy link

neugartf commented May 8, 2021

Nice! Good idea btw to run ktlint standalone. Running this with gradle takes too much config time currently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment