]> git.phdru.name Git - git-scripts.git/blob - submodules/remove
Feat(submodules/remove): Add option `-c`
[git-scripts.git] / submodules / remove
1 #! /bin/sh
2 # See https://stackoverflow.com/q/1260748/7976758
3
4 case "$1" in
5     -c|--cached) cached=--cached; shift ;;
6 esac &&
7
8 if [ $# != 1 ]; then
9     echo "Usage: $0 [-c|--cached] submodule_name" >&2
10     exit 1
11 fi
12
13 cd "`git rev-parse --show-toplevel`" || exit 1
14 if [ \! -f .gitmodules ]; then
15     echo "Cannot find .gitmodules. The command must be run " >&2
16     echo "in the top-level directory of a repository with submodules" >&2
17     exit 1
18 fi
19
20 name="$1"
21 path="$(git config -f .gitmodules --get "submodule.$name.path")"
22
23 if [ -z "$path" ]; then
24     echo "Error: submodule $name does not exist" >&2
25     exit 1
26 fi
27
28 git rm $cached "$path" &&
29 rm -rf "`git rev-parse --git-dir`"/modules/"$name" &&
30 git config --remove-section submodule."$name" &&
31
32 if [ \! -s .gitmodules ]; then
33     git rm -f .gitmodules
34 fi &&
35
36 if [ -z $(ls -A "`git rev-parse --git-dir`/modules") ]; then
37     exec rmdir "`git rev-parse --git-dir`/modules"
38 fi