From 556982ad8bdf9228ededf5ba98b633ed5dd01476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Brey=20Vilas?= Date: Fri, 24 Sep 2021 14:07:11 +0200 Subject: [PATCH] Add simple git hooks and gradle task to install them MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Pre-commit hook checks ktlint and detekt - Pre-push hook checks signoff Signed-off-by: Álvaro Brey Vilas --- CONTRIBUTING.md | 8 ++++++++ build.gradle | 7 +++++++ scripts/hooks/pre-commit | 6 ++++++ scripts/hooks/pre-push | 31 +++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100755 scripts/hooks/pre-commit create mode 100755 scripts/hooks/pre-push diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c578bd3b29..519ea35ab2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -119,6 +119,14 @@ There are three build variants * gplay: with Google Stuff (Push notification), used for Google Play Store * versionDev: based on master and library master, available as direct download and FDroid +### Git hooks +We provide git hooks to make development process easier for both the developer and the reviewers. +To install them, just run: + +```bash +./gradlew installGitHooks +``` + ## Contribution process * Contribute your code in the branch 'master'. It will give us a better chance to test your code before merging it with stable code. * For your first contribution start a pull request on master. diff --git a/build.gradle b/build.gradle index 37c0613443..1c61e7caa7 100644 --- a/build.gradle +++ b/build.gradle @@ -440,6 +440,13 @@ task ktlintFormat(type: JavaExec, group: "formatting") { args "-F", "src/**/*.kt" } +task installGitHooks(type: Copy) { + from('scripts/hooks') { + include '*' + } + into '.git/hooks' +} + detekt { reports { xml { diff --git a/scripts/hooks/pre-commit b/scripts/hooks/pre-commit new file mode 100755 index 0000000000..4707b2d632 --- /dev/null +++ b/scripts/hooks/pre-commit @@ -0,0 +1,6 @@ +#!/bin/bash +# Pre-commit hook: don't allow commits if detekt or ktlint fail. Skip with "git commit --no-verify". +set -euo pipefail + +./gradlew --daemon --quiet detekt +./gradlew --daemon --quiet ktlint diff --git a/scripts/hooks/pre-push b/scripts/hooks/pre-push new file mode 100755 index 0000000000..aaaa929bfb --- /dev/null +++ b/scripts/hooks/pre-push @@ -0,0 +1,31 @@ +#!/bin/bash +# Pre-push: Don't allow commits without Signed-off-by. Skip with "git push --no-verify". +set -euo pipefail + +z40=0000000000000000000000000000000000000000 # magic deleted ref + +while read local_ref local_sha remote_ref remote_sha +do + if [ "$local_sha" != $z40 ] + then + if [ "$remote_sha" = $z40 ] + then + # New branch, examine all commits + range="$(git merge-base master $local_sha)..$local_sha" + else + # Update to existing branch, examine new commits + range="$remote_sha..$local_sha" + fi + + # Check for WIP commit + commit=$(git rev-list --grep 'Signed-off-by' --invert-grep "$range") + if [ -n "$commit" ] + then + echo >&2 "Found commits without signoff in $local_ref. Aborting push. Offending commits:" + echo >&2 "$commit" + exit 1 + fi + fi +done + +exit 0