我想写一个git钩子,这样如果我们的.env
文件没有加密,提交就会失败。但是,git status
总是以代码0退出。当文件没有提交时,我怎么能让这个命令以错误码退出呢?
# file encrypted
git-crypt status .env && echo "exit 0" || echo "exit 1"
# encrypted: .env
# exit 0
# file not encrypted
git-crypt status package.json && echo "exit 0" || echo "exit 1"
# not encrypted: package.json
# exit 0
我可以使用grep。
# exit 1 if "not encrypted" found
git-crypt status .env |
grep "not encrypted" &&
>&2 echo ".env not encrypted, can't commit" &&
exit 1
# exit normally
exit 0
也适用于多个文件
git-crypt status .env package.json |
grep "not encrypted" &&
>&2 echo "files not encrypted, can't commit"
# not encrypted: package.json
# files not encrypted, can't commit