|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2026 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -o errexit -o nounset -o pipefail |
| 18 | + |
| 19 | +ROOT="$(git rev-parse --show-toplevel)" |
| 20 | +cd "${ROOT}" |
| 21 | + |
| 22 | +# Find clang-format in the PATH, and exit if not found. |
| 23 | +tool="clang-format" |
| 24 | +clangfmt="$(which "$tool" 2>/dev/null || true)" |
| 25 | +if [[ ! -x "${clangfmt}" ]]; then |
| 26 | + echo "Failed to find ${tool}: please make sure it is in your PATH" >&2 |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +# Find all top-level directories containing proto files, and run clangfmt on them. |
| 31 | +# shellcheck disable=SC2207 # reading array |
| 32 | +files=( |
| 33 | + $(git ls-files \ |
| 34 | + -cmo \ |
| 35 | + --exclude-standard \ |
| 36 | + -- \ |
| 37 | + ':(glob)**/*.proto' \ |
| 38 | + ':!:vendor/*' \ |
| 39 | + ':!:**/vendor/*' \ |
| 40 | + ':!:LICENSES/*' \ |
| 41 | + | sort \ |
| 42 | + | uniq) |
| 43 | +) |
| 44 | + |
| 45 | +# Run clang-format on the found files. |
| 46 | +# |
| 47 | +# Don't reflow long lines, wince those tend to be comments which then require |
| 48 | +# re-running the proto generators, which makes "update-all" awkward. |
| 49 | +"${clangfmt}" \ |
| 50 | + -i \ |
| 51 | + --style="{BasedOnStyle: LLVM, ColumnLimit: 0}" \ |
| 52 | + "${files[@]}" |
0 commit comments