]> git.phdru.name Git - git-scripts.git/blob - git-submodules-add
Feat(ls-not-pushed): Improve regular expression
[git-scripts.git] / git-submodules-add
1 #! /bin/sh
2 # Run `git submodule add` on submodules in `.gitmodules`
3 # if said `.gitmodules` has been simply copied into the project.
4
5 # Adapted from https://stackoverflow.com/a/53899440/7976758
6
7 git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
8     while read -r KEY MODULE_PATH
9     do
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}"
14
15         NAME="$(echo "${KEY}" | sed 's/^submodule\.\(.*\)\.path$/\1/')"
16
17         url_key="$(echo "${KEY}" | sed 's/\.path$/.url/')"
18         branch_key="$(echo "${KEY}" | sed 's/\.path$/.branch/')"
19
20         URL="$(git config -f .gitmodules --get "${url_key}")"
21         BRANCH="$(git config -f .gitmodules --get "${branch_key}")"
22
23         if [ -n "$BRANCH" ]; then
24            git submodule add --force -b "${BRANCH}" --name "${NAME}" "${URL}" "${MODULE_PATH}"
25         else
26            git submodule add --force --name "${NAME}" "${URL}" "${MODULE_PATH}"
27         fi
28     done
29
30 exec git submodule update --init --recursive