Using githooks to take a picture of yourself on commit

Will Mitchell
Sep 1, 2018 · 4 min read

I’m at the end of a short vacation, waiting for an airplane, so lets have a little fun with technology, shall we?

In this short tutorial, I’ll show you how to use githooks to take a picture of yourself every time you commit. Useful? Not particularly, unless you want to watch a slideshow of your hair gradually getting a little longer. Nevertheless, this should be fun. This tutorial will use a capture library that is specific to the OSX ecosystem, however the general githook process will work for other operating systems as well.

First off, we’re going to use imagesnap to capture photos of ourselves via the command line. Go ahead and download it in terminal via homebrew.

brew install imagesnap

Imagesnap is a fun open source project that makes it easy to take a photo from the command line. Once you’ve downloaded the software, test it out like so:

imagesnap -w 1 ~/snapshot.png

That -w 1flag is telling imagesnap to “warm-up” for 1 second before taking a picture, which will be saved as snapshot.png in the root directory. Once you run the command, you should see a new .png file in your root directory. Pretty cool huh?

Now, before we go diving into how to run this command everytime a githook fires, let’s first discuss what a githook is. Githooks are script files that execute whenever you take certain actions with git. There are 21 of them all together. These files are user defined, and will run as long as you have an executable file with the correct name in the .git/hooks/ directory of the project you’re working on.

This is an important thing to note; githooks are project specific, so they won’t run globally when you use git. They only run if they’re defined in the .git/hooks/ directory for that project.

With all that in mind, lets create a new example project to work with:

mdkir git-photographer && cd git-photographer
git init

This creates a workspace for us, and initializes a git repository in that workspace. If you’re new to git, you may not even be aware that the .git directory exists in your projects. That’s due to the fact that it’s a *hidden* directory, which starts with a dot. However, we can list directories like these in the terminal easily:

ls -A

cd into the .git directory, and let’s explore a little bit. Here you’ll find a number of different files and directories that make up the git repository.

cd again into the hooks directory. Listing the files in this directory, you should see something like this:

applypatch-msg.sample
commit-msg.sample
post-update.sample
pre-applypatch.sample
pre-commit.sample
pre-push.sample
pre-rebase.sample
pre-receive.sample
prepare-commit-msg.sample
update.sample

These are all githook files, however they won’t run because right now they all end in .sample. We could edit one of these to get our desired behavior, however let’s use a githook that isn’t listed here post-commit. post-commit will run, as its name suggests, after git commit is run. That sounds like what we need. Create a new file, post-commit, and then adjust its permissions, so that it can be executed.

touch post-commit
chmod 755 post-commit

Open up our new file in your text editor of choice. On the first line, we need to specify which interpreter language should be used to run this executable. We’re going to use bash, since our imagesnap program runs via bash, but you could use any language you like. Add the following shebang to the first line of your script:

#!/bin/sh

Now we can go ahead and write our bash script. If you’re fuzzy on how to write bash, you’re not alone. I like to have a cheat sheet handy anytime I’m working with a language or framework I don’t use regularly.

First, lets get a timestamp in seconds, so we can make sure all our picture files have a unique name:

TIMESTAMP=$(date +%s)

Next we’ll construct the file path where we’d like to put these pictures:

FILE=”$HOME/gitSnap_$TIMESTAMP.png”

Notice, that file path uses the environment variable $HOME, which should be your root directory. Thus, all of these photos are going to end up in our root directory. You may want to put them elsewhere, so adjust the file path to your preference. Just make sure any directories you use actually exist, otherwise it won’t work.

Finally, let’s add the call to the imagesnap program, using our filename:

imagesnap -w 1 $FILE

That’s it! Save the file, and now anytime we commit in this project, our machine should take a picture of us! Navigate to your project directory, and do the following:

touch logger.js
git add logger.js
git commit -m ‘first commit’

If you’ve followed all the steps, you should see a new .png file of your smiling face in your root directory. Sweet!

I hope you found that all fun and instructive. If it’s not working for you, feel free to reach out, and I’ll try to help you as best I can. Githooks are a useful tool to have in your workflow, enabling you to take any number of automated actions while utilizing git. Does your team use them for something specific? Post it in the comments below!

Happy hacking!

Will Mitchell

Written by

Instructor at the Turing School of Software and Design wvmitchell.net

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade