pre-commit.copyright 1.7 KB

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