sandbox.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/bin/bash
  2. # Load the configuration file
  3. FILE=.sandbox-config
  4. # Create the configuration file if it does not exist
  5. if ! test -f "$FILE"; then
  6. echo "No sandbox config exists."
  7. echo "What is the sandbox user? [wpdev]"
  8. read SANDBOX_USER
  9. SANDBOX_USER="${SANDBOX_USER:-wpdev}"
  10. echo "What is the sandbox location?"
  11. read SANDBOX_LOCATION
  12. [[ -z "$SANDBOX_LOCATION" ]] && { echo "Come back when you know where your sandbox is." ; exit 1; }
  13. echo "What is the themes folder on the sandbox? [/home/$SANDBOX_USER/public_html/wp-content/themes/pub]"
  14. read SANDBOX_PUBLIC_THEMES_FOLDER
  15. SANDBOX_PUBLIC_THEMES_FOLDER="${SANDBOX_PUBLIC_THEMES_FOLDER:-/home/$SANDBOX_USER/public_html/wp-content/themes/pub}"
  16. echo "Writing $FILE"
  17. /bin/cat <<EOM >$FILE
  18. SANDBOX_USER="$SANDBOX_USER"
  19. SANDBOX_LOCATION="$SANDBOX_LOCATION"
  20. SANDBOX_PUBLIC_THEMES_FOLDER="$SANDBOX_PUBLIC_THEMES_FOLDER"
  21. EOM
  22. fi
  23. source $FILE
  24. if [[ $1 == "clean" ]]; then
  25. ssh -T $SANDBOX_USER@$SANDBOX_LOCATION << EOF
  26. cd '$SANDBOX_PUBLIC_THEMES_FOLDER';
  27. svn revert -R .;
  28. svn cleanup --remove-unversioned;
  29. svn up;
  30. EOF
  31. elif [[ $1 == "push" ]]; then
  32. git remote update
  33. current_branch=$(git branch --show-current)
  34. hash1=$(git rev-parse origin/trunk)
  35. hash2=$(git merge-base origin/trunk ${current_branch})
  36. files_to_ignore=''
  37. if [ ! "${hash1}" = "${hash2}" ]; then
  38. if [[ $2 != '--force' && $2 != '--ignore' ]]; then
  39. echo "!! ----------------------------------------------------------- !!
  40. The branch you are pushing is not up-to-date with /trunk.
  41. This may result in out-of-date resources on your sandbox.
  42. How do you wish to proceed?
  43. 1 - Nevermind, I'll merge/rebase my branch first.
  44. (This option is the safest.)
  45. 2 - Go ahead, I know what I'm doing. I want the old files on my sandbox.
  46. (This is a good option if you are evaluating an old branch for breakage
  47. and want to have the sandbox reflect exactly the way it was back then.)
  48. 3 - IGNORE the files changed in /trunk since this branch diverged.
  49. (This is great during development so that you don't have to keep your
  50. branch completely up-to-date with /trunk and MIGHT be safe for pushing
  51. a build. Use at your own risk.)
  52. !! ----------------------------------------------------------- !!
  53. How do you wish to proceed? [1]"
  54. read sync_type
  55. fi
  56. if [[ $sync_type = "3" || $2 = '--ignore' ]]; then
  57. echo "building ignore list based on divergence"
  58. # These are the files that were changed in trunk since this branch diverged
  59. files_to_ignore=$(git diff ${hash2} origin/trunk --name-only)
  60. files_to_ignore=($files_to_ignore)
  61. # These are the files changed in THIS branch since it diverged
  62. # EVEN IF they changed in trunk we STILL want to include them in our sync
  63. files_to_include=$(git diff ${hash2} ${current_branch} --name-only)
  64. files_to_include=($files_to_include)
  65. for target in "${files_to_include[@]}"; do
  66. for i in "${!files_to_ignore[@]}"; do
  67. if [[ ${files_to_ignore[i]} = $target ]]; then
  68. unset 'files_to_ignore[i]'
  69. fi
  70. done
  71. done
  72. # These are the changes we have made but haven't committed yet
  73. # EVEN IF they changed in trunk we STILL want to include them in our sync
  74. files_to_include=$(git diff HEAD --name-only)
  75. files_to_include=($files_to_include)
  76. for target in "${files_to_include[@]}"; do
  77. for i in "${!files_to_ignore[@]}"; do
  78. if [[ ${files_to_ignore[i]} = $target ]]; then
  79. unset 'files_to_ignore[i]'
  80. fi
  81. done
  82. done
  83. ignore_string=""
  84. for target in "${files_to_ignore[@]}"; do
  85. ignore_string="${ignore_string}${target}','"
  86. done
  87. ignore_string=${ignore_string::${#ignore_string}-2}
  88. ignore_string="{'$ignore_string}"
  89. elif [[ $sync_type = "2" || $2 = '--force' ]]; then
  90. echo "syncing to sandbox exactly what you have here"
  91. else
  92. echo "Exiting. Clean yourself up and come back later."
  93. exit 0;
  94. fi
  95. fi
  96. cmd="rsync -av --no-p --no-times --exclude-from='.sandbox-ignore' --exclude=$ignore_string ./ $SANDBOX_USER@$SANDBOX_LOCATION:$SANDBOX_PUBLIC_THEMES_FOLDER/"
  97. eval $cmd
  98. else
  99. echo 'No known command given. [clean, push]'
  100. fi