Maksims Svecovs | 9d0e79c | 2023-02-09 16:48:34 +0000 | [diff] [blame] | 1 | #!/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 | |
| 8 | RED="\033[00;31m" |
| 9 | YELLOW="\033[00;33m" |
| 10 | BLANK="\033[00;00m" |
| 11 | |
| 12 | FILES=`git diff --cached --name-only HEAD` |
| 13 | YEAR_NOW=`date +"%Y"` |
| 14 | |
| 15 | YEAR_RGX="[0-9][0-9][0-9][0-9]" |
| 16 | ARM_RGX="\(ARM\|Arm\|arm\)" |
| 17 | |
| 18 | exit_code=0 |
| 19 | |
Akshay Belsare | 682257c | 2023-04-14 14:10:09 +0530 | [diff] [blame] | 20 | PLATPROV= |
| 21 | ORG=`echo "$GIT_AUTHOR_EMAIL" | awk -F '[@]' '{ print $2;}'` |
| 22 | |
| 23 | case $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 | ;; |
| 32 | esac |
| 33 | |
Maksims Svecovs | 9d0e79c | 2023-02-09 16:48:34 +0000 | [diff] [blame] | 34 | function user_warning() { |
Maksims Svecovs | 5c54b4c | 2023-03-15 13:24:44 +0000 | [diff] [blame] | 35 | echo -e "Copyright of $RED$FILE$BLANK is out of date/incorrect" |
Maksims Svecovs | 9d0e79c | 2023-02-09 16:48:34 +0000 | [diff] [blame] | 36 | echo -e "Updated copyright to" |
Akshay Belsare | 682257c | 2023-04-14 14:10:09 +0530 | [diff] [blame] | 37 | grep -nr "opyright.*$YEAR_RGX.*$PLATPROV" "$FILE" |
Maksims Svecovs | 9d0e79c | 2023-02-09 16:48:34 +0000 | [diff] [blame] | 38 | echo |
| 39 | } |
| 40 | |
| 41 | while read -r FILE; do |
| 42 | if [ -z "$FILE" ] |
| 43 | then |
| 44 | break |
| 45 | fi |
Akshay Belsare | 682257c | 2023-04-14 14:10:09 +0530 | [diff] [blame] | 46 | |
| 47 | # Check if copyright header exists for the org |
| 48 | if ! grep "opyright.*$YEAR_RGX.*$PLATPROV" "$FILE">/dev/null 2>&1 && [[ $ORG != *arm* ]] |
Maksims Svecovs | 9d0e79c | 2023-02-09 16:48:34 +0000 | [diff] [blame] | 49 | then |
Akshay Belsare | 682257c | 2023-04-14 14:10:09 +0530 | [diff] [blame] | 50 | 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 Svecovs | 9d0e79c | 2023-02-09 16:48:34 +0000 | [diff] [blame] | 57 | then |
Akshay Belsare | 682257c | 2023-04-14 14:10:09 +0530 | [diff] [blame] | 58 | # 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 Svecovs | 9d0e79c | 2023-02-09 16:48:34 +0000 | [diff] [blame] | 82 | fi |
Maksims Svecovs | 9d0e79c | 2023-02-09 16:48:34 +0000 | [diff] [blame] | 83 | fi |
Akshay Belsare | 682257c | 2023-04-14 14:10:09 +0530 | [diff] [blame] | 84 | |
Maksims Svecovs | 9d0e79c | 2023-02-09 16:48:34 +0000 | [diff] [blame] | 85 | done <<< "$FILES" |
| 86 | |
| 87 | if [ $exit_code -eq 1 ] |
| 88 | then |
| 89 | echo -e "$RED""Please stage updated files$BLANK before commiting or use$YELLOW git commit --no-verify$BLANK to skip copyright check" |
| 90 | fi |
| 91 | exit $exit_code |