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 12DIR=$1 13cd "${DIR}" 14 15set -e 16 17echo "Formatting code under $DIR/" 18 19if [[ -f "setup.cfg" ]]; then 20 pycodestyle --show-source . 21 rc=$? 22 if [[ ${rc} -ne 0 ]]; then 23 exit ${rc} 24 fi 25fi 26 27# If .shellcheck exists, stop on error. Otherwise, allow pass. 28if [[ -f ".shellcheck" ]]; then 29 shellcheck_allowfail="false" 30else 31 shellcheck_allowfail="true" 32fi 33 34# Run shellcheck on any shell-script. 35shell_scripts="$(git ls-files | xargs -n1 file -0 | \ 36 grep -a "shell script" | cut -d '' -f 1)" 37for script in ${shell_scripts}; do 38 shellcheck -x "${script}" || ${shellcheck_allowfail} 39done 40 41# Allow called scripts to know which clang format we are using 42export CLANG_FORMAT="clang-format" 43IGNORE_FILE=".clang-ignore" 44declare -a IGNORE_LIST 45 46if [[ -f "${IGNORE_FILE}" ]]; then 47 readarray -t IGNORE_LIST < "${IGNORE_FILE}" 48fi 49 50ignorepaths="" 51ignorefiles="" 52 53for 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 62 ignorepaths+=" ${path}" 63 else 64 ignorefiles+=" ${path}" 65 fi 66done 67 68searchfiles="" 69while read -r path; do 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 87 # shellcheck disable=2089 88 searchfiles+="\"./${path}\" " 89 90# Get C and C++ files managed by git and skip the mako files 91done <<<"$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')" 92 93if [[ -f ".clang-format" ]]; then 94 # shellcheck disable=SC2090 disable=SC2086 95 echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i 96 git --no-pager diff --exit-code 97fi 98 99# Sometimes your situation is terrible enough that you need the flexibility. 100# For example, phosphor-mboxd. 101if [[ -f "format-code.sh" ]]; then 102 ./format-code.sh 103 git --no-pager diff --exit-code 104fi 105