blob: cf54f8342bddd3b86e618cc1cd10278d0d26a0c8 [file] [log] [blame]
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07001#!/bin/bash
2
3#
4# Copyright (c) 2015 Intel Corporation.
5#
Javier B Perez Hernandezf7fffae2015-10-06 11:00:37 -05006# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07009#
Javier B Perez Hernandezf7fffae2015-10-06 11:00:37 -050010# http://www.apache.org/licenses/LICENSE-2.0
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070011#
Javier B Perez Hernandezf7fffae2015-10-06 11:00:37 -050012# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070017#
18
19
20# crawls the source tree to find out the amount of checkpatch issues
21# and optionally update scripts/known_checkpatch_issues
22# usage: check_known_checkpatch_issues.sh [-u]
23# where: -u updates the known_checkpatch_issues db and commits it
24# -q is the quiet mode (don't display the diff on stdout)
25
26exe_name=$(basename $0)
27
Anas Nashifb8823772015-06-05 22:46:00 -040028do_checkpatch_bin=${ZEPHYR_BASE}/scripts/do_checkpatch.sh
29timestamp_bin=${ZEPHYR_BASE}/scripts/timestamp
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070030
31declare update=n
32declare quiet=n
33
34function usage {
35 printf "usage: %s [-u][-q]\n" ${exe_name} >&2
36}
37
38function fail {
39 usage
40 exit -1
41}
42function verify_needed {
43 needed="\
44 ${do_checkpatch_bin} \
45 ${timestamp_bin} \
46 "
47 for i in ${needed}; do
48 type $i &>/dev/null
49 if [ $? != 0 ]; then
50 printf "need '%s' but not found in PATH\n" $i >&2
51 exit -1
52 fi
53 done
54}
55
56function get_opts {
57 declare -r optstr="quh"
58 while getopts ${optstr} opt; do
59 case ${opt} in
60 u) update=y ;;
61 q) quiet=y ;;
62 h) usage; exit 0 ;;
63 *) fail ;;
64 esac
65 done
66}
67
68verify_needed
69get_opts $@
70
71do_checkpatch=${do_checkpatch_bin}
72timestamp="${timestamp_bin} -u"
73ts=$(${timestamp})
74uid=$(id -u)
75pid=$$
76suffix=${uid}-${pid}-${ts}
77checkpatch_results=/tmp/checkpatch.results-${suffix}
Anas Nashifb8823772015-06-05 22:46:00 -040078known_checkpatch_issues=${ZEPHYR_BASE}/scripts/known_checkpatch_issues
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070079checkpatch_issues=/tmp/checkpatch_issues-${suffix}
80git_log_params="\
81 --abbrev=8 \
82 --abbrev-commit \
83"
84
85commit_id_str=$(git log ${git_log_params} HEAD | head -n 1)
86echo ${commit_id_str} > ${checkpatch_issues}
87
88${do_checkpatch} ${checkpatch_results} >> ${checkpatch_issues}
89
90diff_file=/tmp/checkpatch.results.diff-${suffix}
91diff -u ${known_checkpatch_issues} ${checkpatch_issues} > ${diff_file}
92
93if [ ${quiet} = n ]; then
94 cat ${diff_file}
95fi
96
97# find all lines that starts with '+' but not '+commit' or '+++ diff'
98minuses_err_str=(\
99 $(cat ${diff_file} | \
100 grep -v -E "^\-\-\-" | grep -v -E "^\-commit " | grep -E "^\-" | \
101 awk '{print $1}' | cut -d\- -f 2-) \
102)
103minuses_num_err=(\
104 $(cat ${diff_file} | \
105 grep -v -E "^\-\-\-" | grep -v -E "^\-commit " | grep -E "^\-" | \
106 awk '{print $2}') \
107)
108plusses_err_str=(\
109 $(cat ${diff_file} | \
110 grep -v -E "^\+\+\+" | grep -v -E "^\+commit " | grep -E "^\+" | \
111 awk '{print $1}' | cut -d\+ -f 2-) \
112)
113plusses_num_err=(\
114 $(cat ${diff_file} | \
115 grep -v -E "^\+\+\+" | grep -v -E "^\+commit " | grep -E "^\+" | \
116 awk '{print $2}') \
117)
118
119exit_code=0
120declare -i num_plusses=${#plusses_num_err[@]}
121declare -i num_minuses=${#minuses_num_err[@]}
122declare -i test_num=${num_plusses}
123while [ ${test_num} -gt 0 ]; do
124 test_num+=-1
125 match=n
126 declare -i i=${num_minuses}
127 while [ $i -gt 0 ]; do
128 i+=-1
129 if [ ${plusses_err_str[${test_num}]} = ${minuses_err_str[$i]} ]; then
130 n_minus=${minuses_num_err[$i]}
131 n_plus=${plusses_num_err[${test_num}]}
132 if [ ${n_plus} -gt ${n_minus} ]; then
133 exit_code=1
134 break 2
135 fi
136 match=y
137 break 1
138 fi
139 done
140
141 if [ ${match} = n ]; then
142 # there was no match for the plus line, so that is a new error
143 exit_code=1
144 break 1
145 fi
146done
147
148if [ ${update} = y ]; then
149 msg="known_checkpatch_issues: updating to ${commit_id_str}"
150 cp ${checkpatch_issues} ${known_checkpatch_issues}
151 git add ${known_checkpatch_issues}
152 git commit -m "${msg}"
153fi
154
155exit ${exit_code}