Saturday, January 9, 2016

Git – All you need to know about hooks

Share it Please
Unlike other source code management systems, Git provides Hooks. Hooks are scripts that are called when certain events in the work flow of the source control system occur. These Hooks can be used in the work flow like before and after commit etc

Most of these Hooks are something like Pre and Post. So these hooks can be executed either before or after a certain event occurs. Most of these Hooks are executable Scripts. We need to make sure the scripts do the necessary executable permissions. The shebang (#!) in the starting should indicate which interpreter should run the script.

Hooks in Git are pretty simple. Git will execute the Hooks configured for an action. The Hook needs to be a executable for the hook to execute. In this article we will see how to configure these Hooks.

There are 2 types of Hooks,
Client Side Hooks – These hooks run the developer system. Developer can edit the local hooks to execute a action.
Server Side Hooks – These run the system where we are holding the repository. These are similar to the local hooks (Client Hooks) but they execute on a central repository, or a developer’s public repository.

The available Hooks in Git are located at .git/hooks location. We can see the below hooks available,


Client Hooks
The Client side Hooks are

pre-commit - The pre-commit script is executed every time you run git commit
prepare-commit-msg - The prepare-commit-msg hook is called after the pre-commit hook to populate the text editor with a commit message. We can use this to alert the commit message.
commit-msg - The commit-msg hook is much like the prepare-commit-msg hook, but it’s called after the user enters a commit message.
post-commit - The post-commit hook is called immediately after the commit-msg hook. Mostly used for the notication purpose.

post-checkout - The post-checkout hook works a lot like the post-commit hook, but it’s called whenever you successfully check out a reference with git checkout.
pre-rebase - The pre-rebase hook is called before git rebase changes anything, making it a good place to make sure something terrible isn’t about to happen.

Now lets see how to configure a basic example hook using the Shell Script. We will use the prepare-commit-msg hook for this example.

1) Change the name of the prepare-commit-msg.sample to prepare-commit-msg

2) Change the permissions on the file
chmod +x prepare-commit-msg

3) Now write a Sample Shell Script in the prepare-commit-msg as

echo "# Please include a useful commit message!" > $1

Now the above message will be displayed when we go for the commit. The message will be displayed before the commit happens.

4) Lets commit the code
[root@vx111a SampleTest]# git commit -m "hello Git Hook"
[master 68d357a] # Please include a useful commit message!
1 file changed, 1 insertion(+), 1 deletion(-)

Now from the above output we can see the Commit message that we have added in the Shell Script.

The important thing to remember is to add the Correct Shebang for the script we are writing. For Python we can add the “#!/usr/bin/python2.7” based on your environment.

Server Hooks

The Server Hooks are,
Pre -receive - The pre-receive hook is executed every time somebody uses git push to push commits to the repository
Update - The update hook is called after pre-receive, and it works much the same way
Post - receive - The post-receive hook gets called after a successful push operation, making it a good place to perform notifications.

More to Come on This, Happy Learning

No comments :

Post a Comment