Make(file) githooks play nice with your Sitecore repository

Yo yo, Sitecorians πŸ™‚ I hope you are all good out there. Sitecore just released 10.3, this is so wonderful!! I’m a big fan of SXA(And headless), and guess what? In this release there will be more juicy stuff for us “SXA Headless” people πŸ˜‰ Check out Martin Miles’s blog post and be amazed at all the wonders that Sitecore offers – https://blog.martinmiles.net/post/what-is-new-in-sitecore-10-3.

This post will not be about 10.3, I will save it for next year πŸ˜‰

Before I start I want to give a huge shout out to Sitecore Support. You are the best, you have saved my bacon several times. You are quick responders and you are really quick in solving the issues. So again, a huge thank you for the great work you are doing – You are the best!

Today’s post will be a short one πŸ™‚ Not so long ago, Anton Tishchenko did a great post about pre-commit hooks – Validate Your Sitecore Serialization Using Pre-commit Git Hook. This is awesome!

To all you boring naysayers who don’t like pre-commit hooks. Please stop reading and leave this page at once, thank you πŸ˜‰

Ok, let’s continue.

As I said, the blog post is excellent and works great. But… We wanted to add the GitHooks to our repository, and that was indeed tricky. The hooks reside in the .git folder, meaning they will not be versioned 😦 :

So how do we do this? Another blog post came to our rescue – Two Ways to Share Git Hooks with Your Team

The solution is this. We will create a new folder in the root, .githooks. Here we will have a “copy” of Anton Tishchenko‘s great pre-commit file. This means it is now versioned πŸ™‚

So how do we get Git to use the hooks from our newly created .githooks folder?

Let’s follow the blog post πŸ™‚ There are two ways: core.hooksPath or symlinks. By using core.hooksPath, we are simply telling GIT to use the hooks that reside in the new folder .githooks:

$ git config core.hooksPath .githooks

But unfortunately the GIT version, I have, is 2.73. Meaning it will not work. So we have to go for the second approach – symlinks.

A symlink (also called a symbolic link) is a type of file in Linux that points to another file or a folder on your computer.

This means we will have a symlink(shortcut) between “the copy” of Anton Tishchenko‘s pre-commit file(in the .githooks folder) and “the real” pr-commit file(in the .git/hooks folder).

$ find .git/hooks -type l -exec rm {} \; && find .githooks -type f -exec ln -sf ../../{} .git/hooks/ \;

And by using Makefile we will glue it all together πŸ™‚ So what is Makefile?

In software developmentMake is a build automation tool that automatically builds executable programs and libraries from source code by reading files called Makefiles which specify how to derive the target program. 

https://en.wikipedia.org/wiki/Make_(software)

Makefile is like the good old Gulp script we used in the happy Helix days, or like the PowerShell scripts we use today when setting up Sitecore Docker environments. Makefile is a great tool. I highly recommend it πŸ™‚

So let’s install Makefile:

choco install make

Or install it in VS Code πŸ™‚
https://devblogs.microsoft.com/cppblog/now-announcing-makefile-support-in-visual-studio-code/

Next will be to create a “Makefile” in the root of our repo. Here we will add the symlink code πŸ™‚

say-hello:
	echo "This will always run first - Hello Sitecore"
sync-githooks:	
	find .git/hooks -type l -exec rm {} \;
	find .githooks -type f -exec ln -sf ../../{} .git/hooks/ \;

To call(execute the Makefile) we open our PowerShell prompt:

But unfortunately, I could not get the “make sync-githooks” to run in PowerShell.

Instead, we will open our GIT bash prompt since we are using VS code. It will be easy to make the switch πŸ™‚

Let’s rerun the command.

Success! We have now successfully “symlinked” the real(in the .git/hooks folder) to the copy(in the .githooks folder). This means they are directly linked forever.

And since they are linked, we can now make updates/changes in the versioned pre-commit file, which will automatically update the “real” pre-commit in the .git/hooks folder.

That’s all for now folks πŸ˜Š


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.