From c7fdd913c599805a92c29a7d1ddd57140e182b6e Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Fri, 8 Oct 2021 10:38:09 +0530 Subject: Add `installGitHooks` task for pre-push Git hooks (#1516) * build: add a pre-push task and Gradle task to install it * Document `installGitHooks` in contributing.md --- buildSrc/src/main/java/BaseProjectConfig.kt | 6 ++++ buildSrc/src/main/java/GitHooks.kt | 44 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 buildSrc/src/main/java/GitHooks.kt (limited to 'buildSrc/src') diff --git a/buildSrc/src/main/java/BaseProjectConfig.kt b/buildSrc/src/main/java/BaseProjectConfig.kt index 06648ae0..2c6f8fdc 100644 --- a/buildSrc/src/main/java/BaseProjectConfig.kt +++ b/buildSrc/src/main/java/BaseProjectConfig.kt @@ -11,6 +11,7 @@ import org.gradle.api.tasks.testing.Test import org.gradle.api.tasks.testing.logging.TestLogEvent import org.gradle.api.tasks.wrapper.Wrapper import org.gradle.kotlin.dsl.maven +import org.gradle.kotlin.dsl.register import org.gradle.kotlin.dsl.repositories import org.gradle.kotlin.dsl.withType import org.gradle.language.nativeplatform.internal.BuildType @@ -26,6 +27,11 @@ internal fun Project.configureForRootProject() { distributionSha256Sum = "f581709a9c35e9cb92e16f585d2c4bc99b2b1a5f85d2badbd3dc6bff59e1e6dd" } configureBinaryCompatibilityValidator() + tasks.register("installGitHooks") { + val projectDirectory = layout.projectDirectory + hookScript.set(projectDirectory.file("scripts/pre-push-hook.sh").asFile.readText()) + hookOutput.set(projectDirectory.file(".git/hooks/pre-push").asFile) + } } /** Configure all projects including the root project */ diff --git a/buildSrc/src/main/java/GitHooks.kt b/buildSrc/src/main/java/GitHooks.kt new file mode 100644 index 00000000..c7e9066f --- /dev/null +++ b/buildSrc/src/main/java/GitHooks.kt @@ -0,0 +1,44 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +import java.io.File +import java.nio.file.Files +import java.nio.file.attribute.PosixFilePermission.GROUP_EXECUTE +import java.nio.file.attribute.PosixFilePermission.GROUP_READ +import java.nio.file.attribute.PosixFilePermission.OTHERS_EXECUTE +import java.nio.file.attribute.PosixFilePermission.OTHERS_READ +import java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE +import java.nio.file.attribute.PosixFilePermission.OWNER_READ +import java.nio.file.attribute.PosixFilePermission.OWNER_WRITE +import org.gradle.api.DefaultTask +import org.gradle.api.provider.Property +import org.gradle.api.tasks.CacheableTask +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.TaskAction + +@CacheableTask +abstract class GitHooks : DefaultTask() { + @get:Input abstract val hookScript: Property + + @get:OutputFile abstract val hookOutput: Property + + @TaskAction + fun install() { + hookOutput.get().writeText(hookScript.get()) + Files.setPosixFilePermissions( + hookOutput.get().toPath(), + setOf( + OWNER_READ, + OWNER_WRITE, + OWNER_EXECUTE, + GROUP_READ, + GROUP_EXECUTE, + OTHERS_READ, + OTHERS_EXECUTE, + ) + ) + } +} -- cgit v1.2.3