run_clang_format.sh (1699B)
1 #!/usr/bin/env bash 2 3 if [[ $(id -u) -eq 0 ]]; then 4 # Drop privileges by re-running this script. 5 # Note: this mangles arguments, better to avoid running scripts as root. 6 exec su worker -c "$0 $*" 7 fi 8 9 set -e 10 11 # Apply clang-format on the provided folder and verify that this doesn't change any file. 12 # If any file differs after formatting, the script eventually exits with 1. 13 # Any differences between formatted and unformatted files is printed to stdout to give a hint what's wrong. 14 15 # Includes a default set of directories NOT to clang-format on. 16 blocklist=( 17 "./automation" \ 18 "./coreconf" \ 19 "./doc" \ 20 "./pkg" \ 21 "./tests" \ 22 "./lib/libpkix" \ 23 "./lib/zlib" \ 24 "./lib/sqlite" \ 25 "./gtests/google_test" \ 26 "./out" \ 27 ) 28 29 top=$(cd "$(dirname $0)/../.."; pwd -P) 30 31 if [ $# -gt 0 ]; then 32 dirs=("$@") 33 else 34 cd "$top" 35 dirs=($(find . -maxdepth 2 -mindepth 1 -type d ! -path '*/.*' -print)) 36 fi 37 38 format_folder() 39 { 40 for block in "${blocklist[@]}"; do 41 if [[ "$1" == "$block"* ]]; then 42 echo "skip $1" 43 return 1 44 fi 45 done 46 return 0 47 } 48 49 for dir in "${dirs[@]}"; do 50 if format_folder "$dir"; then 51 c="${dir//[^\/]}" 52 echo "formatting $dir ..." 53 depth=() 54 if [ "${#c}" == "1" ]; then 55 depth+=(-maxdepth 1) 56 fi 57 find "$dir" "${depth[@]}" -type f \( -name '*.[ch]' -o -name '*.cc' \) -exec clang-format -sort-includes=false -i {} \+ 58 fi 59 done 60 61 TMPFILE=$(mktemp /tmp/$(basename $0).XXXXXX) 62 trap 'rm -f $TMPFILE' exit 63 if [[ -d "$top/.hg" ]]; then 64 hg diff --git "$top" | tee $TMPFILE 65 else 66 git -C "$top" diff | tee $TMPFILE 67 fi 68 [[ ! -s $TMPFILE ]]