forked from kollerma/git-submodule-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-rm-orphaned-submodule-dirs
executable file
·64 lines (56 loc) · 1.77 KB
/
git-rm-orphaned-submodule-dirs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash -e
## Remove orphaned submodule directories
## read input, display help if necessary
if [[ "$@" == *--help* ]]; then
cat<<EOF
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.
Usage:
git rm-orphaned-submodule-dirs
EOF
exit 0;
fi
## from the git mailinglist:
function git
{
LC_MESSAGES=C command git "$@"
}
export git
## 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
}
## ensure we will not remove the toplevel git directory
rootgitdir=$(cd `git rev-parse --git-dir`; pwd)
## 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
tdir=$PWD
## 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)
## git dir should be relative to current dir... but is it?
gitdir=$(cd `git rev-parse --git-dir`; pwd)
cd "$tdir"
rm -rf "$dir"
## remove git dir as well (might git-file, for v.1.7.8 compatibility)
## but only if it's not equal to root dir
if [[ "$gitdir" != "$rootgitdir" ]]; then
rm -rf "$gitdir"
fi
fi
done
git submodule sync > /dev/null