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 |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 13 | |
Andrew Jeffery | 0cce848 | 2018-04-30 11:37:01 +0930 | [diff] [blame] | 14 | set -e |
| 15 | |
Manojkiran Eda | e6f120a | 2021-03-20 11:13:43 +0530 | [diff] [blame] | 16 | echo "Running spelling check on Commit Message" |
| 17 | |
| 18 | # Run the codespell with openbmc spcific spellings on the patchset |
| 19 | echo "openbmc-dictionary - misspelling count >> " |
| 20 | codespell -D openbmc-spelling.txt -d --count "${DIR}"/.git/COMMIT_EDITMSG |
| 21 | |
| 22 | # Run the codespell with generic dictionary on the patchset |
| 23 | echo "generic-dictionary - misspelling count >> " |
| 24 | codespell --builtin clear,rare,en-GB_to_en-US -d --count "${DIR}"/.git/COMMIT_EDITMSG |
| 25 | |
| 26 | cd "${DIR}" |
| 27 | |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 28 | echo "Formatting code under $DIR/" |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 29 | |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 30 | if [[ -f "setup.cfg" ]]; then |
Adriana Kobylak | bcee22b | 2018-01-10 16:58:27 -0600 | [diff] [blame] | 31 | pycodestyle --show-source . |
| 32 | rc=$? |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 33 | if [[ ${rc} -ne 0 ]]; then |
Adriana Kobylak | bcee22b | 2018-01-10 16:58:27 -0600 | [diff] [blame] | 34 | exit ${rc} |
| 35 | fi |
| 36 | fi |
| 37 | |
Patrick Williams | 01ced28 | 2020-07-17 15:10:15 -0500 | [diff] [blame] | 38 | # If .shellcheck exists, stop on error. Otherwise, allow pass. |
| 39 | if [[ -f ".shellcheck" ]]; then |
| 40 | shellcheck_allowfail="false" |
| 41 | else |
| 42 | shellcheck_allowfail="true" |
| 43 | fi |
| 44 | |
| 45 | # Run shellcheck on any shell-script. |
| 46 | shell_scripts="$(git ls-files | xargs -n1 file -0 | \ |
| 47 | grep -a "shell script" | cut -d '' -f 1)" |
| 48 | for script in ${shell_scripts}; do |
| 49 | shellcheck -x "${script}" || ${shellcheck_allowfail} |
| 50 | done |
| 51 | |
William A. Kennington III | 078c3b5 | 2018-10-04 19:44:31 -0700 | [diff] [blame] | 52 | # Allow called scripts to know which clang format we are using |
Patrick Williams | d83ca9a | 2021-02-22 14:29:21 -0600 | [diff] [blame] | 53 | export CLANG_FORMAT="clang-format" |
Patrick Venture | 366dd76 | 2018-10-22 13:55:03 -0700 | [diff] [blame] | 54 | IGNORE_FILE=".clang-ignore" |
| 55 | declare -a IGNORE_LIST |
| 56 | |
| 57 | if [[ -f "${IGNORE_FILE}" ]]; then |
| 58 | readarray -t IGNORE_LIST < "${IGNORE_FILE}" |
| 59 | fi |
| 60 | |
| 61 | ignorepaths="" |
| 62 | ignorefiles="" |
| 63 | |
| 64 | for 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 |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 73 | ignorepaths+=" ${path}" |
Patrick Venture | 366dd76 | 2018-10-22 13:55:03 -0700 | [diff] [blame] | 74 | else |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 75 | ignorefiles+=" ${path}" |
Patrick Venture | 366dd76 | 2018-10-22 13:55:03 -0700 | [diff] [blame] | 76 | fi |
| 77 | done |
William A. Kennington III | 078c3b5 | 2018-10-04 19:44:31 -0700 | [diff] [blame] | 78 | |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 79 | searchfiles="" |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 80 | while read -r path; do |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 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 |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 98 | # shellcheck disable=2089 |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 99 | searchfiles+="\"./${path}\" " |
| 100 | |
| 101 | # 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] | 102 | 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] | 103 | |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 104 | if [[ -f ".clang-format" ]]; then |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 105 | # shellcheck disable=SC2090 disable=SC2086 |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 106 | echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i |
Adriana Kobylak | bcee22b | 2018-01-10 16:58:27 -0600 | [diff] [blame] | 107 | git --no-pager diff --exit-code |
| 108 | fi |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 109 | |
| 110 | # Sometimes your situation is terrible enough that you need the flexibility. |
| 111 | # For example, phosphor-mboxd. |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 112 | if [[ -f "format-code.sh" ]]; then |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 113 | ./format-code.sh |
| 114 | git --no-pager diff --exit-code |
| 115 | fi |