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

49 lines
1.5 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
echo "[sync] lecture des submodules depuis .gitmodules"
if [ ! -f .gitmodules ]; then
echo "[sync][erreur] .gitmodules introuvable dans $(pwd)" >&2
exit 1
fi
# Récupère la liste des sous-modules (noms logiques)
mapfile -t submodules < <(git config -f .gitmodules --name-only --get-regexp 'submodule\..*\.path' | sed -E 's/\.path$//')
for name in "${submodules[@]}"; 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 "[sync][warn] entrée incomplète pour ${name}, ignoré" >&2
continue
fi
echo "[sync] => ${path} (${url}) branch=${branch}"
if [ ! -d "${path}/.git" ]; then
echo "[sync] init submodule ${path}"
git submodule update --init --recursive -- "${path}"
fi
echo "[sync] fetch ${path}"
git -C "${path}" fetch --all --prune
echo "[sync] checkout ${branch} dans ${path}"
if ! git -C "${path}" checkout "${branch}" 2>/dev/null; then
if git -C "${path}" rev-parse --verify "origin/${branch}" >/dev/null 2>&1; then
git -C "${path}" checkout -b "${branch}" "origin/${branch}"
else
echo "[sync][warn] branche ${branch} introuvable pour ${path}, on reste sur la branche courante" >&2
fi
fi
echo "[sync] pull ${path}"
git -C "${path}" pull --ff-only || true
done
echo "[sync] terminé"