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 13 14set -e 15 16echo "Running spelling check on Commit Message" 17 18# Run the codespell with openbmc spcific spellings on the patchset 19echo "openbmc-dictionary - misspelling count >> " 20codespell -D openbmc-spelling.txt -d --count "${DIR}"/.git/COMMIT_EDITMSG 21 22# Run the codespell with generic dictionary on the patchset 23echo "generic-dictionary - misspelling count >> " 24codespell --builtin clear,rare,en-GB_to_en-US -d --count "${DIR}"/.git/COMMIT_EDITMSG 25 26cd "${DIR}" 27 28echo "Formatting code under $DIR/" 29 30if [[ -f "setup.cfg" ]]; then 31 pycodestyle --show-source --exclude=subprojects . 32 rc=$? 33 if [[ ${rc} -ne 0 ]]; then 34 exit ${rc} 35 fi 36fi 37 38# If .shellcheck exists, stop on error. Otherwise, allow pass. 39if [[ -f ".shellcheck" ]]; then 40 shellcheck_allowfail="false" 41else 42 shellcheck_allowfail="true" 43fi 44 45# Run shellcheck on any shell-script. 46shell_scripts="$(git ls-files | xargs -n1 file -0 | \ 47 grep -a "shell script" | cut -d '' -f 1)" 48for script in ${shell_scripts}; do 49 shellcheck -x "${script}" || ${shellcheck_allowfail} 50done 51 52# Allow called scripts to know which clang format we are using 53export CLANG_FORMAT="clang-format" 54IGNORE_FILE=".clang-ignore" 55declare -a IGNORE_LIST 56 57if [[ -f "${IGNORE_FILE}" ]]; then 58 readarray -t IGNORE_LIST < "${IGNORE_FILE}" 59fi 60 61ignorepaths="" 62ignorefiles="" 63 64for path in "${IGNORE_LIST[@]}"; do 65 # Check for comment, line starting with space, or zero-length string. 66 # Checking for [[:space:]] checks all options. 67 if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then 68 continue 69 fi 70 71 # All paths must start with ./ for find's path prune expectation. 72 if [[ "${path}" =~ ^\.\/.+$ ]]; then 73 ignorepaths+=" ${path}" 74 else 75 ignorefiles+=" ${path}" 76 fi 77done 78 79searchfiles="" 80while read -r path; do 81 # skip ignorefiles 82 if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then 83 continue 84 fi 85 86 skip=false 87 #skip paths in ingorepaths 88 for pathname in $ignorepaths; do 89 if [[ "./${path}" == "${pathname}"* ]]; then 90 skip=true 91 break 92 fi 93 done 94 95 if [ "$skip" = true ]; then 96 continue 97 fi 98 # shellcheck disable=2089 99 searchfiles+="\"./${path}\" " 100 101# Get C and C++ files managed by git and skip the mako files 102done <<<"$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')" 103 104if [[ -f ".clang-format" ]]; then 105 # shellcheck disable=SC2090 disable=SC2086 106 echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i 107 git --no-pager diff --exit-code 108fi 109 110# Sometimes your situation is terrible enough that you need the flexibility. 111# For example, phosphor-mboxd. 112if [[ -f "format-code.sh" ]]; then 113 ./format-code.sh 114 git --no-pager diff --exit-code 115fi 116