12

I am trying to build a pre-commit script in SVN, and I want to run PHP_CodeSniffer on the modified lines only (as opposed to the whole file). So far I have this script:

#!/bin/sh

REPOS="$1"
TXN="$2"

# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook
$SVNLOOK log -t "$TXN" "$REPOS" | \
   grep "[a-zA-Z0-9]" > /dev/null || exit 1

# Check for code validation before commiting the script using PHP_CodeSniffer
/tmp/pear/download/PHP_CodeSniffer-1.4.3/scripts/phpcs-svn-pre-commit  "$REPOS" -t "$TXN" >&2 || exit 1

# All checks passed, so allow the commit.
exit 0
Perception
  • 75,573
  • 19
  • 170
  • 185
Antonio
  • 271
  • 4
  • 12
  • You maybe should outline a little what the problem with what you've tried so far is as well. +1 For building this and asking anyway ;) – hakre Jan 01 '13 at 21:48
  • 2
    phpcs takes the file, parses it to AST and performs analysis on it. So on the checking step - there is no original lines number available. I hardly doubt it's possible – zerkms Jan 01 '13 at 22:37
  • 1
    Like @zerkms said, whole file must be parsed, but it might be possible to check if any of changed line is in the set of lines containing cs errors. – dev-null-dweller Jan 01 '13 at 22:40
  • It seems difficult. I recomend looking at `svnlook help`. You will need to call `svnlook changes` to see which files have changed on that commit. And then you could use `svnlook diff` to get the changes made, and parse that to extract the new lines. The problem is that the changed line might not work alone, so you should better check the whole file. – El Barto Jan 01 '13 at 22:42
  • thanks for your help every one, @El Bardo: that is exactly what am trying to achieve,you said "The problem is that the changed line might not work alone, so you should better check the whole file" i thought phpcs is validating lines of code not blocks.so how about writing these changes to a temp file and phpcs that temp file? – Antonio Jan 02 '13 at 07:25
  • 1
    While the content sniffer does check "lines," it has no way of knowing what's actually changed by the time it runs. Why not diff the output of the file before the changes are applied, then after? It's quick, it's dirty, and it just might work... – Charles Jan 02 '13 at 07:55
  • @Charles: would you write that script ? thanks in advance – Antonio Jan 02 '13 at 12:43
  • 4
    No, I won't do your work for you. – Charles Jan 02 '13 at 17:14
  • This question might be related and at least worth to cross-link: [Git: pre-receive hook with PHP_CodeSniffer](http://stackoverflow.com/questions/6120331/git-pre-receive-hook-with-php-codesniffer) - For the line numbers I would take the report because it contains the line numbers and then filter against changed lines in the code. and taking a look if it matches. But this needs some experimentation. The first thing to solve would be to find out which files have been edited in a commit and then also which lines in those files. That should allow one to run PHPCS on those files and then to filter. – hakre Jan 04 '13 at 23:00

5 Answers5

4

In 2017, since CodeSniffer 3.0.0 release, there is new --cache option.

Or you can use EasyCodingStandard, that does this by default.

Tomas Votruba
  • 19,054
  • 8
  • 65
  • 86
0

Conceptually it does not make sense.

phpcs needs to know the hole context in order to analyze correctly.

Scanning only the changed lines would remove context.

Max Horvath
  • 139
  • 4
  • 1
    PHPCS ony works with context of current file only. I'm aware only about 1 sniff, that does need more intell, but it uses bit advanced technique to do so. https://github.com/Symplify/CodingStandard#possible-unused-public-method – Tomas Votruba Jan 02 '18 at 11:37
  • I actually meant the context of the whole file, not the whole project. So the statement is still correct. – Max Horvath Sep 08 '20 at 13:42
0

There is a project lint-diffs that can run any linter on diff output files.

Use phpcs --report=emacs as the command. The regex can be the same as the example config.

Erik Aronesty
  • 8,927
  • 4
  • 46
  • 29
-1

Here is how it's done in Phabricator's tool Arcanist:

  1. it runs php code sniffer on every changed file and collects all errors
  2. restricts those errors by line number, where only line numbers that were changed in this commit are allowed

This of course won't cover some specific cases, when change in LineA caused error in LineB.

aik099
  • 119
  • 2
  • 4
-1

I am aware the topic is outdated. I build my own solution to achievie this goal (scaning only recently changed lined). It is premature but works

https://github.com/ayeo/sniffer

ayeo
  • 496
  • 1
  • 4
  • 16