Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gracefully handle git secrets not being installed #212

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion git-secrets
Original file line number Diff line number Diff line change
Expand Up @@ -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 "elif [[ ! -z \"\$err\" ]]; then" >> "${dest}"
echo " >&2 echo \"\$err\"" >> "${dest}"
echo " exit 1" >> "${dest}"
echo "fi" >> "${dest}"
Comment on lines +206 to +212
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 "elif [[ ! -z \"\$err\" ]]; then" >> "${dest}"
echo " >&2 echo \"\$err\"" >> "${dest}"
echo " exit 1" >> "${dest}"
echo "fi" >> "${dest}"
cat <<-EOF > "${dest}"
err="\$(git secrets --${cmd} -- "\$@" 2>&1 > /dev/null)"
if [[ "\$err" == *"git: 'secrets' is not a git command"* ]]; then
exit 0
elif [[ ! -z "\$err" ]]; then
>&2 echo "\$err"
exit 1
fi
EOF

I think this will be easier to read, but it needs to be tested.
Also, due to github limitations, this suggestion doesn't replace the first echo on line 205 but should.

chmod +x "${dest}"
say "$(tput setaf 2)✓$(tput sgr 0) Installed ${hook} hook to ${dest}"
}
Expand Down