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
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
20PLATPROV=
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
34function user_warning() {
35	echo -e "Copyright of $RED$FILE$BLANK is out of date/incorrect"
36	echo -e "Updated copyright to"
37	grep -nr "opyright.*$YEAR_RGX.*$PLATPROV" "$FILE"
38	echo
39}
40
41while read -r FILE; do
42	if [ -z "$FILE" ]
43	then
44		break
45	fi
46
47	# Check if copyright header exists for the org
48	if ! grep "opyright.*$YEAR_RGX.*$PLATPROV" "$FILE">/dev/null 2>&1 && [[ $ORG != *arm* ]]
49	then
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
57		then
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
82		fi
83	fi
84
85done <<< "$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
92