Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 51 lines (44 sloc) 1.41 KB
#!/bin/bash -e
## Remove orphaned submodule directories
## read input, display help if necessary
if [[ "$@" == *--help* ]]; then
Remove orphaned submodule directories
This command removed orphaned submodule directories, i.e., if a
submodule was removed (or han its url changed) but the directory was
not removed because it was not empty. Before removing a directory,
the script checks for any changes that might be lost. Afterwards, a
submodule sync is executed.
git rm-orphaned-submodule-dirs
exit 0;
## ensure we are in the toplevel directory
cdup=$(git rev-parse --show-toplevel) &&
cd "$cdup" || {
echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree"
exit 1
## find submodule urls not in .gitmodules:
git config --get-regexp submodule.*url |
while read key url; do
#echo "$key"
dir=`expr "$key" : 'submodule\.\(.*\)\.url'`
#echo "$dir"
if [[ -d "$dir" && $(git config -f .gitmodules --get "$key") != "$url" ]]; then
## Check if the directory is clean
cd "$dir"
git check-clean || exit 1
git check-unpushed || exit 1
git check-non-tracking || exit 1
## find the real git-dir (might be git-file, for v.1.7.8 compatibility)
gitdir=$(git rev-parse --git-dir)
cd "$tdir"
## remove git dir first (might git-file, for v.1.7.8 compatibility)
rm -rf "$dir$gitdir"
rm -rf "$dir"
git submodule sync > /dev/null