Skip to content

Instantly share code, notes, and snippets.

@KenVanHoeylandt
Last active February 24, 2024 14:18
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
Copy link

ghost commented May 7, 2016

./gradlew test --daemon is faster

@FrancescoSantagati
Copy link

FrancescoSantagati commented Nov 2, 2016

pre-commit file generated is not executable and doesn't work. I modified install-git-hooks.gradle in this way:

tasks.create(name: 'gitExecutableHooks') << {
    Runtime.getRuntime().exec("chmod -R +x .git/hooks/");
}

task installGitHooks(type: Copy) {
    from new File(rootProject.rootDir, 'pre-commit')
    into { new File(rootProject.rootDir, '.git/hooks') }
}

gitExecutableHooks.dependsOn installGitHooks
clean.dependsOn gitExecutableHooks

@Bwvolleyball
Copy link

Great script! make sure the apply from line in the build.gradle is below applying the java plugin, otherwise it can't find build (or test) from that plugin

@jonsmithers
Copy link

On unix systems, I believe you can just do this:

(build.gradle)
"cp gradle/pre-commit .git/hooks/pre-commit".execute()

@omainegra
Copy link

I use this for my Android projects (in top build.gradle)

task installGitHooks(type: Copy) {
    from new File(rootProject.rootDir, 'pre-push')
    into { new File(rootProject.rootDir, '.git/hooks') }
    fileMode 0777
}

tasks.getByPath(':app:preBuild').dependsOn installGitHooks

@pollux-
Copy link

pollux- commented Jul 15, 2019

./gradlew not accessible inside the .git/hooks, how to do that? which lies under your app/ folder @omainegra

@jooncco
Copy link

jooncco commented Dec 14, 2021

@pollux- at the moment the pre-commit hook is executed, current working directory is ${rootProject.rootDir}. Not .git/hooks.
No problem running ./gradlew

@jooncco
Copy link

jooncco commented Dec 14, 2021

@FrancescoSantagati Thanks for the great script. In my case, I used doLast instead of << operator.
I'm using Gradle 6.4.

tasks.create(name: 'gitExecutableHooks') {
    doLast {
        Runtime.getRuntime().exec("chmod -R +x .git/hooks/");
    }
}

task installGitHooks(type: Copy) {
    from new File(rootProject.rootDir, 'pre-commit')
    into { new File(rootProject.rootDir, '.git/hooks') }
}

gitExecutableHooks.dependsOn installGitHooks
clean.dependsOn gitExecutableHooks

@leinardi
Copy link

This is my take on the task:

tasks.register("installGitHooks", Copy) {
    from file("$rootDir/.githooks")
    into file("$rootDir/.git/hooks")
    fileMode 0775
}

775 I think is way safer than 777, especially for a script that gets executed automatically.

The task will copy all the hooks that are stored inside the .githooks directory on the root of the project.

For Android it can be added as dependency to the preBuild task pasting this inside the App build.gradle:

afterEvaluate {
    preBuild.dependsOn installGitHooks
}

@sumanabhi
Copy link

I'm using the Kotlin DSL Gradle.

while I'm trying the same but whenever I make any push to the repo it doesn't trigger.

tasks.register("installGitHook", Copy::class) {
    from(file("$rootDir/.githooks"))
    into(file("$rootDir/.git/hooks"))
    fileMode = 0b0111101101 // -rwxr-xr-x
}
tasks.getByPath(":app:preBuild").dependsOn(tasks.named("installGitHook"))

My pre-push.sh file is inside git-hooks.

#!/bin/sh
echo "*********************************************************"
echo "Running git pre-push hook. Running Static analysis... "
echo "*********************************************************"

./gradlew detekt ktlintCheck --daemon

status=$?

if [ "$status" = 0 ] ; then
    echo "Static analysis found no problems."
    exit 0
else
    echo "*********************************************************"
    echo "       ********************************************      "
    echo 1>&2 "Static analysis found violations it could not fix."
    echo "Run ./gradlew ktlintFormat to fix formatting related issues."
    echo "       ********************************************      "
    echo "*********************************************************"
    exit 1
fi

Where and what I'm doing wrong?

@stackunderflows
Copy link

I'm using the Kotlin DSL Gradle.

while I'm trying the same but whenever I make any push to the repo it doesn't trigger.

tasks.register("installGitHook", Copy::class) {
    from(file("$rootDir/.githooks"))
    into(file("$rootDir/.git/hooks"))
    fileMode = 0b0111101101 // -rwxr-xr-x
}
tasks.getByPath(":app:preBuild").dependsOn(tasks.named("installGitHook"))

My pre-push.sh file is inside git-hooks.

#!/bin/sh
echo "*********************************************************"
echo "Running git pre-push hook. Running Static analysis... "
echo "*********************************************************"

./gradlew detekt ktlintCheck --daemon

status=$?

if [ "$status" = 0 ] ; then
    echo "Static analysis found no problems."
    exit 0
else
    echo "*********************************************************"
    echo "       ********************************************      "
    echo 1>&2 "Static analysis found violations it could not fix."
    echo "Run ./gradlew ktlintFormat to fix formatting related issues."
    echo "       ********************************************      "
    echo "*********************************************************"
    exit 1
fi

Where and what I'm doing wrong?

I'm having the same problem. The hook will run properly once it's installed, but it doesn't get installed from preBuild.

@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