From 5e13941a542229d13dcac6860592346330fd160a Mon Sep 17 00:00:00 2001 From: Michael Kesper Date: Thu, 2 Sep 2021 16:49:23 +0200 Subject: [PATCH] Fix shellcheck findings Also update usage as this differs from the gist now. Signed-off-by: Michael Kesper Change-Id: If49248eff746811a882565ae7196cec336c80f0a --- README.md | 2 +- pre-commit-hooks/git-crypt-check | 31 ++++++++++++++----------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 593bc38..2003bfc 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Checks that are not yet released elsewhere: Add to your `.pre-commit-config.yaml`: - - repo: https://review.sysfive.com/tools/pre-commit-hooks + - repo: https://github.com/sysfivecom/pre-commit-hooks rev: 0.1.0 hooks: - id: git-crypt-check diff --git a/pre-commit-hooks/git-crypt-check b/pre-commit-hooks/git-crypt-check index abd9f1c..e43ca17 100755 --- a/pre-commit-hooks/git-crypt-check +++ b/pre-commit-hooks/git-crypt-check @@ -1,28 +1,24 @@ #!/bin/bash -# Time-stamp: ################################################################################ # Pre-commit hook to avoid accidentally adding unencrypted files with # [git-crypt](https://www.agwa.name/projects/git-crypt/) # Fix to [Issue #45](https://github.com/AGWA/git-crypt/issues/45) # # Usage: -# $ cd /path/to/repository -# $ git-crypt init -# $ curl -o .git/hooks/pre-commit -# $ chmod +x .git/hooks/pre-commit # -# Otherwise, you might want to add it as a git submodule, using: -# $ git submodule add https://gist.github.com/848c82daa63710b6c132bb42029b30ef.git config/hooks/pre-commit.git-crypt -# $ cd .git/hooks -# $ ln -s ../../config/hooks/pre-commit.git-crypt/pre-commit.git-crypt.sh pre-commit +# Add to your `.pre-commit-config.yaml`: +# +# - repo: https://github.com/sysfivecom/pre-commit-hooks +# rev: 0.1.0 +# hooks: +# - id: git-crypt-check # -# The latest version of this script can be found as a GIST on: -# https://gist.github.com/Falkor/848c82daa63710b6c132bb42029b30ef # ################################################################################ # MIT License # # Copyright (c) 2016-2021 S. Varrette +# Portions Copyright © 2021 sysfive.com GmbH # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -43,18 +39,19 @@ # SOFTWARE. ################################################################################ # -if [ -d .git-crypt ]; then +set -eu -o pipefail + +if [[ -d .git-crypt ]]; then STAGED_FILES=$(git diff --cached --name-status | awk '$1 != "D" { print $2 }' | xargs echo) - if [ -n "${STAGED_FILES}" ]; then - git-crypt status ${STAGED_FILES} &>/dev/null - if [[ $? -ne 0 ]]; then - git-crypt status -e ${STAGED_FILES} + if [[ -n "${STAGED_FILES}" ]]; then + git-crypt status "${STAGED_FILES}" &>/dev/null || { + git-crypt status -e "${STAGED_FILES}" echo '/!\ You should have first unlocked your repository BEFORE staging the above file(s)' echo '/!\ Proceed now as follows:' echo -e "\t git unstage ${STAGED_FILES}" echo -e "\t git crypt unlock" echo -e "\t git add ${STAGED_FILES}" exit 1 - fi + } fi fi