pre-push.git-hook 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/bash
  2. # To install this script, copy it into .git/hooks/pre-push path in your
  3. # local copy of git repository. Make sure it has permission to execute.
  4. #
  5. # This is git pre-push hook script to prevent "fixup!" and "squash!" commits
  6. # from ending up in upstream branches (master, release-* or maint-*).
  7. #
  8. # The following sample script was used as starting point:
  9. # https://github.com/git/git/blob/master/templates/hooks--pre-push.sample
  10. z40=0000000000000000000000000000000000000000
  11. CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD)
  12. if [ "$CUR_BRANCH" != "master" ] && [[ $CUR_BRANCH != release-* ]] &&
  13. [[ $CUR_BRANCH != maint-* ]]
  14. then
  15. exit 0
  16. fi
  17. echo "Running pre-push hook"
  18. # shellcheck disable=SC2034
  19. while read -r local_ref local_sha remote_ref remote_sha
  20. do
  21. if [ "$local_sha" = $z40 ]
  22. then
  23. # Handle delete
  24. :
  25. else
  26. if [ "$remote_sha" = $z40 ]
  27. then
  28. # New branch, examine all commits
  29. range="$local_sha"
  30. else
  31. # Update to existing branch, examine new commits
  32. range="$remote_sha..$local_sha"
  33. fi
  34. # Check for fixup! commit
  35. commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
  36. if [ -n "$commit" ]
  37. then
  38. echo >&2 "Found fixup! commit in $local_ref, not pushing"
  39. echo >&2 "If you really want to push this, use --no-verify."
  40. exit 1
  41. fi
  42. # Check for squash! commit
  43. commit=$(git rev-list -n 1 --grep '^squash!' "$range")
  44. if [ -n "$commit" ]
  45. then
  46. echo >&2 "Found squash! commit in $local_ref, not pushing"
  47. echo >&2 "If you really want to push this, use --no-verify."
  48. exit 1
  49. fi
  50. fi
  51. done
  52. exit 0