tj.git-extras/bin/git-delete-submodule
2019-11-02 04:32:36 -04:00

32 lines
1 KiB
Bash
Executable file

#!/usr/bin/env bash
test -z "$1" && echo "submodule required" 1>&2 && exit 1
cd "$(git root)"
test ! -f .gitmodules && echo ".gitmodules file not found" 1>&2 && exit 2
NAME="$(echo "$1" | sed 's/\/$//g')"
test -z \
"$( git config --file=.gitmodules submodule."$NAME".url)" \
&& echo "submodule not found" 1>&2 && exit 3
# 1. Handle the .git directory
# 1.a. Delete the relevant section from .git/config
git submodule deinit -f "$NAME" || exit 4
# 1.b. Delete the submodule .git directory
rm -rf .git/modules/"$NAME"
# 1.c. Delete empty submodule directory
git rm -f "$NAME"
# 2. Handle .gitignore file
# 2.a. Delete the relevant line from .gitmodules
git config --file=.gitmodules --remove-section submodule."$NAME"
# 2.b and stage changes
git add .gitmodules
# 2.c. Delete empty .gitmodules
[ "$(wc -l '.gitmodules' | cut -d' ' -f1)" = '0' ] && git rm -f '.gitmodules'
# 3. Need to confirm and commit the changes for yourself
echo
echo "Now submodule $NAME is deleted."
echo 'Confirm with `git submodule status` and commit the changes for yourself.'