Skip to content

Instantly share code, notes, and snippets.

@sfgarza
Last active February 13, 2018 22:12
Show Gist options
  • Save sfgarza/32258b7332a715de4e3948892ba415d3 to your computer and use it in GitHub Desktop.
Save sfgarza/32258b7332a715de4e3948892ba415d3 to your computer and use it in GitHub Desktop.
Pre commit hook for ensuring files get built before committing.
#!/usr/bin/env bash
#
# Commands to be run before a commit can be made to the repository:
#
# Make sure gulp build always runs before commiting.
# Constants for terminal colors
GREEN='\033[00;32m'
YELLOW='\033[00;93m'
RED='\033[00;91m'
END='\033[0m'
# Grab unstaged files before running gulp build.
git diff --name-only --diff-filter=ACMRTUXB > /tmp/unstaged1
# Lets get minifying.
echo "Running gulp build..."
gulp build --staged
if [ $? != 0 ]
then
# Abort commit if something went wrong.
exit 1
fi
# Grab unstaged files after running gulp build.
git diff --name-only --diff-filter=ACMRTUXB > /tmp/unstaged2
# Check if any new minified files got created. If so...
if [ $(comm -3 /tmp/unstaged1 /tmp/unstaged2 | LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 ]
then
# Prompt user to add new files and abort commit.
echo -e ""
echo -ne "${RED}Please commit newly minified files${END}:"
echo -e ""
comm -3 /tmp/unstaged1 /tmp/unstaged2
echo -e ""
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment