|
| 1 | +# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +set +x |
| 16 | + |
| 17 | +# use pre-commit 2.17 |
| 18 | +if ! [[ $(pre-commit --version) == *"2.17.0"* ]]; then |
| 19 | + pip install pre-commit==2.17.0 1>nul |
| 20 | +fi |
| 21 | + |
| 22 | +# Install clang-format before git commit to avoid repeat installation due to |
| 23 | +# pre-commit multi-thread running. |
| 24 | +readonly VERSION="13.0.0" |
| 25 | +version=$(clang-format -version) |
| 26 | +if ! [[ $(python -V 2>&1 | awk '{print $2}' | awk -F '.' '{print $1$2}') -ge 36 ]]; then |
| 27 | + echo "clang-format installation by pip need python version great equal 3.6, |
| 28 | + please change the default python to higher version." |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +diff_files=$(git diff --name-only --diff-filter=ACMR ${BRANCH}) |
| 33 | +num_diff_files=$(echo "$diff_files" | wc -l) |
| 34 | +echo -e "diff files between pr and ${BRANCH}:\n${diff_files}" |
| 35 | + |
| 36 | +echo "Checking code style by pre-commit ..." |
| 37 | +pre-commit run --files ${diff_files};check_error=$? |
| 38 | + |
| 39 | +if test ! -z "$(git diff)"; then |
| 40 | + echo -e '\n************************************************************************************' |
| 41 | + echo -e "These files have been formatted by code format hook. You should use pre-commit to \ |
| 42 | +format them before git push." |
| 43 | + echo -e '************************************************************************************\n' |
| 44 | + git diff 2>&1 |
| 45 | +fi |
| 46 | + |
| 47 | +echo -e '\n************************************************************************************' |
| 48 | +if [ ${check_error} != 0 ];then |
| 49 | + echo "Your PR code style check failed." |
| 50 | + echo "Please install pre-commit locally and set up git hook scripts:" |
| 51 | + echo "" |
| 52 | + echo " pip install pre-commit==2.17.0" |
| 53 | + echo " pre-commit install" |
| 54 | + echo "" |
| 55 | + if [[ $num_diff_files -le 100 ]];then |
| 56 | + echo "Then, run pre-commit to check codestyle issues in your PR:" |
| 57 | + echo "" |
| 58 | + echo " pre-commit run --files" $(echo ${diff_files} | tr "\n" " ") |
| 59 | + echo "" |
| 60 | + fi |
| 61 | + echo "For more information, please refer to our codestyle check guide:" |
| 62 | + echo "https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/dev_guides/git_guides/codestyle_check_guide_cn.html" |
| 63 | +else |
| 64 | + echo "Your PR code style check passed." |
| 65 | +fi |
| 66 | +echo -e '************************************************************************************\n' |
| 67 | + |
| 68 | +exit ${check_error} |
0 commit comments