#9 Issue closed: Check syntax on code-contributions/distributing Rear

dagwieers opened issue at 2012-03-21 12:58:

It's very easy to have Bash syntax-check source code, but still we often commit pieces that fail a simple syntax check.

Jeroen already provided an example of how to hook this into your Git workflow. But it should be part of the workflow to building the official release.

gdha commented at 2012-03-21 13:51:

+1
Do we have to do this on a user basis or can we force it somehow (globally on [source])?

dagwieers commented at 2012-06-08 07:53:

I added a 'make validate' target that will syntax-check all shell scripts. However at this time it reports 3 dangling symlinks.

We can improve this scheme with Jeroen's Git hooks.

dagwieers commented at 2012-06-10 20:14:

@jhoekx Can you repost the information on how you configured a hook to do syntax-checks before commiting ? I cannot find your prior post :-( If we can document this on the website, we can close this ticket.

jhoekx commented at 2012-06-11 06:18:

My git pre-commit hook contains this.

### Check all scripts for syntax errors.
declare -i errcount
for shellscript in $(git diff-index --name-only $against | grep '\.sh') ; do
    if [[ -r "$shellscript" ]] ; then
        bash -n $shellscript
        errcount=$(( errcount + $? ))
    fi
done
if (( errcount > 0 )) ; then
    echo "Scripts with syntax errors."
    echo "Not committing."
    exit 1
fi

The $against variable is defined in the hook as

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

But the same and more is done by make validate?

dagwieers commented at 2012-06-11 07:28:

And how is it being hooked ? The intention is to hook maybe make validate into git pre-commit. One of the main differences is that make validate syntax-checks the whole tree which is a lot slower than if you actually syntax-check only the committed files.

And it might break because of something you didn't change (unless of course everyone uses the pre-commit hook :-))

jhoekx commented at 2012-06-11 07:36:

I have https://gist.github.com/2908915 in .git/hooks/pre-commit .

dagwieers commented at 2012-06-11 15:32:

So the solution is to add the below line to the file .git/hooks/pre-commit:
´´´
make validate
´´´
And to make sure the .git/hooks/pre-commit files is executable.


[Export of Github issue for rear/rear.]