From 1bd4476d44d1377552efdb75f44d8aa7cd22e7f4 Mon Sep 17 00:00:00 2001 From: thk123 Date: Fri, 6 Jan 2017 14:58:23 +0000 Subject: [PATCH 1/2] Adding script to run lint check on modified lines Bash script that uses blame on a range of commits to work out what lines have been touched since master. Finally it uses grep to filter the output of the linting script based only on lines that have been modified. The yml file that runs the linter has been updated to run this script. --- .travis.yml | 2 +- scripts/run_lint.sh | 94 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100755 scripts/run_lint.sh diff --git a/.travis.yml b/.travis.yml index 64ef93f5278..972d563d0c0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,7 +36,7 @@ matrix: compiler: clang env: COMPILER=clang++ - env: NAME="CPP-LINT" - script: DIFF=`git diff --name-only master HEAD` && if [ "$DIFF" != "" ]; then python scripts/cpplint.py $DIFF; fi + script: scripts/run_lint.sh master HEAD script: - make -C src minisat2-download diff --git a/scripts/run_lint.sh b/scripts/run_lint.sh new file mode 100755 index 00000000000..6e084ed2bd3 --- /dev/null +++ b/scripts/run_lint.sh @@ -0,0 +1,94 @@ +#!/bin/bash + +set -e + +if [[ "$#" -ne 2 ]] +then + echo "Script for running the CPP linter only on modified lines." + echo "Requires two arguments the start and the end" + echo "start - a git reference that marks the first commit whose changes to consider" + echo "end - a git reference that marks the last commit whose changes to consider" + + exit 1 +fi + +git_start=$1 +git_end=$2 + +# Get the list of files that have changed +diff_files=`git diff --name-only $git_start $git_end` + +# Build a filter that will filter the blame output +# to only include lines that come from one of the relevant_commits +# We do this by making the blame tool output the same hash for all +# lines that are too old. +blame_grep_filter=`git rev-parse "$git_start"` + +# Build a regex for finding the line number of a given line inside blame +# First matches the 40 digit hash of the commi +# Then match an arbitary length number that represents the line in the original file +# Finally matches (and groups) another arbitary length digit which is the +# line in the final file +regex="[0-9a-f]{40} [0-9]+ ([0-9]+)" + +# We only split on lines or otherwise the git blame output is nonsense +IFS=$'\n' + +are_errors=0 + +for file in $diff_files; do + # We first filter only the lines that start with a commit hash + # Then we filter out the ones that come from the start commit + modified_lines=`git blame $git_start..$git_end --line-porcelain $file | grep -E "^[0-9a-f]{40}" | grep -v "$blame_grep_filter"` + + if [ -z "$modified_lines" ] + then + # No modified lines in this file (perhaps only lines have been deleted) + # So we bail on reporting errors on this file + continue + fi + + # Next we build another grep filter the output of the linting script + lint_grep_filter="^(" + + # For each modified line we find the line number + for line in $modified_lines; do + + # Use the above regex to match the line number + if [[ $line =~ $regex ]] + then + # Some bash magic to get the first group from the regex (the line number) + LINENUM="${BASH_REMATCH[1]}" + + # The format from the linting script is filepath:linenum: [error type] + # So we build the first bit to filter out relevant lines + LINE_FILTER=$file:$LINENUM + + # Add the line filter on to the grep expression as we want + # lines that match any of the line filters + lint_grep_filter+=$LINE_FILTER + lint_grep_filter+="|" + fi + done + + # Knock off the final pipe and add the closing bracket + lint_grep_filter=${lint_grep_filter::-1} + lint_grep_filter+=")" + + # Run the linting script and filter by the filter we've build + # of all the modified lines + # The errors from the linter go to STDERR so must be redirected to STDOUT + result=`python scripts/cpplint.py $file 2>&1 | grep -E "$lint_grep_filter"` + + # Providing some errors were relevant we print them out + if [ "$result" ] + then + are_errors=1 + (>&2 echo "$result") + fi +done + +unset IFS + +# Return an error code if errors are found +exit $are_errors From 2633d64a19d8d84f33253a7d4bd8cdc3f850f23f Mon Sep 17 00:00:00 2001 From: thk123 Date: Tue, 10 Jan 2017 12:33:41 +0000 Subject: [PATCH 2/2] Also include errors assoicated with line 0 (e.g. copyright errors). To do this we need to not error exit if the grep fails to find any valid lines. We still care about these files (this can happen if, for example, only lines are removed from the file). --- scripts/run_lint.sh | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/scripts/run_lint.sh b/scripts/run_lint.sh index 6e084ed2bd3..3234e9bfe98 100755 --- a/scripts/run_lint.sh +++ b/scripts/run_lint.sh @@ -37,19 +37,16 @@ IFS=$'\n' are_errors=0 for file in $diff_files; do - # We first filter only the lines that start with a commit hash - # Then we filter out the ones that come from the start commit - modified_lines=`git blame $git_start..$git_end --line-porcelain $file | grep -E "^[0-9a-f]{40}" | grep -v "$blame_grep_filter"` + # We build another grep filter the output of the linting script + lint_grep_filter="^(" - if [ -z "$modified_lines" ] - then - # No modified lines in this file (perhaps only lines have been deleted) - # So we bail on reporting errors on this file - continue - fi + # Include line 0 errors (e.g. copyright) + lint_grep_filter+=$file + lint_grep_filter+=":0" - # Next we build another grep filter the output of the linting script - lint_grep_filter="^(" + # We first filter only the lines that start with a commit hash + # Then we filter out the ones that come from the start commit + modified_lines=`git blame $git_start..$git_end --line-porcelain $file | grep -E "^[0-9a-f]{40}" | { grep -v "$blame_grep_filter" || true; }` # For each modified line we find the line number for line in $modified_lines; do @@ -66,13 +63,12 @@ for file in $diff_files; do # Add the line filter on to the grep expression as we want # lines that match any of the line filters - lint_grep_filter+=$LINE_FILTER lint_grep_filter+="|" + lint_grep_filter+=$LINE_FILTER fi done - # Knock off the final pipe and add the closing bracket - lint_grep_filter=${lint_grep_filter::-1} + # Add the closing bracket lint_grep_filter+=")" # Run the linting script and filter by the filter we've build