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