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
20function user_warning() {
21	echo -e "Copyright of $RED$FILE$BLANK is out of date/incorrect"
22	echo -e "Updated copyright to"
23	grep -nr "opyright.*$YEAR_RGX.*$ARM_RGX" "$FILE"
24	echo
25}
26
27while read -r FILE; do
28	if [ -z "$FILE" ]
29	then
30		break
31	fi
32	# Check if correct copyright notice is in file.
33	# To reduce false positives, we assume files with no
34	# copyright notice do not require it.
35	if ! grep "opyright.*$YEAR_NOW.*$ARM_RGX" "$FILE">/dev/null 2>&1
36	then
37		# If it is "from_date - to_date" type of entry - change to_date entry.
38		if grep "opyright.*$YEAR_RGX.*-.*$YEAR_RGX.*$ARM_RGX" "$FILE" >/dev/null 2>&1
39		then
40			exit_code=1
41			sed -i "s/\(opyright.*\)$YEAR_RGX\(.*$ARM_RGX\)/\1$(date +"%Y"), Arm/" $FILE
42			user_warning
43		# If it is single "date" type of entry - add the copyright extension to current year.
44		elif grep "opyright.*$YEAR_RGX.*$ARM_RGX" "$FILE" >/dev/null 2>&1
45		then
46			exit_code=1
47			sed -i "s/\(opyright.*$YEAR_RGX\)\(.*$ARM_RGX\)/\1-$(date +"%Y"), Arm/" $FILE
48			user_warning
49		fi
50	# Even if the year is correct - verify that Arm copyright is formatted correctly.
51	elif grep "opyright.*\(ARM\|arm\)" "$FILE">/dev/null 2>&1
52	then
53		exit_code=1
54		sed -i "s/\(opyright.*\)\(ARM\|arm\)/\1Arm/" $FILE
55		user_warning
56	fi
57done <<< "$FILES"
58
59if [ $exit_code -eq 1 ]
60then
61	echo -e "$RED""Please stage updated files$BLANK before commiting or use$YELLOW git commit --no-verify$BLANK to skip copyright check"
62fi
63exit $exit_code
64