I have a git repo with key files that have to be all in the format
#possible comment
key "<key with 64 chars>"; # possible comment
vpn .... #optional line
I would like to add a hook into our git repository, that when you try to commit a new file, this regular expression is checked on all files in the repository beforehand:
cat *|grep -v -E "^\s*key\s+\"[0-9a-f]{64}\";\s*(#.*)*$"|grep -v -E "(^#|vpn|^$)"
I created a .git/hooks/pre-commit
file:
#!/bin/sh
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# Redirect output to stderr.
exec 1>&2
diffstr=$(git diff --cached $against | sed 's/^+//g' | grep -E "^\+[^\+]" \
| grep -v -E "^key \"\w{64}\";\s*(#.*)*$" | grep -v -E "(^#|vpn|^$)")
if [ "$diffstr" != "" ] ; then
echo "You have a malformed key in your changes, you can't commit until it has been corrected:"
echo $diffstr
exit 1
fi
I want to be stopped from committing changed/new key-files that are not in the correct format. Any of the following:
- don't start with
key
- dont use
"
-quotes - dont end with
;
(optionally followed by a comment) - where the key is not a 64 hex characters.
- any other lines, that don't start with comment
#
But my solution still doesn't stop me from committing wrong key files. What am I doing wrong?