pre-push.git-hook 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. exit 1
  40. fi
  41. # Check for squash! commit
  42. commit=$(git rev-list -n 1 --grep '^squash!' "$range")
  43. if [ -n "$commit" ]
  44. then
  45. echo >&2 "Found squash! commit in $local_ref, not pushing"
  46. exit 1
  47. fi
  48. fi
  49. done
  50. exit 0