Skip to content

Commit

Permalink
Merge pull request #471 from KotlinIsland/git-hook-format-renames
Browse files Browse the repository at this point in the history
git hook: format renamed files
  • Loading branch information
Tapchicoma committed Apr 27, 2021
2 parents cde76f3 + 40151b7 commit e6e5609
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
### Fixed
- Plugin fails to apply on non-Kotlin projects ([#443](https://github.com/JLLeitschuh/ktlint-gradle/issues/443))
- Pre-commit hook adds entire file to commit when only part of the file was indexed ([#470](https://github.com/JLLeitschuh/ktlint-gradle/pull/470))
- Pre-commit hook doesn't format files that have been renamed ([#471](https://github.com/JLLeitschuh/ktlint-gradle/pull/471))
### Removed
- ?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ private fun postCheck(
""
}

internal const val NF = "\$NF"

@Language("Sh")
internal fun generateGitHook(
taskName: String,
Expand All @@ -65,7 +67,7 @@ internal fun generateGitHook(
) =
"""
CHANGED_FILES="${'$'}(${generateGitCommand(gradleRootDirPrefix)} | awk '$1 != "D" && $2 ~ /\.kts|\.kt/ { print $2}')"
CHANGED_FILES="${'$'}(${generateGitCommand(gradleRootDirPrefix)} | awk '$1 != "D" && $NF ~ /\.kts?$/ { print $NF }')"
if [ -z "${'$'}CHANGED_FILES" ]; then
echo "No Kotlin staged files."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,28 @@ class GitHookTasksTest : AbstractPluginTest() {
}
}

@Test
internal fun `Format hook should format files when they are renamed`() {
projectRoot.setupGradleProject()
val gitDir = projectRoot.initGit()

build(":$INSTALL_GIT_HOOK_FORMAT_TASK").run {
assertThat(task(":$INSTALL_GIT_HOOK_FORMAT_TASK")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(gitDir.preCommitGitHook().readText()).contains("""{ print $NF }""")
}
}

@Test
internal fun `Format hook should only format files that end with kt or kts`() {
projectRoot.setupGradleProject()
val gitDir = projectRoot.initGit()

build(":$INSTALL_GIT_HOOK_FORMAT_TASK").run {
assertThat(task(":$INSTALL_GIT_HOOK_FORMAT_TASK")?.outcome).isEqualTo(TaskOutcome.SUCCESS)
assertThat(gitDir.preCommitGitHook().readText()).contains("""/\.kts?$/""")
}
}

private fun File.initGit(): File {
val repo = RepositoryBuilder().setWorkTree(this).setMustExist(false).build()
repo.create()
Expand Down

0 comments on commit e6e5609

Please sign in to comment.