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
13WORKSPACE=$PWD
14
15set -e
16
17echo "Running spelling check on Commit Message"
18
19# Run the codespell with openbmc spcific spellings on the patchset
20echo "openbmc-dictionary - misspelling count >> "
21codespell -D openbmc-spelling.txt -d --count "${DIR}"/.git/COMMIT_EDITMSG
22
23# Run the codespell with generic dictionary on the patchset
24echo "generic-dictionary - misspelling count >> "
25codespell --builtin clear,rare,en-GB_to_en-US -d --count "${DIR}"/.git/COMMIT_EDITMSG
26
27cd "${DIR}"
28
29echo "Formatting code under $DIR/"
30
31ESLINT_CONFIG="eslint ${DIR} --no-eslintrc -c ${WORKSPACE}/eslint-global-config.json \
32               --ext .json --resolve-plugins-relative-to /usr/local/lib/node_modules\
33               --no-error-on-unmatched-pattern"
34ESLINT_IGNORE=" --ignore-path ${DIR}/.eslintignore"
35
36# Get the eslint configuration from the repository
37if [[ -f ".eslintrc.json" ]] || [[ -f ".eslintignore" ]]; then
38    echo "Running the json validator on the repo using it's config > "
39    if [[ ! -f ".eslintrc.json" ]]; then
40        eslint_command="${ESLINT_CONFIG} ${ESLINT_IGNORE}"
41    else
42        eslint_command="$ESLINT_CONFIG"
43    fi
44else
45    echo "Running the json validator on the repo using the global config > "
46        eslint_command="$ESLINT_CONFIG"
47fi
48
49# Print eslint command
50echo "$eslint_command"
51# Run eslint
52$eslint_command
53
54if [[ -f "setup.cfg" ]]; then
55  pycodestyle --show-source --exclude=subprojects .
56  rc=$?
57  if [[ ${rc} -ne 0 ]]; then
58    exit ${rc}
59  fi
60fi
61
62# If .shellcheck exists, stop on error.  Otherwise, allow pass.
63if [[ -f ".shellcheck" ]]; then
64  shellcheck_allowfail="false"
65else
66  shellcheck_allowfail="true"
67fi
68
69# Run shellcheck on any shell-script.
70shell_scripts="$(git ls-files | xargs -n1 file -0 | \
71                 grep -a "shell script" | cut -d '' -f 1)"
72for script in ${shell_scripts}; do
73  shellcheck --color=never -x "${script}" || ${shellcheck_allowfail}
74done
75
76# Allow called scripts to know which clang format we are using
77export CLANG_FORMAT="clang-format"
78IGNORE_FILE=".clang-ignore"
79declare -a IGNORE_LIST
80
81if [[ -f "${IGNORE_FILE}" ]]; then
82  readarray -t IGNORE_LIST < "${IGNORE_FILE}"
83fi
84
85ignorepaths=""
86ignorefiles=""
87
88for path in "${IGNORE_LIST[@]}"; do
89  # Check for comment, line starting with space, or zero-length string.
90  # Checking for [[:space:]] checks all options.
91  if [[ -z "${path}" ]] || [[ "${path}" =~ ^(#|[[:space:]]).*$ ]]; then
92    continue
93  fi
94
95  # All paths must start with ./ for find's path prune expectation.
96  if [[ "${path}" =~ ^\.\/.+$ ]]; then
97    ignorepaths+=" ${path}"
98  else
99    ignorefiles+=" ${path}"
100  fi
101done
102
103searchfiles=""
104while read -r path; do
105  # skip ignorefiles
106  if [[ $ignorefiles == *"$(basename "${path}")"* ]]; then
107    continue
108  fi
109
110  skip=false
111  #skip paths in ingorepaths
112  for pathname in $ignorepaths; do
113    if [[ "./${path}" == "${pathname}"* ]]; then
114       skip=true
115       break
116    fi
117  done
118
119  if [ "$skip" = true ]; then
120   continue
121  fi
122  # shellcheck disable=2089
123  searchfiles+="\"./${path}\" "
124
125# Get C and C++ files managed by git and skip the mako files
126done <<<"$(git ls-files | grep -e '\.[ch]pp$' -e '\.[ch]$' | grep -v '\.mako\.')"
127
128if [[ -f ".clang-format" ]]; then
129  # shellcheck disable=SC2090 disable=SC2086
130  echo ${searchfiles} | xargs "${CLANG_FORMAT}" -i
131  git --no-pager diff --exit-code
132fi
133
134# Sometimes your situation is terrible enough that you need the flexibility.
135# For example, phosphor-mboxd.
136if [[ -f "format-code.sh" ]]; then
137  ./format-code.sh
138  git --no-pager diff --exit-code
139fi
140