I would like to customize my Git prompt in such a way that it reminds me or runs checks for me before I push something to a remote repo.
For instance, when I run
git push
Git should ask the user
Did you run unit tests locally?
or something to that effect, so that I don't accidentally push code that is not unit-tested.
Set up your pre-push hook to prevent pushing unless the file .testspassed
exists. Example:
cat > .git/hooks/pre-push <<EOF
#!/bin/sh -e
if ! [ -f .testspassed ]; then
echo 1>&2 "push aborted because tests were not run or did not all pass"
exit 1
fi
exit 0
EOF
chmod +x .git/hooks/pre-push
Set up your prepare-commit-msg hook to remove .testspassed
if it exists:
cat > .git/hooks/prepare-commit-msg <<EOF
#!/bin/sh -e
rm -f .testspassed
exit 0
EOF
I'm using prepare-commit-msg
instead of pre-commit
because prepare-commit-msg
runs on merges too. Any time you commit or merge, git will remove the .testspassed
file, preventing you from pushing.
Tell git to ignore the .testspassed
file so it doesn't end up in your repo:
echo .testspassed >> .gitignore
git commit -m 'add .testspassed to .gitignore' .gitignore
Finally, modify your test-run process to create (“touch”) .testspassed
if all your tests pass. How you do this depends on how you run your tests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With