1*91f16700Schasinglulu#!/bin/bash 2*91f16700Schasinglulu 3*91f16700Schasinglulu# A hook script that checks if files staged for commit have updated Arm copyright year. 4*91f16700Schasinglulu# In case they are not - updates the years and prompts user to add them to the change. 5*91f16700Schasinglulu# This hook is called on "git commit" after changes have been staged, but before commit 6*91f16700Schasinglulu# message has to be provided. 7*91f16700Schasinglulu 8*91f16700SchasingluluRED="\033[00;31m" 9*91f16700SchasingluluYELLOW="\033[00;33m" 10*91f16700SchasingluluBLANK="\033[00;00m" 11*91f16700Schasinglulu 12*91f16700SchasingluluFILES=`git diff --cached --name-only HEAD` 13*91f16700SchasingluluYEAR_NOW=`date +"%Y"` 14*91f16700Schasinglulu 15*91f16700SchasingluluYEAR_RGX="[0-9][0-9][0-9][0-9]" 16*91f16700SchasingluluARM_RGX="\(ARM\|Arm\|arm\)" 17*91f16700Schasinglulu 18*91f16700Schasingluluexit_code=0 19*91f16700Schasinglulu 20*91f16700Schasinglulufunction user_warning() { 21*91f16700Schasinglulu echo -e "Copyright of $RED$FILE$BLANK is out of date/incorrect" 22*91f16700Schasinglulu echo -e "Updated copyright to" 23*91f16700Schasinglulu grep -nr "opyright.*$YEAR_RGX.*$ARM_RGX" "$FILE" 24*91f16700Schasinglulu echo 25*91f16700Schasinglulu} 26*91f16700Schasinglulu 27*91f16700Schasingluluwhile read -r FILE; do 28*91f16700Schasinglulu if [ -z "$FILE" ] 29*91f16700Schasinglulu then 30*91f16700Schasinglulu break 31*91f16700Schasinglulu fi 32*91f16700Schasinglulu # Check if correct copyright notice is in file. 33*91f16700Schasinglulu # To reduce false positives, we assume files with no 34*91f16700Schasinglulu # copyright notice do not require it. 35*91f16700Schasinglulu if ! grep "opyright.*$YEAR_NOW.*$ARM_RGX" "$FILE">/dev/null 2>&1 36*91f16700Schasinglulu then 37*91f16700Schasinglulu # If it is "from_date - to_date" type of entry - change to_date entry. 38*91f16700Schasinglulu if grep "opyright.*$YEAR_RGX.*-.*$YEAR_RGX.*$ARM_RGX" "$FILE" >/dev/null 2>&1 39*91f16700Schasinglulu then 40*91f16700Schasinglulu exit_code=1 41*91f16700Schasinglulu sed -i "s/\(opyright.*\)$YEAR_RGX\(.*$ARM_RGX\)/\1$(date +"%Y"), Arm/" $FILE 42*91f16700Schasinglulu user_warning 43*91f16700Schasinglulu # If it is single "date" type of entry - add the copyright extension to current year. 44*91f16700Schasinglulu elif grep "opyright.*$YEAR_RGX.*$ARM_RGX" "$FILE" >/dev/null 2>&1 45*91f16700Schasinglulu then 46*91f16700Schasinglulu exit_code=1 47*91f16700Schasinglulu sed -i "s/\(opyright.*$YEAR_RGX\)\(.*$ARM_RGX\)/\1-$(date +"%Y"), Arm/" $FILE 48*91f16700Schasinglulu user_warning 49*91f16700Schasinglulu fi 50*91f16700Schasinglulu # Even if the year is correct - verify that Arm copyright is formatted correctly. 51*91f16700Schasinglulu elif grep "opyright.*\(ARM\|arm\)" "$FILE">/dev/null 2>&1 52*91f16700Schasinglulu then 53*91f16700Schasinglulu exit_code=1 54*91f16700Schasinglulu sed -i "s/\(opyright.*\)\(ARM\|arm\)/\1Arm/" $FILE 55*91f16700Schasinglulu user_warning 56*91f16700Schasinglulu fi 57*91f16700Schasingluludone <<< "$FILES" 58*91f16700Schasinglulu 59*91f16700Schasingluluif [ $exit_code -eq 1 ] 60*91f16700Schasingluluthen 61*91f16700Schasinglulu echo -e "$RED""Please stage updated files$BLANK before commiting or use$YELLOW git commit --no-verify$BLANK to skip copyright check" 62*91f16700Schasinglulufi 63*91f16700Schasingluluexit $exit_code 64