pre-push 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/bin/sh
  2. # This hook script prevents the user from pushing to Savannah if any of the new
  3. # commits' OpenPGP signatures cannot be verified.
  4. # Called by "git push" after it has checked the remote status, but before
  5. # anything has been pushed. If this script exits with a non-zero status nothing
  6. # will be pushed.
  7. #
  8. # This hook is called with the following parameters:
  9. #
  10. # $1 -- Name of the remote to which the push is being done
  11. # $2 -- URL to which the push is being done
  12. #
  13. # If pushing without using a named remote those arguments will be equal.
  14. #
  15. # Information about the commits which are being pushed is supplied as lines to
  16. # the standard input in the form:
  17. #
  18. # <local ref> <local sha1> <remote ref> <remote sha1>
  19. z40=0000000000000000000000000000000000000000
  20. # Only use the hook when pushing to Savannah.
  21. case "$2" in
  22. *git.sv.gnu.org*)
  23. break
  24. ;;
  25. *)
  26. exit 0
  27. ;;
  28. esac
  29. while read local_ref local_sha remote_ref remote_sha
  30. do
  31. if [ "$local_sha" = $z40 ]
  32. then
  33. # Handle delete
  34. :
  35. else
  36. if [ "$remote_sha" = $z40 ]
  37. then
  38. # We are pushing a new branch. To prevent wasting too
  39. # much time for this relatively rare case, we examine
  40. # all commits since the first signed commit, rather than
  41. # the full history. This check *will* fail, and the user
  42. # will need to temporarily disable the hook to push the
  43. # new branch.
  44. range="e3d0fcbf7e55e8cbe8d0a1c5a24d73f341d7243b..$local_sha"
  45. else
  46. # Update to existing branch, examine new commits
  47. range="$remote_sha..$local_sha"
  48. fi
  49. # Verify the signatures of all commits being pushed.
  50. ret=0
  51. for commit in $(git rev-list $range)
  52. do
  53. if ! git verify-commit $commit >/dev/null 2>&1
  54. then
  55. printf "%s failed signature check\n" $commit
  56. ret=1
  57. fi
  58. done
  59. exit $ret
  60. fi
  61. done
  62. exit 0