• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Format the specified source files to conform the code style."
37  echo "Usage:"
38  echo "bash $0 [-a] [-c] [-l] [-h]"
39  echo "e.g. $0 -c"
40  echo ""
41  echo "Options:"
42  echo "    -a format of all files"
43  echo "    -c format of the files changed compared to last commit, default case"
44  echo "    -l 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="changed"    # default format changed 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
86FMT_FILE_LIST='__format_files_list__'
87
88if [[ "X${mode}" == "Xall" ]]; then
89  find mindspore/{ccsrc,core,lite} -type f -name "*" | grep -E "(\.h$|\.cc$|\.c$)" > "${FMT_FILE_LIST}" || true
90elif [[ "X${mode}" == "Xchanged" ]]; then
91  git diff --name-only | grep "mindspore/ccsrc\|mindspore/core\|mindspore/lite\|include" | grep -E "(\.h$|\.cc$|\.c$)" > "${FMT_FILE_LIST}" || true
92else  # "X${mode}" == "Xlastcommit"
93  git diff --name-only HEAD~ HEAD | grep "mindspore/ccsrc\|mindspore/core\|mindspore/lite\|include" | grep -E "(\.h$|\.cc$|\.c$)" > "${FMT_FILE_LIST}" || true
94fi
95
96while read line; do
97  if [ -f "${line}" ]; then
98    ${CLANG_FORMAT} -i "${line}"
99  fi
100done < "${FMT_FILE_LIST}"
101
102rm "${FMT_FILE_LIST}"
103cd "${CURRENT_PATH}" || exit 1
104
105echo "Specified cpp source files have been format successfully."
106