2016-06-24 13:23:10 +02:00
|
|
|
#! /bin/bash
|
2015-10-30 22:12:31 +01:00
|
|
|
#
|
|
|
|
# Linter script that checks for common style issues in Dolphin's codebase.
|
|
|
|
|
2017-07-19 00:32:35 +02:00
|
|
|
set -euo pipefail
|
|
|
|
|
2017-08-07 03:19:15 +02:00
|
|
|
if ! [ -x "$(command -v git)" ]; then
|
|
|
|
echo >&2 "error: git is not installed"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2018-04-12 10:49:55 +02:00
|
|
|
REQUIRED_CLANG_FORMAT_MAJOR=5
|
|
|
|
REQUIRED_CLANG_FORMAT_MINOR=0
|
2017-08-07 03:19:15 +02:00
|
|
|
|
|
|
|
if ! [ -x "$(command -v clang-format)" ]; then
|
|
|
|
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
|
|
|
|
CLANG_FORMAT_VERSION=$(clang-format -version | cut -d' ' -f3)
|
|
|
|
CLANG_FORMAT_MAJOR=$(echo $CLANG_FORMAT_VERSION | cut -d'.' -f1)
|
|
|
|
CLANG_FORMAT_MINOR=$(echo $CLANG_FORMAT_VERSION | cut -d'.' -f2)
|
|
|
|
|
|
|
|
if [ $CLANG_FORMAT_MAJOR != $REQUIRED_CLANG_FORMAT_MAJOR ] || [ $CLANG_FORMAT_MINOR != $REQUIRED_CLANG_FORMAT_MINOR ]; then
|
|
|
|
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
|
|
|
|
|
2015-10-30 22:12:31 +01:00
|
|
|
fail=0
|
|
|
|
|
2017-06-05 02:16:29 +02:00
|
|
|
# Default to staged files, unless a commit was passed.
|
|
|
|
COMMIT=${1:---cached}
|
|
|
|
|
2017-07-19 00:32:35 +02:00
|
|
|
# Get modified files (must be on own line for exit-code handling)
|
|
|
|
modified_files=$(git diff --name-only --diff-filter=ACMRTUXB $COMMIT)
|
|
|
|
|
2017-05-08 20:20:42 +02:00
|
|
|
# Loop through each modified file.
|
2017-07-19 00:32:35 +02:00
|
|
|
for f in ${modified_files}; do
|
2017-05-08 20:20:42 +02:00
|
|
|
# Filter them.
|
2016-06-24 13:23:10 +02:00
|
|
|
if ! echo "${f}" | egrep -q "[.](cpp|h|mm)$"; then
|
|
|
|
continue
|
|
|
|
fi
|
2017-08-14 04:03:21 +02:00
|
|
|
if ! echo "${f}" | egrep -q "^Source"; then
|
2016-06-24 13:23:10 +02:00
|
|
|
continue
|
|
|
|
fi
|
2017-05-08 20:20:42 +02:00
|
|
|
|
|
|
|
# Check for clang-format issues.
|
2017-07-19 00:32:35 +02:00
|
|
|
d=$(clang-format ${f} | (diff -u "${f}" - || true))
|
2016-06-24 13:23:10 +02:00
|
|
|
if ! [ -z "${d}" ]; then
|
|
|
|
echo "!!! ${f} not compliant to coding style, here is the fix:"
|
|
|
|
echo "${d}"
|
|
|
|
fail=1
|
|
|
|
fi
|
2017-05-08 20:20:42 +02:00
|
|
|
|
|
|
|
# Check for newline at EOF.
|
2017-07-19 00:32:35 +02:00
|
|
|
last_line="$(tail -c 1 ${f})"
|
|
|
|
if [ -n "${last_line}" ]; then
|
2017-05-08 20:20:42 +02:00
|
|
|
echo "!!! ${f} not compliant to coding style:"
|
|
|
|
echo "Missing newline at end of file"
|
|
|
|
fail=1
|
|
|
|
fi
|
2016-06-24 13:23:10 +02:00
|
|
|
done
|
2015-10-30 22:12:31 +01:00
|
|
|
|
|
|
|
exit ${fail}
|