2021-01-22 19:44:05 +03:00
|
|
|
#!/usr/bin/env bash
|
2020-09-18 12:34:43 +03:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
|
|
|
|
cd "${script_path}/.." || exit 1
|
|
|
|
|
2020-12-27 17:34:06 +03:00
|
|
|
if [ "$#" -eq "1" ]; then
|
|
|
|
mapfile -t files < <(
|
|
|
|
git ls-files -- \
|
|
|
|
'*.cpp' \
|
|
|
|
'*.h' \
|
2023-09-19 17:49:21 +03:00
|
|
|
'*.mm' \
|
2020-12-27 17:34:06 +03:00
|
|
|
':!:Base' \
|
2022-10-24 10:02:37 +03:00
|
|
|
':!:Kernel/FileSystem/Ext2FS/Definitions.h' \
|
2022-05-14 17:09:24 +03:00
|
|
|
':!:Userland/Libraries/LibCodeComprehension/Cpp/Tests/*' \
|
2021-08-13 19:24:16 +03:00
|
|
|
':!:Userland/Libraries/LibCpp/Tests/parser/*' \
|
|
|
|
':!:Userland/Libraries/LibCpp/Tests/preprocessor/*'
|
2020-12-27 17:34:06 +03:00
|
|
|
)
|
|
|
|
else
|
|
|
|
files=()
|
|
|
|
for file in "${@:2}"; do
|
2023-09-19 17:49:21 +03:00
|
|
|
if [[ "${file}" == *".cpp" || "${file}" == *".h" || "${file}" == *".mm" ]]; then
|
2020-12-27 17:34:06 +03:00
|
|
|
files+=("${file}")
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2020-09-18 12:34:43 +03:00
|
|
|
|
2020-12-27 17:34:06 +03:00
|
|
|
if (( ${#files[@]} )); then
|
2022-04-10 12:21:54 +03:00
|
|
|
TOOLCHAIN_DIR=Toolchain/Local/clang/bin
|
2021-01-10 10:20:21 +03:00
|
|
|
CLANG_FORMAT=false
|
2023-07-08 03:21:41 +03:00
|
|
|
if command -v clang-format-16 >/dev/null 2>&1 ; then
|
|
|
|
CLANG_FORMAT=clang-format-16
|
|
|
|
elif command -v brew >/dev/null 2>&1 && command -v "$(brew --prefix llvm@16)"/bin/clang-format >/dev/null 2>&1 ; then
|
|
|
|
CLANG_FORMAT="$(brew --prefix llvm@16)"/bin/clang-format
|
|
|
|
elif command -v $TOOLCHAIN_DIR/clang-format >/dev/null 2>&1 && $TOOLCHAIN_DIR/clang-format --version | grep -qF ' 16.' ; then
|
2022-04-10 12:21:54 +03:00
|
|
|
CLANG_FORMAT=$TOOLCHAIN_DIR/clang-format
|
2021-01-10 10:20:21 +03:00
|
|
|
elif command -v clang-format >/dev/null 2>&1 ; then
|
|
|
|
CLANG_FORMAT=clang-format
|
2023-07-08 03:21:41 +03:00
|
|
|
if ! "${CLANG_FORMAT}" --version | awk '{ if (substr($NF, 1, index($NF, ".") - 1) < 16) exit 1; }'; then
|
|
|
|
echo "You are using '$("${CLANG_FORMAT}" --version)', which appears to not be clang-format 16 or later."
|
2021-01-10 10:20:21 +03:00
|
|
|
echo "It is very likely that the resulting changes are not what you wanted."
|
|
|
|
fi
|
|
|
|
else
|
2023-07-08 03:21:41 +03:00
|
|
|
echo "clang-format-16 is not available, but C or C++ files need linting! Either skip this script, or install clang-format-16."
|
|
|
|
echo "(If you install a package 'clang-format', please make sure it's version 16 or later.)"
|
2021-01-10 10:20:21 +03:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-10-26 21:14:17 +03:00
|
|
|
if [ "$#" -gt "0" ] && [ "--overwrite-inplace" = "$1" ] ; then
|
2021-01-10 10:20:21 +03:00
|
|
|
true # The only way to run this script.
|
|
|
|
else
|
|
|
|
# Note that this branch also covers --help, -h, -help, -?, etc.
|
|
|
|
echo "USAGE: $0 --overwrite-inplace"
|
|
|
|
echo "The argument is necessary to make you aware that this *will* overwrite your local files."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Using ${CLANG_FORMAT}"
|
|
|
|
|
2020-12-27 17:34:06 +03:00
|
|
|
"${CLANG_FORMAT}" -style=file -i "${files[@]}"
|
|
|
|
echo "Maybe some files have changed. Sorry, but clang-format doesn't indicate what happened."
|
|
|
|
else
|
2023-09-19 17:49:21 +03:00
|
|
|
echo "No .cpp, .h or .mm files to check."
|
2020-12-27 17:34:06 +03:00
|
|
|
fi
|