#!/bin/bash -e # Based on article http://davidwalsh.name/git-remove-submodule MODULE_PATH="${1:?You must speicify the module path}" determinePaths(){ [ -d "${MODULE_PATH}" ] || die "Module directory does not exist: ${MODULE_PATH}" MODULE_PATH="$( cd "${MODULE_PATH}"; pwd )" GIT_ROOT="$( cd "$( dirname "${MODULE_PATH}" )"; git rev-parse --show-toplevel )" MODULE_PATH="${MODULE_PATH#"${GIT_ROOT}/"}" } changeDirToGitRoot(){ cd "${GIT_ROOT}" } editGitModulesFile(){ local search="$( escapeForSearch "submodule \"${MODULE_PATH}\"" )" local file=".gitmodules" sed -i '/\['"${search}"'\]/,/^\[/ { /\['"${search}"'\]/ d /^\s/ d /^\[/ n }' "${file}" git add "${file}" } editGitConfig(){ local search="$( escapeForSearch "submodule \"${MODULE_PATH}\"" )" sed -i '/\['"${search}"'\]/,/^\[/ { /\['"${search}"'\]/ d /^\s/ d /^\[/ n }' ".git/config" } removeFromGit(){ git rm --cached "${MODULE_PATH}" rm -rf ".git/modules/${MODULE_PATH}" rm -rf "${MODULE_PATH}" } die(){ local msg="$1" echo "${msg}" >&2 exit 1 } escapeForSearch(){ local str="$1" echo "${str}" | sed -e 's/[\/&]/\\&/g' } determinePaths changeDirToGitRoot editGitModulesFile editGitConfig removeFromGit