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