pre-push.git-hook 4.5 KB

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