Skip to content

Instantly share code, notes, and snippets.

@tclancy
Created October 7, 2016 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tclancy/b9da01d85d420a20944964c2d7ebce9e to your computer and use it in GitHub Desktop.
Save tclancy/b9da01d85d420a20944964c2d7ebce9e to your computer and use it in GitHub Desktop.
git pre-commit hook for Python linting, image CRUSHING and a bit of JavaScript hygiene
#!/usr/bin/env bash
rc=0
head=`git symbolic-ref HEAD`
#if echo $head | grep -E 'releases?/[0-9]\.[0-9]+|master'
#then
# echo "You cannot commit directly to a release branch"
# echo "Stash your changes and apply them to another branch"
# echo "or create a branch directly from these changes"
# rc=1
#fi
needs_love=0
for file in $(git diff-index --name-only --diff-filter=ACM HEAD -- | grep '\.py$' | grep -Ev 'migrations'); do
echo ${file}
pep8 ${file} --ignore=W293 --max-line-length=165
if [ $? -ne 0 ] ; then
rc=1
fi
pylint ${file} --rcfile=deployment/pylint.rc --reports=no
if [ $? -ne 0 ] ; then
needs_love=$[$needs_love +1]
fi
done
for file in `git diff --cached --name-status|awk '$1 ~ /[AM]/ && tolower($2) ~ /\.png$/ {print $2}'`
do
echo "Crushing $file"
pngcrush -rem alla -brute -reduce $file ${file%.png}.new | grep "filesize"
mv -f ${file%.png}.new $file
git add $file
done
for file in `git diff --cached --name-status|awk '$1 ~ /[AM]/ && tolower($2) ~ /\.jpe?g$/ {print $2}'`
do
echo "Crushing $file"
jpegtran -copy none -progressive -optimize -outfile $file $file | grep "filesize"
git add $file
done
if [ $needs_love -ne 0 ]
then
printf "\n\\033[31;01mPlease try to address the issues above before pushing this code\\033[m\n"
fi
for file in $(git diff-index --name-only --diff-filter=ACM HEAD -- | grep -E "\.js$|\.css$|\.coffee|\.html$"); do
if grep -q 'sourceMappingURL' ${file}; then
printf "\n\\033[31;1m${file}\\033[m\n"
echo "- File references a source map, please remove reference and commit again."
rc=1
fi
if grep -q 'console\.log' ${file}; then
printf "\n\\033[31;1m${file}\\033[m\n"
echo "- File contains a console.log function call. Please remove the call and commit again."
rc=1
fi
done
if [[ $rc == 1 ]]
then
printf "\n\\033[31;01mGIT COMMIT FAILED\\033[m\n"
fi
exit $rc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment