tools/format-cpp: accept optional starting directory
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 26 Oct 2023 13:25:16 +0000 (09:25 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 30 Oct 2023 18:15:46 +0000 (14:15 -0400)
This patch makes `tools/format-cpp` accept an optional starting
directory (instead of the root directory of the project) as its first
argument:

    $ pwd
    /home/dboucher/Mes Logiciels/babeltrace/src/plugins/ctf

    $ ../../../tools/format-cpp .
    clang-format -i /home/eepp/dev/babeltrace/src/plugins/ctf/fs-src/fs.cpp
    clang-format -i /home/eepp/dev/babeltrace/src/plugins/ctf/fs-src/file.cpp
    clang-format -i /home/eepp/dev/babeltrace/src/plugins/ctf/fs-src/query.cpp
    ...
    clang-format -i /home/eepp/dev/babeltrace/src/plugins/ctf/lttng-live/metadata.hpp
    clang-format -i /home/eepp/dev/babeltrace/src/plugins/ctf/lttng-live/data-stream.hpp

Using realpath(1) to transform the starting directory because find(1)
will only consider file names relative to its starting point, therefore
if the starting point is `.` (and the CWD is `src/cpp-common`),
`*/src/cpp-common/optional.hpp` and so on don't match anything and the
script will format files to be excluded.

Using `-path` instead of `-wholename` which is more precise and also
seems more portable.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I46219035e9b815dffe9200bb924478e4a7b02a04
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11145

CONTRIBUTING.adoc
tools/format-cpp

index 23808c041e61e99dc856f9044a4f2b74fe11a64c..d5241ff4a8d611b924a9713f94116d292059cbc9 100644 (file)
@@ -1683,11 +1683,17 @@ To automatically format all the project's {cpp} files, run:
 $ ./tools/format-cpp
 ----
 
+Pass a directory path to only format the {cpp} files it contains:
+
+----
+$ ./tools/format-cpp ./src/cli
+----
+
 Use the `FORMATTER` environment variable to override the default
 formatter (`clang-format{nbsp}-i`):
 
 ----
-$ FORMATTER='clang-format-10 -i' ./tools/format-cpp
+$ FORMATTER='my-clang-format-15 -i' ./tools/format-cpp
 ----
 
 ==== Naming
index dfc4bbd5061a74e3dd5f179e5e53069312a2cfe3..8094f13518990af8ce268630e85f2ca8cdc730f5 100755 (executable)
@@ -2,12 +2,22 @@
 #
 # SPDX-License-Identifier: GPL-2.0-only
 #
-# Copyright (C) 2020-2022 Philippe Proulx <pproulx@efficios.com>
+# Copyright (C) 2020-2023 Philippe Proulx <pproulx@efficios.com>
 
 expected_formatter_major_version=15
 
-# Runs the formatter, making sure it's the expected version.
+# 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
 
@@ -28,19 +38,19 @@ format_cpp() {
                return 1
        fi
 
-       local root_dir
-
-       root_dir="$(dirname "${BASH_SOURCE[0]}")/.."
-
-       # Using xargs to fail as soon as the formatter fails (`-exec`
+       # Using xargs(1) to fail as soon as the formatter fails (`-exec`
        # won't stop if its subprocess fails).
-       find "$root_dir" \( -name '*.cpp' -o -name '*.hpp' \) \
-               ! -wholename '*/cpp-common/optional.hpp' \
-               ! -wholename '*/cpp-common/string_view.hpp' \
-               ! -wholename '*/cpp-common/nlohmann/json.hpp' \
+       #
+       # 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' \
                -print0 | xargs -P"$(nproc)" -n1 -t -0 "${formatter[@]}"
 }
 
+# Choose formatter
 if [[ -n "$FORMATTER" ]]; then
        # Try using environment-provided formatter
        read -ra formatter <<< "$FORMATTER"
@@ -52,5 +62,18 @@ else
        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 "${formatter[@]}"
+format_cpp "$root_dir" "${formatter[@]}"
This page took 0.02678 seconds and 4 git commands to generate.