tools/format-cpp: add `-t` to `xargs`
[babeltrace.git] / tools / format-cpp
index 56064ce0c1f7c4acca5329db90a0caf917999d81..29a0bf7efa63bdc6ae11b73c02c7099189863543 100755 (executable)
@@ -4,18 +4,15 @@
 #
 # Copyright (C) 2020-2022 Philippe Proulx <pproulx@efficios.com>
 
-expected_formatter_major_version=14
+expected_formatter_major_version=15
 
 # Runs the formatter, making sure it's the expected version.
 format_cpp() {
-       local formatter=$1
+       local formatter=("$@")
        local version
 
-       version=$($formatter --version)
-
-        # shellcheck disable=SC2181
-       if (($? != 0)); then
-               echo "Cannot execute \`$formatter --version\`." >&2
+       if ! version=$("${formatter[@]}" --version); then
+               echo "Cannot execute \`${formatter[*]} --version\`." >&2
                return 1
        fi
 
@@ -34,24 +31,23 @@ format_cpp() {
 
        # Using xargs to fail as soon as the formatter fails (`-exec`
        # won't stop if its subprocess fails).
-       #
-       # shellcheck disable=SC2086
        find "$root_dir" \( -name '*.cpp' -o -name '*.hpp' \) \
                ! -wholename '*/cpp-common/optional.hpp' \
                ! -wholename '*/cpp-common/string_view.hpp' \
-               -print0 | xargs -n1 -0 $formatter
+               ! -wholename '*/cpp-common/nlohmann/json.hpp' \
+               -print0 | xargs -P"$(nproc)" -n1 -t -0 "${formatter[@]}"
 }
 
 if [[ -n "$FORMATTER" ]]; then
        # Try using environment-provided formatter
-       formatter=$FORMATTER
+       read -ra formatter <<< "$FORMATTER"
 elif command -v clang-format-$expected_formatter_major_version &> /dev/null; then
        # Try using the expected version of clang-format
-       formatter="clang-format-$expected_formatter_major_version -i"
+       formatter=("clang-format-$expected_formatter_major_version" -i)
 else
        # Try using `clang-format` as is
-       formatter='clang-format -i'
+       formatter=(clang-format -i)
 fi
 
 # Try to format files
-format_cpp "$formatter"
+format_cpp "${formatter[@]}"
This page took 0.024208 seconds and 4 git commands to generate.