pre-push.git-hook 2.6 KB

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