Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

git pre-commit error : Check filenames : .git/hooks/pre-commit; line 25; syntax error : unexpected end of file

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading