1#!/usr/bin/env bash
2#
3# SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
4#
5# SPDX-License-Identifier: Apache-2.0
6#
7# Licensed under the Apache License, Version 2.0 (the License); you may
8# not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an AS IS BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19# Version: 1.0
20# Date: 2024-05-21
21# This bash script checks that files changed in the Source and Include directory have
22# modifed the Revision and Date fields below the license in each file.
23
24SHA=$(git rev-parse origin/upstream/main)
25CHANGED_FILES=$(git diff --name-only ${SHA} HEAD -- Source Include)
26ANY_FAILURE=0
27
28echo "++ Checking if version and date was updated in changed files"
29echo " ------ "
30for file in ${CHANGED_FILES[@]}
31do
32    if [[ ${file} != *"CMake"* ]]; then
33        diff=$(git diff ${SHA} HEAD ${file})
34        # This pattern matches "(+|-) * $DATE: Day Month Year"
35        echo "$diff" | grep -E '(-|\+)\s*\*\s*\$[Date]+:\s*[0-9]+\s*[A-Za-z]+\s*[0-9]+' -vqz
36        if [[ $? -eq 0 ]]; then
37            echo "${file}: FAILED"
38            ANY_FAILURE=1
39        else
40            # This pattern matches "(+|-) * $REVISION: V.X.X.X"
41            echo "$diff" | grep -E '(-|\+)\s*\*\s*\$[Revision]+:\s*V\.[0-9]+\.[0-9]+\.[0-9]+' -vqz
42            if [[ $? -eq 0 ]]; then
43                echo "${file}: FAILED"
44                ANY_FAILURE=1
45            else
46                echo "${file}: OK"
47            fi
48        fi
49    fi
50done
51
52if [[ $ANY_FAILURE -eq 1 ]]; then
53    echo " ------ "
54    echo "At least one file did not pass the revision and date check. Exiting with failure."
55    exit 1
56elif [[ $ANY_FAILURE -eq 0 ]]; then
57    echo " ------ "
58    echo "All files passed the revision and date check. Exiting successfully."
59fi