#!/bin/bash # --------------------------------------------------------------------------- # commit.command — Faz commit e push para o Gitea (G-ART) # # Uso: # ./admin/commit.command # mensagem genérica # ./admin/commit.command "sua mensagem" # mensagem customizada # # Requer: git configurado com remote HTTPS + token no Gitea. # Repo: https://gitea.nacarmed.cloud/joaohenrique/gart.git # --------------------------------------------------------------------------- set -euo pipefail DIR="$(cd "$(dirname "$0")" && pwd)" PROJECT_DIR="$(cd "$DIR/.." && pwd)" cd "$PROJECT_DIR" # ── Cores ────────────────────────────────────────────────────────────────── RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' BLUE='\033[0;34m' NC='\033[0m' info() { echo -e "${BLUE}==> $1${NC}"; } ok() { echo -e "${GREEN} ✓ $1${NC}"; } warn() { echo -e "${YELLOW} ⚠ $1${NC}"; } erro() { echo -e "${RED} ✗ ERRO: $1${NC}" >&2; exit 1; } # ── 1. Verificar se é um repositório git ─────────────────────────────────── if [ ! -d "$PROJECT_DIR/.git" ]; then erro "Não é um repositório git. Execute 'git init' primeiro." fi # ── 2. Configurar remote se necessário ───────────────────────────────────── REMOTE_URL="https://gitea.nacarmed.cloud/joaohenrique/gart.git" if ! git remote get-url origin &>/dev/null; then info "Configurando remote origin..." git remote add origin "$REMOTE_URL" ok "Remote adicionado: $REMOTE_URL" else ATUAL=$(git remote get-url origin) # Verificar se o remote já tem token (HTTPS com credenciais) if [[ "$ATUAL" != *"@"* ]] && [[ "$ATUAL" == *"gitea.nacarmed.cloud"* ]]; then warn "Remote sem token de autenticação." warn "Atual: $ATUAL" echo "" echo " Para autenticar, execute:" echo " git remote set-url origin https://USUARIO:TOKEN@gitea.nacarmed.cloud/joaohenrique/gart.git" echo "" echo " Ou gere um token em:" echo " https://gitea.nacarmed.cloud/-/user/settings/tokens" echo "" fi fi # ── 3. Configurar branch principal como main ─────────────────────────────── CURRENT_BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")" if [ -z "$CURRENT_BRANCH" ]; then info "Primeiro commit — criando branch main..." git checkout -b main 2>/dev/null || true CURRENT_BRANCH="main" fi # ── 4. Verificar alterações ──────────────────────────────────────────────── info "Verificando alterações em: $PROJECT_DIR" info "Branch: $CURRENT_BRANCH" echo "" # Mostrar o que será commitado (resumo) CHANGES=$(git status --porcelain) if [ -z "$CHANGES" ]; then ok "Nada para commitar — repositório limpo." exit 0 fi echo -e "${YELLOW}Arquivos que serão commitados:${NC}" echo "$CHANGES" | head -30 TOTAL=$(echo "$CHANGES" | wc -l | tr -d ' ') if [ "$TOTAL" -gt 30 ]; then echo -e "${YELLOW} ... e mais $((TOTAL - 30)) arquivo(s)${NC}" fi echo "" # ── 5. Mensagem do commit ────────────────────────────────────────────────── COMMIT_MSG="${1:-}" USED_DESC_FILE=false if [ -z "$COMMIT_MSG" ] && [ -s "$PROJECT_DIR/commit-desc.txt" ]; then COMMIT_MSG="$(cat "$PROJECT_DIR/commit-desc.txt")" USED_DESC_FILE=true fi COMMIT_MSG="${COMMIT_MSG:-chore: atualização geral}" # ── 6. Adicionar e commitar ──────────────────────────────────────────────── info "Adicionando arquivos..." git add -A # Commit com mensagem (suporta múltiplas linhas) SUBJECT="$(printf '%s\n' "$COMMIT_MSG" | awk 'NF{print; exit}')" BODY="$(printf '%s\n' "$COMMIT_MSG" | sed '1d' | awk 'NF' | tr '\n' ' ' | sed 's/ *$//')" info "Commitando..." if [ -n "$BODY" ]; then git commit -m "$SUBJECT" -m "$BODY" else git commit -m "$SUBJECT" fi ok "Commit realizado com sucesso." # Limpar commit-desc.txt se foi usado if [ "$USED_DESC_FILE" = true ]; then : > "$PROJECT_DIR/commit-desc.txt" fi # ── 7. Push para o Gitea ─────────────────────────────────────────────────── info "Enviando para o Gitea (origin/$CURRENT_BRANCH)..." if git push origin "$CURRENT_BRANCH" 2>/dev/null; then ok "Push concluído com sucesso!" else warn "Push falhou. Verifique a autenticação." echo "" echo -e "${YELLOW}Passos para resolver:${NC}" echo "" echo " 1. Gere um token no Gitea:" echo " https://gitea.nacarmed.cloud/-/user/settings/tokens" echo "" echo " 2. Configure o remote com o token:" echo " git remote set-url origin https://USUARIO:TOKEN@gitea.nacarmed.cloud/joaohenrique/gart.git" echo "" echo " 3. Execute novamente:" echo " ./admin/commit.command" echo "" fi echo "" ok "Processo de commit concluído."