Skip to content

Instantly share code, notes, and snippets.

@hkoba
Created May 10, 2022 03:01
Show Gist options
  • Save hkoba/ef8a2b614c692271230907ddc3e50613 to your computer and use it in GitHub Desktop.
Save hkoba/ef8a2b614c692271230907ddc3e50613 to your computer and use it in GitHub Desktop.
Safely sync git submodule urls to new repo+new .gitmodules for submodules in submodules
#!/bin/zsh
set -e
autoload colors; colors;
#----------------------------------------
zparseopts -D -K q=o_quiet x=o_xtrace
((ARGC)) || { echo 1>&2 "Usage: $0 GIT_URL"; exit 1 }
remote=$1; shift
#----------------------------------------
if (($#o_quiet)); then
export GIT_SSH_COMMAND='ssh -x'
fi
if (($#o_xtrace)); then set -x; fi
#----------------------------------------
git rev-parse --show-toplevel || exit 1
git remote set-url origin $remote
git fetch $o_quiet --no-recurse-submodules
git checkout main
git submodule sync $o_quiet
#----------------------------------------
# この時点では remote.origin.url は置き換わるけど、
# submodule.$mod.url が以前の間違った値のまま
#----------------------------------------
git submodule foreach $o_quiet git fetch $o_quiet --no-recurse-submodules
git submodule foreach $o_quiet git submodule sync $o_quiet
# これで submodule.$mod.url は正常化する…?(位置が同じなら)
git submodule update $o_quiet
# これで submodule 内の .gitmodules が正しいものに置き換わったので、再帰sync出来る
git submodule sync $o_quiet --recursive
git submodule update $o_quiet --recursive
echo $bg[green]DONE$bg[default]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment