DEV Community


Posted on

A simple pre-commit hook that saves you big time!

Irrespective of how thoroughly the code is tested, working in an agile startup demands you to do some last-minute changes many a time.

Most of the times, everything goes well.

Sometimes, unfortunately, you get an error after the staging/testing deployment and the server wouldn't start. It could be as simple as missing an end. Yes, syntax error is what we are talking about.

This is an embarrassment particularly when a lot of resources are working on a single branch and you get a slack alert from people saying, Your code has a syntax error, please fix!

To save me from these, as a practice before committing I always run a simple

ruby -c 'file_path'

that displays all the syntax errors in the file. This saves me from the embarrassment. But it is a gruelling activity especially when there are a lot of files to commit.

This should be automated, right?

Yes! Git Hooks
If you find the terminology new, read about it here

Let's talk about the pre-commit hook!

What's a pre-commit hook?

The pre-commit hook is run first before you even type in 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 whatever you need to inspect in the code. Exiting non-zero from this hook aborts the commit

Yes, this is tailor-made for our use-case.

I wanted to create a pre-commit hook which would pick all the staged files and run the syntax check and halt the commit if there are errors.

It is so simple that I managed to create it in less than an hour. Never should I worry about my syntax errors anymore forever. You can see it in action here. 😎

Pre-commit halting the commit

Ah, embarrassment prevented! Here the pre-commit halts the commit because it found syntax issues in the staged files.

When all the files are good to go syntactically, you wouldn't even notice the hook.
Pre-commit success
(Using terminal to show the success case, as Sourcetree closes the popup quickly)

That's all I needed and I wonder why didn't I do it before.

If you are in love with the hook already, start writing one straight away. If you need any inspiration to write one, have a look at my hook here. You can customise the hook however you want.

Further, I am planning to do more with this pre-commit hook

  • Implement the same for go, YAML files
  • Run rubocop/lint tools to check for specific offences

Top comments (8)

swarupkm profile image
Swarup Kumar Mahapatra

Smaller commits, and unit testing the code will not let syntax errors at the first place. And hence you won't need that pre-commit.

rahul_ramfort profile image

Agreed. That's the right way to do.

But I don't see any harm in having this either. Particularly for the "last-minute changes". Human errors are always possible, so why not put some checks.

To err is human; to automate, divine

nombrekeff profile image

We use pre-commit hook at work to run tests before committing and we also run tests in GitHub actions on any PR to prevent human error. It's been working great for now.

The hook could also be used to lint commit messages if the company or project uses some automated changelog generator or some committing standard.

Thread Thread
rahul_ramfort profile image

Thanks for adding this mate. I will make this hook, lint commit messages as well.

camilovietnam profile image

I have a pre-commit hook that aborts the commit if i'm still on the dev or stg branches. Before writing it, I kept forgetting that I needed to create a new branch before writing the code.

rahul_ramfort profile image

That would be a nice catch too. Adding this to the to-do list.

blijblijblij profile image
Rogier Wessel

this might interest you, from the evilmartians guys, great project

rahul_ramfort profile image

Had a quick look, looks really interesting. I will try it out.

Thanks for the suggestion.