Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git hook behaves differently than ktlintCheck #648

Open
mobilekosmos opened this issue Mar 3, 2023 · 3 comments
Open

git hook behaves differently than ktlintCheck #648

mobilekosmos opened this issue Mar 3, 2023 · 3 comments

Comments

@mobilekosmos
Copy link

if I add the plug-in and run gradlew ktlintCheck in the Android Studio terminal the checks works, but if I generate the git hook with gradlew addKtlintCheckGitPreCommitHook, the file pre-commit is generated but then when committing the check doesn't find any issues. What I see in the console is:

Task :loadKtlintReporters UP-TO-DATE
Task :runKtlintCheckOverKotlinScripts NO-SOURCE
Task :ktlintKotlinScriptCheck UP-TO-DATE
Task :ktlintCheck UP-TO-DATE
Task :myapp:feature:myfeature:loadKtlintReporters UP-TO-DATE
Task :myapp:feature:myfeature:runKtlintCheckOverAndroidTestDebugSourceSet NO-SOURCE
Task :myapp:feature:myfeature:ktlintAndroidTestDebugSourceSetCheck SKIPPED
Task :myapp:feature:myfeature:runKtlintCheckOverAndroidTestPreviewSourceSet NO-SOURCE
Task :myapp:feature:myfeature:ktlintAndroidTestPreviewSourceSetCheck SKIPPED
Task :myapp:feature:myfeature:runKtlintCheckOverAndroidTestReleaseSourceSet NO-SOURCE
Task :myapp:feature:myfeature:ktlintAndroidTestReleaseSourceSetCheck SKIPPED
Task :myapp:feature:myfeature:runKtlintCheckOverAndroidTestSourceSet NO-SOURCE
...

@JLLeitschuh
Copy link
Owner

Can you share the commit hook file that was generated for you?

@mobilekosmos
Copy link
Author

mobilekosmos commented Mar 4, 2023

I htink it always generates the same file:

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

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

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

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

diff=.git/unstaged-ktlint-git-hook.diff
git diff --color=never > $diff
if [ -s $diff ]; then
  git apply -R $diff
fi

./gradlew --quiet ktlintCheck -PinternalKtlintGitFilter="$CHANGED_FILES"
gradle_command_exit_code=$?

echo "Completed ktlint run."


if [ -s $diff ]; then
  git apply --ignore-whitespace $diff
fi
rm $diff
unset diff

echo "Completed ktlint hook."
exit $gradle_command_exit_code
######## KTLINT-GRADLE HOOK END ########

@ubuntudroid
Copy link

Just stumbled up this, probably resolved already, but in your case the script only seems to look for changed .kts files.

Try changing the CHANGED_FILES assignment to:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants