From 1a846b63f1fb7251c76f629e3a5eb5b1c44238d7 Mon Sep 17 00:00:00 2001 From: Nicolas Cantu Date: Wed, 10 Sep 2025 12:05:32 +0200 Subject: [PATCH] refactor(ci): structure tag.sh into functions and improve readability --- scripts/tag.sh | 117 ++++++++++++++++++++++++++++++------------------- 1 file changed, 72 insertions(+), 45 deletions(-) diff --git a/scripts/tag.sh b/scripts/tag.sh index 9d64b33..2c22c96 100644 --- a/scripts/tag.sh +++ b/scripts/tag.sh @@ -6,59 +6,86 @@ IFS=$'\n\t' # Par défaut, TAG_NAME="dev-test" TAG_NAME="${1:-dev-test}" - ROOT_DIR="/home/desk/code/4NK_dev" -cd "$ROOT_DIR" -echo "[tag] repository racine: $(basename "$ROOT_DIR") -> ${TAG_NAME}" -DEV_HEAD=$(git rev-parse --verify refs/heads/dev) -if git show-ref --tags --quiet --verify "refs/tags/${TAG_NAME}"; then - git tag -d "${TAG_NAME}" >/dev/null - git push origin ":refs/tags/${TAG_NAME}" >/dev/null || true -fi -git tag -a "${TAG_NAME}" -m "CI ${TAG_NAME} trigger" "${DEV_HEAD}" -git push origin "refs/tags/${TAG_NAME}" +print_info() { echo "[tag] $*"; } +print_warn() { echo "[tag][warn] $*"; } -echo "[tag] lecture des submodules depuis .gitmodules" -mapfile -t SUBS < <(git config -f .gitmodules --name-only --get-regexp 'submodule\..*\.path' | sed -E 's/\.path$//') - -for NAME in "${SUBS[@]}"; do - PATHsm=$(git config -f .gitmodules --get "${NAME}.path" || true) - URLsm=$(git config -f .gitmodules --get "${NAME}.url" || true) - BRANCHsm=$(git config -f .gitmodules --get "${NAME}.branch" || echo dev) - - if [ -z "$PATHsm" ] || [ -z "$URLsm" ]; then - echo "[tag][warn] entrée incomplète: ${NAME}, ignorée" - continue +delete_tag_if_exists() { + local tag="$1" + if git show-ref --tags --quiet --verify "refs/tags/${tag}"; then + git tag -d "${tag}" >/dev/null || true + git push origin ":refs/tags/${tag}" >/dev/null || true fi +} - if [ ! -d "$PATHsm" ]; then - echo "[tag][warn] dossier manquant: $PATHsm, on saute" - continue +ensure_branch_checked_out() { + local branch="$1" + if git rev-parse --verify "origin/${branch}" >/dev/null 2>&1; then + git checkout "${branch}" >/dev/null 2>&1 || git checkout -b "${branch}" "origin/${branch}" >/dev/null 2>&1 || true + git pull --ff-only >/dev/null 2>&1 || true + else + print_info "branche ${branch} introuvable, utilisation de la branche courante" fi +} - echo "[tag] ${PATHsm} (${URLsm}) -> ${TAG_NAME} sur branche ${BRANCHsm}" - ( - cd "$PATHsm" - git fetch --all --prune || true - # Tente checkout de la branche cible si disponible - if git rev-parse --verify "origin/${BRANCHsm}" >/dev/null 2>&1; then - git checkout "${BRANCHsm}" || git checkout -b "${BRANCHsm}" "origin/${BRANCHsm}" || true - git pull --ff-only || true - else - echo "[tag][info] branche ${BRANCHsm} introuvable, utilisation de la branche courante" +tag_current_repo() { + local tag="$1" + local ref_sha + ref_sha=$(git rev-parse --verify HEAD) + delete_tag_if_exists "${tag}" + git tag -a "${tag}" -m "CI ${tag} trigger" "${ref_sha}" + git push origin "refs/tags/${tag}" +} + +get_submodule_names() { + git config -f .gitmodules --name-only --get-regexp 'submodule\..*\.path' | sed -E 's/\.path$//' +} + +tag_root_repo() { + cd "${ROOT_DIR}" + print_info "repository racine: $(basename "${ROOT_DIR}") -> ${TAG_NAME}" + local dev_head + dev_head=$(git rev-parse --verify refs/heads/dev) + delete_tag_if_exists "${TAG_NAME}" + git tag -a "${TAG_NAME}" -m "CI ${TAG_NAME} trigger" "${dev_head}" + git push origin "refs/tags/${TAG_NAME}" +} + +tag_submodules() { + print_info "lecture des submodules depuis .gitmodules" + local names + mapfile -t names < <(get_submodule_names) + for name in "${names[@]}"; do + local sm_path sm_url sm_branch + sm_path=$(git config -f .gitmodules --get "${name}.path" || true) + sm_url=$(git config -f .gitmodules --get "${name}.url" || true) + sm_branch=$(git config -f .gitmodules --get "${name}.branch" || echo dev) + + if [ -z "${sm_path}" ] || [ -z "${sm_url}" ]; then + print_warn "entrée incomplète: ${name}, ignorée" + continue + fi + if [ ! -d "${sm_path}" ]; then + print_warn "dossier manquant: ${sm_path}, on saute" + continue fi - # Supprime le tag local/distant si déjà présent - if git show-ref --tags --quiet --verify "refs/tags/${TAG_NAME}"; then - git tag -d "${TAG_NAME}" >/dev/null || true - git push origin ":refs/tags/${TAG_NAME}" >/dev/null || true - fi + print_info "${sm_path} (${sm_url}) -> ${TAG_NAME} sur branche ${sm_branch}" + ( + cd "${sm_path}" + git fetch --all --prune >/dev/null 2>&1 || true + ensure_branch_checked_out "${sm_branch}" + tag_current_repo "${TAG_NAME}" + ) || print_warn "échec sur ${sm_path}, on continue" + done +} - HEAD_SHA=$(git rev-parse --verify HEAD) - git tag -a "${TAG_NAME}" -m "CI ${TAG_NAME} trigger" "${HEAD_SHA}" - git push origin "refs/tags/${TAG_NAME}" - ) || echo "[tag][warn] échec sur ${PATHsm}, on continue" -done +main() { + cd "${ROOT_DIR}" + tag_root_repo + tag_submodules + print_info "terminé: ${TAG_NAME} posé sur le repo racine et submodules accessibles" +} -echo "[tag] terminé: ${TAG_NAME} posé sur le repo racine et submodules accessibles" \ No newline at end of file +main "$@" \ No newline at end of file