1fd4e5da5Sopenharmony_ci#!/bin/bash
2fd4e5da5Sopenharmony_ci# Copyright (c) 2017 Google Inc.
3fd4e5da5Sopenharmony_ci
4fd4e5da5Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
5fd4e5da5Sopenharmony_ci# you may not use this file except in compliance with the License.
6fd4e5da5Sopenharmony_ci# You may obtain a copy of the License at
7fd4e5da5Sopenharmony_ci#
8fd4e5da5Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
9fd4e5da5Sopenharmony_ci#
10fd4e5da5Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
11fd4e5da5Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
12fd4e5da5Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fd4e5da5Sopenharmony_ci# See the License for the specific language governing permissions and
14fd4e5da5Sopenharmony_ci# limitations under the License.
15fd4e5da5Sopenharmony_ci#
16fd4e5da5Sopenharmony_ci# Script to determine if source code in Pull Request is properly formatted.
17fd4e5da5Sopenharmony_ci# Exits with non 0 exit code if formatting is needed.
18fd4e5da5Sopenharmony_ci#
19fd4e5da5Sopenharmony_ci# This script assumes to be invoked at the project root directory.
20fd4e5da5Sopenharmony_ci
21fd4e5da5Sopenharmony_ciBASE_BRANCH=${1:-main}
22fd4e5da5Sopenharmony_ci
23fd4e5da5Sopenharmony_ciFILES_TO_CHECK=$(git diff --name-only ${BASE_BRANCH} | grep -E ".*\.(cpp|cc|c\+\+|cxx|c|h|hpp)$")
24fd4e5da5Sopenharmony_ci
25fd4e5da5Sopenharmony_ciif [ -z "${FILES_TO_CHECK}" ]; then
26fd4e5da5Sopenharmony_ci  echo "No source code to check for formatting."
27fd4e5da5Sopenharmony_ci  exit 0
28fd4e5da5Sopenharmony_cifi
29fd4e5da5Sopenharmony_ci
30fd4e5da5Sopenharmony_ciFORMAT_DIFF=$(git diff -U0 ${BASE_BRANCH} -- ${FILES_TO_CHECK} | python ./utils/clang-format-diff.py -p1 -style=file)
31fd4e5da5Sopenharmony_ci
32fd4e5da5Sopenharmony_ciif [ -z "${FORMAT_DIFF}" ]; then
33fd4e5da5Sopenharmony_ci  echo "All source code in PR properly formatted."
34fd4e5da5Sopenharmony_ci  exit 0
35fd4e5da5Sopenharmony_cielse
36fd4e5da5Sopenharmony_ci  echo "Found formatting errors!"
37fd4e5da5Sopenharmony_ci  echo "${FORMAT_DIFF}"
38fd4e5da5Sopenharmony_ci  exit 1
39fd4e5da5Sopenharmony_cifi
40