pre-commit.copyright 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. PLATPROV=
  15. ORG=`echo "$GIT_AUTHOR_EMAIL" | awk -F '[@]' '{ print $2;}'`
  16. case $ORG in
  17. amd.com)
  18. PLATPROV="Advanced Micro Devices, Inc. All rights reserved."
  19. ;;
  20. *arm.com)
  21. PLATPROV="$ARM_RGX"
  22. ;;
  23. *)
  24. ;;
  25. esac
  26. function user_warning() {
  27. echo -e "Copyright of $RED$FILE$BLANK is out of date/incorrect"
  28. echo -e "Updated copyright to"
  29. grep -nr "opyright.*$YEAR_RGX.*$PLATPROV" "$FILE"
  30. echo
  31. }
  32. while read -r FILE; do
  33. if [ -z "$FILE" ]
  34. then
  35. break
  36. fi
  37. # Check if copyright header exists for the org
  38. if ! grep "opyright.*$YEAR_RGX.*$PLATPROV" "$FILE">/dev/null 2>&1 && [[ $ORG != *arm* ]]
  39. then
  40. echo -e "Copyright header ""$RED""$PLATPROV""$BLANK"" is missing in ""$YELLOW""$FILE""$BLANK"
  41. fi
  42. # Check if the copyright year is updated for the org and update it
  43. if [ ! -z "$PLATPROV" ]
  44. then
  45. if ! grep "opyright.*$YEAR_NOW.*$PLATPROV" "$FILE">/dev/null 2>&1
  46. then
  47. # If it is "from_date - to_date" type of entry - change to_date entry.
  48. if grep "opyright.*$YEAR_RGX.*-.*$YEAR_RGX.*$PLATPROV" "$FILE" >/dev/null 2>&1
  49. then
  50. exit_code=1
  51. sed -i "s/\(opyright.*\)$YEAR_RGX\(.*$PLATPROV\)/\1$(date +"%Y")\2/" $FILE
  52. user_warning
  53. # If it is single "date" type of entry - add the copyright extension to current year.
  54. elif grep "opyright.*$YEAR_RGX.*$PLATPROV" "$FILE" >/dev/null 2>&1
  55. then
  56. exit_code=1
  57. sed -i "s/\(opyright.*$YEAR_RGX\)\(.*$PLATPROV\)/\1-$(date +"%Y")\2/" $FILE
  58. user_warning
  59. fi
  60. # Even if the year is correct - verify that Arm copyright is formatted correctly.
  61. if [[ $ORG == *arm* ]]
  62. then
  63. if grep "opyright.*\(ARM\|arm\)" "$FILE">/dev/null 2>&1
  64. then
  65. exit_code=1
  66. sed -i "s/\(opyright.*\)\(ARM\|arm\)/\1Arm/" $FILE
  67. user_warning
  68. fi
  69. fi
  70. fi
  71. fi
  72. done <<< "$FILES"
  73. if [ $exit_code -eq 1 ]
  74. then
  75. echo -e "$RED""Please stage updated files$BLANK before commiting or use$YELLOW git commit --no-verify$BLANK to skip copyright check"
  76. fi
  77. exit $exit_code