Automate your workflow with git hooks

florence
Backticks & Tildes
Published in
4 min readMay 22, 2018
Credit: Atlass

What are git hooks

Git hooks are scripts that run prior to certain events like commit, push, rebase etc. Since they are event-based, when you run certain git commands, git will check the hooks within the git repository to see if there is an associated script to run. The hooks are found in the .git/hooks directory.

cd .git/hooks

If you list the files in that directory you will see the following sample hooks.

1. applypatch-msg.sample
2. commit-msg.sample
3. post-update.sample
4. pre-applypatch.sample
5. pre-commit.sample
6. pre-push.sample
7. pre-rebase.sample
8. pre-recieve.sample
9. prepare-commit-msg.sample
10.update.sample

How to use them to improve your git workflow

If you have ever worked on an open-source project with lots of contributors or perhaps you’re the technical team lead on a project and you are looking for ways to improve your team’s git workflow in such a way that certain workflow errors that developers are prone to are avoided before they are committed, perhaps you’re a developer who likes to always have their A game at 100 all the time, perhaps now is a good time to explore git hooks.

In this article, I am going to show you how to use the pre-push git hook to run tests before pushing code to your remote branch. In this post, I will be using PHP.

If you are using a Mac book then follow along. If you’re using a Windows PC, the shell commands will be different. Windows commands are not provided in this post.

Clone this repository. It contains a project built with Laravel and contains some unit tests written with PHPUnit. Once you have that cloned and set up, change directory to git hooks directory and create a pre-push file. Open this up with your favorite text editor, I use Atom.

cd .git/hooks
touch pre-push
atom pre-push // Open this file with any text editor you like

Ideally, I want to be able to run the tests before pushing code to my remote branch. If the hook script exits with 0 then all is well and git continues to push the code to my remote branch. If it exits with a non-zero code, git halts the operation.

Line 7:
The exec function triggers the command to run the tests which is vendor/bin/phpunit in this case.

If you are using any other test framework like codeception then you should replace vendor/bin/phpunit with the specific command to run the tests in your chosen framework. The contents of the output variable will be an array populated with every line of output from the command.

Line 9:
If the exit code is not equal to 0 then the test did not run successfully. You can examine the contents of the output variable and determine which item of the array you want to display on your terminal. I have displayed the item at index 14 which shows a summary of the tests, passes and failures. This part is really up to you. You can output whatever you like on the terminal; the bottom line is that git halts and the push event does not happen anyway.

Line 18:
A zero exit code means we are all green. Git continues with the push event to your remote branch.

What next?

Make the pre-push file executable to make it function as a hook.

chmod a+x pre-push

Now proceed to make a small change to any of the files in the project and stage the changed file(s). You can tweak your code to trigger a failure just so we can see the hook in action immediately.

ga . && gc -m “some small change”
git push origin the-branch-you-are-on

If there is a failure you will see something like what’s below on your terminal:

What’s the benefit of this?

1. Say you forgot to run the test command before pushing, this saves you the embarrassment you might face when the build fails on github/bitbucket/gitlab.
2. Running the test manually all the time before pushing is a repetitive task, what do we do with repetitive tasks? I thought so too! Plus it increases your productivity.
3. You can do really cool stuff like computing the total time it took for the tests to run. That way, you can immediately know if the tests took too long to execute. It’s really all up to you.

Feel free to explore other use cases for the different git hooks. Git hooks is an interesting tool to automate your workflow. Share your thoughts via the comment section below.

Did you find this post useful? Kindly show some love and share.

--

--

florence
Backticks & Tildes

#Mum #AndelaAlumni #Backend Engineer #Mentor Book a 15 minutes session with me free of charge https://calendly.com/okosunuzflorence/15min