我想定制我的git提示符,在我将东西推送到远程repo之前,它会提醒我或为我运行检查。
例如,当我跑步时
git push
git应该询问用户
Did you run unit tests locally?
或者其他类似的东西,这样我就不会意外地推送未经单元测试的代码。
最佳答案
设置预推挂钩以防止推送,除非文件.testspassed
存在。例子:
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
设置prepare commit msg钩子以删除
.testspassed
(如果存在):cat > .git/hooks/prepare-commit-msg <<EOF
#!/bin/sh -e
rm -f .testspassed
exit 0
EOF
我使用
prepare-commit-msg
而不是pre-commit
,因为prepare-commit-msg
也在合并时运行。无论何时提交或合并,git都会删除.testspassed
文件,防止您推送。告诉git忽略
.testspassed
文件,这样它就不会出现在您的repo中:echo .testspassed >> .gitignore
git commit -m 'add .testspassed to .gitignore' .gitignore
最后,修改您的测试运行过程,以便在所有测试都通过时创建(“touch”)
.testspassed
。这取决于如何运行测试。