Skip to content

Instantly share code, notes, and snippets.

@tkirby
Forked from zaius/git-submodule-rm.sh
Created February 4, 2013 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkirby/4705897 to your computer and use it in GitHub Desktop.
Save tkirby/4705897 to your computer and use it in GitHub Desktop.
#!/bin/zsh
function actual_path() {
if [ [ -z "$1" ] -a [ -d $1 ] ]; then
echo $(cd $1 && test `pwd` = `pwd -P`)
return 0
else
return 1
fi
}
function is_submodule() {
local top_level parent_git module_name
if [ -d "$1" ]; then
cd $1
else
return 1
fi
# Find the root of this git repo, then check if its parent dir is also a repo
top_level="$(git rev-parse --show-toplevel)"
if [ ! actual_path $toplevel ]; then
top_level="$(cd $top_level && pwd -P)"
fi
module_name="$(basename "$top_level")"
parent_git="$(cd "$top_level/.." && git rev-parse --show-toplevel 2> /dev/null)"
if [[ -n $parent_git ]]; then
return 0
else
return 1
fi
}
function is_gitroot() {
if [ "$(pwd -P)" = "$(git rev-parse --show-toplevel)" ]; then
return 0
else
return 1
fi
}
function remove_submodule() {
# first check if it's a valid path
if [ ! -d "$1" ]; then
echo "Usage: git submodule rm <path>"
return 1
fi
# then check whether we're at git root
if is_gitroot; then
# finally check whether the given path is a submodule
if $(is_submodule "${1}"); then
echo "let's remove those submodules"
# using ${1%/} to remove trailing slashes
git config -f .gitmodules --remove-section submodule.${1%/}
git config -f .git/config --remove-section submodule.${1%/}
git rm --cached ${1%/}
else
echo "git submodule rm is not recursive yet, aborting."
fi
else
echo "You need to run this command from the toplevel of the working tree."
fi
}
function git() {
if [[ $argv[1] == 'submodule' && $argv[2] == 'rm' ]]; then
remove_submodule $argv[3,-1]
else
command git "$@";
fi;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment