PHP Syntax Checker For Git Update. Test Only Files In The Commit

After my first article on working with git pre-commit hooks, I found a small issue that could cause a developer not to commit, even if all of the code they want to commit is ok. The problem comes with files that are recognized as change, but will not be part of the commit. Luckily sed has an easy solution to this problem. My new git hook now looks like this:

git status |  sed '/# Changed but not updated:/,$d' | sed '/# Untracked files:/,$d' |cut -c 3- | egrep  '^(modified|new file)' | egrep '\.(php|html)$' | awk -F: '{ gsub(/[[:space:]]*/,"", $2); print $2}' | xargs -n1 -r php -l

By using sed, we knock out every line below the “Changed but not updated”, and “Untracked files” sections in the commit message. This way, if you have other sections of code that you are working on, but not pushing into this commit.