blob: 762ea31fb0516965d9d5aab4911b565e16f97eeb [file] [log] [blame]
Andrew Geissler9b0105c2018-01-10 10:58:35 -08001#!/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
Andrew Jeffery0cce8482018-04-30 11:37:01 +093015set -e
16
Andrew Geissler9b0105c2018-01-10 10:58:35 -080017echo "Formatting code under $DIR/"
Andrew Geissler9b0105c2018-01-10 10:58:35 -080018
Patrick Venture30ec0c42018-10-22 11:56:27 -070019if [[ -f "setup.cfg" ]]; then
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060020 pycodestyle --show-source .
21 rc=$?
Patrick Venture30ec0c42018-10-22 11:56:27 -070022 if [[ ${rc} -ne 0 ]]; then
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060023 exit ${rc}
24 fi
25fi
26
William A. Kennington III078c3b52018-10-04 19:44:31 -070027# Allow called scripts to know which clang format we are using
William A. Kennington IIIf6769412019-06-26 12:14:51 -070028export CLANG_FORMAT="clang-format-8"
Patrick Venture366dd762018-10-22 13:55:03 -070029IGNORE_FILE=".clang-ignore"
30declare -a IGNORE_LIST
31
32if [[ -f "${IGNORE_FILE}" ]]; then
33 readarray -t IGNORE_LIST < "${IGNORE_FILE}"
34fi
35
36ignorepaths=""
37ignorefiles=""
38
39for 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 Feistf1665d62019-11-22 09:06:33 -080048 ignorepaths+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -070049 else
James Feistf1665d62019-11-22 09:06:33 -080050 ignorefiles+=" ${path}"
Patrick Venture366dd762018-10-22 13:55:03 -070051 fi
52done
William A. Kennington III078c3b52018-10-04 19:44:31 -070053
James Feistf1665d62019-11-22 09:06:33 -080054searchfiles=""
55while 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
76done <<<$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')
77
Patrick Venture30ec0c42018-10-22 11:56:27 -070078if [[ -f ".clang-format" ]]; then
James Feistf1665d62019-11-22 09:06:33 -080079 echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i
Adriana Kobylakbcee22b2018-01-10 16:58:27 -060080 git --no-pager diff --exit-code
81fi
Andrew Jeffery457b6d12018-03-09 15:28:14 +103082
83# Sometimes your situation is terrible enough that you need the flexibility.
84# For example, phosphor-mboxd.
Patrick Venture30ec0c42018-10-22 11:56:27 -070085if [[ -f "format-code.sh" ]]; then
Andrew Jeffery457b6d12018-03-09 15:28:14 +103086 ./format-code.sh
87 git --no-pager diff --exit-code
88fi