2bnvaa
Last Updated: February 25, 2016
·
2.441K
· stevehalford

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.

5 Responses
Add your response

5601

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
over 1 year ago ·
5602

Thanks @jenkoian !

over 1 year ago ·
18185

Awesome idea! Thanks for sharing :)

over 1 year ago ·
18192

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
over 1 year ago ·
18193

Negation was missing:

#!/bin/bash

FILES=$(git diff --name-only HEAD master);
if [ ! -z "$FILES" ]; then
    phpcs $FILES
fi
over 1 year ago ·