sync 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env bash
  2. #
  3. # Upload changes from inside repository using GIN CLI
  4. # Works with submodules.
  5. # Assumes gin init was performed or the repository was downloaded via gin get
  6. usage() {
  7. echo "$0 <sync-option>"
  8. echo
  9. echo "<sync-option> The sync option defines what to do with the content of large files."
  10. echo " It should be one of the following values:"
  11. echo " download - Download all large file content from the server"
  12. echo " keep - Keep all large file content but do not download missing large files"
  13. echo " remove - Do not download large files and remove existing local large file content"
  14. exit 1
  15. }
  16. syncopt="2"
  17. echo "Optionally enter a commit message, and hit return: "
  18. read commitmessage
  19. if [[ "$commitmessage" == "" ]]; then
  20. echo "using date as commit message"
  21. commitmessage="commit on $(date +%Y-%m-%d)"
  22. fi
  23. checkargs() {
  24. case "$1" in
  25. 0)
  26. echo "Downloading and keep all large file content"
  27. ;;
  28. 1)
  29. echo "Keeping existing local large file content, do not downlad extra files"
  30. ;;
  31. 2)
  32. echo "Removing all local large file content"
  33. ;;
  34. *)
  35. usage
  36. ;;
  37. esac
  38. }
  39. checkargs "${syncopt}"
  40. checkerror() {
  41. err=$1
  42. msg=$2
  43. if [[ ${err} != 0 ]]; then
  44. echo "${msg}" >> ./log/gin.log
  45. echo "${err}" >> ./log/gin.log
  46. echo "${msg}"
  47. echo "Press [Enter] to close this window"
  48. read -r
  49. exit 1
  50. fi
  51. }
  52. loc=$(dirname $0)
  53. projectdir=$(git -C ${loc} rev-parse --show-toplevel)
  54. pushd ${loc} > /dev/null
  55. mkdir -p ./log
  56. echo "$(date +'%Y-%m-%dT%H:%M:%S'): Sync script executed" >> ./.log/gin.log
  57. echo "Synchronising submodules"
  58. git submodule foreach gin commit . -m "$commitmessage"
  59. checkerror $? "Error occurred during 'gin commit'"
  60. git submodule foreach gin sync
  61. checkerror $? "Error occurred during 'gin sync'"
  62. git submodule foreach gin upload
  63. checkerror $? "Error occurred during 'gin upload'"
  64. ## remove uploaded (annexed) content
  65. if [[ "$syncopt" > 1 ]]; then
  66. git submodule foreach gin remove-content
  67. checkerror $? "Error occurred during 'gin remove-content'"
  68. fi
  69. # get annexed content
  70. if [[ "${syncopt}" <1 ]]; then
  71. git submodule foreach gin get-content .
  72. checkerror $? "Error occurred during 'gin get-content .'"
  73. fi
  74. echo "Synchronising main repository"
  75. gin commit . -m "$commitmessage"
  76. gin sync
  77. gin upload
  78. # remove uploaded (annexed) content
  79. if [[ "$syncopt" > 1 ]]; then
  80. gin remove-content
  81. checkerror $? "Error occurred during 'gin remove-content' in main repo"
  82. fi
  83. # get annexed content
  84. if [[ "${syncopt}" <1 ]]; then
  85. gin get-content .
  86. checkerror $? "Error occurred during 'gin get-content . in main repo'"
  87. fi