pre-push.git-hook 2.5 KB

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