Last active
January 22, 2021 16:19
-
-
Save EbenezerGH/895c71695400fe179b87155a9219e841 to your computer and use it in GitHub Desktop.
An example of creating a lint check before each git commit.
This file contains hidden or 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
// Task to be added in root built.gradle | |
task installGitHook(type: Copy) { | |
from new File(rootProject.rootDir, 'pre-commit') | |
into { new File(rootProject.rootDir, '.git/hooks') } | |
fileMode 0777 | |
} | |
tasks.getByPath(':app:preBuild').dependsOn installGitHook | |
------------------------------------------------------------ | |
#script to be added to pre-commit file in project root | |
#!/bin/bash | |
set -e | |
echo "*******************************" | |
echo (~˘▾˘)~ "Running linter" ~(˘▾˘~) | |
echo "*******************************" | |
git stash -q --keep-index | |
./gradlew ktlint --continue | |
RESULT=$? | |
git stash pop -q | |
# return 1 exit code if running checks fails | |
[ $RESULT -ne 0 ] && exit 1 | |
exit 0 | |
----------------------------------------- | |
Copy - Copies files into a destination directory. This task can also rename and filter files as it copies. The task implements CopySpec for specifying what to copy. | |
https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/Copy.html | |
from new File(rootProject.rootDir, 'pre-commit') | |
into { new File(rootProject.rootDir, '.git/hooks') } | |
This looks for a file in my root directory named 'pre-commit' and adds it to the directory '.git/hooks'. The hooks are all stored in the hooks subdirectory of the Git directory. In most projects, by default it'll be in this location .git/hooks. | |
Git hooks are scripts that run automatically every time a particular event occurs in a Git repository. They let you customize Git's internal behavior and trigger customizable actions at key points in the development life cycle. | |
fileMode 0777 file permission equivalent of CHMOD777 | |
preBuild will be automatically called before build task. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/pinterest/ktlint Linting Library used