| #! /bin/bash |
| # Copyright 2023 Google LLC |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| # Wrapper to run yapf-diff for repo preupload. |
| # Usage: if the first argument is --bypass, do nothing (exit 0). If the first |
| # argument is a git hash, run yapf-diff on that commit, otherwise run yapf-diff |
| # on the last commit. Any other arguments are passed to yapf-diff. |
| # The script fails if yapf-diff produces any output. |
| |
| COMMIT="$(git log --format='%H' -1)" |
| |
| if [ $# -gt 0 ] ; then |
| if [ "$1" == "--bypass" ] ; then |
| exit 0 |
| fi |
| |
| if [[ ! "$1" =~ ^- ]] ; then |
| COMMIT="$1" |
| shift |
| fi |
| fi |
| |
| if git show -U0 --no-color "${COMMIT}" | yapf-diff "$@" | grep --color=never . ; then |
| cat <<EOF |
| |
| |
| Please fix the formatting issues above before uploading. |
| You can apply the changes above by running: |
| $0 $COMMIT -i |
| EOF |
| exit 1 |
| fi |