From a75d7dcd2ccd4b0b1444ee1f215b8e6a30180eec Mon Sep 17 00:00:00 2001 From: leleliu008 Date: Wed, 17 Jul 2024 04:56:15 +0800 Subject: [PATCH] optimized --- xcpkg | 90 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 71 insertions(+), 19 deletions(-) diff --git a/xcpkg b/xcpkg index 5d04510..6bcb5a1 100755 --- a/xcpkg +++ b/xcpkg @@ -122,6 +122,74 @@ sedInPlace() { fi } +git_submodule_update_recursive() { + if [ -z "$1" ] ; then + GIT_REPO_ROOT_DIR="$PWD" + else + GIT_REPO_ROOT_DIR="$1" + fi + + GIT_SUBMODULE_HAVE="$(cd "$GIT_REPO_ROOT_DIR" && find . -type f -name '.gitmodules' -print -quit)" + + if [ -n "$GIT_SUBMODULE_HAVE" ] ; then + if [ -z "$XCPKG_URL_TRANSFORM" ] ; then + run git submodule update --init --recursive + else + unset GIT_SUBMODULE_BASEDIR_STACK + + GIT_SUBMODULE_CONFIG_FILE_LIST="$(find "$GIT_REPO_ROOT_DIR" -type f -name '.gitmodules')" + + for f in $GIT_SUBMODULE_CONFIG_FILE_LIST + do + if [ -z "$GIT_SUBMODULE_BASEDIR_STACK" ] ; then + GIT_SUBMODULE_BASEDIR_STACK="${f%/*}" + else + GIT_SUBMODULE_BASEDIR_STACK="$GIT_SUBMODULE_BASEDIR_STACK;${f%/*}" + fi + done + + while [ -n "$GIT_SUBMODULE_BASEDIR_STACK" ] + do + case $GIT_SUBMODULE_BASEDIR_STACK in + *\;*) GIT_SUBMODULE_BASEDIR="${GIT_SUBMODULE_BASEDIR_STACK##*;}" ; GIT_SUBMODULE_BASEDIR_STACK="${GIT_SUBMODULE_BASEDIR_STACK%;*}" ;; + *) GIT_SUBMODULE_BASEDIR="${GIT_SUBMODULE_BASEDIR_STACK}" ; GIT_SUBMODULE_BASEDIR_STACK= + esac + + run cd "$GIT_SUBMODULE_BASEDIR" + + GIT_SUBMODULE_NAME_LIST="$(sed -n '/\[submodule ".*"\]/p' .gitmodules | sed 's|\[submodule "\(.*\)"\]|\1|')" + + for GIT_SUBMODULE_NAME in $GIT_SUBMODULE_NAME_LIST + do + GIT_SUBMODULE_PATH="$(git config --file=.gitmodules --get "submodule.$GIT_SUBMODULE_NAME.path")" + GIT_SUBMODULE_URL="$(git config --file=.gitmodules --get "submodule.$GIT_SUBMODULE_NAME.url")" + GIT_SUBMODULE_URI="$("$XCPKG_URL_TRANSFORM" "$GIT_SUBMODULE_URL")" + + run git submodule set-url "$GIT_SUBMODULE_PATH" "$GIT_SUBMODULE_URI" + done + + run git submodule update --init + + GIT_SUBMODULE_PATH_LIST="$(git submodule | sed 's|^ *||' | cut -d ' ' -f2)" + + for GIT_SUBMODULE_PATH in $GIT_SUBMODULE_PATH_LIST + do + GIT_SUBMODULE_CONFIG_FILE_LIST="$(find "$GIT_SUBMODULE_PATH" -type f -name '.gitmodules')" + + for f in $GIT_SUBMODULE_CONFIG_FILE_LIST + do + if [ -z "$GIT_SUBMODULE_BASEDIR_STACK" ] ; then + GIT_SUBMODULE_BASEDIR_STACK="$GIT_SUBMODULE_BASEDIR/${f%/*}" + else + GIT_SUBMODULE_BASEDIR_STACK="$GIT_SUBMODULE_BASEDIR_STACK;$GIT_SUBMODULE_BASEDIR/${f%/*}" + fi + done + done + done + fi + fi +} + # check if the given two version match the condition # # condition: @@ -2663,7 +2731,8 @@ __fetch_resources_of_the_given_package() { run git remote add origin "$GIT_FETCH_URL" run git -c protocol.version=2 fetch --progress $GIT_FETCH_EXTRA_OPTIONS origin "$GIT_REF_SPEC" run git checkout --progress --force -B "$GIT_BRANCH_NAME" "refs/remotes/origin/$GIT_BRANCH_NAME" - run git submodule update --init + + git_submodule_update_recursive rm -rf "$SESSION_DIR" fi @@ -3778,24 +3847,7 @@ $DOT_CONTENT run git -c protocol.version=2 fetch --progress $GIT_FETCH_EXTRA_OPTIONS origin "$GIT_REF_SPEC" run git checkout --progress --force -B "$GIT_BRANCH_NAME" "refs/remotes/origin/$GIT_BRANCH_NAME" - GIT_SUBMODULE_PATH_LIST="$(find . -name .gitmodules)" - - if [ -n "$GIT_SUBMODULE_PATH_LIST" ] ; then - if [ -n "$XCPKG_URL_TRANSFORM" ] ; then - for GIT_SUBMODULE_PATH in $GIT_SUBMODULE_PATH_LIST - do - GIT_SUBMODULE_URLS="$(grep 'url = ' "$GIT_SUBMODULE_PATH" | sed 's|[[:space:]]||g' | cut -c5-)" - - for GIT_SUBMODULE_URL in $GIT_SUBMODULE_URLS - do - GIT_SUBMODULE_URI="$("$XCPKG_URL_TRANSFORM" "$GIT_SUBMODULE_URL")" - sedInPlace "s|$GIT_SUBMODULE_URL|$GIT_SUBMODULE_URI|" "$GIT_SUBMODULE_PATH" - done - done - fi - - run git submodule update --init - fi + git_submodule_update_recursive fi ;; dir://*)