Make git-crypt exit with error code target file unencrypted

70 Views Asked by At

I'd like to write a git hook such that commit will fail if our .env file is not encrypted. However, git status always exits with code 0. How can I have this command exit with an error code when the the file is not committed.

# 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
1

There are 1 best solutions below

0
On

I was able to do this using 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

Also works on multiple files.

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