Skip to content

Instantly share code, notes, and snippets.

@k3muri84
Last active May 8, 2021 14:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@neugartf
Copy link

neugartf commented May 7, 2021

#!/bin/sh
set -e
######## KTLINT-GRADLE HOOK START ########

    CHANGED_FILES="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $2 ~ /\.kts|\.kt/ { print $2}')"

    if [ -z "$CHANGED_FILES" ]; then
        echo "No Kotlin staged files."
        exit 0
    fi;

    echo "Running ktlint over these files:"
    echo "$CHANGED_FILES"

    # Change depending on the format script
    scripts/ktlint/ktlint -F $CHANGED_FILES

    echo "Completed ktlint run."
    echo "$CHANGED_FILES" | while read -r file; do
    if [ -f $file ]; then
        git add $file
    fi
done
    echo "Completed ktlint hook."
######## KTLINT-GRADLE HOOK END ########

@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