Deploying Git Client-Side Hooks

January 24, 2017

First of all let’s define what Git hook is. Like a Git documentation explain Git hooks is a “way to fire off custom scripts when certain important actions occur”. There are two kinds of hooks: “client-side” and “server-side”. In this post we will talk about how we can deploy our client-side hooks in the simplest way possible.

 

When you think about client-side hooks there are two main issues to think about:

1) If a developer delete the repository and clone it again, how can he add the hooks again?

2) If a developer creates a new repository how can he add the hooks easily?

 

We must keep in mind that generally we want to maintain the hooks as a transparent process for the clients. So how can we achieve this? I personally divided this into two different cases:

1) Hooks that we want to apply for all the repositories in the company

2) Hooks that we want to apply for specific repository

 

Hooks that we want to apply for all the repositories in the company

In the first case what we want to achieve is to change the default content that Git add into the folder .git when it performs the clone or the init command. For this we need to understand how this data get there.

When Git clones a repository he uses a template stored in the client machine to build the repository (the folder .git) and then it download from the remote repository all the data and store it into the repository.

Therefore to change the default content of the .git folder we can do two different things:

1) Change the default template

2) Create a new template and says Git to use it by default

 

Option 1: Change the default template

1)  Go to the following path (for typical Git installations):

–> Windows: C:\Program Files\Git\mingw64\share\git-core

–> Linux: usr/share/git-core

1-1-1.png

 

2) The folder “templates” contains all the items that are added under .git folder

1-1-2.png

 

3) Edit the hooks stored in the hooks folder

1-1-3.png

 

4) Now when you create a new repository or when you clone a existent one, the hooks will be added automatically to the repository

1-1-4.png

 

Option 2: Create a new template and says Git to use it by default

1) Go to the following path:

–> Windows: C:\Program Files\Git\mingw64\share\git-core

–> Linux: usr/share/git-core

1-2-1.png

 

2) Copy the folder “templates” and give a name for your custom template

1-2-2.png

 

3) Edit your template adding your hooks

1-2-3.png

 

4) Run the following command to change the default template used by Git

–> $ git –global init.templateDir <TemplatePath>

1-2-4.png

 

Note: You can specify which template use when you clone/init a repository using [–template]

–> $ git init –template <TemplatePath>

1-3-1.png

Don’t matter which way you choice to use, you will need to perform the changes in each client machine. For this the best approach is to create a script to perform the changes automatically and then ask the clients to run it.

 

Hooks that we want to apply for specific repository

This case is a bit more complicated because it is not generic but there are some things that can be done to achieve it.

Option 1: Write a script that install the desired hooks and store it in the repository. Then run it each time that you clone the repository.

Option 2: Create a default template as above, but add conditions to the hooks to act differently according to the repository name.

Option 3: Create a template for each repository in clients machines and use the option [–template] when clone command is performed to use the desired template.

Note: If you are using Linux my favorite approach is the following:

1) Create a init script that install the desired hooks in the repository

2) Save the init script in the git repository

3) Create a bash function called git that check if you are running the command “git clone” and if so, run the git command and then the init script in the repository. Something like this:

2-1-1.png

 

Add comment
facebook linkedin twitter email

Leave a Reply