Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # |
| 4 | # Copyright (c) 2015 Intel Corporation. |
| 5 | # |
Javier B Perez Hernandez | f7fffae | 2015-10-06 11:00:37 -0500 | [diff] [blame] | 6 | # 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-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 9 | # |
Javier B Perez Hernandez | f7fffae | 2015-10-06 11:00:37 -0500 | [diff] [blame] | 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 11 | # |
Javier B Perez Hernandez | f7fffae | 2015-10-06 11:00:37 -0500 | [diff] [blame] | 12 | # 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-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 17 | # |
| 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 | |
| 26 | exe_name=$(basename $0) |
| 27 | |
Anas Nashif | b882377 | 2015-06-05 22:46:00 -0400 | [diff] [blame] | 28 | do_checkpatch_bin=${ZEPHYR_BASE}/scripts/do_checkpatch.sh |
| 29 | timestamp_bin=${ZEPHYR_BASE}/scripts/timestamp |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 30 | |
| 31 | declare update=n |
| 32 | declare quiet=n |
| 33 | |
| 34 | function usage { |
| 35 | printf "usage: %s [-u][-q]\n" ${exe_name} >&2 |
| 36 | } |
| 37 | |
| 38 | function fail { |
| 39 | usage |
| 40 | exit -1 |
| 41 | } |
| 42 | function 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 | |
| 56 | function 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 | |
| 68 | verify_needed |
| 69 | get_opts $@ |
| 70 | |
| 71 | do_checkpatch=${do_checkpatch_bin} |
| 72 | timestamp="${timestamp_bin} -u" |
| 73 | ts=$(${timestamp}) |
| 74 | uid=$(id -u) |
| 75 | pid=$$ |
| 76 | suffix=${uid}-${pid}-${ts} |
| 77 | checkpatch_results=/tmp/checkpatch.results-${suffix} |
Anas Nashif | b882377 | 2015-06-05 22:46:00 -0400 | [diff] [blame] | 78 | known_checkpatch_issues=${ZEPHYR_BASE}/scripts/known_checkpatch_issues |
Inaky Perez-Gonzalez | 8ddf82c | 2015-04-10 16:44:37 -0700 | [diff] [blame] | 79 | checkpatch_issues=/tmp/checkpatch_issues-${suffix} |
| 80 | git_log_params="\ |
| 81 | --abbrev=8 \ |
| 82 | --abbrev-commit \ |
| 83 | " |
| 84 | |
| 85 | commit_id_str=$(git log ${git_log_params} HEAD | head -n 1) |
| 86 | echo ${commit_id_str} > ${checkpatch_issues} |
| 87 | |
| 88 | ${do_checkpatch} ${checkpatch_results} >> ${checkpatch_issues} |
| 89 | |
| 90 | diff_file=/tmp/checkpatch.results.diff-${suffix} |
| 91 | diff -u ${known_checkpatch_issues} ${checkpatch_issues} > ${diff_file} |
| 92 | |
| 93 | if [ ${quiet} = n ]; then |
| 94 | cat ${diff_file} |
| 95 | fi |
| 96 | |
| 97 | # find all lines that starts with '+' but not '+commit' or '+++ diff' |
| 98 | minuses_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 | ) |
| 103 | minuses_num_err=(\ |
| 104 | $(cat ${diff_file} | \ |
| 105 | grep -v -E "^\-\-\-" | grep -v -E "^\-commit " | grep -E "^\-" | \ |
| 106 | awk '{print $2}') \ |
| 107 | ) |
| 108 | plusses_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 | ) |
| 113 | plusses_num_err=(\ |
| 114 | $(cat ${diff_file} | \ |
| 115 | grep -v -E "^\+\+\+" | grep -v -E "^\+commit " | grep -E "^\+" | \ |
| 116 | awk '{print $2}') \ |
| 117 | ) |
| 118 | |
| 119 | exit_code=0 |
| 120 | declare -i num_plusses=${#plusses_num_err[@]} |
| 121 | declare -i num_minuses=${#minuses_num_err[@]} |
| 122 | declare -i test_num=${num_plusses} |
| 123 | while [ ${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 |
| 146 | done |
| 147 | |
| 148 | if [ ${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}" |
| 153 | fi |
| 154 | |
| 155 | exit ${exit_code} |