Maksims Svecovs | 9d0e79c | 2023-02-09 16:48:34 +0000 | [diff] [blame] | 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 | |
| 8 | RED="\033[00;31m" |
| 9 | YELLOW="\033[00;33m" |
| 10 | BLANK="\033[00;00m" |
| 11 | |
| 12 | FILES=`git diff --cached --name-only HEAD` |
| 13 | YEAR_NOW=`date +"%Y"` |
| 14 | |
| 15 | YEAR_RGX="[0-9][0-9][0-9][0-9]" |
| 16 | ARM_RGX="\(ARM\|Arm\|arm\)" |
| 17 | |
| 18 | exit_code=0 |
| 19 | |
| 20 | function 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 | |
| 27 | while 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 |
| 51 | done <<< "$FILES" |
| 52 | |
| 53 | if [ $exit_code -eq 1 ] |
| 54 | then |
| 55 | echo -e "$RED""Please stage updated files$BLANK before commiting or use$YELLOW git commit --no-verify$BLANK to skip copyright check" |
| 56 | fi |
| 57 | exit $exit_code |