4NK_dev/scripts/readd_submodules.sh
2025-09-10 11:49:43 +02:00

40 lines
1.2 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
echo "[readd] lecture .gitmodules"
mapfile -t entries < <(git config -f .gitmodules --name-only --get-regexp 'submodule\..*\.path' | sed -E 's/\.path$//')
for name in "${entries[@]}"; do
path=$(git config -f .gitmodules --get "${name}.path" || true)
url=$(git config -f .gitmodules --get "${name}.url" || true)
branch=$(git config -f .gitmodules --get "${name}.branch" || echo "master")
if [ -z "${path}" ] || [ -z "${url}" ]; then
echo "[readd][warn] entrée incomplète pour ${name}, ignoré" >&2
continue
fi
mkdir -p "$(dirname "${path}")"
if [ -d "${path}" ] && [ -d "${path}/.git" ]; then
echo "[readd] déjà présent: ${path}"
continue
fi
if [ -d "${path}" ] && [ ! -d "${path}/.git" ]; then
echo "[readd][warn] ${path} existe sans .git, on supprime et on recrée"
rm -rf "${path}"
fi
echo "[readd] ajout ${path} (${url}) branche=${branch}"
if ! git submodule add -f -b "${branch}" "${url}" "${path}"; then
echo "[readd][warn] échec ajout ${path}, on continue" >&2
continue
fi
done
echo "[readd] submodules ajoutés, initialisation recursive"
git submodule update --init --recursive
echo "[readd] terminé"