-
Notifications
You must be signed in to change notification settings - Fork 10
/
invenio-check-branch
executable file
·160 lines (146 loc) · 5.32 KB
/
invenio-check-branch
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
#
# Usage: invenio-check-branch some-base-branch some-topic-branch [--skip-headline-check]
#
# Run Invenio kwalitee tests for files changed in some-topic-branch
# when compared to some-base-branch and compare the results. Useful
# for checking before merging whether a feature branch would not
# introduce into master some more code kwalitee problems.
#
# Tibor Simko <[email protected]>
#
# Copyright (C) 2011, 2012, 2013, 2014 CERN.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
# configuration
CFG_INVENIO_SRCDIR=${CFG_INVENIO_SRCDIR:=~/private/src/invenio}
# check path
case "$(pwd)" in
*$CFG_INVENIO_SRCDIR* ) ;;
* ) echo "[ERROR] You must run this script inside $CFG_INVENIO_SRCDIR." && exit 1;;
esac
# read arguments:
if [ $# -eq 0 ]; then
# no arguments; assume to check current branch against master
BRANCHOLD=master
BRANCHNEW=$(git branch | grep \* | sed -e s/[\ \*]//g)
else
if [ $# -lt 2 ]; then
echo "[ERROR] Usage: $(basename $0) some-base-branch some-topic-branch [--skip-headline-check]"
exit 1
fi
BRANCHOLD=$1
BRANCHNEW=$2
fi
# check arguments:
if [ "$BRANCHOLD" = "$BRANCHNEW" ]; then
echo "[ERROR] Not needed to check branch $BRANCHNEW against $BRANCHOLD. Exiting."
exit 1
fi
# check possible merge conflict leftovers:
nmerge=$(git log $BRANCHOLD..$BRANCHNEW -p | grep -c -E '^\+[><=]{7}(\s|$)')
if [ $nmerge -gt 0 ]; then
echo '[ERROR] Found possible merge conflict leftovers:'
git log $BRANCHOLD..$BRANCHNEW -p | grep -E '^\+[><=]{7}(\s|$)'
echo '[ERROR] Exiting.'
exit 1
fi
# check commit log message headline length and formatting:
if [[ "$@" != *"--skip-headline-check"* ]]; then
problemfound=0
while IFS=$'\n' read line; do
# check commit headline length:
if [ ${#line} -ge 92 ]; then
echo "[ERROR] Commit headline too long:" $line
problemfound=1
fi
# check commit headline formatting:
if [[ ! $line =~ ^[[:alnum:]]{40,40}[[:blank:]][[:alnum:]]+:[[:blank:]] ]]; then
echo "[ERROR] Commit headline does not start with module name:" $line
problemfound=1
fi
if [[ $line =~ \.$ ]]; then
echo "[ERROR] Commit headline ends with dot:" $line
problemfound=1
fi
done < <(git log $BRANCHOLD..$BRANCHNEW --format=oneline)
if [ $problemfound -ne 0 ]; then
exit 1
fi
fi
# set up temporary files:
yyyymmddhhmmss=$(date +"%Y-%m-%d-%H-%M-%S")
kwaliteefileold=/tmp/$(basename $0)-$yyyymmddhhmmss-$BRANCHOLD
kwaliteefilenew=/tmp/$(basename $0)-$yyyymmddhhmmss-$BRANCHNEW
# detect which files to check:
checkfiles=$(git diff $BRANCHOLD..$BRANCHNEW --pretty=format: --name-only | grep '\.py' | sort | uniq)
echo "[INFO] Going to check the following files from $BRANCHOLD to $BRANCHNEW:"
echo $checkfiles
# save current branch to be able to come back to it:
currentdirectory=$(pwd)
currentbranch=$(git branch | grep \* | sed -e s/[\ \*]//g)
cd $CFG_INVENIO_SRCDIR
# run kwalitee checks on old branch, ignoring line numbers:
echo "[INFO] Installing branch $BRANCHOLD ..."
git checkout $BRANCHOLD
invenio-make-install
#pylint -E $checkfiles >& /dev/null
#if [ $? -ne 0 ]; then
# echo "[ERROR] pylint -e does not pass."
# exit 1
#fi
cp /dev/null $kwaliteefileold
for afile in $checkfiles; do
if [ -e $afile ]; then
invenio-check-kwalitee -q --check-some $afile | sed 's/^.*[0-9]://' | sed 's/line [0-9][0-9]*//' >> $kwaliteefileold
fi
done
# run kwalitee checks on new branch, ignoring line numbers:
echo "[INFO] Installing branch $BRANCHNEW ..."
git checkout $BRANCHNEW
invenio-make-install
#pylint -E $checkfiles >& /dev/null
#if [ $? -ne 0 ]; then
# echo "[ERROR] pylint -e does not pass."
# exit 1
#fi
cp /dev/null $kwaliteefilenew
for afile in $checkfiles; do
if [ -e $afile ]; then
invenio-check-kwalitee -q --check-some $afile | sed 's/^.*[0-9]://' | sed 's/line [0-9][0-9]*//' >> $kwaliteefilenew
fi
done
# return to the branch we started from, if different:
if [ "$currentbranch" != "$BRANCHNEW" ]; then
echo "[INFO] Reinstalling the original branch $currentbranch ..."
cd $currentdirectory
git checkout $currentbranch
invenio-make-install
fi
# compare number of warnings found:
echo "[INFO] Comparing kwalitee of $checkfiles between $BRANCHOLD and $BRANCHNEW ..."
nold=$(wc -l $kwaliteefileold | awk '{print $1;}')
nnew=$(wc -l $kwaliteefilenew | awk '{print $1;}')
if [ $nold -gt $nnew ]; then
echo "[INFO] OK, less warnings in branch ${BRANCHNEW}. Good!"
exit 0
fi
# compare contents of warnings found:
diff -u $kwaliteefileold $kwaliteefilenew
if [ $? -eq 0 ]; then
echo "[INFO] OK, identical number of warnings."
else
echo "[ERROR] More warnings in branch ${BRANCHNEW}. Please fix."
exit 1
fi