pre-push.git-hook 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. then
  43. if [ "$remote" == "origin" ]
  44. then
  45. echo >&2 "Not pushing: $local_ref to $remote_ref"
  46. echo >&2 "If you really want to push this, use --no-verify."
  47. exit 1
  48. else
  49. continue
  50. fi
  51. fi
  52. # Check for fixup! commit
  53. commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
  54. if [ -n "$commit" ]
  55. then
  56. echo >&2 "Found fixup! commit in $local_ref, not pushing"
  57. echo >&2 "If you really want to push this, use --no-verify."
  58. exit 1
  59. fi
  60. # Check for squash! commit
  61. commit=$(git rev-list -n 1 --grep '^squash!' "$range")
  62. if [ -n "$commit" ]
  63. then
  64. echo >&2 "Found squash! commit in $local_ref, not pushing"
  65. echo >&2 "If you really want to push this, use --no-verify."
  66. exit 1
  67. fi
  68. fi
  69. done
  70. exit 0