2016-06-24 11:23:10 +00:00
|
|
|
#! /bin/bash
|
2015-10-30 21:12:31 +00:00
|
|
|
#
|
|
|
|
# Linter script that checks for common style issues in Dolphin's codebase.
|
|
|
|
|
2017-07-18 22:32:35 +00:00
|
|
|
set -euo pipefail
|
|
|
|
|
2022-06-09 03:43:45 +00:00
|
|
|
# use Windows' git when working under path mounted from host on wsl2
|
|
|
|
# inspired by https://markentier.tech/posts/2020/10/faster-git-under-wsl2/#solution
|
|
|
|
GIT=git
|
2022-08-15 20:13:18 +00:00
|
|
|
if [ "$(uname -s)" == "Linux" ]; then
|
|
|
|
if [ "$(stat --file-system --format=%T `pwd -P`)" == "v9fs" ]; then
|
|
|
|
GIT=git.exe
|
|
|
|
fi
|
2022-06-09 03:43:45 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [ -x "$(command -v $GIT)" ]; then
|
2017-08-07 01:19:15 +00:00
|
|
|
echo >&2 "error: git is not installed"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-02-02 03:56:08 +00:00
|
|
|
REQUIRED_CLANG_FORMAT_MAJOR=13
|
2018-04-12 08:49:55 +00:00
|
|
|
REQUIRED_CLANG_FORMAT_MINOR=0
|
2019-03-07 14:10:41 +00:00
|
|
|
CLANG_FORMAT=clang-format
|
|
|
|
CLANG_FORMAT_MAJOR=clang-format-${REQUIRED_CLANG_FORMAT_MAJOR}
|
|
|
|
CLANG_FORMAT_MAJOR_MINOR=${CLANG_FORMAT_MAJOR}.${REQUIRED_CLANG_FORMAT_MINOR}
|
2017-08-07 01:19:15 +00:00
|
|
|
|
2019-03-07 14:10:41 +00:00
|
|
|
if [ -x "$(command -v $CLANG_FORMAT_MAJOR)" ]; then CLANG_FORMAT=$CLANG_FORMAT_MAJOR; fi
|
|
|
|
if [ -x "$(command -v $CLANG_FORMAT_MAJOR_MINOR)" ]; then CLANG_FORMAT=$CLANG_FORMAT_MAJOR_MINOR; fi
|
|
|
|
|
|
|
|
if ! [ -x "$(command -v $CLANG_FORMAT)" ]; then
|
2017-08-07 01:19:15 +00:00
|
|
|
echo >&2 "error: clang-format is not installed"
|
|
|
|
echo >&2 "Install clang-format version ${REQUIRED_CLANG_FORMAT_MAJOR}.${REQUIRED_CLANG_FORMAT_MINOR}.*"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
FORCE=0
|
|
|
|
|
|
|
|
if [ $# -gt 0 ]; then
|
|
|
|
case "$1" in
|
|
|
|
-f|--force)
|
|
|
|
FORCE=1
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $FORCE -eq 0 ]; then
|
2021-01-28 17:56:31 +00:00
|
|
|
CLANG_FORMAT_VERSION=$($CLANG_FORMAT --version)
|
|
|
|
clang_format_version_ok=false
|
|
|
|
clang_format_version_re='version ([0-9]+).([0-9]+)'
|
|
|
|
if [[ $CLANG_FORMAT_VERSION =~ $clang_format_version_re ]]; then
|
|
|
|
CLANG_FORMAT_MAJOR="${BASH_REMATCH[1]}"
|
|
|
|
CLANG_FORMAT_MINOR="${BASH_REMATCH[2]}"
|
|
|
|
if [ $CLANG_FORMAT_MAJOR == $REQUIRED_CLANG_FORMAT_MAJOR ] && [ $CLANG_FORMAT_MINOR == $REQUIRED_CLANG_FORMAT_MINOR ]; then
|
|
|
|
clang_format_version_ok=true
|
|
|
|
fi
|
|
|
|
fi
|
2017-08-07 01:19:15 +00:00
|
|
|
|
2021-01-28 17:56:31 +00:00
|
|
|
if ! [ "$clang_format_version_ok" = true ]; then
|
2017-08-07 01:19:15 +00:00
|
|
|
echo >&2 "error: clang-format is the wrong version (${CLANG_FORMAT_VERSION})"
|
|
|
|
echo >&2 "Install clang-format version ${REQUIRED_CLANG_FORMAT_MAJOR}.${REQUIRED_CLANG_FORMAT_MINOR}.* or use --force to ignore"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2018-08-26 16:47:05 +00:00
|
|
|
did_java_setup=0
|
2022-06-09 03:43:45 +00:00
|
|
|
JAVA_CODESTYLE_FILE="./$($GIT rev-parse --show-cdup)/Source/Android/code-style-java.xml"
|
2018-08-26 16:47:05 +00:00
|
|
|
java_temp_dir=""
|
|
|
|
|
|
|
|
function java_setup() {
|
|
|
|
if [ "$did_java_setup" = 1 ]; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
if [ ! -x "${ANDROID_STUDIO_ROOT}/bin/format.sh" ]; then
|
|
|
|
echo >&2 "error: must set ANDROID_STUDIO_ROOT environment variable to the IDE installation directory (current: ${ANDROID_STUDIO_ROOT})"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
java_temp_dir="$(mktemp -d)"
|
|
|
|
trap "{ rm -r ${java_temp_dir}; }" EXIT
|
|
|
|
did_java_setup=1
|
|
|
|
}
|
|
|
|
|
2015-10-30 21:12:31 +00:00
|
|
|
fail=0
|
|
|
|
|
2017-06-05 00:16:29 +00:00
|
|
|
# Default to staged files, unless a commit was passed.
|
|
|
|
COMMIT=${1:---cached}
|
|
|
|
|
2017-07-18 22:32:35 +00:00
|
|
|
# Get modified files (must be on own line for exit-code handling)
|
2022-06-09 03:43:45 +00:00
|
|
|
modified_files=$($GIT diff --name-only --diff-filter=ACMRTUXB $COMMIT)
|
2017-07-18 22:32:35 +00:00
|
|
|
|
2018-08-26 16:47:05 +00:00
|
|
|
function java_check() {
|
|
|
|
"${ANDROID_STUDIO_ROOT}/bin/format.sh" -s "${JAVA_CODESTYLE_FILE}" -R "${java_temp_dir}" >/dev/null
|
|
|
|
|
|
|
|
# ignore 'added'/'deleted' files, we copied only files of interest to the tmpdir
|
2022-06-09 03:43:45 +00:00
|
|
|
d=$($GIT diff --diff-filter=ad . "${java_temp_dir}" || true)
|
2018-08-26 16:47:05 +00:00
|
|
|
if ! [ -z "${d}" ]; then
|
|
|
|
echo "!!! Java code is not compliant to coding style, here is the fix:"
|
|
|
|
echo "${d}"
|
|
|
|
fail=1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-05-08 18:20:42 +00:00
|
|
|
# Loop through each modified file.
|
2017-07-18 22:32:35 +00:00
|
|
|
for f in ${modified_files}; do
|
2017-05-08 18:20:42 +00:00
|
|
|
# Filter them.
|
2023-02-02 04:23:40 +00:00
|
|
|
if echo "${f}" | grep -E -q "[.]java$"; then
|
2018-08-26 16:47:05 +00:00
|
|
|
# Copy Java files to a temporary directory
|
|
|
|
java_setup
|
|
|
|
mkdir -p $(dirname "${java_temp_dir}/${f}")
|
|
|
|
cp "${f}" "${java_temp_dir}/${f}"
|
|
|
|
continue
|
|
|
|
fi
|
2023-02-02 04:23:40 +00:00
|
|
|
if ! echo "${f}" | grep -E -q "[.](cpp|h|mm)$"; then
|
2016-06-24 11:23:10 +00:00
|
|
|
continue
|
|
|
|
fi
|
2023-02-02 04:23:40 +00:00
|
|
|
if ! echo "${f}" | grep -E -q "^Source"; then
|
2016-06-24 11:23:10 +00:00
|
|
|
continue
|
|
|
|
fi
|
2017-05-08 18:20:42 +00:00
|
|
|
|
|
|
|
# Check for clang-format issues.
|
2019-03-07 14:10:41 +00:00
|
|
|
d=$($CLANG_FORMAT ${f} | (diff -u "${f}" - || true))
|
2016-06-24 11:23:10 +00:00
|
|
|
if ! [ -z "${d}" ]; then
|
|
|
|
echo "!!! ${f} not compliant to coding style, here is the fix:"
|
|
|
|
echo "${d}"
|
|
|
|
fail=1
|
|
|
|
fi
|
2017-05-08 18:20:42 +00:00
|
|
|
|
|
|
|
# Check for newline at EOF.
|
2017-07-18 22:32:35 +00:00
|
|
|
last_line="$(tail -c 1 ${f})"
|
|
|
|
if [ -n "${last_line}" ]; then
|
2017-05-08 18:20:42 +00:00
|
|
|
echo "!!! ${f} not compliant to coding style:"
|
|
|
|
echo "Missing newline at end of file"
|
|
|
|
fail=1
|
|
|
|
fi
|
2016-06-24 11:23:10 +00:00
|
|
|
done
|
2015-10-30 21:12:31 +00:00
|
|
|
|
2018-08-26 16:47:05 +00:00
|
|
|
if [ "${did_java_setup}" = 1 ]; then
|
|
|
|
java_check
|
|
|
|
fi
|
|
|
|
|
2015-10-30 21:12:31 +00:00
|
|
|
exit ${fail}
|