From 03ea55ad9d63cba4c3ac6112485835c9808626a9 Mon Sep 17 00:00:00 2001 From: Vahid Garousi Date: Fri, 27 Jan 2023 23:03:36 +0330 Subject: [PATCH] Adding Git Hooks for $54 --- build.gradle | 1 + buildscripts/git-hooks.gradle | 28 ++++++++++++++++++++++++++++ git-hooks/pre-commit.sh | 26 ++++++++++++++++++++++++++ git-hooks/pre-push.sh | 6 ++++++ 4 files changed, 61 insertions(+) create mode 100644 buildscripts/git-hooks.gradle create mode 100644 git-hooks/pre-commit.sh create mode 100644 git-hooks/pre-push.sh diff --git a/build.gradle b/build.gradle index df828ac..27711ee 100644 --- a/build.gradle +++ b/build.gradle @@ -17,5 +17,6 @@ plugins { subprojects { apply from: "../buildscripts/ktlint.gradle" apply from: "../buildscripts/detekt.gradle" + apply from: "../buildscripts/git-hooks.gradle" apply from: "../buildscripts/versionsplugin.gradle" } \ No newline at end of file diff --git a/buildscripts/git-hooks.gradle b/buildscripts/git-hooks.gradle new file mode 100644 index 0000000..6b2994d --- /dev/null +++ b/buildscripts/git-hooks.gradle @@ -0,0 +1,28 @@ +// https://blog.sebastiano.dev/ooga-chaka-git-hooks-to-enforce-code-quality/ +static def isLinuxOrMacOs() { + def osName = System.getProperty('os.name').toLowerCase(Locale.ROOT) + return osName.contains('linux') || osName.contains('mac os') +} + +task copyGitHooks(type: Copy) { + description 'Copies the git hooks from /git-hooks to the .git folder.' + from("${rootDir}/git-hooks/") { + include '**/*.sh' + rename '(.*).sh', '$1' + } + into "${rootDir}/.git/hooks" + onlyIf { isLinuxOrMacOs() } +} + +task installGitHooks(type: Exec) { + description 'Installs the pre-commit git hooks from /git-hooks.' + group 'git hooks' + workingDir rootDir + commandLine 'chmod' + args '-R', '+x', '.git/hooks/' + dependsOn copyGitHooks + onlyIf { isLinuxOrMacOs() } + doLast { + logger.info('Git hook installed successfully.') + } +} \ No newline at end of file diff --git a/git-hooks/pre-commit.sh b/git-hooks/pre-commit.sh new file mode 100644 index 0000000..753486b --- /dev/null +++ b/git-hooks/pre-commit.sh @@ -0,0 +1,26 @@ +#!/bin/sh + +######## KTLINT-GRADLE HOOK START ######## + +CHANGED_FILES="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $2 ~ /\.kts|\.kt/ { print $2}')" + +if [ -z "$CHANGED_FILES" ]; then + echo "No Kotlin staged files." + exit 0 +fi; + +echo "Running ktlint over these files:" +echo "$CHANGED_FILES" + +./gradlew --quiet ktlintFormat -PinternalKtlintGitFilter="$CHANGED_FILES" + +echo "Completed ktlint run." + +echo "$CHANGED_FILES" | while read -r file; do + if [ -f $file ]; then + git add $file + fi +done + +echo "Completed ktlint hook." +######## KTLINT-GRADLE HOOK END ######## \ No newline at end of file diff --git a/git-hooks/pre-push.sh b/git-hooks/pre-push.sh new file mode 100644 index 0000000..a0cd68f --- /dev/null +++ b/git-hooks/pre-push.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +echo "Running static analysis." + +./gradlew ktlintCheck +./gradlew detekt \ No newline at end of file