Skip to content

Instantly share code, notes, and snippets.

@KenVanHoeylandt
Last active November 7, 2024 20:06
Show Gist options
  • Save KenVanHoeylandt/c7a928426bce83ffab400ab1fd99054a to your computer and use it in GitHub Desktop.
Save KenVanHoeylandt/c7a928426bce83ffab400ab1fd99054a to your computer and use it in GitHub Desktop.
Gradle auto-installing pre-commit hook
apply from: rootProject.file('gradle/install-git-hooks.gradle')
task installGitHooks(type: Copy) {
from new File(rootProject.rootDir, 'pre-commit')
into { new File(rootProject.rootDir, '.git/hooks') }
}
build.dependsOn installGitHooks
#!/bin/sh
# From gist at https://gist.github.com/chadmaughan/5889802
# stash any unstaged changes
git stash -q --keep-index
# run the tests with the gradle wrapper
./gradlew test
# store the last exit code in a variable
RESULT=$?
# unstash the unstashed changes
git stash pop -q
# return the './gradlew test' exit code
exit $RESULT
@sumanabhi
Copy link

@stackunderflows I resolves this issue by defining a executable task for Installing the same. Please find the attach snippets for the same.

// Tasks for automatically installing git-hooks and making it executable also.
tasks.register("installGitHook", Copy::class) {
    from(file("$rootDir/scripts/pre-push"))
    into(file("$rootDir/.git/hooks"))
    fileMode = 0b0111101101 // -rwxr-xr-x
}

tasks.create(name = "gitExecutableHooks") {
    doLast {
        Runtime.getRuntime().exec("chmod -R +x .git/hooks/")
    }
}
tasks.getByPath("gitExecutableHooks").dependsOn(tasks.named("installGitHook"))
tasks.getByPath(":app:clean").dependsOn(tasks.named("gitExecutableHooks"))

This is working like charm !!

Let me know if you still face any issues while doing this.

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