3 # SPDX-License-Identifier: GPL-2.0-only
5 # Copyright (C) 2020-2022 Philippe Proulx <pproulx@efficios.com>
7 expected_formatter_major_version
=14
9 # Runs the formatter, making sure it's the expected version.
14 version
=$
($formatter --version)
16 # shellcheck disable=SC2181
18 echo "Cannot execute \`$formatter --version\`." >&2
22 if [[ "$version" != *"clang-format version $expected_formatter_major_version"* ]]; then
23 echo "Expecting clang-format $expected_formatter_major_version." >&2
25 echo " \`$version\`" >&2
27 echo "Use the FORMATTER environment variable to specify the location of clang-format $expected_formatter_major_version"
33 root_dir
="$(dirname "${BASH_SOURCE[0]}")"
35 # Using xargs to fail as soon as the formatter fails (`-exec`
36 # won't stop if its subprocess fails).
38 # shellcheck disable=SC2086
39 find "$root_dir" -path './src/vendor' -prune \
40 -o -type f \
( -name '*\.h' -o -name '*\.hpp' -o -name '*\.c' -o -name '*\.cpp' \
) \
41 -not -path '*/\.*' -print0 |
xargs -n1 -0 $formatter -i --style=file --fallback-style=none
44 if [[ -n "$FORMATTER" ]]; then
45 # Try using environment-provided formatter
47 elif command -v clang-format-
$expected_formatter_major_version &> /dev
/null
; then
48 # Try using the expected version of clang-format
49 formatter
="clang-format-$expected_formatter_major_version -i"
51 # Try using `clang-format` as is
52 formatter
='clang-format -i'
56 format_cpp
"$formatter"