diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2021-10-08 10:38:09 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-08 10:38:09 +0530 |
commit | c7fdd913c599805a92c29a7d1ddd57140e182b6e (patch) | |
tree | 05287fb53feeae9ce6a36a17be57ddd0f43f7c93 /buildSrc/src | |
parent | 62d78dd52a4d2949dcef805bfb0c1c1eef65d623 (diff) |
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
Diffstat (limited to 'buildSrc/src')
-rw-r--r-- | buildSrc/src/main/java/BaseProjectConfig.kt | 6 | ||||
-rw-r--r-- | buildSrc/src/main/java/GitHooks.kt | 44 |
2 files changed, 50 insertions, 0 deletions
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<GitHooks>("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<String> + + @get:OutputFile abstract val hookOutput: Property<File> + + @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, + ) + ) + } +} |