pre-push.git-hook 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env 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. # Furthermore, make sure that TOR_UPSTREAM_REMOTE_NAME environment
  11. # variable is set to local name of git remote that corresponds to upstream
  12. # repository on e.g. git.torproject.org.
  13. #
  14. # The following sample script was used as starting point:
  15. # https://github.com/git/git/blob/master/templates/hooks--pre-push.sample
  16. echo "Running pre-push hook"
  17. z40=0000000000000000000000000000000000000000
  18. upstream_name=${TOR_UPSTREAM_REMOTE_NAME:-"upstream"}
  19. workdir=$(git rev-parse --show-toplevel)
  20. if [ -x "$workdir/.git/hooks/pre-commit" ]; then
  21. if ! "$workdir"/.git/hooks/pre-commit; then
  22. exit 1
  23. fi
  24. fi
  25. PT_DIR=scripts/maint/practracker
  26. if [ -e "${PT_DIR}/practracker.py" ]; then
  27. if [ -e "${PT_DIR}/.enable_practracker_in_hooks" ]; then
  28. if ! python3 "${PT_DIR}/practracker.py" "$workdir"; then
  29. exit 1
  30. fi
  31. fi
  32. fi
  33. remote="$1"
  34. remote_name=$(git remote --verbose | grep "$2" | awk '{print $1}' | head -n 1)
  35. if [[ "$remote_name" != "$upstream_name" ]]; then
  36. echo "Not pushing to upstream - refraining from further checks"
  37. exit 0
  38. fi
  39. ref_is_upstream_branch() {
  40. if [ "$1" == "refs/heads/master" ] ||
  41. [[ "$1" == refs/heads/release-* ]] ||
  42. [[ "$1" == refs/heads/maint-* ]]
  43. then
  44. return 1
  45. fi
  46. }
  47. # shellcheck disable=SC2034
  48. while read -r local_ref local_sha remote_ref remote_sha
  49. do
  50. if [ "$local_sha" = $z40 ]
  51. then
  52. # Handle delete
  53. :
  54. else
  55. if [ "$remote_sha" = $z40 ]
  56. then
  57. # New branch, examine all commits
  58. range="$local_sha"
  59. else
  60. # Update to existing branch, examine new commits
  61. range="$remote_sha..$local_sha"
  62. fi
  63. if (ref_is_upstream_branch "$local_ref" == 0 ||
  64. ref_is_upstream_branch "$remote_ref" == 0) &&
  65. [ "$local_ref" != "$remote_ref" ]
  66. then
  67. if [ "$remote" == "origin" ]
  68. then
  69. echo >&2 "Not pushing: $local_ref to $remote_ref"
  70. echo >&2 "If you really want to push this, use --no-verify."
  71. exit 1
  72. else
  73. continue
  74. fi
  75. fi
  76. # Check for fixup! commit
  77. commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
  78. if [ -n "$commit" ]
  79. then
  80. echo >&2 "Found fixup! commit in $local_ref, not pushing"
  81. echo >&2 "If you really want to push this, use --no-verify."
  82. exit 1
  83. fi
  84. # Check for squash! commit
  85. commit=$(git rev-list -n 1 --grep '^squash!' "$range")
  86. if [ -n "$commit" ]
  87. then
  88. echo >&2 "Found squash! commit in $local_ref, not pushing"
  89. echo >&2 "If you really want to push this, use --no-verify."
  90. exit 1
  91. fi
  92. fi
  93. done
  94. exit 0