pre-push.git-hook 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. z40=0000000000000000000000000000000000000000
  13. remote="$1"
  14. CUR_BRANCH=$(git rev-parse --abbrev-ref HEAD)
  15. # Only allow pushing master, release-* and maint-* branches to origin.
  16. if [ "$remote" == "origin" ]
  17. then
  18. if [ "$CUR_BRANCH" != "master" ] && [[ $CUR_BRANCH != release-* ]] &&
  19. [[ $CUR_BRANCH != maint-* ]]
  20. then
  21. echo >&2 "Not pushing $CUR_BRANCH to origin"
  22. exit 1
  23. fi
  24. fi
  25. if [ "$CUR_BRANCH" != "master" ] && [[ $CUR_BRANCH != release-* ]] &&
  26. [[ $CUR_BRANCH != maint-* ]]
  27. then
  28. exit 0
  29. fi
  30. echo "Running pre-push hook"
  31. # shellcheck disable=SC2034
  32. while read -r local_ref local_sha remote_ref remote_sha
  33. do
  34. if [ "$local_sha" = $z40 ]
  35. then
  36. # Handle delete
  37. :
  38. else
  39. if [ "$remote_sha" = $z40 ]
  40. then
  41. # New branch, examine all commits
  42. range="$local_sha"
  43. else
  44. # Update to existing branch, examine new commits
  45. range="$remote_sha..$local_sha"
  46. fi
  47. # Check for fixup! commit
  48. commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
  49. if [ -n "$commit" ]
  50. then
  51. echo >&2 "Found fixup! commit in $local_ref, not pushing"
  52. echo >&2 "If you really want to push this, use --no-verify."
  53. exit 1
  54. fi
  55. # Check for squash! commit
  56. commit=$(git rev-list -n 1 --grep '^squash!' "$range")
  57. if [ -n "$commit" ]
  58. then
  59. echo >&2 "Found squash! commit in $local_ref, not pushing"
  60. echo >&2 "If you really want to push this, use --no-verify."
  61. exit 1
  62. fi
  63. fi
  64. done
  65. exit 0