Reputation: 69
I am trying to get a pre-commit hook to work, to check the stagged files and read the difference to check for a few strings. If the strings exist, commit has to fail.
#!/bin/bash
#import os
echo "Running pre-commit hook"
checks=os.environ["APPSETTING_DEVPASSWORD"],os.environ["APPSETTING_DEVUSER"],os.environ["APPSETTING_DEVPASS_ELMAH"]
git diff --cached --name-status | while read x file; do
if [ "$x" == 'D' ]; then continue; fi
for word in $checks
do
if egrep $word $file ; then
echo "ERROR: Disallowed expression \"${word}\" in file: ${file}"
exit 1
fi
done
done || exit $?
It still commits the files even though the strings exist in the files. Any guidance would be greatly appreciated. I am fairly new to bash.
Upvotes: 5
Views: 3138
Reputation: 94512
It must be something like this:
#!/bin/bash
echo "Running pre-commit hook"
checks=($APPSETTING_DEVPASSWORD $APPSETTING_DEVUSER $APPSETTING_DEVPASS_ELMAH) # create an array
git diff --cached --name-status | while read flag file; do
if [ "$flag" == 'D' ]; then continue; fi
for word in ${checks[@]}
do
if egrep -q "$word" "$file"; then
echo "ERROR: Disallowed expression \"${word}\" in file: ${file}" >&2
exit 1
fi
done
done
Upvotes: 5