Skip to content

Instantly share code, notes, and snippets.

@barraponto
Created April 25, 2012 16:36
  • Star 59 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save barraponto/2491147 to your computer and use it in GitHub Desktop.
git submodule-rm
#!/bin/bash
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
}
# first check if it's a valid path
if [ ! -d "$1" ]; then
echo "Usage: git submodule rm <path>"
exit
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
@barraponto
Copy link
Author

unfortunately, git aliases can't have spaces in it...
so I'm stuck with git submodule-rm

@barraponto
Copy link
Author

git config is more reliable and readable than sed ;)

@zaius
Copy link

zaius commented May 9, 2012

You can't have an alias with a space, but you can fake it with a function. Check out my fork: https://gist.github.com/2642691

@henrik
Copy link

henrik commented May 31, 2012

My fork fixes a "bad substitution" error and says "submodule-rm" with the dash. I don't mind it :)

@barraponto
Copy link
Author

merged @henrik's fix :)

@tlvince
Copy link

tlvince commented Jun 24, 2012

Is it wise to also remove the untracked submodule files (as per my fork) or is this considered dangerous?

@henrik
Copy link

henrik commented Jun 24, 2012

@tlvince To my knowledge it should be safe. I suppose some instructions don't have you removing the files from your working directory because you may have made changes to the submodule that you want to keep, or something.

You should be able to do just:

git rm ${1%/}

Instead of:

git rm --cached ${1%/}
rm -rf ${1%/}

Regarding .git/modules, I don't have any such directory in my repo (with several submodules). Maybe only older versions of Git use it? Or maybe those files are only created when you do something fancy with the modules. Either way, I suppose it's a good idea to have this script remove them. Leaving them can apparently cause problems; removing them shouldn't.

@henrik
Copy link

henrik commented Jun 24, 2012

Oops, tried git rm ${1%/} and that didn't work, even with -rf. So both lines are needed.

@barraponto
Copy link
Author

@henrik I guess those files only show up if you clone and git init.

@tlvince
Copy link

tlvince commented Jun 25, 2012

Great. I've added a test to see if .git/modules exists and quoted the paths. Feel free to merge.

@barraponto
Copy link
Author

@henrik @tlvince @zaius btw, how did you find this gist?

@henrik
Copy link

henrik commented Jun 26, 2012

@zaius
Copy link

zaius commented Jun 26, 2012

Yep, same here.

@dabrahams
Copy link

I'm getting "fatal: No such section!" from lines 57 and 58. In my files, there is no trailing slash!

@dabrahams
Copy link

Oh, the problem is that this script doesn't work when the submodule is not at the top level of the super-module! Back to "doing it manually," I guess :-P

@dabrahams
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment