From 93d1f4bc749e157cdfbe060b7e10351f460dedef Mon Sep 17 00:00:00 2001 From: Akshay Belsare Date: Fri, 14 Apr 2023 14:10:09 +0530 Subject: [PATCH] style(hooks): copyright year check as per author email Add a check in pre-commit hook to check the - copyright header is present for the authors organisation. - the copyright year for the copyright header is updated. The author email id is parsed to get the organization. Depending upon the parsed info, the copyright header for the organization is checked if its present in the file(s) or not. If the copyright header is present in the file(s) then the copyright year is checked. If the copyright header is not present or the copyright year in the header is not updated it is highlighted to the author which the user then needs to incorporate in the change accordingly. To enable this check, the case statement in .husky/pre-commit.copyright needs to be modified to add the domain from the email id and corresponding copyright header of the organisation. Change-Id: I4dedb68248b3dae997d887dd380155fe326d071d Signed-off-by: Akshay Belsare --- .husky/pre-commit.copyright | 76 +++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 24 deletions(-) diff --git a/.husky/pre-commit.copyright b/.husky/pre-commit.copyright index a4dfee8e3..5f838a688 100755 --- a/.husky/pre-commit.copyright +++ b/.husky/pre-commit.copyright @@ -17,10 +17,24 @@ ARM_RGX="\(ARM\|Arm\|arm\)" exit_code=0 +PLATPROV= +ORG=`echo "$GIT_AUTHOR_EMAIL" | awk -F '[@]' '{ print $2;}'` + +case $ORG in + amd.com) + PLATPROV="Advanced Micro Devices, Inc. All rights reserved." + ;; + *arm.com) + PLATPROV="$ARM_RGX" + ;; + *) + ;; +esac + function user_warning() { echo -e "Copyright of $RED$FILE$BLANK is out of date/incorrect" echo -e "Updated copyright to" - grep -nr "opyright.*$YEAR_RGX.*$ARM_RGX" "$FILE" + grep -nr "opyright.*$YEAR_RGX.*$PLATPROV" "$FILE" echo } @@ -29,31 +43,45 @@ while read -r FILE; do then break fi - # Check if correct copyright notice is in file. - # To reduce false positives, we assume files with no - # copyright notice do not require it. - if ! grep "opyright.*$YEAR_NOW.*$ARM_RGX" "$FILE">/dev/null 2>&1 + + # Check if copyright header exists for the org + if ! grep "opyright.*$YEAR_RGX.*$PLATPROV" "$FILE">/dev/null 2>&1 && [[ $ORG != *arm* ]] then - # If it is "from_date - to_date" type of entry - change to_date entry. - if grep "opyright.*$YEAR_RGX.*-.*$YEAR_RGX.*$ARM_RGX" "$FILE" >/dev/null 2>&1 - then - exit_code=1 - sed -i "s/\(opyright.*\)$YEAR_RGX\(.*$ARM_RGX\)/\1$(date +"%Y"), Arm/" $FILE - user_warning - # If it is single "date" type of entry - add the copyright extension to current year. - elif grep "opyright.*$YEAR_RGX.*$ARM_RGX" "$FILE" >/dev/null 2>&1 - then - exit_code=1 - sed -i "s/\(opyright.*$YEAR_RGX\)\(.*$ARM_RGX\)/\1-$(date +"%Y"), Arm/" $FILE - user_warning - fi - # Even if the year is correct - verify that Arm copyright is formatted correctly. - elif grep "opyright.*\(ARM\|arm\)" "$FILE">/dev/null 2>&1 - then - exit_code=1 - sed -i "s/\(opyright.*\)\(ARM\|arm\)/\1Arm/" $FILE - user_warning + echo -e "Copyright header ""$RED""$PLATPROV""$BLANK"" is missing in ""$YELLOW""$FILE""$BLANK" fi + + # Check if the copyright year is updated for the org and update it + if [ ! -z "$PLATPROV" ] + then + if ! grep "opyright.*$YEAR_NOW.*$PLATPROV" "$FILE">/dev/null 2>&1 + then + # If it is "from_date - to_date" type of entry - change to_date entry. + if grep "opyright.*$YEAR_RGX.*-.*$YEAR_RGX.*$PLATPROV" "$FILE" >/dev/null 2>&1 + then + exit_code=1 + sed -i "s/\(opyright.*\)$YEAR_RGX\(.*$PLATPROV\)/\1$(date +"%Y")\2/" $FILE + user_warning + # If it is single "date" type of entry - add the copyright extension to current year. + elif grep "opyright.*$YEAR_RGX.*$PLATPROV" "$FILE" >/dev/null 2>&1 + then + exit_code=1 + sed -i "s/\(opyright.*$YEAR_RGX\)\(.*$PLATPROV\)/\1-$(date +"%Y")\2/" $FILE + user_warning + fi + + # Even if the year is correct - verify that Arm copyright is formatted correctly. + if [[ $ORG == *arm* ]] + then + if grep "opyright.*\(ARM\|arm\)" "$FILE">/dev/null 2>&1 + then + exit_code=1 + sed -i "s/\(opyright.*\)\(ARM\|arm\)/\1Arm/" $FILE + user_warning + fi + fi + fi + fi + done <<< "$FILES" if [ $exit_code -eq 1 ]