Git pre-hook: pre-commit with Gradle task

There are various Git pre-hooks that are quite helpful for several essential tasks we want to execute before commit or push or rebase etc. Basically, there are various use cases, like running a linting before you commit or running unit tests before pus…


This content originally appeared on DEV Community and was authored by Animesh Kumar

There are various Git pre-hooks that are quite helpful for several essential tasks we want to execute before commit or push or rebase etc. Basically, there are various use cases, like running a linting before you commit or running unit tests before push or commit.

Whenever a Git repository is initialized, Git creates sample hooks inside .git/hooks directory in the project directory. E.g. .git/hooks/pre-commit.sample

Below are steps on how to configure pre-hook for a Gradle project:

1. Create a pre-hook script, let's create pre-commit file inside a new scripts directory, and we want to run unit tests before code commit.

#!/bin/sh

echo "*****Running unit tests******"

git stash -q --keep-index

./gradlew test

status=$?

git stash pop -q

exit $status

Above command stash the working directory changes before running the unit tests, and unstash back. This makes sure, we're running unit tests only in the clean working directory. (as this is been configured for pre-commit, changes must have been staged, make sense??)

2. Next, create Gradle task to install the pre-commit script. Why do we need it? because, we want this to run on all developers' machine, not just on our machine, we all love consistency and wants to put the constraints.


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

build.dependsOn installLocalGitHook

Here pre-commit file was created inside project root directory scripts

Above Gradle task will run wherever someone takes the build and assuming that developer who is making changes will run build task at least once and I hope I'm right?.

Once pre-commit script is copied to .git/hooks/ directory, we're all set.

Next time, whenever someone will run git commit, it will first run the ./gradlew test task.

git commit -m "update code"

-- Output
*****Running unit tests******
.
.
.

The way ./gradlew test is configured, in the same way, any other task can be executed. And of course for other tasks stash and unstash won't compulsory.

If you have reached here, then I did a satisfactory effort to keep you reading. Please be kind to leave any comments or ask for any corrections. Happy Coding!


This content originally appeared on DEV Community and was authored by Animesh Kumar


Print Share Comment Cite Upload Translate Updates
APA

Animesh Kumar | Sciencx (2021-09-16T23:00:32+00:00) Git pre-hook: pre-commit with Gradle task. Retrieved from https://www.scien.cx/2021/09/16/git-pre-hook-pre-commit-with-gradle-task/

MLA
" » Git pre-hook: pre-commit with Gradle task." Animesh Kumar | Sciencx - Thursday September 16, 2021, https://www.scien.cx/2021/09/16/git-pre-hook-pre-commit-with-gradle-task/
HARVARD
Animesh Kumar | Sciencx Thursday September 16, 2021 » Git pre-hook: pre-commit with Gradle task., viewed ,<https://www.scien.cx/2021/09/16/git-pre-hook-pre-commit-with-gradle-task/>
VANCOUVER
Animesh Kumar | Sciencx - » Git pre-hook: pre-commit with Gradle task. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/16/git-pre-hook-pre-commit-with-gradle-task/
CHICAGO
" » Git pre-hook: pre-commit with Gradle task." Animesh Kumar | Sciencx - Accessed . https://www.scien.cx/2021/09/16/git-pre-hook-pre-commit-with-gradle-task/
IEEE
" » Git pre-hook: pre-commit with Gradle task." Animesh Kumar | Sciencx [Online]. Available: https://www.scien.cx/2021/09/16/git-pre-hook-pre-commit-with-gradle-task/. [Accessed: ]
rf:citation
» Git pre-hook: pre-commit with Gradle task | Animesh Kumar | Sciencx | https://www.scien.cx/2021/09/16/git-pre-hook-pre-commit-with-gradle-task/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.