1#!/bin/bash 2 3# This script reformats source files using the clang-format utility. 4# 5# Files are changed in-place, so make sure you don't have anything open in an 6# editor, and you may want to commit before formatting in case of awryness. 7# 8# This must be run on a clean repository to succeed 9# 10# Input parmameter must be full path to git repo to scan 11 12DIR=$1 13WORKSPACE=$PWD 14 15set -e 16 17echo "Running spelling check on Commit Message" 18 19# Run the codespell with openbmc spcific spellings on the patchset 20echo "openbmc-dictionary - misspelling count >> " 21codespell -D openbmc-spelling.txt -d --count "${DIR}"/.git/COMMIT_EDITMSG 22 23# Run the codespell with generic dictionary on the patchset 24echo "generic-dictionary - misspelling count >> " 25codespell --builtin clear,rare,en-GB_to_en-US -d --count \ 26 --ignore-words=openbmc-spelling-ignore.txt \ 27 "${DIR}"/.git/COMMIT_EDITMSG 28 29# Check for commit message issues 30gitlint \ 31 --target "${DIR}" \ 32 --extra-path "${WORKSPACE}/openbmc-build-scripts/config/gitlint/" \ 33 --config "${WORKSPACE}/openbmc-build-scripts/config/.gitlint" 34 35cd "${DIR}" 36 37echo "Formatting code under $DIR/" 38 39if [[ -f ".eslintignore" ]]; then 40 ESLINT_IGNORE="--ignore-path .eslintignore" 41elif [[ -f ".gitignore" ]]; then 42 ESLINT_IGNORE="--ignore-path .gitignore" 43fi 44 45# Get the eslint configuration from the repository 46if [[ -f ".eslintrc.json" ]]; then 47 echo "Running the json validator on the repo using it's config > " 48 ESLINT_RC="-c .eslintrc.json" 49else 50 echo "Running the json validator on the repo using the global config" 51 ESLINT_RC="--no-eslintrc -c ${WORKSPACE}/eslint-global-config.json" 52fi 53 54ESLINT_COMMAND="eslint . ${ESLINT_IGNORE} ${ESLINT_RC} \ 55 --ext .json --format=stylish \ 56 --resolve-plugins-relative-to /usr/local/lib/node_modules \ 57 --no-error-on-unmatched-pattern" 58 59# Print eslint command 60echo "$ESLINT_COMMAND" 61# Run eslint 62$ESLINT_COMMAND 63 64if [[ -f "setup.cfg" ]]; then 65 pycodestyle --show-source --exclude=subprojects . 66 rc=$? 67 if [[ ${rc} -ne 0 ]]; then 68 exit ${rc} 69 fi 70fi 71 72# If .shellcheck exists, stop on error. Otherwise, allow pass. 73if [[ -f ".shellcheck" ]]; then 74 shellcheck_allowfail="false" 75else 76 shellcheck_allowfail="true" 77fi 78 79# Run shellcheck on any shell-script. 80shell_scripts="$(git ls-files | xargs -n1 file -0 | \ 81 grep -a "shell script" | cut -d '' -f 1)" 82for script in ${shell_scripts}; do 83 shellcheck --color=never -x "${script}" || ${shellcheck_allowfail} 84done 85 86# Allow called scripts to know which clang format we are using 87export CLANG_FORMAT="clang-format" 88IGNORE_FILE=".clang-ignore" 89declare -a IGNORE_LIST 90 91if [[ -f "${IGNORE_FILE}" ]]; then 92 readarray -t IGNORE_LIST < "${IGNORE_FILE}" 93fi 94 95ignorepaths="" 96ignorefiles="" 97 98for path in "${IGNORE_LIST[@]}"; do 99 # Check for comment, line starting with space, or zero-length string. 100 # Checking for [[:space:]] checks all options. 101 if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then 102 continue 103 fi 104 105 # All paths must start with ./ for find's path prune expectation. 106 if [[ "${path}" =~ ^\.\/.+$ ]]; then 107 ignorepaths+=" ${path}" 108 else 109 ignorefiles+=" ${path}" 110 fi 111done 112 113searchfiles="" 114while read -r path; do 115 # skip ignorefiles 116 if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then 117 continue 118 fi 119 120 skip=false 121 #skip paths in ingorepaths 122 for pathname in $ignorepaths; do 123 if [[ "./${path}" == "${pathname}"* ]]; then 124 skip=true 125 break 126 fi 127 done 128 129 if [ "$skip" = true ]; then 130 continue 131 fi 132 # shellcheck disable=2089 133 searchfiles+="\"./${path}\" " 134 135# Get C and C++ files managed by git and skip the mako files 136done <<<"$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')" 137 138if [[ -f ".clang-format" ]]; then 139 # shellcheck disable=SC2090 disable=SC2086 140 echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i 141 git --no-pager diff --exit-code 142fi 143 144# Sometimes your situation is terrible enough that you need the flexibility. 145# For example, phosphor-mboxd. 146if [[ -f "format-code.sh" ]]; then 147 ./format-code.sh 148 git --no-pager diff --exit-code 149fi 150