pre-push 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/sh
  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" ]
  11. then
  12. exit 0
  13. fi
  14. echo "Running pre-push hook"
  15. # shellcheck disable=SC2034
  16. while read -r local_ref local_sha remote_ref remote_sha
  17. do
  18. if [ "$local_sha" = $z40 ]
  19. then
  20. # Handle delete
  21. :
  22. else
  23. if [ "$remote_sha" = $z40 ]
  24. then
  25. # New branch, examine all commits
  26. range="$local_sha"
  27. else
  28. # Update to existing branch, examine new commits
  29. range="$remote_sha..$local_sha"
  30. fi
  31. # Check for fixup! commit
  32. commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
  33. if [ -n "$commit" ]
  34. then
  35. echo >&2 "Found fixup! commit in $local_ref, not pushing"
  36. exit 1
  37. fi
  38. # Check for squash! commit
  39. commit=$(git rev-list -n 1 --grep '^squash!' "$range")
  40. if [ -n "$commit" ]
  41. then
  42. echo >&2 "Found squash! commit in $local_ref, not pushing"
  43. exit 1
  44. fi
  45. fi
  46. done
  47. exit 0