Browse Source

Add simple git hooks and gradle task to install them

 - Pre-commit hook checks ktlint and detekt
 - Pre-push hook checks signoff

Signed-off-by: Álvaro Brey Vilas <alvaro.brey@nextcloud.com>
Álvaro Brey Vilas 3 years ago
parent
commit
556982ad8b
4 changed files with 52 additions and 0 deletions
  1. 8 0
      CONTRIBUTING.md
  2. 7 0
      build.gradle
  3. 6 0
      scripts/hooks/pre-commit
  4. 31 0
      scripts/hooks/pre-push

+ 8 - 0
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.

+ 7 - 0
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 {

+ 6 - 0
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

+ 31 - 0
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