This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 284
/
lintdiff.sh
executable file
·128 lines (113 loc) · 3.57 KB
/
lintdiff.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
# Checks if new lint messages have appeared
FETCH_ORIGIN=origin
REF_BRANCH=origin/develop
CURRENT_BRANCH=$(git symbolic-ref --short -q HEAD)
REF_OUT=/tmp/ref-lint.out
CURRENT_OUT=/tmp/current-lint.out
FETCH=true
usage() {
echo "Usage: $0 [-b <branch>] [-o] [-f <origin>] [-h] command"
echo " -h show this help message"
echo " -b <branch> use branch \`branch\` for comparison. Default: origin/develop"
echo " -o offline mode, do not fetch origin"
echo " -f <remote> select the remote to fetch before linting. Default: origin"
exit 1
}
check_errcode() {
# Exitcodes >= 126 have special meaning:
# http://www.tldp.org/LDP/abs/html/exitcodes.html
if [ "$1" -ge 126 ]; then
echo "Fatal: command exited with code: $1. Aborting!"
exit 1
fi
}
if [ $# -eq 0 ]; then
usage
fi
while getopts "b:of:h" opt; do
case $opt in
b)
REF_BRANCH=$OPTARG
;;
o)
FETCH=false
;;
f)
FETCH_ORIGIN=$OPTARG
;;
*)
usage
;;
esac
done
shift $((OPTIND - 1))
# get new changes from develop, GitHub doesn't integrate them on its own
if $FETCH; then
echo "Fetching new changes from develop..."
git fetch "$FETCH_ORIGIN" || exit 1
fi
cleanup_artifacts() {
git reset --hard HEAD
git checkout "$CURRENT_BRANCH" -- || exit 1
}
diff-lines() {
# Function taken from: https://stackoverflow.com/questions/8259851/using-git-diff-how-can-i-get-added-and-modified-lines-numbers
# Adjusted the 'echo' line to not print the line content.
local path=
local line=
while read; do
esc=$'\033'
if [[ $REPLY =~ ---\ (a/)?.* ]]; then
continue
elif [[ $REPLY =~ \+\+\+\ (b/)?([^[:blank:]$esc]+).* ]]; then
path=${BASH_REMATCH[2]}
elif [[ $REPLY =~ @@\ -[0-9]+(,[0-9]+)?\ \+([0-9]+)(,[0-9]+)?\ @@.* ]]; then
line=${BASH_REMATCH[2]}
elif [[ $REPLY =~ ^($esc\[[0-9;]+m)*([\ +-]) ]]; then
echo "$path:$line:"
if [[ ${BASH_REMATCH[2]} != - ]]; then
((line++))
fi
fi
done
}
# we checkout the reference branch first in case there are
# uncommitted changes to be overwritten by the merge
git checkout "$REF_BRANCH" -- || exit 1
trap cleanup_artifacts EXIT
commit=$(git rev-parse HEAD)
# We need to take files responsible for the linting configuration from
# the new commit.
git checkout "$CURRENT_BRANCH" -- .pylintrc setup.cfg
echo "Checking branch $REF_BRANCH, commit: $commit..."
echo $@
$@ >$REF_OUT
check_errcode $?
# Now take back the checked out config, go back to the new branch
git reset --hard HEAD
git checkout "$CURRENT_BRANCH" -- || exit 1
# The trap is no longer needed
trap - EXIT
commit=$(git rev-parse HEAD)
echo "Checking branch $CURRENT_BRANCH, commit: $commit..."
echo $@
$@ >$CURRENT_OUT
check_errcode $?
diff=$(diff --old-line-format="" --unchanged-line-format="" -w <(sort $REF_OUT) <(sort $CURRENT_OUT))
if [ -n "$diff" ]; then
echo -e "New findings:\n"
echo "$diff"
# Remove lines from findings based on lines changed
DIFF_LINES=$(git diff --unified=0 "$REF_BRANCH" "$CURRENT_BRANCH" | diff-lines)
CHANGED_DIFF=$(echo "$diff" | grep -F "$DIFF_LINES")
echo -e "\n\nChanged lines findings:\n"
echo "$CHANGED_DIFF"
# Remove warning lines starting with W
CHANGED_ERR=$(echo "$CHANGED_DIFF" | grep -v -e '^W')
if [ -n "$CHANGED_ERR" ]; then
exit 1
fi
else
echo "Check OK, no new findings..."
fi