Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gwokae
Last active August 29, 2015 14:12
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 gwokae/1e0f9ba24bda731971ea to your computer and use it in GitHub Desktop.
Save gwokae/1e0f9ba24bda731971ea to your computer and use it in GitHub Desktop.

install

cd $(git rev-parse --show-toplevel)/.git/hooks/
wget https://gist.githubusercontent.com/gwokae/1e0f9ba24bda731971ea/raw/1a20f57102cc92d12be953c0c6cd156609b11d65/pre-commit
chmod a+x pre-commit

install all

TMP_FILE=/tmp/pre-commit 
curl -o $TMP_FILE https://gist.githubusercontent.com/gwokae/1e0f9ba24bda731971ea/raw/1a20f57102cc92d12be953c0c6cd156609b11d65/pre-commit
chmod a+x $TMP_FILE
for dir in $(find . -type d -name .git); do 
  cp $TMP_FILE $dir/hooks/;
done

#!/bin/bash
TOP_DIR=$(git rev-parse --show-toplevel)
HOOK_DIR=$TOP_DIR/.git/hooks/
ABORT_FILE=${HOOK_DIR}pre-commit-abort-texts
if [ ! -a $ABORT_FILE ]; then
touch $ABORT_FILE
fi
echo -e "### \e[35m This is prevent BAD COMMIT pre-commit hook \e[0m ###"
echo -e "You can add texts to prevent falsy commit at \e[33m $ABORT_FILE \e[0m . It will match git diff line by line"
if [ -n "$(cat $ABORT_FILE)" ]; then
echo -n "Scanning "
while read line || [ -n "$line" ]; do
echo -n .
RES=$(git diff --cached -a | grep "+" | grep "$line")
if [ -n "$RES" ]; then
echo -e "\nFOUND: \e[31m$line \e[0m @ \e[32m$(git diff --cached -a -S "$line" --name-only) \e[0m"
fi
done < $ABORT_FILE
if [ -n "$RES" ]; then
echo -e "\n\nSome aborting text found! Please try to fix that before you comit."
exit 1
fi
echo " OK!"
fi
exit 0 #no problem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment