【问题标题】:Make git-crypt exit with error code target file unecrypted使用未加密的错误代码目标文件使 git-crypt 退出
【发布时间】:2022-07-19 03:25:06
【问题描述】:

我想写一个 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

【问题讨论】:

    标签: git githooks git-crypt


    【解决方案1】:

    我可以使用 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    
    

    【讨论】: