Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get git push to ask for confirmation or run some checks?

Tags:

git

git-push

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.

like image 809
Anand Sunderraman Avatar asked Sep 27 '22 23:09

Anand Sunderraman


1 Answers

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.

like image 153
rob mayoff Avatar answered Oct 05 '22 06:10

rob mayoff