2 # Run `git submodule add` on submodules in `.gitmodules`
3 # if said `.gitmodules` has been simply copied into the project.
5 # Adapted from https://stackoverflow.com/a/53899440/7976758
7 git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
8 while read -r KEY MODULE_PATH
10 # If the module's path exists, remove it.
11 # This is done b/c the module's path is currently
12 # not a valid git repo and adding the submodule will cause an error.
13 [ -d "${MODULE_PATH}" ] && rm -rf "${MODULE_PATH}"
15 NAME="$(echo "${KEY}" | sed 's/^submodule\.\(.*\)\.path$/\1/')"
17 url_key="$(echo "${KEY}" | sed 's/\.path$/.url/')"
18 branch_key="$(echo "${KEY}" | sed 's/\.path$/.branch/')"
20 URL="$(git config -f .gitmodules --get "${url_key}")"
21 BRANCH="$(git config -f .gitmodules --get "${branch_key}")"
23 if [ -n "$BRANCH" ]; then
24 git submodule add --force -b "${BRANCH}" --name "${NAME}" "${URL}" "${MODULE_PATH}"
26 git submodule add --force --name "${NAME}" "${URL}" "${MODULE_PATH}"
30 exec git submodule update --init --recursive