#!/usr/bin/env bash # ============================================================================== # Script de Ciclo de Vida: Atualização do Submódulo .agent ao Iniciar Sessão # Executa: git submodule update --init --recursive --remote na branch master # Compatível com CLI e Lifecycle Hook do Antigravity (PreInvocation). # ============================================================================== set -e INPUT_JSON="" IS_PIPE=0 if [ ! -t 0 ]; then IS_PIPE=1 INPUT_JSON=$(cat 2>/dev/null || true) fi # Extrair conversationId se chamado via Hook Antigravity CONV_ID="" if [ -n "$INPUT_JSON" ]; then CONV_ID=$(echo "$INPUT_JSON" | grep -o '"conversationId":[[:space:]]*"[^"]*' | sed 's/.*"//' || true) fi # Idempotência: executar apenas uma vez por sessão de conversa if [ -n "$CONV_ID" ]; then LOCK_FILE="/tmp/.antigravity_agent_updated_${CONV_ID}" if [ -f "$LOCK_FILE" ]; then echo "{}" exit 0 fi fi # Detectar diretório raiz do superprojeto SUPER_DIR=$(git rev-parse --show-superproject-working-tree 2>/dev/null || true) if [ -z "$SUPER_DIR" ]; then if [ -f ".gitmodules" ]; then SUPER_DIR="." elif [ -f "../.gitmodules" ]; then SUPER_DIR=".." elif [ -f "../../.gitmodules" ]; then SUPER_DIR="../.." else SUPER_DIR="." fi fi if [ -f "$SUPER_DIR/.gitmodules" ]; then # Garantir configuração branch = master no .gitmodules CURRENT_BRANCH=$(git -C "$SUPER_DIR" config -f .gitmodules submodule..agent.branch 2>/dev/null || true) if [ "$CURRENT_BRANCH" != "master" ]; then git -C "$SUPER_DIR" config -f .gitmodules submodule..agent.branch master 2>/dev/null || true fi # Executar a atualização remota do submódulo .agent preservando commits locais (rebase) if git -C "$SUPER_DIR" submodule update --init --recursive --remote --rebase .agent >/dev/null 2>&1 || \ git -C "$SUPER_DIR" submodule update --init --recursive --remote .agent >/dev/null 2>&1; then # Remover script legado menu/graphify-atualizar.sh (substituído por .agent/scripts/graphify.sh) e garantir .gitignore rm -f "$SUPER_DIR/menu/graphify-atualizar.sh" 2>/dev/null || true if [ -f "$SUPER_DIR/.agent/scripts/graphify.sh" ]; then bash "$SUPER_DIR/.agent/scripts/graphify.sh" gitignore >/dev/null 2>&1 || true fi [ -n "$CONV_ID" ] && touch "$LOCK_FILE" 2>/dev/null || true if [ "$IS_PIPE" -eq 1 ] && [ -n "$INPUT_JSON" ]; then echo '{"injectSteps":[{"ephemeralMessage":"ℹ️ Submódulo .agent sincronizado com sucesso na branch master (git submodule update --init --recursive --remote)."}]}' else echo "✅ Submódulo .agent sincronizado com sucesso na branch master." fi exit 0 else if [ "$IS_PIPE" -eq 1 ] && [ -n "$INPUT_JSON" ]; then echo '{"injectSteps":[{"ephemeralMessage":"⚠️ Não foi possível sincronizar o submódulo .agent com origin/master (sem conexão ou erro de rede). Continuando com versão local."}]}' else echo "⚠️ Não foi possível sincronizar o submódulo .agent com origin/master. Continuando com versão local." fi exit 0 fi fi if [ "$IS_PIPE" -eq 1 ]; then echo "{}" else echo "ℹ️ Nenhum arquivo .gitmodules detectado." fi exit 0