mirror-submodule-update.sh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. # SED parses the strings like:
  41. #
  42. #-b991c67c1d91574ef22336cc3a5944d1e63230c9 roms/ipxe
  43. #b991c67c1d91574ef22336cc3a5944d1e63230c9 roms/ipxe (v1.0.0-2388-gb991c67)
  44. #
  45. for SUBPATH in $(git submodule status | sed -E 's/.*[[:space:]](.*)([[:space:]].*|$)/\1/')
  46. do
  47. SUBMIRROR=$(join -o"2.2" <(echo ${SUBPATH}) <(sort ${MIRRORLIST}))
  48. [ ${SUBMIRROR} ] || continue
  49. SUBMIRROR=${SUBMIRROR//@GENERAL_MIRROR_SERVER@/${GITLAB_SSH_SERVER}}
  50. echo -e "[switch mirror] $SUBPATH \tto\t $SUBMIRROR"
  51. git config submodule.${SUBPATH}.url ${SUBMIRROR}
  52. done
  53. # 3
  54. # Getting submodules of the current repository from the local mirrors
  55. git submodule update || exit $ERR_CANNOT_UPDATE
  56. # 4
  57. # Replacing URLs for each sub-submodule.
  58. # The script runs recursively
  59. git submodule foreach "${SCRIPT_SH}" # No '--recursive'
  60. popd >/dev/null