Skip to content

Instantly share code, notes, and snippets.

@EbenezerGH
Last active January 22, 2021 16:19
Show Gist options
  • Save EbenezerGH/895c71695400fe179b87155a9219e841 to your computer and use it in GitHub Desktop.
Save EbenezerGH/895c71695400fe179b87155a9219e841 to your computer and use it in GitHub Desktop.
An example of creating a lint check before each git commit.
// 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.
@EbenezerGH
Copy link
Author

EbenezerGH commented Jan 22, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment