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 |
| 13 | cd ${DIR} |
| 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 | |
William A. Kennington III | 078c3b5 | 2018-10-04 19:44:31 -0700 | [diff] [blame] | 27 | # Allow called scripts to know which clang format we are using |
Patrick Williams | 80d0728 | 2020-05-20 10:56:22 -0500 | [diff] [blame^] | 28 | export CLANG_FORMAT="clang-format-10" |
Patrick Venture | 366dd76 | 2018-10-22 13:55:03 -0700 | [diff] [blame] | 29 | IGNORE_FILE=".clang-ignore" |
| 30 | declare -a IGNORE_LIST |
| 31 | |
| 32 | if [[ -f "${IGNORE_FILE}" ]]; then |
| 33 | readarray -t IGNORE_LIST < "${IGNORE_FILE}" |
| 34 | fi |
| 35 | |
| 36 | ignorepaths="" |
| 37 | ignorefiles="" |
| 38 | |
| 39 | for path in "${IGNORE_LIST[@]}"; do |
| 40 | # Check for comment, line starting with space, or zero-length string. |
| 41 | # Checking for [[:space:]] checks all options. |
| 42 | if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then |
| 43 | continue |
| 44 | fi |
| 45 | |
| 46 | # All paths must start with ./ for find's path prune expectation. |
| 47 | if [[ "${path}" =~ ^\.\/.+$ ]]; then |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 48 | ignorepaths+=" ${path}" |
Patrick Venture | 366dd76 | 2018-10-22 13:55:03 -0700 | [diff] [blame] | 49 | else |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 50 | ignorefiles+=" ${path}" |
Patrick Venture | 366dd76 | 2018-10-22 13:55:03 -0700 | [diff] [blame] | 51 | fi |
| 52 | done |
William A. Kennington III | 078c3b5 | 2018-10-04 19:44:31 -0700 | [diff] [blame] | 53 | |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 54 | searchfiles="" |
| 55 | while read path; do |
| 56 | # skip ignorefiles |
| 57 | if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then |
| 58 | continue |
| 59 | fi |
| 60 | |
| 61 | skip=false |
| 62 | #skip paths in ingorepaths |
| 63 | for pathname in $ignorepaths; do |
| 64 | if [[ "./${path}" == "${pathname}"* ]]; then |
| 65 | skip=true |
| 66 | break |
| 67 | fi |
| 68 | done |
| 69 | |
| 70 | if [ "$skip" = true ]; then |
| 71 | continue |
| 72 | fi |
| 73 | searchfiles+="\"./${path}\" " |
| 74 | |
| 75 | # Get C and C++ files managed by git and skip the mako files |
| 76 | done <<<$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.') |
| 77 | |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 78 | if [[ -f ".clang-format" ]]; then |
James Feist | f1665d6 | 2019-11-22 09:06:33 -0800 | [diff] [blame] | 79 | echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i |
Adriana Kobylak | bcee22b | 2018-01-10 16:58:27 -0600 | [diff] [blame] | 80 | git --no-pager diff --exit-code |
| 81 | fi |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 82 | |
| 83 | # Sometimes your situation is terrible enough that you need the flexibility. |
| 84 | # For example, phosphor-mboxd. |
Patrick Venture | 30ec0c4 | 2018-10-22 11:56:27 -0700 | [diff] [blame] | 85 | if [[ -f "format-code.sh" ]]; then |
Andrew Jeffery | 457b6d1 | 2018-03-09 15:28:14 +1030 | [diff] [blame] | 86 | ./format-code.sh |
| 87 | git --no-pager diff --exit-code |
| 88 | fi |