Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 1 | #!/bin/bash |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 2 | set -e |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 3 | |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 4 | # This script reformats source files using various formatters and linters. |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 5 | # |
| 6 | # Files are changed in-place, so make sure you don't have anything open in an |
| 7 | # editor, and you may want to commit before formatting in case of awryness. |
| 8 | # |
| 9 | # This must be run on a clean repository to succeed |
| 10 | # |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 11 | function display_help() |
| 12 | { |
| 13 | echo "usage: format-code.sh [-h | --help] " |
| 14 | echo " [<path>]" |
| 15 | echo |
| 16 | echo "Format and lint a repository." |
| 17 | echo |
| 18 | echo "Arguments:" |
| 19 | echo " path Path to git repository (default to pwd)" |
| 20 | } |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 21 | |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 22 | eval set -- "$(getopt -o 'h' --long 'help' -n 'format-code.sh' -- "$@")" |
| 23 | while true; do |
| 24 | case "$1" in |
| 25 | '-h'|'--help') |
| 26 | display_help && exit 0 |
| 27 | ;; |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 28 | |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 29 | '--') |
| 30 | shift |
| 31 | break |
| 32 | ;; |
| 33 | |
| 34 | *) |
| 35 | echo "unknown option: $1" |
| 36 | display_help && exit 1 |
| 37 | ;; |
| 38 | esac |
| 39 | done |
| 40 | |
| 41 | # Detect tty and set nicer colors. |
| 42 | if [ -t 1 ]; then |
| 43 | BLUE="\e[34m" |
| 44 | GREEN="\e[32m" |
| 45 | NORMAL="\e[0m" |
| 46 | RED="\e[31m" |
| 47 | YELLOW="\e[33m" |
| 48 | else # non-tty, no escapes. |
| 49 | BLUE="" |
| 50 | GREEN="" |
| 51 | NORMAL="" |
| 52 | RED="" |
| 53 | YELLOW="" |
| 54 | fi |
| 55 | |
| 56 | # Path to default config files for linters. |
| 57 | CONFIG_PATH="$(git -C "$(dirname "${BASH_SOURCE[0]}")" rev-parse --show-toplevel)/config" |
| 58 | |
| 59 | # Find repository root for `pwd` or $1. |
| 60 | if [ -z "$1" ]; then |
| 61 | DIR="$(git rev-parse --show-toplevel || pwd)" |
| 62 | else |
| 63 | DIR="$(git -C "$1" rev-parse --show-toplevel)" |
| 64 | fi |
| 65 | if [ ! -d "$DIR/.git" ]; then |
| 66 | echo "${RED}Error:${NORMAL} Directory ($DIR) does not appear to be a git repository" |
| 67 | exit 1 |
| 68 | fi |
Andrew Jeffery | 0cce848 | 2018-04-30 11:37:01 +0930 | [diff] [blame] | 69 | |
Manojkiran Eda | e6f120a | 2021-03-20 11:13:43 +0530 | [diff] [blame] | 70 | cd "${DIR}" |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 71 | echo -e " ${BLUE}Formatting code under${NORMAL} $DIR" |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 72 | |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 73 | ALL_OPERATIONS=( \ |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 74 | commit_gitlint \ |
| 75 | commit_spelling \ |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 76 | clang_format \ |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 77 | eslint \ |
| 78 | pycodestyle \ |
| 79 | shellcheck \ |
| 80 | ) |
Manojkiran Eda | 87111bb | 2021-08-14 11:26:16 +0530 | [diff] [blame] | 81 | |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 82 | function do_commit_spelling() { |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 83 | if [ ! -e .git/COMMIT_EDITMSG ]; then |
| 84 | return |
| 85 | fi |
| 86 | echo -e " ${BLUE}Running codespell${NORMAL}" |
Ed Tanous | ac5915f | 2022-03-10 08:32:54 -0800 | [diff] [blame] | 87 | |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 88 | # Run the codespell with openbmc spcific spellings on the patchset |
| 89 | echo "openbmc-dictionary - misspelling count >> " |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 90 | sed "s/Signed-off-by.*//" .git/COMMIT_EDITMSG | \ |
| 91 | codespell -D "${CONFIG_PATH}/openbmc-spelling.txt" -d --count - |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 92 | |
| 93 | # Run the codespell with generic dictionary on the patchset |
| 94 | echo "generic-dictionary - misspelling count >> " |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 95 | sed "s/Signed-off-by.*//" .git/COMMIT_EDITMSG | \ |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 96 | codespell --builtin clear,rare,en-GB_to_en-US -d --count - |
| 97 | } |
| 98 | |
| 99 | function do_commit_gitlint() { |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 100 | echo -e " ${BLUE}Running gitlint${NORMAL}" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 101 | # Check for commit message issues |
| 102 | gitlint \ |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 103 | --extra-path "${CONFIG_PATH}/gitlint/" \ |
| 104 | --config "${CONFIG_PATH}/.gitlint" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | function do_eslint() { |
| 108 | if [[ -f ".eslintignore" ]]; then |
| 109 | ESLINT_IGNORE="--ignore-path .eslintignore" |
| 110 | elif [[ -f ".gitignore" ]]; then |
| 111 | ESLINT_IGNORE="--ignore-path .gitignore" |
| 112 | fi |
| 113 | |
| 114 | # Get the eslint configuration from the repository |
| 115 | if [[ -f ".eslintrc.json" ]]; then |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 116 | echo -e " ${BLUE}Running eslint${NORMAL}" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 117 | ESLINT_RC="-c .eslintrc.json" |
| 118 | else |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 119 | echo -e " ${BLUE}Running eslint using ${YELLOW}the global config${NORMAL}" |
| 120 | ESLINT_RC="--no-eslintrc -c ${CONFIG_PATH}/eslint-global-config.json" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 121 | fi |
| 122 | |
| 123 | ESLINT_COMMAND="eslint . ${ESLINT_IGNORE} ${ESLINT_RC} \ |
Ed Tanous | 0c4153b | 2022-08-17 10:20:26 -0700 | [diff] [blame] | 124 | --ext .json --format=stylish \ |
Ed Tanous | ac5915f | 2022-03-10 08:32:54 -0800 | [diff] [blame] | 125 | --resolve-plugins-relative-to /usr/local/lib/node_modules \ |
| 126 | --no-error-on-unmatched-pattern" |
| 127 | |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 128 | # Print eslint command |
| 129 | echo "$ESLINT_COMMAND" |
| 130 | # Run eslint |
| 131 | $ESLINT_COMMAND |
| 132 | } |
Manojkiran Eda | 87111bb | 2021-08-14 11:26:16 +0530 | [diff] [blame] | 133 | |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 134 | function do_pycodestyle() { |
| 135 | if [[ -f "setup.cfg" ]]; then |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 136 | echo -e " ${BLUE}Running pycodestyle${NORMAL}" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 137 | pycodestyle --show-source --exclude=subprojects . |
| 138 | rc=$? |
| 139 | if [[ ${rc} -ne 0 ]]; then |
| 140 | exit ${rc} |
| 141 | fi |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 142 | fi |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 143 | } |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 144 | |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 145 | function do_shellcheck() { |
| 146 | # If .shellcheck exists, stop on error. Otherwise, allow pass. |
| 147 | if [[ -f ".shellcheck" ]]; then |
| 148 | local shellcheck_allowfail="false" |
| 149 | else |
| 150 | local shellcheck_allowfail="true" |
| 151 | fi |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 152 | |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 153 | # Run shellcheck on any shell-script. |
| 154 | shell_scripts="$(git ls-files | xargs -n1 file -0 | \ |
| 155 | grep -a "shell script" | cut -d '' -f 1)" |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 156 | if [ -n "${shell_scripts}" ]; then |
| 157 | echo -e " ${BLUE}Running shellcheck${NORMAL}" |
| 158 | fi |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 159 | for script in ${shell_scripts}; do |
| 160 | shellcheck --color=never -x "${script}" || ${shellcheck_allowfail} |
| 161 | done |
| 162 | } |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 163 | |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 164 | |
| 165 | do_clang_format() { |
| 166 | # Allow called scripts to know which clang format we are using |
| 167 | export CLANG_FORMAT="clang-format" |
| 168 | IGNORE_FILE=".clang-ignore" |
| 169 | declare -a IGNORE_LIST |
| 170 | |
| 171 | if [[ -f "${IGNORE_FILE}" ]]; then |
| 172 | readarray -t IGNORE_LIST < "${IGNORE_FILE}" |
| 173 | fi |
| 174 | |
| 175 | ignorepaths="" |
| 176 | ignorefiles="" |
| 177 | |
| 178 | for path in "${IGNORE_LIST[@]}"; do |
| 179 | # Check for comment, line starting with space, or zero-length string. |
| 180 | # Checking for [[:space:]] checks all options. |
| 181 | if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then |
| 182 | continue |
| 183 | fi |
| 184 | |
| 185 | # All paths must start with ./ for find's path prune expectation. |
| 186 | if [[ "${path}" =~ ^\.\/.+$ ]]; then |
| 187 | ignorepaths+=" ${path}" |
| 188 | else |
| 189 | ignorefiles+=" ${path}" |
| 190 | fi |
| 191 | done |
| 192 | |
| 193 | searchfiles="" |
| 194 | while read -r path; do |
| 195 | # skip ignorefiles |
| 196 | if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then |
| 197 | continue |
| 198 | fi |
| 199 | |
| 200 | skip=false |
| 201 | #skip paths in ingorepaths |
| 202 | for pathname in $ignorepaths; do |
| 203 | if [[ "./${path}" == "${pathname}"* ]]; then |
| 204 | skip=true |
| 205 | break |
| 206 | fi |
| 207 | done |
| 208 | |
| 209 | if [ "$skip" = true ]; then |
| 210 | continue |
| 211 | fi |
| 212 | # shellcheck disable=2089 |
| 213 | searchfiles+="\"./${path}\" " |
| 214 | |
| 215 | # Get C and C++ files managed by git and skip the mako files |
| 216 | done <<<"$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')" |
| 217 | |
| 218 | if [[ -f ".clang-format" ]]; then |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 219 | echo -e " ${BLUE}Running clang-format${NORMAL}" |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 220 | # shellcheck disable=SC2090 disable=SC2086 |
| 221 | echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 222 | fi |
| 223 | |
| 224 | } |
| 225 | |
| 226 | for op in "${ALL_OPERATIONS[@]}"; do |
| 227 | "do_$op" |
| 228 | done |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 229 | |
Patrick Williams | c1513c8 | 2022-11-27 17:37:08 -0600 | [diff] [blame^] | 230 | echo -e " ${BLUE}Result differences...${NORMAL}" |
| 231 | if ! git --no-pager diff --exit-code ; then |
| 232 | echo -e "Format: ${RED}FAILED${NORMAL}" |
| 233 | exit 1 |
| 234 | else |
| 235 | echo -e "Format: ${GREEN}PASSED${NORMAL}" |
| 236 | fi |
| 237 | |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 238 | # Sometimes your situation is terrible enough that you need the flexibility. |
| 239 | # For example, phosphor-mboxd. |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 240 | if [[ -f "format-code.sh" ]]; then |
Patrick Williams | 38515a3 | 2022-11-27 06:58:56 -0600 | [diff] [blame] | 241 | ./format-code.sh |
| 242 | git --no-pager diff --exit-code |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 243 | fi |