my question is about git pre-commit hook.
i want to figure out, and disallow commits with filenames containing whitespaces.
#!/bin/bash
listchange=$(git diff --name-only)
echo "Check filenames:"
wrongfilenames=$(
for filename in $listchange
do
fname=$(basename $filename)
if ! [[ $fname =~ ^[0-9abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._]+$ ]]; then
echo $fname;
fi
if [[ $fname =~ [[:space:]]* ]]; then
echo $fname;
echo "contains space"
fi
done
);
if [ -n "$wrongfilenames" ] ; then
echo "The following filename contains invalid characters:"
echo "$wrongfilenames"
echo "Commit rejected, fix it and try again."
exit 1
else
echo "....OK"
The First if, will check if the filename contains something else than
0-9, a-z, A-Z and . and _ .
there's no way to add a file containing "-".
For me it's not that clear, why whitespace passes this filter. Thats why i tried to create another if to check if filename contains a space. But this doesn't work as expected. \
thanks in advance