mirror-submodule-update.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. ERR_CANNOT_UPDATE=13
  23. REPO_DIR=${1:-"${PWD}"}
  24. REPO_DIR=$(readlink -f -- "${REPO_DIR}")
  25. SCRIPT_DIR=$(dirname -- "${0}")
  26. SCRIPT_DIR=$(readlink -f -- "${SCRIPT_DIR}")
  27. SCRIPT_SH=$(readlink -f -- "${0}")
  28. MIRRORLIST=${SCRIPT_DIR}/mirror-list.txt
  29. [ -d "${REPO_DIR}" ] || die "${REPO_DIR} is not directory!"
  30. [ -f "${SCRIPT_SH}" ] || die "${SCRIPT_SH} does not exist!"
  31. [ -f "${MIRRORLIST}" ] || die "${MIRRORLIST} does not exist!"
  32. pushd ${REPO_DIR} >/dev/null
  33. # 0
  34. [ -f ".gitmodules" ] || exit 0
  35. # 1
  36. git submodule init
  37. # 2
  38. # Replacing each submodule URL of the current repository
  39. # according to the one found in the MIRRORLIST
  40. #
  41. # Selecting paths among lines:
  42. # ...
  43. #submodule.components/esp32/lib.path
  44. #submodule.components/esp32/lib.url
  45. #submodule.components/esptool_py/esptool.path
  46. #submodule.components/esptool_py/esptool.url
  47. #...
  48. for SUBPATH in $(git config -f .gitmodules --list --name-only | grep "\.path" | sed 's/^submodule\.\([^ ]*\)\.path$/\1/')
  49. do
  50. SUBMIRROR=$(join -o"2.2" <(echo ${SUBPATH}) <(sort ${MIRRORLIST}))
  51. [ ${SUBMIRROR} ] || continue
  52. SUBMIRROR=${SUBMIRROR//@GENERAL_MIRROR_SERVER@/${GITLAB_SSH_SERVER}}
  53. echo -e "[switch mirror] $SUBPATH \tto\t $SUBMIRROR"
  54. git config submodule.${SUBPATH}.url ${SUBMIRROR}
  55. done
  56. # 3
  57. # Getting submodules of the current repository from the local mirrors
  58. git submodule update || exit $ERR_CANNOT_UPDATE
  59. # 4
  60. # Replacing URLs for each sub-submodule.
  61. # The script runs recursively
  62. git submodule foreach "${SCRIPT_SH}" # No '--recursive'
  63. popd >/dev/null