manage_hooks.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/bash
  2. USAGEMSG="Usage: manage_hooks <action> <hook> <option>"
  3. if [[ $# -lt 1 ]] || [[ $# -gt 3 ]]; then
  4. echo "$USAGEMSG"
  5. exit 1
  6. fi
  7. action="$1"
  8. case $1 in
  9. list)
  10. ls --color=if-tty -l ../.git/hooks --ignore='*.sample'
  11. ;;
  12. install)
  13. #Grab our list of files if all was passed.
  14. #Couldn't get * to function without globbing
  15. if [[ "$2" == "all" ]]; then
  16. hooks=( $(readlink -f $(ls --ignore='*.md' --ignore='*.sh' --ignore='*.bash')) )
  17. else
  18. hooks=( $(readlink -f "$2") )
  19. fi
  20. #Default to symbolic
  21. if [[ -z $3 ]]; then
  22. option="symbolic"
  23. else
  24. option="$3"
  25. fi
  26. case $option in
  27. copy)
  28. #If copying, copy the update_editor file as well
  29. hooks+=( "$(readlink -f "update_editor.sh")" "${hooks[@]}" )
  30. for hook in "${hooks[@]}"; do
  31. #If the file doesnt already exist copy it over
  32. if [[ ! -f "../.git/hooks/$(basename "$hook")" ]]; then
  33. echo "$hook"
  34. cp "$hook" "../.git/hooks"
  35. fi
  36. done
  37. ;;
  38. symbolic)
  39. #Switch to the git hook directory, because symlink hooks are required
  40. #to be relative
  41. cd "../.git/hooks"
  42. for hook in "${hooks[@]}"; do
  43. #If the file doesnt already exist, symlink it
  44. if [[ ! -f "$(basename $hook)" ]]; then
  45. echo "$(readlink -f .)/$(basename $hook)"
  46. ln --symbolic --relative "../../hooks/$(basename "$hook")"
  47. fi
  48. done
  49. ;;
  50. esac
  51. ;;
  52. remove)
  53. #Switch to the git hook directory, so we dont have to deal with
  54. #relative file paths
  55. cd "../.git/hooks"
  56. #Grab the canonical path to the file, readlink always follows symlinks
  57. if [[ "$2" == "all" ]]; then
  58. hooks=( $(realpath --no-symlinks $(ls --ignore='*.sample' --ignore='*.sh')) )
  59. else
  60. hooks=( $(realpath --no-symlinks "$2") )
  61. fi
  62. for hook in "${hooks[@]}"; do
  63. echo "$hook"
  64. rm "$hook"
  65. done
  66. ;;
  67. *)
  68. echo "$USAGEMSG"
  69. exit 1
  70. ;;
  71. esac