pre-push.git-hook 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. # Furthermore, it disallows pushing branches other than master, release-*
  5. # and maint-* to origin (e.g. gitweb.torproject.org).
  6. #
  7. # To install this script, copy it into .git/hooks/pre-push path in your
  8. # local copy of git repository. Make sure it has permission to execute.
  9. #
  10. # The following sample script was used as starting point:
  11. # https://github.com/git/git/blob/master/templates/hooks--pre-push.sample
  12. echo "Running pre-push hook"
  13. z40=0000000000000000000000000000000000000000
  14. remote="$1"
  15. ref_is_upstream_branch() {
  16. if [ "$1" == "refs/heads/master" ] ||
  17. [[ "$1" == refs/heads/release-* ]] ||
  18. [[ "$1" == refs/heads/maint-* ]]
  19. then
  20. return 1
  21. fi
  22. }
  23. # shellcheck disable=SC2034
  24. while read -r local_ref local_sha remote_ref remote_sha
  25. do
  26. if [ "$local_sha" = $z40 ]
  27. then
  28. # Handle delete
  29. :
  30. else
  31. if [ "$remote_sha" = $z40 ]
  32. then
  33. # New branch, examine all commits
  34. range="$local_sha"
  35. else
  36. # Update to existing branch, examine new commits
  37. range="$remote_sha..$local_sha"
  38. fi
  39. if ref_is_upstream_branch "$local_ref" == 0 ||
  40. ref_is_upstream_branch "$remote_ref" == 0
  41. then
  42. if [ "$remote" == "origin" ]
  43. then
  44. echo >&2 "Not pushing: $local_ref to $remote_ref"
  45. echo >&2 "If you really want to push this, use --no-verify."
  46. exit 1
  47. else
  48. continue
  49. fi
  50. fi
  51. # Check for fixup! commit
  52. commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
  53. if [ -n "$commit" ]
  54. then
  55. echo >&2 "Found fixup! commit in $local_ref, not pushing"
  56. echo >&2 "If you really want to push this, use --no-verify."
  57. exit 1
  58. fi
  59. # Check for squash! commit
  60. commit=$(git rev-list -n 1 --grep '^squash!' "$range")
  61. if [ -n "$commit" ]
  62. then
  63. echo >&2 "Found squash! commit in $local_ref, not pushing"
  64. echo >&2 "If you really want to push this, use --no-verify."
  65. exit 1
  66. fi
  67. fi
  68. done
  69. exit 0