pre-push.git-hook 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/bin/bash
  2. # git pre-push hook script to prevent "fixup!" and "squash!" commit
  3. # from ending up in master, or in any branch if CUR_BRANCH check is removed.
  4. # It is meant to be placed in .git/hooks directory.
  5. #
  6. # The following sample script was used as starting point:
  7. # https://github.com/git/git/blob/master/templates/hooks--pre-push.sample
  8. z40=0000000000000000000000000000000000000000
  9. CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD)
  10. if [ "$CUR_BRANCH" != "master" ] && [[ $CUR_BRANCH != release-* ]] &&
  11. [[ $CUR_BRANCH != maint-* ]]
  12. then
  13. exit 0
  14. fi
  15. echo "Running pre-push hook"
  16. # shellcheck disable=SC2034
  17. while read -r local_ref local_sha remote_ref remote_sha
  18. do
  19. if [ "$local_sha" = $z40 ]
  20. then
  21. # Handle delete
  22. :
  23. else
  24. if [ "$remote_sha" = $z40 ]
  25. then
  26. # New branch, examine all commits
  27. range="$local_sha"
  28. else
  29. # Update to existing branch, examine new commits
  30. range="$remote_sha..$local_sha"
  31. fi
  32. # Check for fixup! commit
  33. commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
  34. if [ -n "$commit" ]
  35. then
  36. echo >&2 "Found fixup! commit in $local_ref, not pushing"
  37. exit 1
  38. fi
  39. # Check for squash! commit
  40. commit=$(git rev-list -n 1 --grep '^squash!' "$range")
  41. if [ -n "$commit" ]
  42. then
  43. echo >&2 "Found squash! commit in $local_ref, not pushing"
  44. exit 1
  45. fi
  46. fi
  47. done
  48. exit 0