Skip to content

Commit 1bd4476

Browse files
author
thk123
committed
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.
1 parent ddea715 commit 1bd4476

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ matrix:
3636
compiler: clang
3737
env: COMPILER=clang++
3838
- env: NAME="CPP-LINT"
39-
script: DIFF=`git diff --name-only master HEAD` && if [ "$DIFF" != "" ]; then python scripts/cpplint.py $DIFF; fi
39+
script: scripts/run_lint.sh master HEAD
4040

4141
script:
4242
- make -C src minisat2-download

scripts/run_lint.sh

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
if [[ "$#" -ne 2 ]]
6+
then
7+
echo "Script for running the CPP linter only on modified lines."
8+
echo "Requires two arguments the start and the end"
9+
echo "start - a git reference that marks the first commit whose changes to consider"
10+
echo "end - a git reference that marks the last commit whose changes to consider"
11+
12+
exit 1
13+
fi
14+
15+
git_start=$1
16+
git_end=$2
17+
18+
# Get the list of files that have changed
19+
diff_files=`git diff --name-only $git_start $git_end`
20+
21+
# Build a filter that will filter the blame output
22+
# to only include lines that come from one of the relevant_commits
23+
# We do this by making the blame tool output the same hash for all
24+
# lines that are too old.
25+
blame_grep_filter=`git rev-parse "$git_start"`
26+
27+
# Build a regex for finding the line number of a given line inside blame
28+
# First matches the 40 digit hash of the commi
29+
# Then match an arbitary length number that represents the line in the original file
30+
# Finally matches (and groups) another arbitary length digit which is the
31+
# line in the final file
32+
regex="[0-9a-f]{40} [0-9]+ ([0-9]+)"
33+
34+
# We only split on lines or otherwise the git blame output is nonsense
35+
IFS=$'\n'
36+
37+
are_errors=0
38+
39+
for file in $diff_files; do
40+
# We first filter only the lines that start with a commit hash
41+
# Then we filter out the ones that come from the start commit
42+
modified_lines=`git blame $git_start..$git_end --line-porcelain $file | grep -E "^[0-9a-f]{40}" | grep -v "$blame_grep_filter"`
43+
44+
if [ -z "$modified_lines" ]
45+
then
46+
# No modified lines in this file (perhaps only lines have been deleted)
47+
# So we bail on reporting errors on this file
48+
continue
49+
fi
50+
51+
# Next we build another grep filter the output of the linting script
52+
lint_grep_filter="^("
53+
54+
# For each modified line we find the line number
55+
for line in $modified_lines; do
56+
57+
# Use the above regex to match the line number
58+
if [[ $line =~ $regex ]]
59+
then
60+
# Some bash magic to get the first group from the regex (the line number)
61+
LINENUM="${BASH_REMATCH[1]}"
62+
63+
# The format from the linting script is filepath:linenum: [error type]
64+
# So we build the first bit to filter out relevant lines
65+
LINE_FILTER=$file:$LINENUM
66+
67+
# Add the line filter on to the grep expression as we want
68+
# lines that match any of the line filters
69+
lint_grep_filter+=$LINE_FILTER
70+
lint_grep_filter+="|"
71+
fi
72+
done
73+
74+
# Knock off the final pipe and add the closing bracket
75+
lint_grep_filter=${lint_grep_filter::-1}
76+
lint_grep_filter+=")"
77+
78+
# Run the linting script and filter by the filter we've build
79+
# of all the modified lines
80+
# The errors from the linter go to STDERR so must be redirected to STDOUT
81+
result=`python scripts/cpplint.py $file 2>&1 | grep -E "$lint_grep_filter"`
82+
83+
# Providing some errors were relevant we print them out
84+
if [ "$result" ]
85+
then
86+
are_errors=1
87+
(>&2 echo "$result")
88+
fi
89+
done
90+
91+
unset IFS
92+
93+
# Return an error code if errors are found
94+
exit $are_errors

0 commit comments

Comments
 (0)