blob: 5f838a688719fbfb2848e3a4c6c24982a5f4eb00 [file] [log] [blame]
Maksims Svecovs9d0e79c2023-02-09 16:48:34 +00001#!/bin/bash
2
3# A hook script that checks if files staged for commit have updated Arm copyright year.
4# In case they are not - updates the years and prompts user to add them to the change.
5# This hook is called on "git commit" after changes have been staged, but before commit
6# message has to be provided.
7
8RED="\033[00;31m"
9YELLOW="\033[00;33m"
10BLANK="\033[00;00m"
11
12FILES=`git diff --cached --name-only HEAD`
13YEAR_NOW=`date +"%Y"`
14
15YEAR_RGX="[0-9][0-9][0-9][0-9]"
16ARM_RGX="\(ARM\|Arm\|arm\)"
17
18exit_code=0
19
Akshay Belsare682257c2023-04-14 14:10:09 +053020PLATPROV=
21ORG=`echo "$GIT_AUTHOR_EMAIL" | awk -F '[@]' '{ print $2;}'`
22
23case $ORG in
24 amd.com)
25 PLATPROV="Advanced Micro Devices, Inc. All rights reserved."
26 ;;
27 *arm.com)
28 PLATPROV="$ARM_RGX"
29 ;;
30 *)
31 ;;
32esac
33
Maksims Svecovs9d0e79c2023-02-09 16:48:34 +000034function user_warning() {
Maksims Svecovs5c54b4c2023-03-15 13:24:44 +000035 echo -e "Copyright of $RED$FILE$BLANK is out of date/incorrect"
Maksims Svecovs9d0e79c2023-02-09 16:48:34 +000036 echo -e "Updated copyright to"
Akshay Belsare682257c2023-04-14 14:10:09 +053037 grep -nr "opyright.*$YEAR_RGX.*$PLATPROV" "$FILE"
Maksims Svecovs9d0e79c2023-02-09 16:48:34 +000038 echo
39}
40
41while read -r FILE; do
42 if [ -z "$FILE" ]
43 then
44 break
45 fi
Akshay Belsare682257c2023-04-14 14:10:09 +053046
47 # Check if copyright header exists for the org
48 if ! grep "opyright.*$YEAR_RGX.*$PLATPROV" "$FILE">/dev/null 2>&1 && [[ $ORG != *arm* ]]
Maksims Svecovs9d0e79c2023-02-09 16:48:34 +000049 then
Akshay Belsare682257c2023-04-14 14:10:09 +053050 echo -e "Copyright header ""$RED""$PLATPROV""$BLANK"" is missing in ""$YELLOW""$FILE""$BLANK"
51 fi
52
53 # Check if the copyright year is updated for the org and update it
54 if [ ! -z "$PLATPROV" ]
55 then
56 if ! grep "opyright.*$YEAR_NOW.*$PLATPROV" "$FILE">/dev/null 2>&1
Maksims Svecovs9d0e79c2023-02-09 16:48:34 +000057 then
Akshay Belsare682257c2023-04-14 14:10:09 +053058 # If it is "from_date - to_date" type of entry - change to_date entry.
59 if grep "opyright.*$YEAR_RGX.*-.*$YEAR_RGX.*$PLATPROV" "$FILE" >/dev/null 2>&1
60 then
61 exit_code=1
62 sed -i "s/\(opyright.*\)$YEAR_RGX\(.*$PLATPROV\)/\1$(date +"%Y")\2/" $FILE
63 user_warning
64 # If it is single "date" type of entry - add the copyright extension to current year.
65 elif grep "opyright.*$YEAR_RGX.*$PLATPROV" "$FILE" >/dev/null 2>&1
66 then
67 exit_code=1
68 sed -i "s/\(opyright.*$YEAR_RGX\)\(.*$PLATPROV\)/\1-$(date +"%Y")\2/" $FILE
69 user_warning
70 fi
71
72 # Even if the year is correct - verify that Arm copyright is formatted correctly.
73 if [[ $ORG == *arm* ]]
74 then
75 if grep "opyright.*\(ARM\|arm\)" "$FILE">/dev/null 2>&1
76 then
77 exit_code=1
78 sed -i "s/\(opyright.*\)\(ARM\|arm\)/\1Arm/" $FILE
79 user_warning
80 fi
81 fi
Maksims Svecovs9d0e79c2023-02-09 16:48:34 +000082 fi
Maksims Svecovs9d0e79c2023-02-09 16:48:34 +000083 fi
Akshay Belsare682257c2023-04-14 14:10:09 +053084
Maksims Svecovs9d0e79c2023-02-09 16:48:34 +000085done <<< "$FILES"
86
87if [ $exit_code -eq 1 ]
88then
89 echo -e "$RED""Please stage updated files$BLANK before commiting or use$YELLOW git commit --no-verify$BLANK to skip copyright check"
90fi
91exit $exit_code