Writing Git Hooks using Javascript

Javascript only 😍

Hey everyone welcome back to git series if you have not seen my previous post got through this link and comeback. lets start I hope in our developer lifecycle we all have encountered incorrect commit messages or not according to our company policy. So today I am going to show you how we can avoid those mistakes lets begin.

What are hooks🤔

Git hooks are scripts that Git executes before or after events such as: commit, push, and pull. Git hooks are a built-in feature — no need to download anything. Git hooks are run locally. without wasting time lets dig deep dive into hooks.

Lets create empty Repository and initialize with git if you don’t know how to initialize go back to my previous post and come back and start again now lets initialize .

As you are seeing above I created empty folder named “hooksdemo” and initialized with git. if we see inside “.git” directory. It contains subdirectory named “hooks” . now if we expand the folder you can see many files. It looks like as below.

I hope now you have question in your mind. what are they? where they actually come from? what they do? why they are here? Answer for all your question. These are the predefined hooks generated by git when you are initialized directory with git and these hooks come from when you have installed git software in you pc. by default these hooks are also created and stored in your user workspace. Okay lets verify where they actually present.

As you are seeing above they are came from above location. By default they are suffixed with “.sample” so now these hooks are dumb. To make them work we need remove the “.sample” suffix. As you seen above some of the hooks prefixed with “pre” and some of them with “post” this because hooks prefixed with “pre” runs before particular event to be happened and another vice versa. Above hooks are written in “perl” language. You can use your favourite language ex:-java , javascript, c# ,perl , ruby bla bla bla.I love javascript so iam using “Nodejs” for writing hooks. Now i am going to write hook that will run before commit message .Lets make our hand dirty by writing some code.

Writing Git Hooks Using javascript😎

We are going to write hook that will check the commit message that statisfy our policy (In my case commit message length should be greater than 10) so lets start.

As you are seeing above “commit-msg” we are going to activate by default it contains some scripts written in shell we don’t care about that as of now we delete this file create another empty file with same name .So lets do it first.

#!/usr/bin/env/node

On the top of the file we need to add the above specified line. The above line tells the operating system to use nodejs to execute this file. lets write our business logic using javascript. I use nodejs process module to get command inputs inside hooks if you dont know what is process module in nodejs kindly go through this link and understand. Now lets get the user inputs and console it as of now.

In the above snippet we are getting the arguments passed in commit message and exiting with code ‘1’ .By default any shell script exit with code ‘1’ treated as failure and exit with code ‘0’ treated as success. For testing as now I am using ‘1’ later we can use it on condition based .now lets run git command and see what happens.

As you are seeing array passed to the hook from our command prompt which contains node execution path and index ‘0’ and our hook file path at index ‘1’ and another path to the file at ‘2’ . The Index ‘0’ and ‘1’ we are not interested .Lets read the file “.git/COMMIT_EDITMSG” which we have got at index ‘2’.

As you see it contains our commit message so we want that message in our hook to perform business validation.

Now lets read that file using nodejs filesystem module and get the commit message and perform validation without wasting time lets start doing it.

As in the code above we are checking the commit message length and performing validation. Now we are done with the hook time to test it.

Now our hook is working as expected this is all about git hooks.

Conclusion

In this post we have talked about git hooks and how they work under the hood . I hope you enjoyed it . In the upcoming post I will share different event capturing using hooks till then Bye bye.

Abhishek is a Software developer who is passionate about learning technology related stuff and implementing it in real world.