Skip to content

Instantly share code, notes, and snippets.

@kolomenkin
Forked from vi/submodule_update_alturl.sh
Last active April 25, 2016 12:56
Show Gist options
  • Save kolomenkin/4f448a84ee9913ee9c1e1b43b4361b01 to your computer and use it in GitHub Desktop.
Save kolomenkin/4f448a84ee9913ee9c1e1b43b4361b01 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/bash
script_version="1.04"
unset CDPATH
if [[ $1 = "--help" ]]; then
echo >&2 "Script version: $script_version"
echo >&2 "Expected environment example: ALTURL_repo1name=https://github.com/user1/repo1name.git ALTURL_repo2name=https://github.com/user1/repo2name.git ..."
echo >&2 "Expected environment example: mangle_submodule_name() - it should mangle its argument: 'repo/1/name' -> 'repo_1_name'"
echo >&2 "Expected environment example: submodule_recurse_script=update-checkout.sh submodule_recurse_arguments='arg1 arg2 arg3'"
echo >&2 "Script arguments: --force - optional argument for passing to embedded 'git submodule update' command"
exit 1
fi
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() {
# output for submodule "Sub.path.Name" for command git config -f .gitmodules --list:
#submodule.Sub.path.Name.path=Sub/path/Name
#submodule.Sub.path.Name.url=git@domain:group/repo.git
git config -f .gitmodules --list | \
grep '^submodule\.' | sed 's/^submodule\.//' | sed 's/\.[^\.]\{1,\}=.\{1,\}$//' | sort | uniq
}
handle_submodules_in_current_directory() {
if [ ! -f .gitmodules ]; then return 0; fi
#git submodule init
#git submodule sync || true
IFS='
'
local submodule_list=($(list_all_submodule_names))
for MODULE_NAME in "${submodule_list[@]}"; do
local MANGLED_MODULE_NAME=$(mangle_submodule_name "${MODULE_NAME}")
printf 'Processing submodule %-15s %-20s in\t %s\n' "${MANGLED_MODULE_NAME}" "($MODULE_NAME)" "$PWD"
local SP=$(git config -f .gitmodules --get submodule."$MODULE_NAME".path || true)
if [ -z $SP ]; then SP="$MODULE_NAME"; fi
# First time trying without hacks
if git submodule update --init "$@" -- "$SP" ; then
# OK, no need for tricks
:;
else
local ADDITIONAL_URL_NAME=ALTURL_${MANGLED_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
# go deeper:
if [[ ! -z $submodule_recurse_script && -f "$SP/$submodule_recurse_script" ]]; then
echo "Entering $SP to update its submodules (its own script)"
echo "===================================== (caller pid: $$)"
(
cd "$SP"
"./$submodule_recurse_script" $submodule_recurse_arguments
)
echo "===================================== (caller pid: $$)"
echo "Leaving $SP"
elif [ -f "$SP/.gitmodules" ]; then
echo "Entering $SP to update its submodules (subproc)"
(
cd "$SP"
handle_submodules_in_current_directory
)
echo "Leaving $SP"
fi
done
}
handle_submodules_in_current_directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment