blob: b5087a74652d9ae0ab9cfdd8cd843aff72243015 [file] [log] [blame]
Maksims Svecovs9d0e79c2023-02-09 16:48:34 +00001#!/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"
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")\2/" $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")\2/" $FILE
48 user_warning
49 fi
50 fi
51done <<< "$FILES"
52
53if [ $exit_code -eq 1 ]
54then
55 echo -e "$RED""Please stage updated files$BLANK before commiting or use$YELLOW git commit --no-verify$BLANK to skip copyright check"
56fi
57exit $exit_code