diff --git a/update.sh b/update.sh index e6ddca1..bfb16c9 100755 --- a/update.sh +++ b/update.sh @@ -1,35 +1,57 @@ #!/bin/sh -# Set PATH so cron can find git, date, ssh, etc. +# Set PATH so cron can find git, date, ssh, flock, etc. export PATH="/usr/local/bin:/usr/bin:/bin" # Explicitly set target directory (don't rely on $HOME in cron) TARGET_DIR="/home/benji/Documents/COURS" +# Éviter les exécutions concurrentes (cron simultanés ou exécution manuelle) +LOCKFILE="/tmp/cours_update.lock" +exec 200>"$LOCKFILE" +if ! flock -n 200; then + echo "Un autre processus de synchronisation est déjà en cours." + exit 0 +fi + # Navigate to target directory cd "$TARGET_DIR" || { - echo "Directory not found" + echo "Directory not found: $TARGET_DIR" exit 1 } -# 1. Pull remote changes (repo principal + sous-modules) -git pull --rebase --autostash || { +# 1. Pull remote changes sur le repo principal COURS (Gitea Homelab) +git pull --rebase --autostash origin main || { echo "Git pull failed on COURS" exit 1 } -git submodule update --remote --merge || { - echo "Git submodule update failed" - exit 1 -} + +# S'assurer que le sous-module est initialisé +git submodule update --init 2>/dev/null || true # 2. Traitement du sous-module TP (Gitea IUT) if [ -d "DEV1.1/TP" ]; then - cd DEV1.1/TP || return + cd DEV1.1/TP || { + echo "Impossible d'accéder au sous-module DEV1.1/TP" + exit 1 + } + + # S'assurer d'être sur la branche main (éviter HEAD détachée) + CURRENT_BRANCH=$(git branch --show-current 2>/dev/null) + if [ "$CURRENT_BRANCH" != "main" ]; then + git checkout main 2>/dev/null || git checkout -B main origin/main 2>/dev/null || true + fi + + # Récupérer les changements distants du sous-module + git pull --rebase --autostash origin main || echo "Avertissement : Git pull a échoué sur le sous-module TP" + + # Valider et pousser les modifications locales du sous-module git add -A if ! git diff-index --quiet HEAD --; then git commit -m "sync: $(date +'%Y-%m-%d %H:%M')" - git push || echo "Avertissement : Git push à échoué sur le sous-module TP" + git push origin main || echo "Avertissement : Git push a échoué sur le sous-module TP" fi + cd "$TARGET_DIR" || exit 1 fi @@ -38,7 +60,7 @@ git add -A if ! git diff-index --quiet HEAD --; then git commit -m "sync: $(date +'%Y-%m-%d %H:%M')" - git push || { + git push origin main || { echo "Git push failed on COURS" exit 1 }