From c7ddefc8d0dd0eda9ad0d18b4d7acaa19a9d7be2 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Fri, 7 Jun 2019 12:49:46 +0300 Subject: [PATCH] Add git-submodules-add Run `git submodule add` on submodules in `.gitmodules` if said `.gitmodules` has been simply copied into the project. --- git-submodules-add | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 git-submodules-add diff --git a/git-submodules-add b/git-submodules-add new file mode 100755 index 0000000..d60f9b4 --- /dev/null +++ b/git-submodules-add @@ -0,0 +1,30 @@ +#! /bin/sh +# Run `git submodule add` on submodules in `.gitmodules` +# if said `.gitmodules` has been simply copied into the project. + +# Adapted from https://stackoverflow.com/a/53899440/7976758 + +git config -f .gitmodules --get-regexp '^submodule\..*\.path$' | + while read -r KEY MODULE_PATH + do + # If the module's path exists, remove it. + # This is done b/c the module's path is currently + # not a valid git repo and adding the submodule will cause an error. + [ -d "${MODULE_PATH}" ] && rm -rf "${MODULE_PATH}" + + NAME="$(echo "${KEY}" | sed 's/^submodule\.\(.*\)\.path$/\1/')" + + url_key="$(echo "${KEY}" | sed 's/\.path$/.url/')" + branch_key="$(echo "${KEY}" | sed 's/\.path$/.branch/')" + + URL="$(git config -f .gitmodules --get "${url_key}")" + BRANCH="$(git config -f .gitmodules --get "${branch_key}")" + + if [ -n "$BRANCH" ]; then + git submodule add --force -b "${BRANCH}" --name "${NAME}" "${URL}" "${MODULE_PATH}" + else + git submodule add --force --name "${NAME}" "${URL}" "${MODULE_PATH}" + fi + done + +exec git submodule update --init --recursive -- 2.39.5