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 |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 13 | cd "${DIR}" |
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 | |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 17 | echo "Formatting code under $DIR/" |
Andrew Geissler | 9b0105c | 2018-01-10 10:58:35 -0800 | [diff] [blame] | 18 | |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 19 | if [[ -f "setup.cfg" ]]; then |
Adriana Kobylak | bcee22b | 2018-01-10 16:58:27 -0600 | [diff] [blame] | 20 | pycodestyle --show-source . |
| 21 | rc=$? |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 22 | if [[ ${rc} -ne 0 ]]; then |
Adriana Kobylak | bcee22b | 2018-01-10 16:58:27 -0600 | [diff] [blame] | 23 | exit ${rc} |
| 24 | fi |
| 25 | fi |
| 26 | |
Patrick Williams | 01ced28 | 2020-07-17 15:10:15 -0500 | [diff] [blame] | 27 | # If .shellcheck exists, stop on error. Otherwise, allow pass. |
| 28 | if [[ -f ".shellcheck" ]]; then |
| 29 | shellcheck_allowfail="false" |
| 30 | else |
| 31 | shellcheck_allowfail="true" |
| 32 | fi |
| 33 | |
| 34 | # Run shellcheck on any shell-script. |
| 35 | shell_scripts="$(git ls-files | xargs -n1 file -0 | \ |
| 36 | grep -a "shell script" | cut -d '' -f 1)" |
| 37 | for script in ${shell_scripts}; do |
| 38 | shellcheck -x "${script}" || ${shellcheck_allowfail} |
| 39 | done |
| 40 | |
William A. Kennington III | 078c3b5 | 2018-10-04 19:44:31 -0700 | [diff] [blame] | 41 | # Allow called scripts to know which clang format we are using |
Patrick Williams | d83ca9a | 2021-02-22 14:29:21 -0600 | [diff] [blame] | 42 | export CLANG_FORMAT="clang-format" |
Patrick Venture | 366dd76 | 2018-10-22 13:55:03 -0700 | [diff] [blame] | 43 | IGNORE_FILE=".clang-ignore" |
| 44 | declare -a IGNORE_LIST |
| 45 | |
| 46 | if [[ -f "${IGNORE_FILE}" ]]; then |
| 47 | readarray -t IGNORE_LIST < "${IGNORE_FILE}" |
| 48 | fi |
| 49 | |
| 50 | ignorepaths="" |
| 51 | ignorefiles="" |
| 52 | |
| 53 | for path in "${IGNORE_LIST[@]}"; do |
| 54 | # Check for comment, line starting with space, or zero-length string. |
| 55 | # Checking for [[:space:]] checks all options. |
| 56 | if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then |
| 57 | continue |
| 58 | fi |
| 59 | |
| 60 | # All paths must start with ./ for find's path prune expectation. |
| 61 | if [[ "${path}" =~ ^\.\/.+$ ]]; then |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 62 | ignorepaths+=" ${path}" |
Patrick Venture | 366dd76 | 2018-10-22 13:55:03 -0700 | [diff] [blame] | 63 | else |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 64 | ignorefiles+=" ${path}" |
Patrick Venture | 366dd76 | 2018-10-22 13:55:03 -0700 | [diff] [blame] | 65 | fi |
| 66 | done |
William A. Kennington III | 078c3b5 | 2018-10-04 19:44:31 -0700 | [diff] [blame] | 67 | |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 68 | searchfiles="" |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 69 | while read -r path; do |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 70 | # skip ignorefiles |
| 71 | if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then |
| 72 | continue |
| 73 | fi |
| 74 | |
| 75 | skip=false |
| 76 | #skip paths in ingorepaths |
| 77 | for pathname in $ignorepaths; do |
| 78 | if [[ "./${path}" == "${pathname}"* ]]; then |
| 79 | skip=true |
| 80 | break |
| 81 | fi |
| 82 | done |
| 83 | |
| 84 | if [ "$skip" = true ]; then |
| 85 | continue |
| 86 | fi |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 87 | # shellcheck disable=2089 |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 88 | searchfiles+="\"./${path}\" " |
| 89 | |
| 90 | # 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] | 91 | 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] | 92 | |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 93 | if [[ -f ".clang-format" ]]; then |
Patrick Williams | 384d741 | 2020-11-06 16:15:41 -0600 | [diff] [blame] | 94 | # shellcheck disable=SC2090 disable=SC2086 |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 95 | echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i |
Adriana Kobylak | bcee22b | 2018-01-10 16:58:27 -0600 | [diff] [blame] | 96 | git --no-pager diff --exit-code |
| 97 | fi |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 98 | |
| 99 | # Sometimes your situation is terrible enough that you need the flexibility. |
| 100 | # For example, phosphor-mboxd. |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 101 | if [[ -f "format-code.sh" ]]; then |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 102 | ./format-code.sh |
| 103 | git --no-pager diff --exit-code |
| 104 | fi |