Files
git-operaciones/instalar-subir.sh

144 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
echo "🚀 Instalando comando git subir..."
# Solicitar datos
read -p "👤 Usuario de Gitea: " USUARIO
read -s -p "🔑 Token o contraseña: " TOKEN
echo ""
read -p "🌐 URL base de la API de Gitea: " API_BASE
# Verificar credenciales antes de cifrar
ENDPOINTS=("$API_BASE/user" "$API_BASE/api/v1/user")
ENDPOINT_VALIDO=""
for URL in "${ENDPOINTS[@]}"; do
RESP=$(curl -s -o /dev/null -w "%{http_code}" -u "$USUARIO:$TOKEN" "$URL")
echo "🔍 Probando $URL → Código: $RESP"
if [ "$RESP" == "200" ]; then
ENDPOINT_VALIDO="$URL"
break
fi
done
if [ -z "$ENDPOINT_VALIDO" ]; then
echo "❌ Error de autenticación. No se encontró endpoint válido."
exit 1
fi
echo "✅ Autenticación exitosa con $ENDPOINT_VALIDO"
# Crear carpeta segura
CONFIG_DIR="$HOME/.config/git-subir"
mkdir -p "$CONFIG_DIR"
# Preguntar si desea cifrar
read -p "🔐 ¿Querés cifrar las credenciales? (s/n): " CIFRAR
if [[ "$CIFRAR" == "s" ]]; then
read -s -p "🔑 Contraseña para cifrado: " CLAVE
echo ""
echo "$USUARIO:$TOKEN:$API_BASE" | openssl enc -aes-256-cbc -pbkdf2 -salt -out "$CONFIG_DIR/credenciales.enc" -pass pass:"$CLAVE"
else
echo "$USUARIO:$TOKEN:$API_BASE" > "$CONFIG_DIR/credenciales.txt"
fi
# Crear script git-subir
cat > "$CONFIG_DIR/git-subir" <<'EOF'
#!/bin/bash
CONFIG_DIR="$HOME/.config/git-subir"
# 🔐 Cargar credenciales
if [ -f "$CONFIG_DIR/credenciales.enc" ]; then
read -s -p "🔑 Ingresá la contraseña de cifrado: " CLAVE
echo ""
CREDS=$(openssl enc -aes-256-cbc -pbkdf2 -d -in "$CONFIG_DIR/credenciales.enc" -pass pass:"$CLAVE" 2>/dev/null)
if [ -z "$CREDS" ]; then
echo "❌ Error al descifrar credenciales."
exit 1
fi
else
CREDS=$(cat "$CONFIG_DIR/credenciales.txt")
fi
USUARIO=$(echo "$CREDS" | cut -d: -f1)
TOKEN=$(echo "$CREDS" | cut -d: -f2)
API_BASE=$(echo "$CREDS" | cut -d: -f3)
# 🔁 Reconfigurar credenciales
if [[ "$1" == "--reconfigurar" ]]; then
read -p "👤 Nuevo usuario: " USUARIO
read -s -p "🔑 Nuevo token: " TOKEN
echo ""
read -p "🌐 Nueva URL base: " API_BASE
read -p "🔐 ¿Cifrar credenciales? (s/n): " CIFRAR
if [[ "$CIFRAR" == "s" ]]; then
read -s -p "🔑 Contraseña para cifrado: " CLAVE
echo ""
echo "$USUARIO:$TOKEN:$API_BASE" | openssl enc -aes-256-cbc -pbkdf2 -salt -out "$CONFIG_DIR/credenciales.enc" -pass pass:"$CLAVE"
rm -f "$CONFIG_DIR/credenciales.txt"
else
echo "$USUARIO:$TOKEN:$API_BASE" > "$CONFIG_DIR/credenciales.txt"
rm -f "$CONFIG_DIR/credenciales.enc"
fi
echo "✅ Credenciales reconfiguradas."
exit 0
fi
# 📂 Verificar si estamos en un repo
git rev-parse --is-inside-work-tree &>/dev/null || {
echo "❌ No estás en un repositorio Git."
exit 1
}
# 🧼 Detectar si el repo está vacío
git rev-parse HEAD &>/dev/null
if [ $? -ne 0 ]; then
echo "📁 El repositorio está vacío."
read -p "¿Querés crear README.md inicial? (s/n): " RESP
if [[ "$RESP" == "s" ]]; then
touch README.md
git add README.md
git commit -m "Commit inicial por git subir"
fi
fi
# 🌐 Verificar remote origin
git remote -v | grep origin >/dev/null || {
read -p "🌐 No hay remote origin. Ingresá URL del remote: " REMOTE
git remote add origin "$REMOTE"
}
# 📦 Detectar cambios sin commitear
if ! git diff-index --quiet HEAD --; then
echo "📦 Hay cambios sin commitear."
read -p "¿Querés hacer commit automático? (s/n): " RESP
if [[ "$RESP" == "s" ]]; then
git add .
git commit -m "Commit automático por git subir"
fi
fi
# 🚀 Intentar push
echo "🚀 Ejecutando git push..."
git push
if [ $? -ne 0 ]; then
echo "⚠️ Push falló. Intentando git pull y reintento..."
git pull --rebase
git push
fi
EOF
chmod +x "$CONFIG_DIR/git-subir"
# Instalar en ~/.local/bin
mkdir -p "$HOME/.local/bin"
cp "$CONFIG_DIR/git-subir" "$HOME/.local/bin/git-subir"
chmod +x "$HOME/.local/bin/git-subir"
# Asegurar que ~/.local/bin esté en PATH
SHELL_RC="$HOME/.bashrc"
[[ "$SHELL" == */zsh ]] && SHELL_RC="$HOME/.zshrc"
grep -q 'export PATH="$HOME/.local/bin:$PATH"' "$SHELL_RC" || echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$SHELL_RC"
echo "✅ Instalación completa. Usá 'git subir' desde cualquier repositorio."