From f95664dcdad3372111e925fb17139f59d368832b Mon Sep 17 00:00:00 2001 From: Lukas Oberhuber Date: Wed, 20 Jul 2022 00:00:46 +0100 Subject: [PATCH 1/2] gracefully handle git secrets not being installed For whatever reason, sometimes the env does not have access to git secrets (for example in `vscode` this seems to happen frequently). This change allows it to fail gracefully instead of blocking all commits. --- git-secrets | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/git-secrets b/git-secrets index 11be153..ec85a8b 100755 --- a/git-secrets +++ b/git-secrets @@ -203,7 +203,13 @@ install_hook() { [ -f "${dest}" ] && [ "${FORCE}" -ne 1 ] \ && die "${dest} already exists. Use -f to force" echo "#!/usr/bin/env bash" > "${dest}" - echo "git secrets --${cmd} -- \"\$@\"" >> "${dest}" + echo "err=\"\$(git secrets --${cmd} -- \"\$@\" 2>&1 > /dev/null)\"" >> "${dest}" + echo "if [[ \"\$err\" == *\"git: 'secrets' is not a git command\"* ]]; then" >> "${dest}" + echo " exit 0" >> "${dest}" + echo "else" >> "${dest}" + echo " >&2 echo \"\$err\"" >> "${dest}" + echo " exit 1" >> "${dest}" + echo "fi" >> "${dest}" chmod +x "${dest}" say "$(tput setaf 2)✓$(tput sgr 0) Installed ${hook} hook to ${dest}" } From 4669ed45b7d1bf15e19b04290ed93d0aa0361483 Mon Sep 17 00:00:00 2001 From: Lukas Oberhuber Date: Thu, 21 Jul 2022 12:18:04 +0100 Subject: [PATCH 2/2] Fix algo to ensure success does not error --- git-secrets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-secrets b/git-secrets index ec85a8b..4f7ead2 100755 --- a/git-secrets +++ b/git-secrets @@ -206,7 +206,7 @@ install_hook() { echo "err=\"\$(git secrets --${cmd} -- \"\$@\" 2>&1 > /dev/null)\"" >> "${dest}" echo "if [[ \"\$err\" == *\"git: 'secrets' is not a git command\"* ]]; then" >> "${dest}" echo " exit 0" >> "${dest}" - echo "else" >> "${dest}" + echo "elif [[ ! -z \"\$err\" ]]; then" >> "${dest}" echo " >&2 echo \"\$err\"" >> "${dest}" echo " exit 1" >> "${dest}" echo "fi" >> "${dest}"