pre-push 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env bash
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # pre-push — sample Git hook to validate commits before pushing.
  12. #
  13. # To install this hook, copy or symlink it into .git/hooks/pre-push:
  14. #
  15. # $ ln -s ../../misc/githooks/pre-push .git/hooks/pre-push
  16. #
  17. set -euo pipefail
  18. . misc/shlib/shlib.bash
  19. # We declare some unused variables here to make it clear what fields
  20. # are provided to us as input.
  21. # shellcheck disable=SC2034
  22. while read -r local_ref local_sha remote_ref remote_sha; do
  23. oldlist=$(git stash list)
  24. git stash save --quiet --include-untracked
  25. newlist=$(git stash list)
  26. oldref=$(git symbolic-ref --quiet HEAD)
  27. if [[ -z "$oldref" ]]; then
  28. oldref=$(git rev-parse HEAD)
  29. fi
  30. git checkout --quiet "$local_sha"
  31. reset() {
  32. if [[ "$oldlist" != "$newlist" ]]; then
  33. git stash pop --quiet --index || true
  34. fi
  35. if [[ "$oldref" != "$local_sha" ]]; then
  36. git checkout --quiet -
  37. fi
  38. }
  39. trap reset EXIT
  40. bin/pre-push
  41. done