pre-push.git-hook 3.1 KB

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