|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. |
| 3 | +# For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md |
| 4 | + |
| 5 | + |
| 6 | +SCRIPT=$(readlink -f $0) |
| 7 | +SCRIPT_DIR="$(dirname ${SCRIPT})" |
| 8 | +REPO_ROOT="${SCRIPT_DIR%/*}" |
| 9 | + |
| 10 | +#. ${SCRIPT_DIR}/bootstrap_config |
| 11 | +. ${SCRIPT_DIR}/common_scripsts_lib |
| 12 | +. ${SCRIPT_DIR}/format-config.sh |
| 13 | + |
| 14 | +#printVar SCRIPT |
| 15 | +#printVar SCRIPT_DIR |
| 16 | +#printVar REPO_ROOT |
| 17 | + |
| 18 | +pushd ${REPO_ROOT} >> /dev/null |
| 19 | + |
| 20 | +LICENSE1="Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved." |
| 21 | +LICENSE2="For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md" |
| 22 | + |
| 23 | +CHECK_ONLY="false" |
| 24 | +EXIT_CODE=0 |
| 25 | + |
| 26 | +#file extension; comment;checker/replacer function |
| 27 | +declare -A FileTypes=( |
| 28 | + ["header"]="hpp;cppChecker;" |
| 29 | + ["source"]="cpp;cppChecker;" |
| 30 | + ["python"]="py;pythonChecker;" |
| 31 | + ["shell"]="sh;bashChecker;" |
| 32 | + ) |
| 33 | +declare -a paths_to_ignore |
| 34 | + |
| 35 | + |
| 36 | +function cppChecker(){ |
| 37 | + FILE=$1 |
| 38 | + LICENSE_LINE1="//${LICENSE1}" |
| 39 | + LICENSE_LINE2="//${LICENSE2}" |
| 40 | + |
| 41 | + CHECK_LICENSE=$(head -n5 ${FILE} | grep "${LICENSE1}") |
| 42 | + if [[ -z ${CHECK_LICENSE} ]]; then |
| 43 | + if [[ ${CHECK_ONLY} == "true" ]]; then |
| 44 | + echo -e "${ERROR}" |
| 45 | + EXIT_CODE=1 |
| 46 | + else |
| 47 | + sed -i "1i${LICENSE_LINE1}" ${FILE} |
| 48 | + sed -i "2i${LICENSE_LINE2}" ${FILE} |
| 49 | + echo -e "${FIXED}" |
| 50 | + fi |
| 51 | + else |
| 52 | + echo -e "${OK}" |
| 53 | + fi |
| 54 | +} |
| 55 | + |
| 56 | +function scriptChecker(){ |
| 57 | + CHECK_LICENSE=$( head -n5 ${FILE} | grep "${LICENSE1}") |
| 58 | + if [[ -z ${CHECK_LICENSE} ]]; then |
| 59 | + if [[ ${CHECK_ONLY} == "true" ]]; then |
| 60 | + echo -e "${ERROR}" |
| 61 | + EXIT_CODE=1 |
| 62 | + else |
| 63 | + FIST_LINE=$(head -n1 ${FILE}|grep "$SHA_BANG_EXEC") |
| 64 | + if [[ -n "${FIST_LINE}" ]]; then |
| 65 | + sed -i "2i${LICENSE_LINE1}" ${FILE} |
| 66 | + sed -i "3i${LICENSE_LINE2}" ${FILE} |
| 67 | + else |
| 68 | + sed -i "1i${LICENSE_LINE1}" ${FILE} |
| 69 | + sed -i "2i${LICENSE_LINE2}" ${FILE} |
| 70 | + fi |
| 71 | + echo -e "${FIXED}" |
| 72 | + fi |
| 73 | + else |
| 74 | + echo -e "${OK}" |
| 75 | + fi |
| 76 | + |
| 77 | +} |
| 78 | +function pythonChecker(){ |
| 79 | + FILE=$1 |
| 80 | + LICENSE_LINE1="#${LICENSE1}" |
| 81 | + LICENSE_LINE2="#${LICENSE2}" |
| 82 | + SHA_BANG_EXEC="python" |
| 83 | + scriptChecker |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +function bashChecker(){ |
| 88 | + FILE=$1 |
| 89 | + LICENSE_LINE1="#${LICENSE1}" |
| 90 | + LICENSE_LINE2="#${LICENSE2}" |
| 91 | + SHA_BANG_EXEC="bash" |
| 92 | + scriptChecker |
| 93 | +} |
| 94 | + |
| 95 | +SUBMODULES=( $(git submodule | cut -f3 -d" " ) ) |
| 96 | + |
| 97 | +function extractValues() { |
| 98 | + KEY=$1 |
| 99 | + |
| 100 | + readarray -td ';' DATA <<< ${FileTypes[${KEY}]} |
| 101 | + |
| 102 | + FILE_EXT=${DATA[0]} |
| 103 | + CHECK_FUNCTION=${DATA[1]} |
| 104 | +} |
| 105 | + |
| 106 | +function excludePathFromFind(){ |
| 107 | + # ignore submodule paths |
| 108 | + I=0 |
| 109 | + SUB_COUNT=${#SUBMODULES[@]} |
| 110 | + while [[ $I -lt ${SUB_COUNT} ]]; |
| 111 | + do |
| 112 | + module=${SUBMODULES[$I]} |
| 113 | + NOT_PATH="${NOT_PATH} -not ( -path ./${module} -prune )" |
| 114 | + I=$(( $I + 1)) |
| 115 | + done |
| 116 | + |
| 117 | + # ignore paths excluded from format |
| 118 | + I=0 |
| 119 | + IGNORE_PATHS_COUNT=${#ignore_paths[@]} |
| 120 | + while [[ $I -lt ${IGNORE_PATHS_COUNT} ]] |
| 121 | + do |
| 122 | + path=${ignore_paths[$I]} |
| 123 | + if [[ -d ${path} ]]; then |
| 124 | + NOT_PATH="${NOT_PATH} -not ( -path ${path%*/} -prune )" |
| 125 | + else |
| 126 | + paths_to_ignore+=("${path}") |
| 127 | + fi |
| 128 | + I=$(( $I + 1 )) |
| 129 | + done |
| 130 | + declare -p paths_to_ignore |
| 131 | + |
| 132 | +} |
| 133 | + |
| 134 | +function findFiles() { |
| 135 | + # find files except submodule directories |
| 136 | + echo "find . ${NOT_PATH} -iname \"*.${FILE_EXT}\" -print0 " |
| 137 | + readarray -d '' FILES_TO_CHECK < <(find . ${NOT_PATH} -iname "*.${FILE_EXT}" -print0) |
| 138 | +} |
| 139 | + |
| 140 | +function shouldnt_ignore() { |
| 141 | + FILE=$1 |
| 142 | + for el in ${paths_to_ignore[@]}; do |
| 143 | + if [[ ${FILE} =~ ^${el}.* ]]; then |
| 144 | + echo -e "${ORANGE}Ignore: ${FILE} checking due to: $el match!\e[0m" |
| 145 | + return 1 |
| 146 | + fi |
| 147 | + done |
| 148 | + return 0 |
| 149 | +} |
| 150 | + |
| 151 | +function help() { |
| 152 | + cat <<END |
| 153 | + check and update header files with copyright notice |
| 154 | + ussage: |
| 155 | + $0 [--help | --check-only] |
| 156 | + $0 --hook [ --check-only] |
| 157 | + Run as git "pre-commith.hook", checks against files in stash |
| 158 | + $0 --ci |
| 159 | + Run on CI, check files that are new to the current master |
| 160 | + |
| 161 | + --help - this help message |
| 162 | + --check-only - only checks, do not change a file |
| 163 | +
|
| 164 | +END |
| 165 | + exit 0 |
| 166 | +} |
| 167 | + |
| 168 | +function hookMain() { |
| 169 | + parseArgs $@ |
| 170 | + readarray -d '' FILES_TO_CHECK < <( ${GET_FILES_CMD} ) |
| 171 | + |
| 172 | + for FILE_TYPE in "${!FileTypes[@]}" |
| 173 | + do |
| 174 | + echo "=== ${FILE_TYPE} ===" |
| 175 | + extractValues ${FILE_TYPE} |
| 176 | + I=0 |
| 177 | + while [[ $I -lt ${#FILES_TO_CHECK[@]} ]] |
| 178 | + do |
| 179 | + FILE=${FILES_TO_CHECK[$I]} |
| 180 | + if shouldnt_ignore ${FILE}; then |
| 181 | + if [[ ${FILE} =~ .*\.${FILE_EXT}$ ]]; then |
| 182 | + echo -en "${FILE}:" |
| 183 | + ${CHECK_FUNCTION} ${FILE} |
| 184 | + git add ${FILE} |
| 185 | + fi |
| 186 | + fi |
| 187 | + I=$(( $I + 1 )) |
| 188 | + done |
| 189 | + done |
| 190 | + echo "Hook ${EXIT_CODE}" |
| 191 | + exit ${EXIT_CODE} |
| 192 | +} |
| 193 | + |
| 194 | +function standAloneMain() { |
| 195 | + NOT_PATH="" |
| 196 | + excludePathFromFind |
| 197 | + |
| 198 | + for FILE_TYPE in "${!FileTypes[@]}" |
| 199 | + do |
| 200 | + echo "=== ${FILE_TYPE} ===" |
| 201 | + extractValues ${FILE_TYPE} |
| 202 | + findFiles |
| 203 | + I=0 |
| 204 | + while [[ $I -lt ${#FILES_TO_CHECK[@]} ]] |
| 205 | + do |
| 206 | + FILE=${FILES_TO_CHECK[$I]} |
| 207 | + if shouldnt_ignore ${FILE}; then |
| 208 | + echo -en "${I}/${#FILES_TO_CHECK[@]}:{${FILE}: " |
| 209 | + ${CHECK_FUNCTION} ${FILE} |
| 210 | + fi |
| 211 | + I=$(( $I + 1 )) |
| 212 | + done |
| 213 | + done |
| 214 | + echo "StandAlone: ${EXIT_CODE}" |
| 215 | + exit ${EXIT_CODE} |
| 216 | +} |
| 217 | + |
| 218 | +function parseArgs() { |
| 219 | + if [[ $# -ge 1 ]]; then |
| 220 | + case $1 in |
| 221 | + "--help") |
| 222 | + help |
| 223 | + ;; |
| 224 | + "--check-only") |
| 225 | + CHECK_ONLY=true |
| 226 | + ;; |
| 227 | + "--hook") |
| 228 | + GET_FILES_CMD="git diff --cached --name-only --diff-filter=A -z ${against:-HEAD}" |
| 229 | + shift |
| 230 | + hookMain $@ |
| 231 | + ;; |
| 232 | + "--ci") |
| 233 | + shift |
| 234 | + CHECK_ONLY=true |
| 235 | + GET_FILES_CMD="git diff --name-only --diff-filter=A -z remotes/origin/master...HEAD" |
| 236 | + hookMain $@ |
| 237 | + ;; |
| 238 | + *) |
| 239 | + ;; |
| 240 | + esac |
| 241 | + fi |
| 242 | +} |
| 243 | +parseArgs $@ |
| 244 | +standAloneMain |
0 commit comments