pre-push.git-hook 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. # Are you adding a new check to the git hooks?
  18. # - Common checks belong in the pre-commit hook
  19. # - Push-only checks belong in the pre-push hook
  20. echo "Running pre-push hook"
  21. z40=0000000000000000000000000000000000000000
  22. upstream_name=${TOR_UPSTREAM_REMOTE_NAME:-"upstream"}
  23. workdir=$(git rev-parse --show-toplevel)
  24. cd "$workdir" || exit 1
  25. remote="$1"
  26. remote_name=$(git remote --verbose | grep "$2" | awk '{print $1}' | head -n 1)
  27. ref_is_upstream_branch() {
  28. if [ "$1" == "refs/heads/master" ] ||
  29. [[ "$1" == refs/heads/release-* ]] ||
  30. [[ "$1" == refs/heads/maint-* ]]; then
  31. return 1
  32. fi
  33. }
  34. # shellcheck disable=SC2034
  35. while read -r local_ref local_sha remote_ref remote_sha
  36. do
  37. if [ "$local_sha" = $z40 ]; then
  38. # Handle delete
  39. :
  40. else
  41. if [ "$remote_sha" = $z40 ]; then
  42. # New branch, examine commits not in master
  43. range="master...$local_sha"
  44. else
  45. # Update to existing branch, examine new commits
  46. range="$remote_sha..$local_sha"
  47. fi
  48. # Call the pre-commit hook for the common checks, if it is executable
  49. if [ -x scripts/git/pre-commit.git-hook ]; then
  50. # Only check the files newly modified in this branch
  51. CHECK_FILTER="git diff --name-only --diff-filter=ACMR $range"
  52. # Use the appropriate owned tor source list to filter the changed
  53. # files
  54. # This is the layout in 0.3.5
  55. # Keep these lists consistent:
  56. # - OWNED_TOR_C_FILES in Makefile.am
  57. # - CHECK_FILES in pre-commit.git-hook and pre-push.git-hook
  58. # - try_parse in check_cocci_parse.sh
  59. CHECK_FILES="$($CHECK_FILTER \
  60. src/lib/*/*.[ch] \
  61. src/core/*/*.[ch] \
  62. src/feature/*/*.[ch] \
  63. src/app/*/*.[ch] \
  64. src/test/*.[ch] \
  65. src/test/*/*.[ch] \
  66. src/tools/*.[ch] \
  67. )"
  68. # We want word splitting here, because file names are space
  69. # separated
  70. # shellcheck disable=SC2086
  71. if ! scripts/git/pre-commit.git-hook $CHECK_FILES ; then
  72. exit 1
  73. fi
  74. fi
  75. if [[ "$remote_name" != "$upstream_name" ]]; then
  76. echo "Not pushing to upstream - refraining from further checks"
  77. continue
  78. fi
  79. if (ref_is_upstream_branch "$local_ref" == 0 ||
  80. ref_is_upstream_branch "$remote_ref" == 0) &&
  81. [ "$local_ref" != "$remote_ref" ]; then
  82. if [ "$remote" == "origin" ]; then
  83. echo >&2 "Not pushing: $local_ref to $remote_ref"
  84. echo >&2 "If you really want to push this, use --no-verify."
  85. exit 1
  86. else
  87. continue
  88. fi
  89. fi
  90. # Check for fixup! commit
  91. commit=$(git rev-list -n 1 --grep '^fixup!' "$range")
  92. if [ -n "$commit" ]; then
  93. echo >&2 "Found fixup! commit in $local_ref, not pushing"
  94. echo >&2 "If you really want to push this, use --no-verify."
  95. exit 1
  96. fi
  97. # Check for squash! commit
  98. commit=$(git rev-list -n 1 --grep '^squash!' "$range")
  99. if [ -n "$commit" ]; then
  100. echo >&2 "Found squash! commit in $local_ref, not pushing"
  101. echo >&2 "If you really want to push this, use --no-verify."
  102. exit 1
  103. fi
  104. fi
  105. done
  106. exit 0