mirror of
https://github.com/tj/git-extras.git
synced 2026-09-14 09:26:19 -04:00
45 lines
1.3 KiB
Bash
Executable file
45 lines
1.3 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
abort() {
|
|
error="$1" && shift
|
|
echo "FATAL: $*" 1>&2 && exit "$error"
|
|
}
|
|
|
|
test -z "$1" && abort 1 'Submodule required'
|
|
cd "$(git root)" || abort 5 'Cannot change to repository root'
|
|
test ! -f '.gitmodules' && abort 2 '.gitmodules file not found'
|
|
|
|
NAME="${1%/}"
|
|
test -z "$(git config --file='.gitmodules' "submodule.$NAME.url")" \
|
|
&& abort 3 'Submodule not found'
|
|
|
|
# 1. Handle the .git directory
|
|
# 1.a. Delete the relevant section from .git/config
|
|
git submodule deinit -f "$NAME" || abort 4 "Failed to deinitialize $NAME"
|
|
# 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
|
|
if git submodule status >/dev/null 2>&1 \
|
|
&& ! git submodule status | grep "$NAME"; then
|
|
echo "Successfully deleted $NAME."
|
|
else
|
|
abort 6 "Failed to delete $NAME."
|
|
fi
|
|
echo
|
|
git submodule status
|
|
echo
|
|
echo 'Confirm the output of `git submodule status` above' \
|
|
' and commit the changes for yourself.'
|