]> git.phdru.name Git - git-scripts.git/blobdiff - submodules/remove
Feat(submodules/remove): Add option `-c`
[git-scripts.git] / submodules / remove
index 685fbb5477ec2cb368c5c63e88a3b7a57b496d00..f9436cead70e12fdd84b414fb746d3c89aff7e83 100755 (executable)
@@ -1,14 +1,19 @@
 #! /bin/sh
 # See https://stackoverflow.com/q/1260748/7976758
 
+case "$1" in
+    -c|--cached) cached=--cached; shift ;;
+esac &&
+
 if [ $# != 1 ]; then
-    echo "Usage: $0 submodule_name" >&2
+    echo "Usage: $0 [-c|--cached] submodule_name" >&2
     exit 1
 fi
 
-cd `git rev-parse --show-toplevel` || exit 1
+cd "`git rev-parse --show-toplevel`" || exit 1
 if [ \! -f .gitmodules ]; then
-    echo "The command must be run in the top-level directory" >&2
+    echo "Cannot find .gitmodules. The command must be run " >&2
+    echo "in the top-level directory of a repository with submodules" >&2
     exit 1
 fi
 
@@ -20,6 +25,14 @@ if [ -z "$path" ]; then
     exit 1
 fi
 
-git rm "$path"
-rm -rf .git/modules/"$name"
-exec git config --remove-section submodule."$name"
+git rm $cached "$path" &&
+rm -rf "`git rev-parse --git-dir`"/modules/"$name" &&
+git config --remove-section submodule."$name" &&
+
+if [ \! -s .gitmodules ]; then
+    git rm -f .gitmodules
+fi &&
+
+if [ -z $(ls -A "`git rev-parse --git-dir`/modules") ]; then
+    exec rmdir "`git rev-parse --git-dir`/modules"
+fi