#!/bin/bash echo "🚀 Instalando comando git subir..." CONFIG="$HOME/.config/git-subir.conf" BIN="$HOME/.local/bin/git-subir" # Crear directorios mkdir -p "$(dirname "$CONFIG")" mkdir -p "$(dirname "$BIN")" # Solicitar credenciales read -p "👤 Usuario de Gitea: " usuario read -s -p "🔑 Token o contraseña: " token echo "" read -p "🌐 URL base de la API de Gitea: " url # Validar autenticación resp=$(curl -s -u "$usuario:$token" "$url/api/v1/user") if echo "$resp" | grep -q "\"login\":\"$usuario\""; then echo "✅ Autenticación exitosa con $url/api/v1/user" else echo "❌ Falló la autenticación. Abortando." exit 1 fi # Cifrado opcional read -p "🔐 ¿Querés cifrar las credenciales? (s/n): " cifrar if [[ "$cifrar" == "s" ]]; then read -s -p "🔑 Contraseña para cifrado: " pass echo "" SALT=$(openssl rand -hex 16) CRED=$(echo -n "$usuario:$token" | openssl enc -aes-256-cbc -a -pbkdf2 -salt -pass pass:"$pass") else CRED="$usuario:$token" SALT="" fi # Guardar configuración cat > "$CONFIG" < "$BIN" <<'EOF' #!/bin/bash CONFIG="$HOME/.config/git-subir.conf" mkdir -p "$(dirname "$CONFIG")" source "$CONFIG" guardar_config() { cat > "$CONFIG" </dev/null) if [[ -z "$cred_descifrada" ]]; then echo "❌ Contraseña incorrecta. Abortando." exit 1 fi read -s -p "🔑 Nueva contraseña: " newpass echo "" SALT=$(openssl rand -hex 16) CRED=$(echo -n "$cred_descifrada" | openssl enc -aes-256-cbc -a -pbkdf2 -salt -pass pass:"$newpass") guardar_config echo "✅ Contraseña de cifrado actualizada." exit 0 fi # Descifrar credenciales si es necesario if [[ -n "$SALT" ]]; then read -s -p "🔑 Ingresá la contraseña para descifrar credenciales: " pass echo "" CRED=$(echo "$CRED" | openssl enc -aes-256-cbc -a -d -pbkdf2 -salt -pass pass:"$pass" 2>/dev/null) if [[ -z "$CRED" ]]; then echo "❌ Contraseña incorrecta. Abortando." exit 1 fi fi usuario=$(echo "$CRED" | cut -d: -f1) token=$(echo "$CRED" | cut -d: -f2) # Menú interactivo echo "📤 ¿Qué querés hacer?" echo "1) Subir a main (Producción)" echo "2) Subir a develop (Desarrollo)" echo "3) 🚀 Deploy merge (merge develop → main + push)" read -p "Seleccioná una opción (1-3): " opcion case $opcion in 1) rama_destino="main" ;; 2) rama_destino="develop" ;; 3) rama_destino="main" merge_desde="develop" ;; *) echo "❌ Opción inválida. Abortando." exit 1 ;; esac # Inicializar repo si es necesario if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then echo "📁 No estás en un repositorio git. Inicializando..." git init read -p "🌐 URL del remoto origin: " remote_url git remote add origin "$remote_url" fi # Merge si corresponde if [[ -n "$merge_desde" ]]; then git fetch origin git checkout $rama_destino git merge origin/$merge_desde fi # Detectar cambios if [[ -n "$(git status --porcelain)" ]]; then echo "📦 Hay cambios sin commitear." read -p "¿Querés escribir el mensaje de commit? (s/n): " respuesta if [[ "$respuesta" == "s" ]]; then read -p "📝 Ingresá el mensaje de commit: " mensaje else fecha=$(date +"%Y-%m-%d %H:%M:%S") mensaje="Commit automático a $rama_destino - $fecha" fi git add -A git commit -m "$mensaje" else echo "✅ No hay cambios pendientes. Continuando con el push..." fi # Push remote=$(git config --get branch.$rama_destino.remote) if [[ -z "$remote" ]]; then git push --set-upstream origin "$rama_destino" else git push origin "$rama_destino" fi # Deploy local if [[ "$rama_destino" == "main" ]]; then destino="/var/www/html/intranet" else destino="/var/www/html/empleados" fi echo "📂 Copiando archivos a $destino..." rsync -avz --delete ./ "$destino"/ echo "🎉 Deploy completado en $destino" # Volver a rama original si cambió rama_actual=$(git rev-parse --abbrev-ref HEAD) if [[ "$rama_actual" != "$rama_destino" && "$rama_actual" != "HEAD" ]]; then git checkout "$rama_actual" fi EOF # Dar permisos de ejecución chmod +x "$BIN" # Validar PATH SHELL_RC="$HOME/.bashrc" [[ "$SHELL" == */zsh ]] && SHELL_RC="$HOME/.zshrc" grep -q "$HOME/.local/bin" <<< "$PATH" || echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$SHELL_RC" # Configurar alias git config --global alias.subir "!git-subir" echo "✅ Alias 'git subir' configurado." echo "👉 Ejecutá 'source $SHELL_RC' o reiniciá la terminal para aplicar el PATH." echo "🎉 Instalación completa. Ahora 'git subir' está listo para usarse."