2017-06-14 03:52:33 -04:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# Short script that is run as part of the CI environment
|
|
|
|
# in .gitlab-ci.yml
|
|
|
|
#
|
|
|
|
# Sets up submodules in the ESP-IDF source tree
|
|
|
|
# - Ideally, this just means doing a "git submodule update"
|
|
|
|
# - But if something goes wrong we re-clone the repo from scratch
|
|
|
|
#
|
|
|
|
# This is a "best of both worlds" for GIT_STRATEGY: fetch & GIT_STRATEGY: clone
|
|
|
|
#
|
|
|
|
|
2017-07-03 04:28:58 -04:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Common bash
|
2018-03-27 01:13:35 -04:00
|
|
|
if [[ ! -z ${DEBUG_SHELL} ]]; then
|
2017-07-03 04:28:58 -04:00
|
|
|
set -x # Activate the expand mode if DEBUG is anything but empty.
|
|
|
|
fi
|
|
|
|
|
|
|
|
set -o errexit # Exit if command failed.
|
|
|
|
set -o pipefail # Exit if pipe failed.
|
|
|
|
set -o nounset # Exit if variable not set.
|
|
|
|
|
2017-06-14 03:52:33 -04:00
|
|
|
die() {
|
|
|
|
echo "${1:-"Unknown Error"}" 1>&2
|
2017-07-03 04:28:58 -04:00
|
|
|
exit ${2:-1}
|
2017-06-14 03:52:33 -04:00
|
|
|
}
|
2017-07-03 04:28:58 -04:00
|
|
|
# -----------------------------------------------------------------------------
|
2017-06-14 03:52:33 -04:00
|
|
|
|
|
|
|
[ -z ${CI_PROJECT_DIR} ] && die "This internal script should only be run by a Gitlab CI runner."
|
2017-07-03 04:28:58 -04:00
|
|
|
[ -z ${GITLAB_SSH_SERVER} ] && die "GITLAB_SSH_SERVER should be defined to run mirror-submodule-update.sh"
|
|
|
|
[ -z ${CI_REPOSITORY_URL} ] && die "CI_REPOSITORY_URL should be defined to run mirror-submodule-update.sh"
|
|
|
|
[ -z ${CI_COMMIT_SHA} ] && die "CI_COMMIT_SHA should be defined to run mirror-submodule-update.sh"
|
2018-07-23 05:03:05 -04:00
|
|
|
DONT_USE_MIRROR=${DONT_USE_MIRROR:-"0"}
|
2017-06-18 21:59:18 -04:00
|
|
|
|
2017-07-03 04:28:58 -04:00
|
|
|
ERR_CANNOT_UPDATE=13
|
|
|
|
|
2017-06-18 21:59:18 -04:00
|
|
|
SCRIPT_DIR=$(dirname -- "${0}")
|
|
|
|
update_submodules() {
|
2018-07-23 05:03:05 -04:00
|
|
|
if [ "${DONT_USE_MIRROR}" = "1" ]; then
|
2017-06-18 21:59:18 -04:00
|
|
|
git submodule update --init --recursive
|
2018-07-23 05:03:05 -04:00
|
|
|
else
|
|
|
|
${SCRIPT_DIR}/mirror-submodule-update.sh || return $?
|
2017-06-18 21:59:18 -04:00
|
|
|
fi
|
2020-01-14 01:28:18 -05:00
|
|
|
# possibility that there are some untracked files left in submodule working directories
|
|
|
|
git submodule foreach --recursive git clean -ffdx
|
2017-06-18 21:59:18 -04:00
|
|
|
}
|
2017-06-14 03:52:33 -04:00
|
|
|
|
|
|
|
del_files() {
|
2017-07-03 04:28:58 -04:00
|
|
|
DELETED_FILES=$(mktemp --tmpdir -d tmp_XXXX)
|
2017-06-14 03:52:33 -04:00
|
|
|
# if non-empty
|
|
|
|
[ "$(ls -A .)" ] && ( shopt -s dotglob; mv * "${DELETED_FILES}/" )
|
2017-07-03 04:28:58 -04:00
|
|
|
trap 'del_files_rollback' ERR
|
2017-06-14 03:52:33 -04:00
|
|
|
}
|
|
|
|
del_files_confirm() {
|
2017-07-03 04:28:58 -04:00
|
|
|
[ -d "${DELETED_FILES}" ] && rm -rf "${DELETED_FILES}"
|
|
|
|
trap ERR
|
|
|
|
}
|
|
|
|
del_files_rollback() {
|
|
|
|
[ "$(ls -A .)" ] && [ "$(ls -A ${DELETED_FILES}/)" ] && ( shopt -s dotglob; rm -rf * )
|
|
|
|
[ "$(ls -A ${DELETED_FILES}/)" ] && ( shopt -s dotglob; mv "${DELETED_FILES}/"* . )
|
|
|
|
[ -d "${DELETED_FILES}" ] && rmdir "${DELETED_FILES}"
|
|
|
|
trap ERR
|
2017-06-14 03:52:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
RETRIES=10
|
|
|
|
# we're in gitlab-ci's build phase, so GET_SOURCES_ATTEMPTS doesn't apply here...
|
|
|
|
|
|
|
|
# For the first time, we try the fastest way.
|
|
|
|
for try in `seq $RETRIES`; do
|
|
|
|
echo "Trying to add submodules to existing repo..."
|
2017-06-18 21:59:18 -04:00
|
|
|
update_submodules &&
|
2017-06-14 03:52:33 -04:00
|
|
|
echo "Fetch strategy submodules succeeded" &&
|
|
|
|
exit 0
|
2017-07-03 04:28:58 -04:00
|
|
|
|
2017-08-20 23:51:15 -04:00
|
|
|
git submodule foreach --recursive "git reset --hard HEAD && git submodule deinit --force -- . || true"
|
|
|
|
git reset --hard HEAD && git submodule deinit --force -- . || true
|
2017-06-14 03:52:33 -04:00
|
|
|
done
|
|
|
|
|
|
|
|
# Then we use the clean way.
|
|
|
|
for try in `seq $RETRIES`; do
|
|
|
|
cd ${CI_PROJECT_DIR} # we are probably already here but pays to be certain
|
|
|
|
echo "Trying a clean clone of IDF..."
|
|
|
|
del_files
|
|
|
|
git clone ${CI_REPOSITORY_URL} . &&
|
|
|
|
git checkout ${CI_COMMIT_SHA} &&
|
2017-06-18 21:59:18 -04:00
|
|
|
update_submodules &&
|
2017-06-14 03:52:33 -04:00
|
|
|
echo "Clone strategy succeeded" &&
|
|
|
|
del_files_confirm &&
|
|
|
|
exit 0
|
2017-07-03 04:28:58 -04:00
|
|
|
ERR_RES=$?
|
|
|
|
del_files_rollback
|
2017-06-14 03:52:33 -04:00
|
|
|
echo "Clean clone failed..."
|
2017-07-03 04:28:58 -04:00
|
|
|
if [ $ERR_RES -eq $ERR_CANNOT_UPDATE ]; then
|
|
|
|
echo "###"
|
|
|
|
echo "### If you have updated one of the submodules,"
|
|
|
|
echo "### you have to synchronize the local mirrors manually"
|
|
|
|
echo "###"
|
|
|
|
echo "### https://gitlab.espressif.cn:6688/idf/esp-idf/wikis/ci-use-guide#submodule-mirroring-for-private-branches"
|
|
|
|
echo "###"
|
|
|
|
|
|
|
|
die "Failed to clone repo & submodules together" $ERR_RES
|
|
|
|
fi
|
2017-06-14 03:52:33 -04:00
|
|
|
done
|
|
|
|
|
2017-06-18 21:59:18 -04:00
|
|
|
die "Failed to clone repo & submodules together"
|