]> git.phdru.name Git - git-scripts.git/blobdiff - submodules/remove
Refactor: Edit `.git/description` as the last step
[git-scripts.git] / submodules / remove
index 90cb02f6fc10904725a88f461df2c99902706bea..d66ef3403be51c818943b8c9bd79f4e3a0c051e2 100755 (executable)
@@ -1,8 +1,12 @@
 #! /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
 
@@ -14,13 +18,22 @@ if [ \! -f .gitmodules ]; then
 fi
 
 name="$1"
-path="$(git config -f .gitmodules --get "submodule.$name.path")"
+path="`git config -f .gitmodules --get "submodule.$name.path"`"
 
 if [ -z "$path" ]; then
     echo "Error: submodule $name does not exist" >&2
     exit 1
 fi
 
-git rm "$path" &&
-rm -rf "`git rev-parse --git-dir`"/modules/"$name" &&
-exec git config --remove-section submodule."$name"
+git rm $cached "$path" &&
+GIT_DIR=`git rev-parse --git-dir` &&
+rm -rf "$GIT_DIR"/modules/"$name" &&
+git config --remove-section submodule."$name" &&
+
+if [ \! -s .gitmodules ]; then
+    git rm -f .gitmodules
+fi &&
+
+if [ -z "`ls -A \"$GIT_DIR/modules\"`" ]; then
+    exec rmdir "$GIT_DIR/modules"
+fi