Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 74 lines (64 sloc) 1.77 KB
#!/bin/bash -e
## Move the submodule
## Usage: git mv-submodule [ --no-commit ] from to
## does not commit if --no-commit is given
## read input, display help if necessary
if [[ "$@" == "" || "$@" == *--help* ]]; then
cat<<EOF
Move a submodule
This command moves the submodule <from> to <to> and creates a new
commit with a simple commit message including the url of the
removed submodule. It just removes <from> and then adds <to>.
This means, that you will require an internet connection to download
the submodule again.
Usage:
git mv-submodule [--no-commit] <from> <to>
--no-commit: do not commit the result
EOF
exit 0;
fi
if [ "$1" == "--no-commit" ]; then
nocommit=1
from=$2
to=$3
else
from=$1
to=$2
fi
## check if this is a valid submodule
if [[ ! "$from" || $(git ls-files --error-unmatch --stage -- "$from" | grep -E '^160000') == "" ]]; then
echo >&2 "Error: \"$from\" is not a submodule."
exit 1
fi
## check destination
if [[ ! "$to" ]]; then
cat >&2 <<EOF
Error: argument <to> missing.
Use git mv-submodule [--no-commit] <from> <to>
EOF
exit 1
fi
if [ -e "$to" ]; then
echo >&2 "Error: \"$to\" exists already. Remove it first."
exit 1
fi
tdir=$PWD
cd "$from"
## check if submodule is clean
git check-clean || exit 1
## check for unpushed changes
git check-unpushed || exit 1
## check for local non-tracking-branches
git check-non-tracking || exit 1
cd "$tdir"
## seems we're safe, so start moving
## get submodule url
url=`git config -f .gitmodules --get submodule."$from".url`
## remove it
git rm-submodule --no-commit "$from"
## add it again at the new location
git submodule add "$url" "$to"
## commit changes
if [[ ! "$nocommit" ]]; then
git commit -m "moved submodule \"$from\" to \"$to\" (url: $url)"
fi