DEV Community

Cover image for An automatic interactive pre-commit checklist, in the style of infomercials
Victoria Drake
Victoria Drake

Posted on

An automatic interactive pre-commit checklist, in the style of infomercials

What's that, you say? You've become tired of regular old boring paper checklists? Well, my friend, today is your lucky day! You, yes, you, can become the proud owner of a brand-spanking-new automatic interactive pre-commit hook checklist! You're gonna love this! Your life will be so much easier! Just wait until your friends see you.

What's a pre-commit hook?

Did you know that nearly 1 out of 5 coders are too embarrassed to ask this question? Don't worry, it's perfectly normal. In the next 60 seconds we'll tell you all you need to know to pre-commit with confidence.

A Git hook is a feature of Git that triggers custom scripts at useful moments. They can be used for all kinds of reasons to help you automate your work, and best of all, you already have them! In every repository that you initialize with git init, you'll have a set of example scripts living in .git/hooks. They all end with .sample and activating them is as easy as renaming the file to remove the .sample part.

Git hooks are not copied when a repository is cloned, so you can make them as personal as you like.

The useful moment in particular that we'll talk about today is the pre-commit. This hook is run after you do git commit, and before you write a commit message. Exiting this hook with a non-zero status will abort the commit, which makes it extremely useful for last-minute quality checks. Or, a bit of fun. Why not both!

How do I get a pre-commit checklist?

I only want the best for my family and my commits, and that's why I choose an interactive pre-commit checklist. Not only is it fun to use, it helps to keep my projects safe from unexpected off-spec mistakes!

It's so easy! I just write a bash script that can read user input, and plop it into .git/hooks as a file named pre-commit. Then I do chmod +x .git/hooks/pre-commit to make it executable, and I'm done!

Oh look, here comes an example bash script now!

#!/bin/sh

echo "Would you like to play a game?"

# Read user input, assign stdin to keyboard
exec < /dev/tty

while read -p "Have you double checked that only relevant files were added? (Y/n) " yn; do
    case $yn in
        [Yy] ) break;;
        [Nn] ) echo "Please ensure the right files were added!"; exit 1;;
        * ) echo "Please answer y (yes) or n (no):" && continue;
    esac
done
while read -p "Has the documentation been updated? (Y/n) " yn; do
    case $yn in
        [Yy] ) break;;
        [Nn] ) echo "Please add or update the docs!"; exit 1;;
        * ) echo "Please answer y (yes) or n (no):" && continue;
    esac
done
while read -p "Do you know which issue or PR numbers to reference? (Y/n) " yn; do
    case $yn in
        [Yy] ) break;;
        [Nn] ) echo "Better go check those tracking numbers!"; exit 1;;
        * ) echo "Please answer y (yes) or n (no):" && continue;
    esac
done

exec <&-
Enter fullscreen mode Exit fullscreen mode

Take my money!

Don't delay! Take advantage right now of this generous one-time offer! An interactive pre-commit hook checklist can be yours, today, for the low, low price of... free? Wait, who wrote this script?

Latest comments (31)

Collapse
 
bootcode profile image
Robin Palotai

Nice! See also pre-commit.com/. I wonder if such manual y/n questions can be integrated into that?

Collapse
 
cmmata profile image
Carles Mata

Shut up and take my money! I mean.. Great post! I think I'll use that!

Collapse
 
victoria profile image
Victoria Drake

:D Thanks Carles!

Collapse
 
offendingcommit profile image
Jonathan Irvin

I'm a big fan of Git and love writing about it. Hooks are one of those hidden gems that people rarely use. Thanks for enlightening us all!

Git is like an assembly line and as you write code and prepare to commit, there's a lot you can do along the process. This is DevOps 101. Asking questions precommit is a great way to keep yourself honest, but it's probably hard to enforce as a team. I would add using a solid PR template that asks these questions.

One thing this template may throttle is small commits. Forgive my strong opinions about Git, but I believe in small, agile, and transactional commits. This script might hinder that.

However, for someone starting out, it's a solid example to make sure you have your bases covered.

I would love to read more about using this method along the software development pipeline and employing checks and balances along the way to ensure solid code.

Collapse
 
derrxb profile image
Derrick Bol

Oh my gosh!! This is amazing! I can definitely see myself using this

Collapse
 
allanlrh profile image
AllanLRH

Haha, I could really use something like this, though I expect that it won't work well with the "commit often commit early" philosophy... and it could trip up GUI clients for those who use such abominations (ok, I also use them once in a while).

Collapse
 
wheelstep profile image
Stefano Pernat

Awesome!!!

Thanks for sharing this will be so useful

Collapse
 
berkmann18 profile image
Maximilian Berkmann • Edited

This combined with automated testing systems would be amazing.

Collapse
 
vlasales profile image
Vlastimil Pospichal

The problem with these three questions is that the developer learns to snap - without reading. Questions must include some relevant information, otherwise they are useless.

Collapse
 
renich profile image
Renich Bon Ciric

You definitely made me smile!

Collapse
 
victoria profile image
Victoria Drake

:D

Collapse
 
arximughal profile image
Muhammad Arslan Aslam

I love it ♥️ ♥️ ♥️ You are a 🦄