-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathformat-files.sh
executable file
·37 lines (30 loc) · 965 Bytes
/
format-files.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env bash
set -euo pipefail
MODE="${1:-default}"
PROJ_ROOT=$(dirname $0)
if ! type -p clang-format >/dev/null; then
echo "'clang-format' not found"
exit 1
fi
CLANG_FORMAT_VERSION=$(clang-format --version 2>/dev/null)
if [[ $CLANG_FORMAT_VERSION =~ ([0-9]+)\. ]]; then
MAJOR_VERSION=${BASH_REMATCH[1]}
if (( MAJOR_VERSION < 13 )); then
echo "clang-format version >= 13 required, got $MAJOR_VERSION"
exit 1
else
echo "clang-format version $MAJOR_VERSION"
fi
else
echo "unable to determine clang-format version"
exit 1
fi
EXTRA_FLAGS=""
if [[ "$MODE" == "check" ]]; then
echo "check mode enabled, not formatting files, only checking"
EXTRA_FLAGS="--dry-run --Werror"
fi
find "${PROJ_ROOT}" -type f \
\( -name "*.cpp" -o -name "*.c" -o -name "*.hpp" -o -name "*.h" \) \
-not -path "${PROJ_ROOT}/build/*" \
-print0 | xargs -0 clang-format $EXTRA_FLAGS --style=file --verbose -i