Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
- ?

### Fixed
- ?
- Pre-commit hook causing conflicts ([issue: #443](https://github.com/JLLeitschuh/ktlint-gradle/issues/443)) ([#502](https://github.com/JLLeitschuh/ktlint-gradle/pull/502))

### Removed
- ?
Expand Down
12 changes: 10 additions & 2 deletions plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/GitHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,22 @@ internal fun generateGitHook(
echo "Running ktlint over these files:"
echo "${'$'}CHANGED_FILES"

git stash push --keep-index
diff=.git/unstaged-ktlint-git-hook.diff
git diff --color=never > ${'$'}diff
if [ -s ${'$'}diff ]; then
git apply -R ${'$'}diff
fi

${generateGradleCommand(taskName, gradleRootDirPrefix)}

echo "Completed ktlint run."
${postCheck(shouldUpdateCommit)}

git stash pop
if [ -s ${'$'}diff ]; then
git apply --ignore-whitespace ${'$'}diff
fi
rm ${'$'}diff
unset diff

echo "Completed ktlint hook."

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,10 @@ class GitHookTasksTest : AbstractPluginTest() {
build(":$INSTALL_GIT_HOOK_FORMAT_TASK").run {
assertThat(task(":$INSTALL_GIT_HOOK_FORMAT_TASK")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
val hookText = gitDir.preCommitGitHook().readText()
assertThat(hookText).contains("git stash push")
assertThat(hookText).contains("git stash pop")
assertThat(hookText).contains("git diff --color=never > \$diff")
assertThat(hookText).contains("git apply -R \$diff")
assertThat(hookText).contains("git apply --ignore-whitespace \$diff")
assertThat(hookText).contains("rm \$diff")
}
}

Expand Down