Skip to content

Instantly share code, notes, and snippets.

@jlav1n
Created November 30, 2017 22:20
Show Gist options
  • Save jlav1n/0665e39f705e48ba8c91ccf5cd898143 to your computer and use it in GitHub Desktop.
Save jlav1n/0665e39f705e48ba8c91ccf5cd898143 to your computer and use it in GitHub Desktop.
pre-commit Git hook
#!/bin/sh
#
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Redirect output to stderr.
exec 1>&2
# if committing files matching '300x300', but not also files matching
# '110x110', then they did not run the image script.
if
test $(git diff --cached --name-only --diff-filter=ACMR $against | grep -c '300x300') != 0 &&
test $(git diff --cached --name-only $against | grep -c '110x110') = 0
then
cat <<\EOF
You are adding "master" images (300x300).
EOF
# just the unique prefix (first part of filename before the hyphen):
for i in `git diff --cached --name-only --diff-filter=ACMR $against | grep '300x300' | awk '{ split($1,fld,/300x300\//); split(fld[2],pre,/-/); print pre[1] }' | uniq`
do
echo "Running image script on prefix: $i..." &&
bin/thumb_images_file.pl -limit_prefix=$i -overwrite > /dev/null 2>/dev/null &&
bin/thumb_images_file.pl -limit_prefix=$i -overwrite -keylines > /dev/null 2>/dev/null
done
cat <<\EOF
*** Please add the newly-created files, and re-commit.
EOF
exit 1
fi
# if committing files in /js or /css directories, but not also files in
# /dist, then need to run minify script.
if
test $(git diff --cached --name-only --diff-filter=ACMR $against | grep -cE '/(css|js)/') != 0 &&
test $(git diff --cached --name-only $against | grep -c '/dist/') = 0
then
cat <<\EOF
You are modifying CSS and/or JS files. We are running the minify script...
EOF
bin/minify &&
git add htdocs/dist/ &&
cat <<\EOF
*** Please now run your commit again.
EOF
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment