Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 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 | |
| 12 | DIR=$1 |
Manojkiran Eda | 87111bb | 2021-08-14 11:26:16 +0530 | [diff] [blame^] | 13 | WORKSPACE=$PWD |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 14 | |
Andrew Jeffery | 0cce848 | 2018-04-30 11:37:01 +0930 | [diff] [blame] | 15 | set -e |
| 16 | |
Manojkiran Eda | e6f120a | 2021-03-20 11:13:43 +0530 | [diff] [blame] | 17 | echo "Running spelling check on Commit Message" |
| 18 | |
| 19 | # Run the codespell with openbmc spcific spellings on the patchset |
| 20 | echo "openbmc-dictionary - misspelling count >> " |
| 21 | codespell -D openbmc-spelling.txt -d --count "${DIR}"/.git/COMMIT_EDITMSG |
| 22 | |
| 23 | # Run the codespell with generic dictionary on the patchset |
| 24 | echo "generic-dictionary - misspelling count >> " |
| 25 | codespell --builtin clear,rare,en-GB_to_en-US -d --count "${DIR}"/.git/COMMIT_EDITMSG |
| 26 | |
| 27 | cd "${DIR}" |
| 28 | |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 29 | echo "Formatting code under $DIR/" |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 30 | |
Manojkiran Eda | 87111bb | 2021-08-14 11:26:16 +0530 | [diff] [blame^] | 31 | ESLINT_CONFIG="eslint ${DIR} --no-eslintrc -c ${WORKSPACE}/eslint-global-config.json \ |
| 32 | --ext .json --resolve-plugins-relative-to /usr/local/lib/node_modules\ |
| 33 | --no-error-on-unmatched-pattern" |
| 34 | ESLINT_IGNORE=" --ignore-path ${DIR}/.eslintignore" |
| 35 | |
| 36 | # Get the eslint configuration from the repository |
| 37 | if [[ -f ".eslintrc.json" ]] || [[ -f ".eslintignore" ]]; then |
| 38 | echo "Running the json validator on the repo using it's config > " |
| 39 | if [[ ! -f ".eslintrc.json" ]]; then |
| 40 | eslint_command="${ESLINT_CONFIG} ${ESLINT_IGNORE}" |
| 41 | else |
| 42 | eslint_command="$ESLINT_CONFIG" |
| 43 | fi |
| 44 | else |
| 45 | echo "Running the json validator on the repo using the global config > " |
| 46 | eslint_command="$ESLINT_CONFIG" |
| 47 | fi |
| 48 | |
| 49 | # Print eslint command |
| 50 | echo "$eslint_command" |
| 51 | # Run eslint |
| 52 | $eslint_command |
| 53 | |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 54 | if [[ -f "setup.cfg" ]]; then |
Patrick Williams | 481a507 | 2021-04-22 12:16:25 -0500 | [diff] [blame] | 55 | pycodestyle --show-source --exclude=subprojects . |
Adriana Kobylak | bcee22b | 2018-01-10 16:58:27 -0600 | [diff] [blame] | 56 | rc=$? |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 57 | if [[ ${rc} -ne 0 ]]; then |
Adriana Kobylak | bcee22b | 2018-01-10 16:58:27 -0600 | [diff] [blame] | 58 | exit ${rc} |
| 59 | fi |
| 60 | fi |
| 61 | |
Patrick Williams | 01ced28 | 2020-07-17 15:10:15 -0500 | [diff] [blame] | 62 | # If .shellcheck exists, stop on error. Otherwise, allow pass. |
| 63 | if [[ -f ".shellcheck" ]]; then |
| 64 | shellcheck_allowfail="false" |
| 65 | else |
| 66 | shellcheck_allowfail="true" |
| 67 | fi |
| 68 | |
| 69 | # Run shellcheck on any shell-script. |
| 70 | shell_scripts="$(git ls-files | xargs -n1 file -0 | \ |
| 71 | grep -a "shell script" | cut -d '' -f 1)" |
| 72 | for script in ${shell_scripts}; do |
| 73 | shellcheck -x "${script}" || ${shellcheck_allowfail} |
| 74 | done |
| 75 | |
William A. Kennington III | 078c3b5 | 2018-10-04 19:44:31 -0700 | [diff] [blame] | 76 | # Allow called scripts to know which clang format we are using |
Patrick Williams | d83ca9a | 2021-02-22 14:29:21 -0600 | [diff] [blame] | 77 | export CLANG_FORMAT="clang-format" |
Patrick Venture | 366dd76 | 2018-10-22 13:55:03 -0700 | [diff] [blame] | 78 | IGNORE_FILE=".clang-ignore" |
| 79 | declare -a IGNORE_LIST |
| 80 | |
| 81 | if [[ -f "${IGNORE_FILE}" ]]; then |
| 82 | readarray -t IGNORE_LIST < "${IGNORE_FILE}" |
| 83 | fi |
| 84 | |
| 85 | ignorepaths="" |
| 86 | ignorefiles="" |
| 87 | |
| 88 | for path in "${IGNORE_LIST[@]}"; do |
| 89 | # Check for comment, line starting with space, or zero-length string. |
| 90 | # Checking for [[:space:]] checks all options. |
| 91 | if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then |
| 92 | continue |
| 93 | fi |
| 94 | |
| 95 | # All paths must start with ./ for find's path prune expectation. |
| 96 | if [[ "${path}" =~ ^\.\/.+$ ]]; then |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 97 | ignorepaths+=" ${path}" |
Patrick Venture | 366dd76 | 2018-10-22 13:55:03 -0700 | [diff] [blame] | 98 | else |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 99 | ignorefiles+=" ${path}" |
Patrick Venture | 366dd76 | 2018-10-22 13:55:03 -0700 | [diff] [blame] | 100 | fi |
| 101 | done |
William A. Kennington III | 078c3b5 | 2018-10-04 19:44:31 -0700 | [diff] [blame] | 102 | |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 103 | searchfiles="" |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 104 | while read -r path; do |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 105 | # skip ignorefiles |
| 106 | if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then |
| 107 | continue |
| 108 | fi |
| 109 | |
| 110 | skip=false |
| 111 | #skip paths in ingorepaths |
| 112 | for pathname in $ignorepaths; do |
| 113 | if [[ "./${path}" == "${pathname}"* ]]; then |
| 114 | skip=true |
| 115 | break |
| 116 | fi |
| 117 | done |
| 118 | |
| 119 | if [ "$skip" = true ]; then |
| 120 | continue |
| 121 | fi |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 122 | # shellcheck disable=2089 |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 123 | searchfiles+="\"./${path}\" " |
| 124 | |
| 125 | # Get C and C++ files managed by git and skip the mako files |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 126 | done <<<"$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')" |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 127 | |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 128 | if [[ -f ".clang-format" ]]; then |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 129 | # shellcheck disable=SC2090 disable=SC2086 |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 130 | echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i |
Adriana Kobylak | bcee22b | 2018-01-10 16:58:27 -0600 | [diff] [blame] | 131 | git --no-pager diff --exit-code |
| 132 | fi |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 133 | |
| 134 | # Sometimes your situation is terrible enough that you need the flexibility. |
| 135 | # For example, phosphor-mboxd. |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 136 | if [[ -f "format-code.sh" ]]; then |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 137 | ./format-code.sh |
| 138 | git --no-pager diff --exit-code |
| 139 | fi |