pre-push.git-hook 3.1 KB

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