Open tarekbecker opened 9 years ago
Works on mac. Please test on windows.
put in file ./.git/hooks/pre-commit
execute chmod +x ./.git/hooks/pre-commit
test to commit with a failing test
#!/bin/sh
# this hook is in SCM so that it can be shared
# to install it, create a symbolic link in the projects .git/hooks folder
#
# i.e. - from the .git/hooks directory, run
# $ ln -s ../../git-hooks/pre-commit.sh pre-commit
#
# to skip the tests, run with the --no-verify argument
# i.e. - $ 'git commit --no-verify'
# stash any unstaged changes
git stash -q --keep-index
# run the tests with the gradle wrapper
./gradlew test
# store the last exit code in a variable
RESULT=$?
# unstash the unstashed changes
git stash pop -q
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
# Find files with trailing whitespace
# Doubled commands are _linux_version_ || _mac_osx_version_
for FILE in $(
git diff-index --check --cached $against -- | \
sed '/^[+-]/d' | \
( sed -r 's/:[0-9]+:.*//' 2>/dev/null || \
sed -E 's/:[0-9]+:.*//' ) | \
uniq
); do
# Fix them!
( sed -i 's/[[:space:]]*$//' "$FILE" > /dev/null 2>&1 || \
sed -i '' -E 's/[[:space:]]*$//' "$FILE" )
echo "Removed trailing whitespace from $FILE"
git add "$FILE"
done
exit $RESULT
works on windows too. instruction: execute chmod +x ./.git/hooks/pre-commit
is not applicable for windows, it only changes the modifier for the file.
prevent of pushing to master
create a pre-push
file in the hook folder
#!/bin/bash
protected_branch='master'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_branch = $current_branch ]
then
exit 1 # push will not execute
else
exit 0 # push will execute
fi
Git Hooks