Skip to content

Commit 0234c0f

Browse files
committed
Import git-submodule-rm and git-submodule-purge.
0 parents  commit 0234c0f

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

git-submodule-purge

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/sh
2+
#
3+
# git-submodule-purge - purges the copies of obsoleted submodule repositories
4+
#
5+
# Copyright (c) 2012 Akinori MUSHA
6+
#
7+
# All rights reserved.
8+
#
9+
# Redistribution and use in source and binary forms, with or without
10+
# modification, are permitted provided that the following conditions
11+
# are met:
12+
# 1. Redistributions of source code must retain the above copyright
13+
# notice, this list of conditions and the following disclaimer.
14+
# 2. Redistributions in binary form must reproduce the above copyright
15+
# notice, this list of conditions and the following disclaimer in the
16+
# documentation and/or other materials provided with the distribution.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27+
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28+
# SUCH DAMAGE.
29+
#
30+
31+
main () {
32+
if [ ! -d .git ]; then
33+
echo 'You need to run this command from the toplevel of the working tree.' >&2
34+
exit 1
35+
fi
36+
37+
[ -d .git/modules ] || return 0
38+
39+
repodir=`pwd -L`/.git/modules
40+
git submodule foreach --quiet --recursive '
41+
[ -f .git ] && (while read -r key gitdir; do
42+
[ "$key" = "gitdir:" ] || continue
43+
cd -L -- "$gitdir" 2>/dev/null && pwd -L
44+
break
45+
done < .git) || :' |
46+
while read -r gitdir; do
47+
case "$gitdir" in
48+
"$repodir/"*)
49+
touch -- "$gitdir/.do_not_purge_me"
50+
;;
51+
esac
52+
done
53+
54+
find .git/modules -type f -name config -print |
55+
while read -r config; do
56+
gitdir="${config%/config}"
57+
if [ -f "$gitdir/.do_not_purge_me" ]; then
58+
rm -- "$gitdir/.do_not_purge_me"
59+
continue
60+
fi
61+
[ -f "$gitdir/index" -a -d "$gitdir/objects" ] || continue
62+
printf '%s\n' "$gitdir"
63+
done | sort -r |
64+
while read -r dir; do
65+
echo "Purging $dir"
66+
rm -rf -- "$dir"/*
67+
rmdir -p -- "$dir" 2>/dev/null || :
68+
done
69+
}
70+
71+
main "$@"

git-submodule-rm

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/bin/sh
2+
#
3+
# git-submodule-rm - stages the removal of given submodules
4+
#
5+
# Copyright (c) 2012 Akinori MUSHA
6+
#
7+
# All rights reserved.
8+
#
9+
# Redistribution and use in source and binary forms, with or without
10+
# modification, are permitted provided that the following conditions
11+
# are met:
12+
# 1. Redistributions of source code must retain the above copyright
13+
# notice, this list of conditions and the following disclaimer.
14+
# 2. Redistributions in binary form must reproduce the above copyright
15+
# notice, this list of conditions and the following disclaimer in the
16+
# documentation and/or other materials provided with the distribution.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27+
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28+
# SUCH DAMAGE.
29+
#
30+
31+
usage () {
32+
cat <<EOF >&2
33+
usage: $0 <submodule>..
34+
35+
`basename $0` stages the removal of given submodules.
36+
EOF
37+
}
38+
39+
main () {
40+
local opt OPTIND=1
41+
42+
while getopts '' opt; do
43+
:
44+
done
45+
46+
shift $(($OPTIND - 1))
47+
48+
if [ $# -eq 0 ]; then
49+
usage
50+
exit 1
51+
fi
52+
53+
if [ ! -d .git ]; then
54+
echo 'You need to run this command from the toplevel of the working tree.' >&2
55+
exit 1
56+
fi
57+
58+
if [ ! -f .gitmodules ]; then
59+
echo 'No submodule defined.' >&2
60+
exit 1
61+
fi
62+
63+
local name path cwd abspath error
64+
65+
for name; do
66+
path="$(git config --file .gitmodules --get "submodule.$name.path")"
67+
if [ -n "$path" ]; then
68+
cwd="$(pwd -L)"
69+
abspath="$(cd -L "$path" && pwd -L)"
70+
case "$abspath" in
71+
"$cwd"/*)
72+
git rm --cached "$path"
73+
rm -rf "$path"
74+
git config --file .gitmodules --remove-section "submodule.$name"
75+
git add .gitmodules
76+
;;
77+
*)
78+
echo "Not removing \`$name' - \`$path' is outside of the working tree." >&2
79+
error=t
80+
;;
81+
esac
82+
else
83+
echo "No submodule \`$name' found" >&2
84+
error=t
85+
fi
86+
done
87+
88+
[ -z "$error" ] || exit 1
89+
}
90+
91+
main "$@"

0 commit comments

Comments
 (0)