#!/bin/bash die() { echo "FATAL program error" exit 1 } gitTry() { echo "git $*" git $* } gitNoFail() { echo "git $*" git $* || die } createRepo() { local name=$1 [[ -n "$name" ]] || die mkdir $name || die pushd $name >/dev/null pwd gitNoFail init echo line1>readme.txt gitNoFail add readme.txt gitNoFail commit -m first popd >/dev/null echo "created git repo '$name'" } printHeader() { echo "==============================================================" if [[ -n "$1" ]]; then echo "$1" echo "==============================================================" fi } setupEnv() { if [[ -n "$GIT_PATH" ]]; then [[ -e "$GIT_PATH/git" ]] || die echo "user provided custom git in ${GIT_PATH}" export PATH="${GIT_PATH}:${PATH}" gitNoFail --version fi } main() { setupEnv local root=$(pwd) rm -rf child{1,2} user1 server1 local gitArgs="-c protocol.file.allow=always" #create submodule repos printHeader "create submodule repo 1" createRepo child1 printHeader "create submodule repo 2" createRepo child2 #workaround for identical initial SHA (is this a bug?) pushd child2 >/dev/null touch foobar gitNoFail add foobar gitNoFail commit -m second popd >/dev/null #create a repo intended as the server repo that all users clone from printHeader "setup server repo" createRepo server1 #add submodule pushd server1 >/dev/null gitNoFail ${gitArgs} submodule add -b master "${root}/child1" sub1 gitNoFail commit -m added_submodule popd >/dev/null #a user clones from server printHeader "setup user repo" gitNoFail ${gitArgs} clone --recurse-submodules server1 user1 #change a submodule url in an arbitrary branch. This branch would have been pushed to the server by another user. printHeader "switch submodule url" pushd server1 >/dev/null pwd gitNoFail checkout -b exper gitNoFail config --file=.gitmodules submodule.sub1.url "${root}/child2" gitNoFail ${gitArgs} submodule sync -- sub1 gitNoFail ${gitArgs} submodule update --remote -- sub1 gitNoFail add --all gitNoFail commit -m switch_submodule_url popd >/dev/null #now user will update pushd user1 >/dev/null printHeader "user updates" pwd #this will give an error, but shouldn't gitTry fetch #test again printHeader "try again" gitNoFail branch -r -D origin/exper gitTry fetch #test workaround printHeader "workaround" gitNoFail branch -r -D origin/exper gitNoFail config submodule.recurse false gitNoFail fetch popd >/dev/null } main $@