Skip to content

Instantly share code, notes, and snippets.

@vi
Last active April 22, 2016 06:19
Show Gist options
  • Save vi/613a84dc34eb6329d776 to your computer and use it in GitHub Desktop.
Save vi/613a84dc34eb6329d776 to your computer and use it in GitHub Desktop.
"git submodule update --init --recursive" with second change for submodules referencing unpushed code (alternative URIs for submodules)
#!/bin/sh
unset CDPATH
cd_to_toplevel () {
cdup=$(git rev-parse --show-toplevel) &&
cd "$cdup" || {
echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree"
exit 1
}
}
cd_to_toplevel
set -e
#set -x
list_all_submodule_names() {
git config --list --name-only | \
grep '^submodule\.' | \
rev | cut -d. -f 2- | rev | \
cut -d. -f 2- | \
sort | uniq
}
handle_submodules_in_current_directory() {
git submodule init
IFS='
'
for MODULE_NAME in $(list_all_submodule_names); do
local SP=$(git config --get submodule."$MODULE_NAME".path || true)
if [ -z "$SP" ]; then SP="$MODULE_NAME"; fi
# First time trying without hacks
if git submodule update "$@" -- "$SP" ; then
# OK, no need for tricks
:;
else
local ADDITIONAL_URL_NAME=ALTURL_${MODULE_NAME}
local ADDITIONAL_URL=$(eval "echo \"\$$ADDITIONAL_URL_NAME\"")
if [ -n "$ADDITIONAL_URL" ]; then
echo Trying alternative URL $ADDITIONAL_URL
git -C "$SP" fetch "$ADDITIONAL_URL" '+refs/heads/*:refs/remotes/alturl/*' '+refs/tags/*:refs/tags/alturl/*'
else
echo "No alternative URL for $MODULE_NAME"
echo "Use $ADDITIONAL_URL_NAME environment variable to give it a second chance"
exit 1
fi
# Now retry
git submodule update "$@" -- "$SP"
fi
(
echo Entering "$SP"
cd "$SP"
handle_submodules_in_current_directory
echo Leaving "$SP"
)
done
}
handle_submodules_in_current_directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment