Skip to content

Instantly share code, notes, and snippets.

@leafnode
Forked from ilyakatz/pre-commit
Created May 15, 2018 12:24
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 leafnode/60d24c49833d140a4ea7210cf5a123e2 to your computer and use it in GitHub Desktop.
Save leafnode/60d24c49833d140a4ea7210cf5a123e2 to your computer and use it in GitHub Desktop.
helpful precommit hooks
#!/bin/bash -l
# A git hook script to find and fix trailing whitespace
# in your commits. Bypass it with the --no-verify option
# to git-commit
#
.git/hooks/pre-commit-master-no-no
if [[ $? == 1 ]]
then
exit 1
fi
.git/hooks/pre-commit-debugger
.git/hooks/pre-commit-trailing-spaces
.git/hooks/pre-commit-images
.git/hooks/pre-commit-pair
#!/bin/sh
# http://mark-story.com/posts/view/using-git-commit-hooks-to-prevent-stupid-mistakes
if git-rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
for FILE in `git diff-index --check --name-status $against -- | cut -c3-` ; do
# Check if the file contains 'debugger'
if [ "grep 'debugger' $FILE" ]
then
echo $FILE ' contains debugger!'
exit 1
fi
done
#!/bin/bash -l
echo "Optimizing images"
# Find image files and optimize them
for FILE in `exec git diff --cached --name-status | grep ".png$" | awk '$1 != "R" { print $2 }'`; do
echo "Optimizing $FILE"
bundle exec smusher $FILE
git add "$FILE"
done
#!/bin/bash
#Hook to verify that comments are not done to the master branch accidentally
#Need to check the status and exit the hook if this script exits with a non-zero code
#.git/hooks/pre-commit-master-no-no
#if [[ $? == 1 ]]
#then
# exit 1
#fi
if [[ `git symbolic-ref HEAD` == "refs/heads/master" ]]
then
if [[ ! -f /tmp/master_commit ]]
then
echo "*************************"
echo "CANNOT COMMIT TO MASTER!"
echo "To override this behavior"
echo "touch /tmp/master_commit"
echo "*************************"
exit 1
else
echo "Removing /tmp/master_commit"
rm /tmp/master_commit
fi
fi
#!/bin/bash
echo `git about |grep "git user"`
#!/bin/bash
echo "Remove trailing spaces"
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
echo $against
# Find files with trailing whitespace
for FILE in `exec git diff-index --check $against -- | sed '/^[+-]/d' | (sed -r 's/:[0-9]+:.*//' > /dev/null 2>&1 || sed -E 's/:[0-9]+:.*//') | uniq` ; do
# Fix them!
echo $FILE
(sed -i 's/[[:space:]]*$//' "$FILE" > /dev/null 2>&1 || sed -i '' -E 's/[[:space:]]*$//' "$FILE")
git add "$FILE"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment