arm-trusted-firmware/.husky/pre-commit.copyright
Akshay Belsare 93d1f4bc74 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 <akshay.belsare@amd.com>
2024-01-19 14:29:29 +01:00

91 lines
2.4 KiB
Bash
Executable file

#!/bin/bash
# A hook script that checks if files staged for commit have updated Arm copyright year.
# In case they are not - updates the years and prompts user to add them to the change.
# This hook is called on "git commit" after changes have been staged, but before commit
# message has to be provided.
RED="\033[00;31m"
YELLOW="\033[00;33m"
BLANK="\033[00;00m"
FILES=`git diff --cached --name-only HEAD`
YEAR_NOW=`date +"%Y"`
YEAR_RGX="[0-9][0-9][0-9][0-9]"
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.*$PLATPROV" "$FILE"
echo
}
while read -r FILE; do
if [ -z "$FILE" ]
then
break
fi
# Check if copyright header exists for the org
if ! grep "opyright.*$YEAR_RGX.*$PLATPROV" "$FILE">/dev/null 2>&1 && [[ $ORG != *arm* ]]
then
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 ]
then
echo -e "$RED""Please stage updated files$BLANK before commiting or use$YELLOW git commit --no-verify$BLANK to skip copyright check"
fi
exit $exit_code