Skip to content

Instantly share code, notes, and snippets.

@ephemerr
Last active July 5, 2017 08:50
Show Gist options
  • Save ephemerr/17a8da3ae1be13f68ddf9bf4ba4aa3e8 to your computer and use it in GitHub Desktop.
Save ephemerr/17a8da3ae1be13f68ddf9bf4ba4aa3e8 to your computer and use it in GitHub Desktop.
Create clone of SUBMODULE based git repository(MODULAR) convering it to SUBREPO repository(TARGET).
#!/bin/bash
# Create clone of SUBMODULE based git repository(BASE) convering it to SUBREPO repository(TARGET).
#
# Use this script with care it can make severe changes both to TARGET and to BASE.
#
# Call it from directory of TARGET, with path to BASE.
# On a go, script make changes to BASE, creating branches named MODULAR_PRJ_NAME/ROOT_BRANCH_NAME for each submodule.
# It overrides already existing ones!
# In TARGET project it creates remote named 'modular', if there is alredy one - it is overrided!
# In TARGET project it creates branch named as curren branch in BASE, if there is alredy one - behavoir undefined!
# You shouldn't confirm continuation if there is any errors! It may cause severe damage to BASE repo.
declare -x REMOTE_PRJ=$1
declare -x CURRENT_PRJ=`pwd`
function confirm {
read -r -p "Confirm to continue: $1 [Y/n]" response;
if [[ "${response,,}" =~ ^(n)$ ]];
then exit 0
else echo -e $($1) # Do
fi
}
export -f confirm
function git-submodule-rm {
if [[ -z $1 ]] ; then
return -1
fi
declare SUBMODULE=$1
# Remove the submodule entry from .git/config
git submodule deinit -f "$SUBMODULE"
# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/"$SUBMODULE"
# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f "$SUBMODULE"
}
export -f git-submodule-rm
function git-submodule-branch-unify {
git branch -D $SUBBRANCH # !!delete branch in remote project if exist and not checked out
git checkout -b $SUBBRANCH # create named branch
}
export -f git-submodule-branch-unify
function git-submodule-clone-back {
declare SUBMODULE="$1"
declare TOP_LEVEL="$2"
declare LEVEL_AT_LOCAL=`realpath --relative-to="$REMOTE_PRJ_ABS" "$TOP_LEVEL"`
declare REMOTE_SUBMODULE="$TOP_LEVEL/$SUBMODULE"
declare LOCAL_SUBMODULE="$LEVEL_AT_LOCAL/$SUBMODULE"
cd "$CURRENT_PRJ"
echo "Changed dir to: $CURRENT_PRJ"
confirm "git subrepo clone --branch=$SUBBRANCH --force $REMOTE_SUBMODULE $LOCAL_SUBMODULE"
cd "$LOCAL_SUBMODULE"
echo "Changed dir to: $(pwd)"
declare REMOVE_COMMAND=\'"cd $CURRENT_PRJ/$LOCAL_SUBMODULE; git-submodule-rm \$name"\'
confirm "git submodule foreach $REMOVE_COMMAND" # clear subsub modules
cd "$REMOTE_SUBMODULE"
git submodule foreach 'git-submodule-clone-back $name $toplevel'
}
export -f git-submodule-clone-back
cd "$REMOTE_PRJ"
declare -x REMOTE_PRJ_ABS=`pwd`
declare -x PRJ_NAME=$(basename `pwd`)
declare -x BRANCH=`git rev-parse --abbrev-ref HEAD`
declare -x SUBBRANCH=$PRJ_NAME/$BRANCH
cd "$CURRENT_PRJ"
git remote remove modular
git remote add modular "$REMOTE_PRJ"
git fetch modular
git checkout $BRANCH
git submodule foreach 'cd $toplevel; git-submodule-rm $name'
cd "$REMOTE_PRJ"
git submodule update --recursive
git submodule foreach --recursive 'git-submodule-branch-unify'
git submodule foreach 'git-submodule-clone-back $name $toplevel'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment