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