Files
kotlin-fork/libraries/tools/kotlin-prepush-hook/build.gradle.kts
T
Bogdan Mukvich e97ecc13b0 Cleanup pre-push hook
KTI-673
2022-12-20 08:53:41 +00:00

27 lines
809 B
Kotlin

import org.gradle.api.Project
import java.io.File
project.removePrePushHookIfExists()
fun Project.removePrePushHookIfExists() {
val prePushHookPath = rootProject.getGitDirectory().toPath()
.resolve("hooks")
.resolve("pre-push")
java.nio.file.Files.deleteIfExists(prePushHookPath)
}
fun Project.getGitDirectory(): File {
val dotGitFile = File(projectDir, ".git")
return if (dotGitFile.isFile) {
val workTreeLink = dotGitFile.readLines().single { it.startsWith("gitdir: ") }
val mainRepoPath = workTreeLink
.substringAfter("gitdir: ", "")
.substringBefore("/.git/worktrees/", "")
.also { require(it.isNotEmpty()) }
File(mainRepoPath, ".git").also { require(it.isDirectory) }
} else {
dotGitFile
}
}