Run Code Sniffer on modified files
If you've got a big project, running
phpcs .
before each commit can be fairly time consuming. Doing a git status and running phpcs on each modified file manually is just as fiddly.
Try this...
#!/bin/bash
files=$(git ls-files -om --exclude-standard)
phpcs $files
Save it as an executable called git-cs in your PATH and typing the following will run phpcs on just the modified files...
git cs
Currently, there's no filtering of file types, but phpcs seems to do a pretty good job of ignoring files it can't sniff.
Written by Steve Halford
Related protips
5 Responses

Great tip!
I found if no files had changed it would just run phpcs
for the entire project, so use the following:
#!/bin/bash
files=$(git ls-files -om --exclude-standard)
if [ -z "$files" ]; then
echo 'No files to check';
else
phpcs $files
fi

Thanks @jenkoian !

Awesome idea! Thanks for sharing :)

One more improvement. If I want to have pre-commit hook, I need to include files in the commit. git ls-files -om --exclude-standard
exclude those files.
This is the solution (so far):
#!/bin/bash
files=$(git diff --name-only HEAD master);
if [ -z $files ]; then
phpcs $files
fi

Negation was missing:
#!/bin/bash
FILES=$(git diff --name-only HEAD master);
if [ ! -z "$FILES" ]; then
phpcs $FILES
fi