Skip to content

Commit

Permalink
Add parse-diff-ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed May 19, 2015
1 parent 2890efc commit c61307a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
26 changes: 26 additions & 0 deletions parse-diff-ranges.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
# Take input from `git diff --no-prefix --unified=0`
# Outputs path/to/file.ext:123-456
# Where 123 is the start line in a diff and 456 is the end line.
# A line appears for each patch occuring in a file.

$current_file = null;

while ( $line = fgets( STDIN ) ) {
if ( preg_match( '#^\+\+\+ (?P<file_path>.+)#', $line, $matches ) ) {
$current_file = $matches['file_path'];
$file_ranges[ $current_file ] = array();
continue;
}
if ( empty( $current_file ) ) {
continue;
}
if ( preg_match( '#^@@ -(\d+)(?:,(\d+))? \+(?P<line_number>\d+)(?:,(?P<line_count>\d+))? @@#', $line, $matches ) ) {
if ( empty( $matches['line_count'] ) ) {
$matches['line_count'] = 0;
}
$start_line = intval( $matches['line_number'] );
$end_line = intval( $matches['line_number'] ) + intval( $matches['line_count'] );
echo "$current_file:$start_line-$end_line\n";
}
}
26 changes: 18 additions & 8 deletions travis.script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,44 @@
set -e

if [ "$TRAVIS_PULL_REQUEST" != 'false' ] && [ "$LIMIT_TRAVIS_PR_CHECK_SCOPE" != '0' ]; then
git diff --diff-filter=AM --name-only $TRAVIS_BRANCH...$TRAVIS_COMMIT -- $PATH_INCLUDES | cat - | sort > /tmp/checked-files
git diff --diff-filter=AM --no-prefix --unified=0 $TRAVIS_BRANCH...$TRAVIS_COMMIT -- $PATH_INCLUDES | php $DEV_LIB_PATH/parse-diff-ranges.php > /tmp/checked-files
else
find $PATH_INCLUDES -type f | sed 's:^\.//*::' | sort > /tmp/checked-files
find $PATH_INCLUDES -type f | sed 's:^\.//*::' > /tmp/checked-files
fi

echo "TRAVIS_BRANCH: $TRAVIS_BRANCH"
echo "Files to check:"
cat /tmp/checked-files
echo

function remove_diff_range {
sed 's/:[0-9][0-9]*-[0-9][0-9]*$//' | sort | uniq
}
function filter_php_files {
grep '.php'
}
function filter_js_files {
grep '.js'
}

# Run PHP syntax check
cat /tmp/checked-files | grep '.php' | xargs --no-run-if-empty php -lf
cat /tmp/checked-files | remove_diff_range | filter_php_files | xargs --no-run-if-empty php -lf

# Run JSHint
if ! cat /tmp/checked-files | grep '.js' | xargs --no-run-if-empty jshint --reporter=unix $( if [ -e .jshintignore ]; then echo "--exclude-path .jshintignore"; fi ) > /tmp/jshint-report; then
if ! cat /tmp/checked-files | remove_diff_range | filter_js_files | xargs --no-run-if-empty jshint --reporter=unix $( if [ -e .jshintignore ]; then echo "--exclude-path .jshintignore"; fi ) > /tmp/jshint-report; then
echo "Here are the problematic JSHINT files:"
cat /tmp/jshint-report
fi

# Run JSCS
if [ -n "$JSCS_CONFIG" ] && [ -e "$JSCS_CONFIG" ]; then
# TODO: Restrict to lines changed (need an emacs/unix reporter)
cat /tmp/checked-files | grep '.js' | xargs --no-run-if-empty jscs --verbose --config="$JSCS_CONFIG"
cat /tmp/checked-files | remove_diff_range | filter_js_files | xargs --no-run-if-empty jscs --verbose --config="$JSCS_CONFIG"
fi

# Run PHP_CodeSniffer
# TODO: Restrict to lines changed
if ! cat /tmp/checked-files | grep '.php' | xargs --no-run-if-empty $PHPCS_DIR/scripts/phpcs -s --report-full --report-emacs=/tmp/phpcs-report --standard=$WPCS_STANDARD $(if [ -n "$PHPCS_IGNORE" ]; then echo --ignore=$PHPCS_IGNORE; fi); then
if ! cat /tmp/checked-files | remove_diff_range | filter_php_files | xargs --no-run-if-empty $PHPCS_DIR/scripts/phpcs -s --report-full --report-emacs=/tmp/phpcs-report --standard=$WPCS_STANDARD $(if [ -n "$PHPCS_IGNORE" ]; then echo --ignore=$PHPCS_IGNORE; fi); then
echo "Here are the problematic PHPCS files:"
cat /tmp/phpcs-report
fi
Expand All @@ -41,8 +51,8 @@ if [ -e phpunit.xml ] || [ -e phpunit.xml.dist ]; then
fi

# Run YUI Compressor Check
if [ "$YUI_COMPRESSOR_CHECK" == 1 ] && [ 0 != $( cat /tmp/checked-files | grep '.js' | wc -l ) ]; then
if [ "$YUI_COMPRESSOR_CHECK" == 1 ] && [ 0 != $( cat /tmp/checked-files | filter_js_files | wc -l ) ]; then
YUI_COMPRESSOR_PATH=/tmp/yuicompressor-2.4.8.jar
wget -O "$YUI_COMPRESSOR_PATH" https://github.com/yui/yuicompressor/releases/download/v2.4.8/yuicompressor-2.4.8.jar
cat /tmp/checked-files | grep '.js' | xargs --no-run-if-empty java -jar "$YUI_COMPRESSOR_PATH" -o /dev/null 2>&1
cat /tmp/checked-files | remove_diff_range | filter_js_files | xargs --no-run-if-empty java -jar "$YUI_COMPRESSOR_PATH" -o /dev/null 2>&1
fi

0 comments on commit c61307a

Please sign in to comment.