xref: /aosp_15_r20/external/grpc-grpc/tools/distrib/clang_format_code.sh (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1#!/bin/bash
2# Copyright 2015 gRPC authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16set -e
17
18# change to root directory
19cd $(dirname $0)/../..
20REPO_ROOT=$(pwd)
21
22if [ "$CLANG_FORMAT_SKIP_DOCKER" == "" ]; then
23  CLANG_FORMAT_ROOT="/local-code"
24  # build clang-format docker image
25  docker build -t grpc_clang_format tools/dockerfile/grpc_clang_format
26
27  if [ "$CHANGED_FILES" != "" ]; then
28    # ensure the CLANG_FORMAT_ROOT is properly set for any CHANGED_FILES
29    CHANGED_FILES=$(printf "$CLANG_FORMAT_ROOT/%s " $CHANGED_FILES)
30  fi
31
32  # run clang-format against the checked out codebase
33  # when modifying the checked-out files, the current user will be impersonated
34  # so that the updated files don't end up being owned by "root".
35  docker run -e TEST="$TEST" -e CHANGED_FILES="$CHANGED_FILES" -e CLANG_FORMAT_ROOT=$CLANG_FORMAT_ROOT --rm=true -v "${REPO_ROOT}":/local-code --user "$(id -u):$(id -g)" -t grpc_clang_format /clang_format_all_the_things.sh
36else
37  export CLANG_FORMAT_ROOT="${REPO_ROOT}"
38  if [ "$CHANGED_FILES" != "" ]; then
39    # ensure the CLANG_FORMAT_ROOT is properly set for any CHANGED_FILES
40    CHANGED_FILES=$(printf "$CLANG_FORMAT_ROOT/%s " $CHANGED_FILES)
41  fi
42  tools/dockerfile/grpc_clang_format/clang_format_all_the_things.sh
43fi
44