Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # install-pre-push-hook
  3. # this script installs the pre-push hook into the current .git repo.
  4. # by default, the pre-push hook protects the "master" branch.
  5. set_git_hooks_dir_path() {
  6. set_git_dir_path
  7. git_hooks_dir_path="$git_dir_path/hooks"
  8. if [[ ! -d "$git_hooks_dir_path" ]] ; then
  9. echo 1>&2 "$git_hooks_dir_path does not exist!"
  10. exit
  11. fi
  12. }
  13. set_git_dir_path() {
  14. local dir_path=`git rev-parse --git-dir`
  15. git_dir_path=`absolute_path "$dir_path"`
  16. if [[ ! -d "$git_dir_path" ]]; then
  17. echo 1>&2 "Not in a repo!"
  18. exit
  19. fi
  20. }
  21. absolute_path() {
  22. case "$1" in
  23. /*) echo "$1" ;;
  24. *) echo "`pwd`/$1" ;;
  25. esac
  26. }
  27. install_pre_push_script() {
  28. set_git_hooks_dir_path
  29. pre_push_path="$git_hooks_dir_path/pre-push"
  30. if [[ -e "$pre_push_path" ]] ; then
  31. mv "$pre_push_path" "$pre_push_path.old"
  32. fi
  33. create_pre_push_script "$pre_push_path"
  34. }
  35. create_pre_push_script() {
  36. local dest_path="$1"
  37. cat <<'EOF' >"$dest_path"
  38. #!/bin/bash
  39. protected_branch='master'
  40. current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
  41. if [ $protected_branch = $current_branch ] ; then
  42. read -p "You're about to push master, is that what you intended? [y|n] " -n 1 -r < /dev/tty
  43. echo
  44. if echo $REPLY | grep -E '^[Yy]$' > /dev/null ; then
  45. exit 0 # push will execute
  46. fi
  47. exit 1 # push will not execute
  48. fi
  49. exit 0 # push will execute
  50. EOF
  51. chmod +x $dest_path
  52. echo 1>&2 "Created $dest_path"
  53. }
  54. install_pre_push_script
  55. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement