I am writing a pre-commit hook to run Python tests, and everything works wonderfully ... until I get to the merge conflict. In this merge conflict, some documentation files are introduced that have trailing spaces, and every time I try to commit, I get these messages:
<stdin>:28: trailing whitespace.
Ticket Link:
<stdin>:54: trailing whitespace.
Then visit `http:`
<stdin>:13528: trailing whitespace.
//most be provided for ALL resource updates, and
<stdin>:13531: trailing whitespace.
"deleted": false, //indicates if this action resulted in a resource delete;
warning: squelched 415 whitespace errors
warning: 420 lines add whitespace errors.
fatal: could not open '.git/MERGE_HEAD' for reading: No such file or directory
And then, when I open my editor to write a commit message, it is completely empty.
My pre-commit script:
#!/bin/sh
RED='\033[0;31m'
NC='\033[0m'
proper_pop() {
git reset --hard -q
git stash apply -q --index && git stash drop -q
}
exit_and_pop() {
proper_pop
if [ "$1" -ne 0 ]; then
echo "${RED}Your code failed the pre-commit hook! Please examine the output and fix your issues!${NC}"
fi
exit $1
}
run_and_bail() {
bash -c "$1";
ret=$?;
if [ "${ret}" -ne 0 ]; then
exit_and_pop "${ret}"
fi
}
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
old_stash=$(git rev-parse -q --verify refs/stash)
git stash -q --keep-index
new_stash=$(git rev-parse -q --verify refs/stash)
if [ "$old_stash" = "$new_stash" ]; then
echo "pre-commit script: No changes to test. Not running."
sleep 1
exit 0
fi
allownonascii=$(git config --bool hooks.allownonascii)
exec 1>&2
if [ "$allownonascii" != "true" ] &&
test $(git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
cat <<\EOF
Error: Attempt to add a non-ASCII file name.
This can cause problems if you want to work with people on other platforms.
To be portable it is advisable to rename the file.
If you know what you are doing you can disable this check using:
git config hooks.allownonascii true
EOF
exit_and_pop 1
fi
if ! [ -z "$(which git-pylint-commit-hook)" ]; then
run_and_bail "git-pylint-commit-hook"
fi
if ! [ -z "$(which pep8)" ]; then
run_and_bail "python hooks/pep8-hook-check.py"
fi
proper_pop
Through my debugging, I produced a minimal script down to reproduce this error as:
#!/bin/sh
RED='\033[0;31m'
NC='\033[0m'
proper_pop() {
git reset --hard -q
git stash apply -q --index && git stash drop -q
}
exit_and_pop() {
proper_pop
if [ "$1" -ne 0 ]; then
echo "${RED}Your code failed the pre-commit hook! Please examine the output and fix your issues!${NC}"
fi
exit $1
}
run_and_bail() {
bash -c "$1";
ret=$?;
if [ "${ret}" -ne 0 ]; then
exit_and_pop "${ret}"
fi
}
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
old_stash=$(git rev-parse -q --verify refs/stash)
git stash -q --keep-index
new_stash=$(git rev-parse -q --verify refs/stash)
if [ "$old_stash" = "$new_stash" ]; then
echo "pre-commit script: No changes to test. Not running."
sleep 1
exit 0
fi
proper_pop
, - : git reset --hard -q in proper_pop ( ). , git: 1.8.1.2, 2.5.0, . - , ?
stdout stderr /dev/null , , , ...
, , , , ( , , - ), , ( git reset, ).
EDIT:
, : https://github.com/hjc1710/so-git-hook-question, README.md, , , .
My git config ( , 1.8.x, , 2.5.x), . , .