-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit-submodule-rm
executable file
·65 lines (56 loc) · 1.49 KB
/
git-submodule-rm
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
65
#!/bin/bash
function actual_path() {
if [ [ -z "$1" ] -a [ -d $1 ] ]; then
echo $(cd $1 && test `pwd` = `pwd -P`)
return 0
else
return 1
fi
}
function is_submodule() {
local top_level parent_git module_name
if [ -d "$1" ]; then
cd $1
else
return 1
fi
# Find the root of this git repo, then check if its parent dir is also a repo
top_level="$(git rev-parse --show-toplevel)"
if [ ! actual_path $toplevel ]; then
top_level="$(cd $top_level && pwd -P)"
fi
module_name="$(basename "$top_level")"
parent_git="$(cd "$top_level/.." && git rev-parse --show-toplevel 2> /dev/null)"
if [[ -n $parent_git ]]; then
return 0
else
return 1
fi
}
function is_gitroot() {
if [ "$(pwd -P)" = "$(git rev-parse --show-toplevel)" ]; then
return 0
else
return 1
fi
}
# first check if it's a valid path
if [ ! -d "$1" ]; then
echo "Usage: git submodule rm <path>"
exit
fi
# then check whether we're at git root
if is_gitroot; then
# finally check whether the given path is a submodule
if $(is_submodule "${1}"); then
echo "let's remove those submodules"
# using ${1%/} to remove trailing slashes
git config -f .gitmodules --remove-section submodule.${1%/}
git config -f .git/config --remove-section submodule.${1%/}
git rm --cached ${1%/}
else
echo "git submodule rm is not recursive yet, aborting."
fi
else
echo "You need to run this command from the toplevel of the working tree."
fi