1#!/bin/bash 2# Copyright 2019 Huawei Technologies Co., Ltd 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# ============================================================================ 16 17set -e 18 19CLANG_FORMAT=$(which clang-format) || (echo "Please install 'clang-format' tool first"; exit 1) 20 21version=$("${CLANG_FORMAT}" --version | sed -n "s/.*\ \([0-9]*\)\.[0-9]*\.[0-9]*.*/\1/p") 22if [[ "${version}" -lt "8" ]]; then 23 echo "clang-format's version must be at least 8.0.0" 24 exit 1 25fi 26 27CURRENT_PATH=$(pwd) 28SCRIPTS_PATH=$(dirname "$0") 29 30echo "CURRENT_PATH=$CURRENT_PATH" 31echo "SCRIPTS_PATH=$SCRIPTS_PATH" 32 33# print usage message 34function usage() 35{ 36 echo "Check whether the specified source files were well formatted" 37 echo "Usage:" 38 echo "bash $0 [-a] [-c] [-l] [-h]" 39 echo "e.g. $0 -a" 40 echo "" 41 echo "Options:" 42 echo " -a Check code format of all files, default case" 43 echo " -c Check code format of the files changed compared to last commit" 44 echo " -l Check code format of the files changed in last commit" 45 echo " -h Print usage" 46} 47 48# check and set options 49function checkopts() 50{ 51 # init variable 52 mode="all" # default check all files 53 54 # Process the options 55 while getopts 'aclh' opt 56 do 57 case "${opt}" in 58 a) 59 mode="all" 60 ;; 61 c) 62 mode="changed" 63 ;; 64 l) 65 mode="lastcommit" 66 ;; 67 h) 68 usage 69 exit 0 70 ;; 71 *) 72 echo "Unknown option ${opt}!" 73 usage 74 exit 1 75 esac 76 done 77} 78 79# init variable 80# check options 81checkopts "$@" 82 83# switch to project root path, which contains clang-format config file '.clang-format' 84cd "${SCRIPTS_PATH}/.." || exit 1 85 86CHECK_LIST_FILE='__checked_files_list__' 87 88if [ "X${mode}" == "Xall" ]; then 89 find mindspore/{ccsrc,core,lite} -type f -name "*" | grep "\.h$\|\.cc$\|\.c$" > "${CHECK_LIST_FILE}" || true 90elif [ "X${mode}" == "Xchanged" ]; then 91 # --diff-filter=ACMRTUXB will ignore deleted files in commit 92 git diff --diff-filter=ACMRTUXB --name-only | grep "mindspore/ccsrc\|mindspore/core\|mindspore/lite" | grep "\.h$\|\.cc$\|\.c$" > "${CHECK_LIST_FILE}" || true 93else # "X${mode}" == "Xlastcommit" 94 git diff --diff-filter=ACMRTUXB --name-only HEAD~ HEAD | grep "mindspore/ccsrc\|mindspore/core\|mindspore/lite" | grep "\.h$\|\.cc$\|\.c$" > "${CHECK_LIST_FILE}" || true 95fi 96 97CHECK_RESULT_FILE=__code_format_check_result__ 98echo "0" > "$CHECK_RESULT_FILE" 99 100# check format of files modified in the latest commit 101while read line; do 102 if [ ! -e ${line} ]; then 103 continue 104 fi 105 BASE_NAME=$(basename "${line}") 106 TEMP_FILE="__TEMP__${BASE_NAME}" 107 cp "${line}" "${TEMP_FILE}" 108 ${CLANG_FORMAT} -i "${TEMP_FILE}" 109 diff "${TEMP_FILE}" "${line}" 110 ret=$? 111 rm "${TEMP_FILE}" 112 if [[ "${ret}" -ne 0 ]]; then 113 echo "File ${line} is not formatted, please format it." 114 echo "1" > "${CHECK_RESULT_FILE}" 115 break 116 fi 117done < "${CHECK_LIST_FILE}" 118 119result=$(cat "${CHECK_RESULT_FILE}") 120rm "${CHECK_RESULT_FILE}" 121rm "${CHECK_LIST_FILE}" 122cd "${CURRENT_PATH}" || exit 1 123if [[ "X${result}" == "X0" ]]; then 124 echo "Check PASS: specified files are well formatted!" 125fi 126exit "${result}" 127