git hooks
2020 30 Dec

Git Hooks ~ “Git” -ing code the better way.

With teams growing in size, it’s more important than ever to define a paradigm of deploying code to achieve a synonymous rule about the code and to keep contributors on the same page. This can be achieved with “Git Hooks”.

Git hooks are scripts which trigger specific automated actions based on an event performed in a git repository. The git hook name usually indicates the hook’s trigger (e.g. a pre-commit, a post-commit, etc). These hooks can be useful in automating tasks on your git workflow. For instance, based on defined rules, it can help us validate the code or run a specific set of tests before a code is committed. 

Setting the Git Hooks

Git hooks, thankfully, are a built-in feature. Which means we can access them and use them with our modifications as long as we have a git repository initialized. For better understanding, let us set up one.

Create a directory and cd into it:

gh 1 img

Initialize the repository with git and check the contents:

gh 2 img

On doing so, you would notice a hidden directory has just been created. This .git repo is used from git and is responsible for storing all the information related to the repository we created such as commit info, remote repository addresses, version control hashes, etc. Also, it is this folder where the hooks actually reside. If we cd into this folder we would be able to see certain scripts that are already present in the repository.

gh 3 img

One thing to notice here is that these hooks have an extension “.sample”. This means that they are not executable yet and not ready to be used as of this instance.

Let’s Cook inside the Hook!

Now that we know what a Git Hook is, let us take a look at how we can bring it to use, through an example. While we are inside the .git/hooks repository, we can see that there are many hooks present but not yet ready to be used. Let us use the pre-commit hook. But let us create one from scratch, instead of using the one that is provided.

Our objective here would be to validate the git config's global user email and the name respectively. Once all seems to be fine then it would show the user a message “Changes are ready to be committed” when a file is committed to git and another message showing “Changes have been committed” once the user commits the file to git.

Step 1

cd into the .git/hooks repository and create a new file called pre-commit.

gh 4

Step 2

The next step is to make the pre-commit file that we created, executable. Once inside the directory .git/hooks, make the file executable by typing in the command chmod +x pre-commit.

gh 5 img

Step 3

Now we need to create our script. In order for the script to run, we first need to specify our shell. Do this by using #!/bin/bash at the beginning of your script for bash or #!/bin/zsh if using the zsh shell like I am. Specifying an incorrect script would result in an exit code 0 and will be considered a fail.

This pre-commit hook will watch for incorrect commit authors using the script below.

gh 6 img

We will repeat the same steps for our post-commit hook.

  1. Create a post-commit hook.
  2. Make it executable.
  3. Insert your code in the post-commit hook.

If everything works fine then you should be able to see something like this below, when the correct credentials exist.

gh 7 img

In the above example, we have written our scripts in zsh but we can do the same with any other scripting language as well like Python, Javascript, Node.js, etc. The only thing to keep in mind is that we use the correct format at the first line of our executable script.

Latest Blogs

upgrade site to latest drupal 9 version

Drupal 9 Key Features & Why Should You Upgrade Your Site

Drupal 9 with all its new key features was launched on June 3, 2020. This new upgraded version made enterprises upgrade their systems to it later to acquire complete functionality.

Read More

iso_27001

Why and How Innoraft became an ISO 27001 certified organization

ISO 27001 is basically- “ISO/IEC 27001 – Information technology — Security techniques — Information security management systems — Requirements.”

Read More

iso_9001

Why and How Innoraft became an ISO 9001 certified organization

What is ISO 9001 certification?

The ISO certification determines that an organization meets a set of prescribed standards that will help them meeting customer and other stakeholder needs

Read More

php8-part-3

PHP 8 new features and changes - Part 3

This is part 3 of the blog. Here we will continue our discussions on the other features and changes in PHP 8.

Read More