我想编写一个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发布于 2022-07-18 19:25:06
我可以用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 https://stackoverflow.com/questions/73027611
复制相似问题