Common utilities
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4 vuotta sitten
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash
  2. #
  3. # Usage: gsync [rsync-options] source destination
  4. #
  5. # Synchronize a git source directory with a remote directory, excluding files according to then
  6. # rules found in .gitignore. If subdirectories also contain .gitignore files, then those rules
  7. # will be applied (but only in each respective subdirectory).
  8. #
  9. # Note that ONLY .gitignore files in the directory where this command is run from will be considered
  10. # Thus, when the source or destination is a local path, it should be specified relative to the current
  11. # directory.
  12. #
  13. # There will be two rsync statements - one to exclude everything that should be excluded,
  14. # and a second to handle the exceptions to the exclusion rules - the lines in .gitignore that begin with !
  15. #
  16. # The exceptions to the exclusions are rsync'd first, and if that succeeds, the second rsync
  17. # copies everything else.
  18. #
  19. #
  20. # --- SUPPORT OPEN SOURCE ---
  21. # If you find this script has saved you a decent amount time, please consider dropping me some coin.
  22. # I will be forever grateful and your name will be permanently emblazoned on my Wall of Honor.
  23. # My bitcoin wallet address is 1HoiSHKxYM4EtsP3xFGsY2xWYvh4hAuJ2q
  24. # Paypal or Dwolla: jonathan (replace this with the 'AT' sign on your keyboard) kyuss.org
  25. #
  26. # Thank You.
  27. #
  28. # - jonathan.
  29. #
  30. if [[ -z "${1}" || -z "${2}" || "${1}" == "--help" || "${1}" == "-help" || "${1}" == "-h" ]] ; then
  31. echo "Usage: gsync [rsync-options] source destination"
  32. exit 1
  33. fi
  34. includes=""
  35. excludes='--exclude=.git*'
  36. base="$(pwd)"
  37. function process_git_ignore () {
  38. git_ignore="${1}"
  39. if [ "$(dirname ${git_ignore})" = "${base}" ] ; then
  40. prefix=""
  41. else
  42. prefix=".$(echo -n "$(dirname ${git_ignore})" | sed -e 's,^'${base}',,')"
  43. fi
  44. while read -r line || [[ -n "${line}" ]] ; do
  45. # todo: there is probably a cleaner test for "first char == !"
  46. if [ $(echo "${line}" | head -c 1 | grep -- '!' | wc -l) -gt 0 ] ; then
  47. includes="${includes}
  48. --include='${prefix}$(echo "${line}" | sed -e 's/^!//' | sed -e 's/ /\\ /g')'"
  49. else
  50. excludes="${excludes}
  51. --exclude='${prefix}$(echo "${line}" | sed -e 's/ /\\ /g')'"
  52. fi
  53. done < ${git_ignore}
  54. }
  55. # root .gitignore file
  56. if [ -f .gitignore ] ; then
  57. process_git_ignore "$(pwd)/.gitignore"
  58. fi
  59. # check for other .gitignore files
  60. for i in $(find $(pwd) -mindepth 2 -type f -name .gitignore) ; do
  61. process_git_ignore "${i}"
  62. done
  63. rsync ${includes} --exclude="*" ${@} && rsync ${excludes} ${@}
  64. # for debugging
  65. #echo "rsync ${includes} --exclude=\"*\" ${@}" && echo "rsync ${excludes} ${@}"
  66. #echo "rsync ${excludes} ${@}"