tools/format-cpp: add `.sh` extension to identify it
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 30 Oct 2023 18:12:41 +0000 (14:12 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 30 Oct 2023 18:15:46 +0000 (14:15 -0400)
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ifb57e0a26a4d7a2da99bd74fdf24d8aadcec4cf6
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11173

CONTRIBUTING.adoc
Makefile.am
tools/format-cpp [deleted file]
tools/format-cpp.sh [new file with mode: 0755]

index d5241ff4a8d611b924a9713f94116d292059cbc9..268c4445ae4e8b3f33db45a8a2654e2c20edd17d 100644 (file)
@@ -1680,20 +1680,20 @@ You need clang-format{nbsp}15 to use the project's `.clang-format` file.
 To automatically format all the project's {cpp} files, run:
 
 ----
-$ ./tools/format-cpp
+$ ./tools/format-cpp.sh
 ----
 
 Pass a directory path to only format the {cpp} files it contains:
 
 ----
-$ ./tools/format-cpp ./src/cli
+$ ./tools/format-cpp.sh ./src/cli
 ----
 
 Use the `FORMATTER` environment variable to override the default
 formatter (`clang-format{nbsp}-i`):
 
 ----
-$ FORMATTER='my-clang-format-15 -i' ./tools/format-cpp
+$ FORMATTER='my-clang-format-15 -i' ./tools/format-cpp.sh
 ----
 
 ==== Naming
index 642466fa21c93dce6d47a34a2ea7c8150eebc55e..b3a5f29c8c46060f723c18a13e54a6417df57241 100644 (file)
@@ -26,5 +26,5 @@ EXTRA_DIST = \
        LICENSES/LGPL-2.1 \
        LICENSES/MIT \
        std-ext-lib.txt \
-       tools/format-cpp \
+       tools/format-cpp.sh \
        version
diff --git a/tools/format-cpp b/tools/format-cpp
deleted file mode 100755 (executable)
index 364dafe..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-#!/bin/bash
-#
-# SPDX-License-Identifier: GPL-2.0-only
-#
-# Copyright (C) 2020-2023 Philippe Proulx <pproulx@efficios.com>
-
-expected_formatter_major_version=15
-
-# Runs the formatter, returning 1 if it's not the expected version.
-#
-# Argument 1:
-#     Starting directory.
-#
-# Remaining arguments:
-#     Formatter command.
-format_cpp() {
-       local root_dir=$1
-
-       shift
-
-       local formatter=("$@")
-       local version
-
-       if ! version=$("${formatter[@]}" --version); then
-               echo "Cannot execute \`${formatter[*]} --version\`." >&2
-               return 1
-       fi
-
-       if [[ "$version" != *"clang-format version $expected_formatter_major_version"* ]]; then
-               {
-                       echo "Expecting clang-format $expected_formatter_major_version."
-                       echo
-                       echo Got:
-                       echo
-                       echo "$version"
-               } >& 2
-
-               return 1
-       fi
-
-       # Using xargs(1) to fail as soon as the formatter fails (`-exec`
-       # won't stop if its subprocess fails).
-       #
-       # We want an absolute starting directory because find(1) excludes
-       # files in specific subdirectories.
-       find "$(realpath "$root_dir")" \( -name '*.cpp' -o -name '*.hpp' \) \
-               ! -path '*/src/cpp-common/optional.hpp' \
-               ! -path '*/src/cpp-common/string_view.hpp' \
-               ! -path '*/src/cpp-common/nlohmann/json.hpp' \
-               ! -path '*/src/plugins/ctf/common/metadata/parser.*' \
-               ! -path '*/src/plugins/ctf/common/metadata/lexer.*' \
-               -print0 | xargs -P"$(nproc)" -n1 -t -0 "${formatter[@]}"
-}
-
-# Choose formatter
-if [[ -n "$FORMATTER" ]]; then
-       # Try using environment-provided 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)
-else
-       # Try using `clang-format` as is
-       formatter=(clang-format -i)
-fi
-
-# Choose root directory
-if (($# == 1)); then
-       root_dir=$1
-
-       if [[ ! -d "$root_dir" ]]; then
-               echo "\`$root_dir\`: expecting an existing directory." >& 2
-               exit 1
-       fi
-else
-       # Default: root of the project, processing all C++ files
-       root_dir="$(dirname "${BASH_SOURCE[0]}")/.."
-fi
-
-# Try to format files
-format_cpp "$root_dir" "${formatter[@]}"
diff --git a/tools/format-cpp.sh b/tools/format-cpp.sh
new file mode 100755 (executable)
index 0000000..364dafe
--- /dev/null
@@ -0,0 +1,81 @@
+#!/bin/bash
+#
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Copyright (C) 2020-2023 Philippe Proulx <pproulx@efficios.com>
+
+expected_formatter_major_version=15
+
+# Runs the formatter, returning 1 if it's not the expected version.
+#
+# Argument 1:
+#     Starting directory.
+#
+# Remaining arguments:
+#     Formatter command.
+format_cpp() {
+       local root_dir=$1
+
+       shift
+
+       local formatter=("$@")
+       local version
+
+       if ! version=$("${formatter[@]}" --version); then
+               echo "Cannot execute \`${formatter[*]} --version\`." >&2
+               return 1
+       fi
+
+       if [[ "$version" != *"clang-format version $expected_formatter_major_version"* ]]; then
+               {
+                       echo "Expecting clang-format $expected_formatter_major_version."
+                       echo
+                       echo Got:
+                       echo
+                       echo "$version"
+               } >& 2
+
+               return 1
+       fi
+
+       # Using xargs(1) to fail as soon as the formatter fails (`-exec`
+       # won't stop if its subprocess fails).
+       #
+       # We want an absolute starting directory because find(1) excludes
+       # files in specific subdirectories.
+       find "$(realpath "$root_dir")" \( -name '*.cpp' -o -name '*.hpp' \) \
+               ! -path '*/src/cpp-common/optional.hpp' \
+               ! -path '*/src/cpp-common/string_view.hpp' \
+               ! -path '*/src/cpp-common/nlohmann/json.hpp' \
+               ! -path '*/src/plugins/ctf/common/metadata/parser.*' \
+               ! -path '*/src/plugins/ctf/common/metadata/lexer.*' \
+               -print0 | xargs -P"$(nproc)" -n1 -t -0 "${formatter[@]}"
+}
+
+# Choose formatter
+if [[ -n "$FORMATTER" ]]; then
+       # Try using environment-provided 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)
+else
+       # Try using `clang-format` as is
+       formatter=(clang-format -i)
+fi
+
+# Choose root directory
+if (($# == 1)); then
+       root_dir=$1
+
+       if [[ ! -d "$root_dir" ]]; then
+               echo "\`$root_dir\`: expecting an existing directory." >& 2
+               exit 1
+       fi
+else
+       # Default: root of the project, processing all C++ files
+       root_dir="$(dirname "${BASH_SOURCE[0]}")/.."
+fi
+
+# Try to format files
+format_cpp "$root_dir" "${formatter[@]}"
This page took 0.02756 seconds and 4 git commands to generate.