pre-push.git-hook 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. workdir=$(git rev-parse --show-toplevel)
  16. if [ -x "$workdir/.git/hooks/pre-commit" ]; then
  17. if ! "$workdir"/.git/hooks/pre-commit; then
  18. exit 1
  19. fi
  20. fi
  21. if [ -e scripts/maint/practracker/practracker.py ]; then
  22. if ! python3 ./scripts/maint/practracker/practracker.py "$workdir"; then
  23. exit 1
  24. fi
  25. fi
  26. remote="$1"
  27. remote_loc="$2"
  28. if [[ "$remote_loc" != *github.com/torproject/tor.git ]] &&
  29. [[ "$remote_loc" != *torproject.org/tor.git ]]; then
  30. echo "Not pushing to upstream - refraining from further checks"
  31. exit 0
  32. fi
  33. ref_is_upstream_branch() {
  34. if [ "$1" == "refs/heads/master" ] ||
  35. [[ "$1" == refs/heads/release-* ]] ||
  36. [[ "$1" == refs/heads/maint-* ]]
  37. then
  38. return 1
  39. fi
  40. }
  41. # shellcheck disable=SC2034
  42. while read -r local_ref local_sha remote_ref remote_sha
  43. do
  44. if [ "$local_sha" = $z40 ]
  45. then
  46. # Handle delete
  47. :
  48. else
  49. if [ "$remote_sha" = $z40 ]
  50. then
  51. # New branch, examine all commits
  52. range="$local_sha"
  53. else
  54. # Update to existing branch, examine new commits
  55. range="$remote_sha..$local_sha"
  56. fi
  57. if (ref_is_upstream_branch "$local_ref" == 0 ||
  58. ref_is_upstream_branch "$remote_ref" == 0) &&
  59. [ "$local_ref" != "$remote_ref" ]
  60. then
  61. if [ "$remote" == "origin" ]
  62. then
  63. echo >&2 "Not pushing: $local_ref to $remote_ref"
  64. echo >&2 "If you really want to push this, use --no-verify."
  65. exit 1
  66. else
  67. continue
  68. fi
  69. fi
  70. # Check for fixup! commit
  71. commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
  72. if [ -n "$commit" ]
  73. then
  74. echo >&2 "Found fixup! commit in $local_ref, not pushing"
  75. echo >&2 "If you really want to push this, use --no-verify."
  76. exit 1
  77. fi
  78. # Check for squash! commit
  79. commit=$(git rev-list -n 1 --grep '^squash!' "$range")
  80. if [ -n "$commit" ]
  81. then
  82. echo >&2 "Found squash! commit in $local_ref, not pushing"
  83. echo >&2 "If you really want to push this, use --no-verify."
  84. exit 1
  85. fi
  86. fi
  87. done
  88. exit 0