setup_githooks 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/bash
  2. githooksdir="githooks"
  3. dest=".git/hooks"
  4. if ! type realpath >/dev/null; then
  5. echo "required program not found 'realpath'"
  6. exit 10
  7. fi
  8. echo "Run this only once after git clone, it will symlink all hooks from '$githooksdir' dir into '$dest' dir, unless they already exist."
  9. echo "You can run this again in the future to check for symlinks consistency"
  10. if test ! -d "$dest" ; then
  11. echo "'$dest' folder not detected!"
  12. exit 2
  13. fi
  14. for fd in "${githooksdir}"/*; do
  15. f=${fd##*/}
  16. ffull="`realpath -- "$fd"`"
  17. d="${dest}/$f"
  18. destfull="`realpath -- "$d"`"
  19. echo "processing: '$f'"
  20. echo " from '$ffull'"
  21. echo " to '$d'"
  22. if test -z "$f" -o -z "$ffull" -o -z "$d" -o -z "$destfull"; then
  23. echo "one of the following is empty: '$f' or '$ffull' or '$d' or '$destfull'"
  24. exit 81
  25. fi
  26. if test -h "$d" ; then
  27. echo " existing symlink"
  28. if test "$ffull" != "$destfull"; then
  29. echo " symlink doesn't point to '$ffull' but rather to '$destfull'"
  30. echo " you may want to manually check that folder for possibly broken links"
  31. echo " aborting"
  32. exit 6
  33. else
  34. echo " points to same thing as expected"
  35. fi
  36. echo " skipped"
  37. continue
  38. elif test -e "$f" ; then
  39. echo " '$f' exists as something that's not a symlink"
  40. echo " bailing out!"
  41. exit 3
  42. else
  43. echo " symlinking(relative)"
  44. ln --relative --symbolic --target-directory="${dest}" -- "$ffull"
  45. ec=$?
  46. if test "$ec" -ne 0; then
  47. echo " failed, ln exitcode=$ec bailing out"
  48. exit 5
  49. fi
  50. echo " done $?"
  51. continue
  52. fi
  53. echo "unreached"
  54. exit 80
  55. done
  56. echo "Everything OK!"