Issue
Below is hook code I am using for pre-commit in VS 2019. I want to restrict some files being committed always. Trying with string.exe. But getting error. Help needed on this.
#!/bin/sh
#!/usr/bin/env bash
listchange=$(git diff --name-only)
echo "Check filenames:"
wrongfilenames=$(
for filename in $listchange
do
fname= $filename
if [[ $fname = 'strings.exe' ]]; then
echo $fname;
echo "has strings.exe"
fi
done
);
if [ -n "$wrongfilenames" ] ; then
echo "The following files need not to commit:"
echo "$wrongfilenames"
echo "Commit rejected, fix it and try again."
exit 1
else
echo "....OK"
Solution
It looks like you are missing a fi
at the end of the conditional check.
#!/bin/bash
listchange=$(git diff --name-only)
echo "Check filenames:"
wrongfilenames=$(
for filename in $listchange
do
if [[ "$filename" = "strings.exe" ]]; then
echo "$filename"
echo "has strings.exe"
# TODO: take the appropriate action(s) here
return 1 # => quit with error
fi
done
);
if [ -n "$wrongfilenames" ] ; then
echo "The following files need not to commit:"
echo "$wrongfilenames"
echo "Commit rejected, fix it and try again."
exit 1
else
echo "....OK"
fi
NOTE: it is a good practice to add double quotes around $var when checking content against string. Indeed, if the $var has white-spaces it will not work as expected.
Answered By - Jib Answer Checked By - Robin (WPSolving Admin)