pre-push.git-hook 2.9 KB

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