Skip to content

Commit e1db23f

Browse files
authored
clang-format: Use config directly instead of modifying repository [AP-3053] (#146)
The clang-format logic relied on copying the config file into the repo root, which pollutes the repository and might fail if there is already a symlink. This change modifies the logic to directly pass the config location to the `clang-format` CLI. Tested in `starling-core`: ``` bazel run @rules_swiftnav//clang_format INFO: Analyzed target @rules_swiftnav//clang_format:clang_format (0 packages loaded, 0 targets configured). INFO: Found 1 target... Target @rules_swiftnav//clang_format:clang_format up-to-date: bazel-bin/external/rules_swiftnav/clang_format/clang_format INFO: Elapsed time: 0.255s, Critical Path: 0.12s INFO: 1 process: 1 internal. INFO: Build completed successfully, 1 total action INFO: Running command line: bazel-bin/external/rules_swiftnav/clang_format/clang_format format_diff external/aarch64-darwin-llvm/bin/clang-format external/rules_swiftnav/clang_format/.clang-format ```
1 parent 2deb089 commit e1db23f

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

clang_format/run_clang_format.sh

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,20 @@
55
set -ue
66

77
format_all() {
8-
CLANG_FORMAT_CONFIG=$(realpath $1)
8+
CLANG_FORMAT_CONFIG=$(realpath "$1")
99

10-
cd $BUILD_WORKSPACE_DIRECTORY
11-
if ! test -f .clang-format; then
12-
echo ".clang-format file not found. Bazel will copy the default .clang-format file."
13-
cp $CLANG_FORMAT_CONFIG .
14-
fi
10+
cd "$BUILD_WORKSPACE_DIRECTORY"
1511
git ls-files '*.[ch]' '*.cpp' '*.cxx' '*.cc' '*.hpp' '*.hxx' \
16-
| xargs -r $CLANG_FORMAT_BIN -i
12+
| xargs -r "$CLANG_FORMAT_BIN" -i --style=file:"$CLANG_FORMAT_CONFIG"
1713
}
1814

1915
format_diff() {
20-
CLANG_FORMAT_CONFIG=$(realpath $1)
16+
CLANG_FORMAT_CONFIG=$(realpath "$1")
2117

22-
cd $BUILD_WORKSPACE_DIRECTORY
23-
if ! test -f .clang-format; then
24-
echo ".clang-format file not found. Bazel will copy the default .clang-format file."
25-
cp $CLANG_FORMAT_CONFIG .
26-
fi
18+
cd "$BUILD_WORKSPACE_DIRECTORY"
2719
git describe --tags --abbrev=0 --always \
28-
| xargs -rI % git diff --diff-filter=ACMRTUXB --name-only --line-prefix=`git rev-parse --show-toplevel`/ % -- '*.[ch]' '*.cpp' '*.cxx' '*.cc' '*.hpp' '*.hxx' \
29-
| xargs -r $CLANG_FORMAT_BIN -i
20+
| xargs -rI % git diff --diff-filter=ACMRTUXB --name-only --line-prefix="$(git rev-parse --show-toplevel)"/ % -- '*.[ch]' '*.cpp' '*.cxx' '*.cc' '*.hpp' '*.hxx' \
21+
| xargs -r "$CLANG_FORMAT_BIN" -i --style=file:"$CLANG_FORMAT_CONFIG"
3022
}
3123

3224
check_file() {
@@ -36,14 +28,14 @@ check_file() {
3628

3729
# .clang-format config file has to be placed in the current working directory
3830
if [ ! -f ".clang-format" ]; then
39-
ln -s $CONFIG .clang-format
31+
ln -s "$CONFIG" .clang-format
4032
fi
4133

42-
$CLANG_FORMAT_BIN $INPUT --dry-run -Werror > $OUTPUT
34+
$CLANG_FORMAT_BIN "$INPUT" --dry-run -Werror > "$OUTPUT"
4335
}
4436

4537
ARG=$1
46-
CLANG_FORMAT_BIN=$(realpath $2)
38+
CLANG_FORMAT_BIN=$(realpath "$2")
4739
shift 2
4840

4941
if [ "$ARG" == "format_diff" ]; then

0 commit comments

Comments
 (0)