fun uninstallHook()

in bunch-cli/src/main/kotlin/org/jetbrains/bunches/hooks/hooks.kt [191:218]


fun uninstallHook(type: HookType, dotGitPath: File) {
    val hooksPath = File(dotGitPath, "hooks")
    val hookFile = File(hooksPath, type.hookName)
    if (!hookFile.exists()) {
        exitWithLocalError("${type.hookName} hook is not found")
    }

    val hookCode = hookFile.readText()

    if (!checkHookMarker(hookCode, type)) {
        exitWithLocalError("${type.hookName} hook exists but this is not the bunch tool hook")
    }

    val hookParams = hookCodeParams(hookCode)
    if (!hookFile.delete()) {
        exitWithLocalError("Couldn't delete ${type.hookName} hook file")
    }

    if (hookParams.oldHookPath == ":")
        println("Successfully uninstalled ${type.hookName} hook")
    else {
        val oldHookFile = File(hookParams.oldHookPath)
        if (oldHookFile.renameTo(File(hooksPath, type.hookName)))
            println("Successfully uninstalled. Old ${type.hookName} hook was reverted.")
        else
            exitWithLocalError("Successfully uninstalled. Old ${type.hookName} hook wasn't restored.")
    }
}