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 --- CONTRIBUTING.md | 4 +++ buildSrc/src/main/java/BaseProjectConfig.kt | 6 ++++ buildSrc/src/main/java/GitHooks.kt | 44 +++++++++++++++++++++++++++++ scripts/pre-push-hook.sh | 9 ++++++ 4 files changed, 63 insertions(+) create mode 100644 buildSrc/src/main/java/GitHooks.kt create mode 100644 scripts/pre-push-hook.sh diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b29e4cdd..e3e8fd0e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -42,6 +42,10 @@ The app comes in two 'flavors', a FOSS-only **free** variant and a **nonFree** v You can find the generated APK at `app/build/outputs/apk//debug/app--debug.apk`. +## Pre-push checks + +The project enforces codestyle conventions and library API stability by virtue of a carefully curated Gradle build. To setup a Git pre-push hook to run them automatically, run `./gradlew installGitHooks`. + # Things to do before you start writing code If you're trying to fix a bug that already has an open issue, it's a good idea to drop a comment mentioning that you're working on a fix. If no open issue exists, ensure that you explain the bug you're fixing in some detail in the pull request body. This helps us as maintainers get a better sense of why you're making specific changes, and we might have insight into better ways of fixing the problem. 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, + ) + ) + } +} diff --git a/scripts/pre-push-hook.sh b/scripts/pre-push-hook.sh new file mode 100644 index 00000000..78dafecb --- /dev/null +++ b/scripts/pre-push-hook.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +set -e +set -u +set -o pipefail + +GRADLE_EXEC="${GRADLE_EXEC:-./gradlew}" + +"${GRADLE_EXEC}" spotlessCheck apiCheck -- cgit v1.2.3