-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from VahidGarousi/54-add-git-hooks
Adding Git Hooks for $54
- Loading branch information
Showing
4 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ######## |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/sh | ||
|
||
echo "Running static analysis." | ||
|
||
./gradlew ktlintCheck | ||
./gradlew detekt |