Using Makefile to Create Git Hook

technical
Author

Hiromi Suenaga

Published

February 6, 2022

In our previous post, we created a Git Hook that will strip all the superfluous metadata automatically when you commit notebooks. We wanted to make the installation of this hook easier by creating a Makefile target.

Here is what we came up with:

SHELL := /bin/bash

.PHONY: hook
hook: 
    @echo -e '#!/bin/sh\nfor file in $$(git diff --diff-filter=d --cached --name-only | grep -E '"'"'\.ipynb$$'"'"')\ndo\n\tjupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace "$$file"\n\tgit add "$$file"\ndone\n'  > .git/hooks/pre-commit
    @chmod +x .git/hooks/pre-commit

[Download]

Run make hook and Voila! You have your Git Hook that will reduce merge conflicts headaches.


My web searches while creating this Makefile

How to convert multiline file into a string with newline \n characters:

awk '$1=$1' ORS='\\n' filename

This is how I converted an existing hook file (pre-commit) into a single string. I had to add tabs (\t) manually, but this awk got me close enough.

awk '$1=$1' ORS='\\n' pre-commit

will return:

#!/bin/sh\nfor file in $(git diff --diff-filter=d --cached --name-only | grep -E '\.ipynb$')\ndo\njupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace "$file"\ngit add "$file"\ndone\n


How to write a multiline file with echo:

echo -e "Line 1\r\nLine2" >> readme.txt


How to escape single quotes in single quoted strings:

'"'"' is interpreted as '


How to escape dollar signs $ in Makefile:

Escape $ by adding another $ (i.e. $$)


References