JLLeitschuh / ktlint-gradle

A ktlint gradle plugin
MIT License
1.45k stars 159 forks source link

Gradlew and .git is not in the same root folder- `addKtlintCheckGitPreCommitHook` always successfully even i broke the rule [pre-commit] #374

Open ksyamkrishnan opened 4 years ago

ksyamkrishnan commented 4 years ago

Under the same ref: https://github.com/JLLeitschuh/ktlint-gradle/issues/351 Hi @Tapchicoma , As per our discussions I created a new sample project and was able to reproduce the same issues. The details are provided in the readme file.

Please find the sample project here. I think the issue is with the changed file path. Can you please provide more insights on this. Also if multiple files are there in the changed files from different modules, this script will handle it.

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

CHANGED_FILES="$(git --no-pager diff --name-status --no-color --cached -- public/trunk/ | 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"

./public/trunk/gradlew -p ./public/trunk --quiet ktlintCheck -PinternalKtlintGitFilter="$CHANGED_FILES"

echo "Completed ktlint run."

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

Thanks in Advance, Syam

@Tapchicoma the changed file path coming is public/trunk/app/src/main/java/com/example/myapplication/MainActivity.kt which is not working but if I change this to app/src/main/java/com/example/myapplication/MainActivity.kt it is working. Can you please suggest what changes we need to make to work it across?

ksyamkrishnan commented 4 years ago

@Tapchicoma the changed file path coming is public/trunk/app/src/main/java/com/example/myapplication/MainActivity.kt which is not working but if I change this to app/src/main/java/com/example/myapplication/MainActivity.kt it is working. Can you please suggest what changes we need to make to work it across?

Thanks in advance

gregblaszczuk commented 3 years ago

I just ran into the same problem and the issue has to do with the filepath of the changed files that are fed to PinternalKtlintGitFilter. The files include the full dir path, but it looks like the git filter does not understand the full path. When I removed the ./public/trunk/ prefix from those files, I was able to get PinternalKtlintGitFilter to work.

ravjain-adb commented 3 years ago

Can you provide the solution here

Simon3 commented 1 year ago

I've just ran into the same problem too. Basically, the git hook doesn't work if your build.gradle is not at the root of your git repository (e.g. if you are hosting a backend and a frontend in the same git repository, which is quite common).

akhil-handa commented 7 months ago

Was anyone able to figure out the solution for this, also how to make this work for multimodule app?

ecabestan commented 3 months ago

I think I add the same issue. The problem is related to the root directory of the filter. I have fixed by adding the --relative option to git diff

in your case, it should be :

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

JLLeitschuh commented 3 months ago

Feel free to contribute a pull request if you believe you have a fix. Please also add a unit test if possible! 😄