Created
October 8, 2015 16:36
-
-
Save jwliechty/9004da9a9966439bd019 to your computer and use it in GitHub Desktop.
Remove Git submodule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment