~/codewithstu

Use Git Hooks To Automate Your Workflow

Transcript

Hi, my name is Stu and if you're new here, this channel is all about me breaking down different technologies into easily digestible chunks. In this video we're going to be taking a look at how to extend the Git version control system by using client-side hooks.

Git is a really powerful version control system that allows you to fire off custom scripts whenever certain actions occur. Generally speaking, these are grouped into two types of hooks. The first is client-side, which always runs on the client machine and requires a bit of manual setup. The other is server-side hooks. Client-side hooks are triggered by operations such as committing and merging, while server-side hooks run on network operations such as receiving push commands. In this video we're only going to be taking a look at client-side hooks, as typically server-side hooks are not supported by third-party providers such as GitHub.

So to get started with Git hooks, we first need a Git repository to work with. So I'm going to open my terminal window and just initialize that now quickly. As you can see on the left hand side, I now have my .git folder. And if I expand this, you can see that we have a hooks folder. Inside of this hooks folder is a load of samples of all the different types of hooks that you could want to create.

In my opinion, there's three different hooks that we could use to extend our developer workflow. The first is the pre-commit hook. This hook is run before you even type a commit message. It's used to inspect the snapshot that's about to be committed to see if you've forgotten something, to make sure tests run, or to examine what you need to inspect in the code. The next hook is the prepare-commit-msg hook. This is run before the commit message editor is fired up but after the default message is created. This allows you to edit the default message before the commit author sees it. The last hook is the commit-msg hook. This hook is most useful for validating a commit message, for example making sure a Jira issue number is contained inside of the message.

The easiest way to get started with Git hooks is to take one of the samples and rename it to take away the .sample at the end. This commit hook is now live. Inside of the script we can put in whatever we want to do depending on the type of hook that we're trying to extend.

So now let's see this in action with a script that I created earlier. In this script, all that we're going to do is check that there is a Jira issue number-like format inside of the commit message. Git saves the commit message to a file on disk and that path to that file is passed in as the first parameter. So you can see on line four we are extracting the message from that file that gets passed in to us. And from there we have our regex pattern to make sure that the Jira issue number is supplied. We do an if statement to check that the commit message passes the

regex. If it doesn't, then we give an error to the user and we exit with code 1. If we exit with anything other than zero, then this will cause the Git commit process to stop.

Because what we're creating is a client-side hook, these hooks are not automatically transported to the server. So we need to create a different folder inside of our Git repository to make sure that others can use this as well. To do this, let's create a new folder on the root of our repository called .githooks. Then let's take our commit-msg hook and copy and paste it into that folder.

Now if we go to our source control view, you'll see that we have a single file in the .githooks folder. So you can see here, now if I attempt to add in something like "my new Git hook" and try to save it, you can see that we get a message saying "please add an issue number to the commit message". So now I'm going to do that and say ISS-123, as if it was a Jira issue. Save, and now it's committed.

And that's pretty much it. You've just written your first Git hook. The script that I've given you here today is an example of how you can add a Jira issue number to a commit message using client-side validation. You can also do things like grabbing issue numbers from the branch name, and I'll give you that example in the repository which I'll link in the description below.

With Git hooks, you're really only limited by what it is that you want to do and what you can write into a bash script. In the next video, we're going to take a look at how we can use GitHub Actions to do the same sort of validation but on the server side.

If you enjoyed this video, consider subscribing to the YouTube channel for more content like this.

// share_this