pre-push.git-hook 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. # shellcheck disable=SC2034
  25. while read -r local_ref local_sha remote_ref remote_sha
  26. do
  27. if [ "$local_sha" = $z40 ]
  28. then
  29. # Handle delete
  30. :
  31. else
  32. if [ "$remote_sha" = $z40 ]
  33. then
  34. # New branch, examine all commits
  35. range="$local_sha"
  36. else
  37. # Update to existing branch, examine new commits
  38. range="$remote_sha..$local_sha"
  39. fi
  40. if (ref_is_upstream_branch "$local_ref" == 0 ||
  41. ref_is_upstream_branch "$remote_ref" == 0) &&
  42. [ "$local_ref" != "$remote_ref" ]
  43. then
  44. if [ "$remote" == "origin" ]
  45. then
  46. echo >&2 "Not pushing: $local_ref to $remote_ref"
  47. echo >&2 "If you really want to push this, use --no-verify."
  48. exit 1
  49. else
  50. continue
  51. fi
  52. fi
  53. # Check for fixup! commit
  54. commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
  55. if [ -n "$commit" ]
  56. then
  57. echo >&2 "Found fixup! commit in $local_ref, not pushing"
  58. echo >&2 "If you really want to push this, use --no-verify."
  59. exit 1
  60. fi
  61. # Check for squash! commit
  62. commit=$(git rev-list -n 1 --grep '^squash!' "$range")
  63. if [ -n "$commit" ]
  64. then
  65. echo >&2 "Found squash! commit in $local_ref, not pushing"
  66. echo >&2 "If you really want to push this, use --no-verify."
  67. exit 1
  68. fi
  69. fi
  70. done
  71. exit 0