Skip to content

Commit

Permalink
Merge pull request #55 from VahidGarousi/54-add-git-hooks
Browse files Browse the repository at this point in the history
Adding Git Hooks for $54
  • Loading branch information
VahidGarousi authored Jan 27, 2023
2 parents 794c55e + 03ea55a commit f7d74f4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
28 changes: 28 additions & 0 deletions buildscripts/git-hooks.gradle
Original file line number Diff line number Diff line change
@@ -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.')
}
}
26 changes: 26 additions & 0 deletions git-hooks/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -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 ########
6 changes: 6 additions & 0 deletions git-hooks/pre-push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

echo "Running static analysis."

./gradlew ktlintCheck
./gradlew detekt

0 comments on commit f7d74f4

Please sign in to comment.