|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright 2016 Istio Authors. All Rights Reserved. |
| 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 | +################################################################################ |
| 18 | +# |
| 19 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 20 | + |
| 21 | +# Install required clang version to a folder and cache it. |
| 22 | +CLANG_VERSION_REQUIRED="3.8.0" |
| 23 | +CLANG_DIRECTORY="${HOME}/clang" |
| 24 | +CLANG_FORMAT="${CLANG_DIRECTORY}/bin/clang-format" |
| 25 | + |
| 26 | +CLANG_VERSION="$(${CLANG_FORMAT} -version | cut -d ' ' -f 3)" |
| 27 | +if [[ "${CLANG_VERSION}" != "${CLANG_VERSION_REQUIRED}" ]]; then |
| 28 | + echo "Installing required clang-format ${CLANG_VERSION_REQUIRED} to ${CLANG_DIRECTORY}" |
| 29 | + mkdir -p ${CLANG_DIRECTORY} |
| 30 | + curl --silent --show-error --retry 10 \ |
| 31 | + "http://releases.llvm.org/${CLANG_VERSION_REQUIRED}/clang+llvm-${CLANG_VERSION_REQUIRED}-x86_64-linux-gnu-ubuntu-14.04.tar.xz" \ |
| 32 | + | tar Jx -C "${CLANG_DIRECTORY}" --strip=1 \ |
| 33 | + || { echo "Could not install required clang-format. Skip formating." ; exit 0 ; } |
| 34 | +fi |
| 35 | + |
| 36 | +echo "Checking file format ..." |
| 37 | + |
| 38 | +pushd ${ROOT} > /dev/null |
| 39 | + |
| 40 | +SOURCE_FILES=($(git ls-tree -r HEAD --name-only | grep -E '\.(h|c|cc|proto)$')) |
| 41 | +"${CLANG_FORMAT}" -style=Google -i "${SOURCE_FILES[@]}" \ |
| 42 | + || { echo "Could not run clang-format." ; exit 1 ; } |
| 43 | + |
| 44 | +CHANGED_FILES=($(git diff HEAD --name-only | grep -E '\.(h|c|cc|proto)$')) |
| 45 | + |
| 46 | +if [[ "${#CHANGED_FILES}" -ne 0 ]]; then |
| 47 | + echo "Files not formatted: ${CHANGED_FILES[@]}" |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | +echo "All files are properly formatted." |
| 51 | + |
| 52 | +popd |
0 commit comments