pre-push.git-hook 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env bash
  2. # git pre-push hook script to:
  3. # 0) Call the pre-commit hook, if it is available
  4. # 1) prevent "fixup!" and "squash!" commit from ending up in master, release-*
  5. # or maint-*
  6. # 2) Disallow pushing branches other than master, release-*
  7. # and maint-* to origin (e.g. gitweb.torproject.org)
  8. #
  9. # To install this script, copy it into .git/hooks/pre-push path in your
  10. # local copy of git repository. Make sure it has permission to execute.
  11. # Furthermore, make sure that TOR_UPSTREAM_REMOTE_NAME environment
  12. # variable is set to local name of git remote that corresponds to upstream
  13. # repository on e.g. git.torproject.org.
  14. #
  15. # The following sample script was used as starting point:
  16. # https://github.com/git/git/blob/master/templates/hooks--pre-push.sample
  17. echo "Running pre-push hook"
  18. z40=0000000000000000000000000000000000000000
  19. upstream_name=${TOR_UPSTREAM_REMOTE_NAME:-"upstream"}
  20. # Are you adding a new check to the git hooks?
  21. # - Common checks belong in the pre-commit hook
  22. # - Push-only checks belong in the pre-push hook
  23. #
  24. # Call the pre-commit hook for the common checks, if it is executable.
  25. workdir=$(git rev-parse --show-toplevel)
  26. if [ -x "$workdir/.git/hooks/pre-commit" ]; then
  27. if ! "$workdir"/.git/hooks/pre-commit; then
  28. exit 1
  29. fi
  30. fi
  31. remote="$1"
  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