mirror-submodule-update.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/bash
  2. #
  3. # Redirects git submodules to the specified local mirrors and updates these recursively.
  4. #
  5. # To revert the changed URLs use 'git submodule deinit .'
  6. #
  7. # -----------------------------------------------------------------------------
  8. # Common bash
  9. if [[ ! -z ${DEBUG_SHELL} ]]
  10. then
  11. set -x # Activate the expand mode if DEBUG is anything but empty.
  12. fi
  13. set -o errexit # Exit if command failed.
  14. set -o pipefail # Exit if pipe failed.
  15. set -o nounset # Exit if variable not set.
  16. die() {
  17. echo "${1:-"Unknown Error"}" 1>&2
  18. exit 1
  19. }
  20. # -----------------------------------------------------------------------------
  21. [ -z ${GITLAB_SSH_SERVER:-} ] && die "Have to set up GITLAB_SSH_SERVER environment variable"
  22. if [ "${LOCAL_GITLAB_SSH_SERVER:-}" ]; then
  23. GITLAB_SSH_SERVER="${LOCAL_GITLAB_SSH_SERVER}"
  24. fi
  25. ERR_CANNOT_UPDATE=13
  26. REPO_DIR=${1:-"${PWD}"}
  27. REPO_DIR=$(readlink -f -- "${REPO_DIR}")
  28. SCRIPT_DIR=$(dirname -- "${0}")
  29. SCRIPT_DIR=$(readlink -f -- "${SCRIPT_DIR}")
  30. SCRIPT_SH=$(readlink -f -- "${0}")
  31. MIRRORLIST=${SCRIPT_DIR}/mirror-list.txt
  32. [ -d "${REPO_DIR}" ] || die "${REPO_DIR} is not directory!"
  33. [ -f "${SCRIPT_SH}" ] || die "${SCRIPT_SH} does not exist!"
  34. [ -f "${MIRRORLIST}" ] || die "${MIRRORLIST} does not exist!"
  35. pushd ${REPO_DIR} >/dev/null
  36. # 0
  37. [ -f ".gitmodules" ] || exit 0
  38. # 1
  39. git submodule init
  40. # 2
  41. # Replacing each submodule URL of the current repository
  42. # according to the one found in the MIRRORLIST
  43. #
  44. # Selecting paths among lines:
  45. # ...
  46. #submodule.components/esp32/lib.path
  47. #submodule.components/esp32/lib.url
  48. #submodule.components/esptool_py/esptool.path
  49. #submodule.components/esptool_py/esptool.url
  50. #...
  51. for SUBPATH in $(git config -f .gitmodules --list --name-only | grep "\.path" | sed 's/^submodule\.\([^ ]*\)\.path$/\1/')
  52. do
  53. SUBMIRROR=$(join -o"2.2" <(echo ${SUBPATH}) <(sort ${MIRRORLIST}))
  54. [ ${SUBMIRROR} ] || continue
  55. SUBMIRROR=${SUBMIRROR//@GENERAL_MIRROR_SERVER@/${GITLAB_SSH_SERVER}}
  56. echo -e "[switch mirror] $SUBPATH \tto\t $SUBMIRROR"
  57. git config submodule.${SUBPATH}.url ${SUBMIRROR}
  58. done
  59. # 3
  60. # Getting submodules of the current repository from the local mirrors
  61. git submodule update || exit $ERR_CANNOT_UPDATE
  62. # 4
  63. # Replacing URLs for each sub-submodule.
  64. # The script runs recursively
  65. git submodule foreach "${SCRIPT_SH}" # No '--recursive'
  66. popd >/dev/null