Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # pre-receive hook for Commit Check
  4. #
  5.  
  6. COMPANY_EMAIL="mycorp.org"
  7.  
  8. readonly PROGNAME=$(basename $0)
  9. readonly PROGDIR=$(readlink -m $(dirname $0))
  10.  
  11. check_single_commit()
  12. {
  13. #
  14. # Put here any logic you want for your commit
  15. #
  16. # COMMIT_MESSAGE contains commit message
  17. # COMMIT_AUTHOR contains commit author (without email)
  18. #
  19. # Set COMMIT_CHECK_STATUS to non zero to indicate an error
  20. if [[ "$COMMIT_MESSAGE" =~ ^(refacto|feat|test|fix|style|docs|chore|perf):[[:space:]].*$ ]]
  21. then
  22. COMMIT_CHECK_STATUS=0
  23. echo "Commit message is conform."
  24. else
  25. COMMIT_CHECK_STATUS=1
  26. echo "Commit message \"$COMMIT_MESSAGE\" is not conform."
  27. echo "The push has been refused by the server."
  28. echo "You can change you commit message with the command: git commit --amend -m \"<NEW MESSAGE>\""
  29. fi
  30. }
  31.  
  32. check_all_commits()
  33. {
  34. if [ "$OLD_REVISION" = "0000000000000000000000000000000000000000" ]
  35. then
  36. OLD_REVISION=$NEW_REVISION
  37. fi
  38. REVISIONS=$(git rev-list $OLD_REVISION..$NEW_REVISION)
  39. IFS='\n' read -ra LIST_OF_REVISIONS <<< "$REVISIONS"
  40.  
  41. for rid in "${!LIST_OF_REVISIONS[@]}"; do
  42. REVISION=${LIST_OF_REVISIONS[rid]}
  43. COMMIT_MESSAGE=$(git cat-file commit $REVISION | sed '1,/^$/d')
  44. COMMIT_AUTHOR=$(git cat-file commit $REVISION | grep committer | sed 's/^.* \([^@ ]\+@[^ ]\+\) \?.*$/\1/' | sed 's/<//' | sed 's/>//' | sed 's/@$COMPANY_EMAIL//')
  45. check_single_commit
  46.  
  47. if [ "$COMMIT_CHECK_STATUS" != "0" ]; then
  48. echo "Commit validation failed for commit $REVISION ($COMMIT_AUTHOR)" >&2
  49. exit 1
  50. fi
  51.  
  52. done
  53. }
  54.  
  55. # Get custom commit message format
  56. while read OLD_REVISION NEW_REVISION REFNAME ; do
  57. check_all_commits
  58. done
  59.  
  60. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement