Commit automático por git subir
This commit is contained in:
@ -1,147 +1,143 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
echo "🔧 Instalación del comando git subir"
|
||||
echo "🚀 Instalando comando git subir..."
|
||||
|
||||
# 📦 Verificar openssl
|
||||
if ! command -v openssl &> /dev/null; then
|
||||
echo "⚠️ openssl no está instalado. Instalando..."
|
||||
if command -v apt &> /dev/null; then
|
||||
sudo apt update && sudo apt install -y openssl
|
||||
elif command -v pacman &> /dev/null; then
|
||||
sudo pacman -Sy --noconfirm openssl
|
||||
else
|
||||
echo "❌ No se pudo instalar openssl automáticamente. Instalalo manualmente."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# 📁 Crear carpeta
|
||||
mkdir -p "$HOME/.scripts"
|
||||
|
||||
# 🔄 Reconfigurar si existen credenciales
|
||||
if [[ -f "$HOME/.scripts/.env" || -f "$HOME/.scripts/.env.enc" ]]; then
|
||||
read -p "⚠️ Ya existen credenciales. ¿Querés reconfigurarlas? (s/n): " reconfig
|
||||
[[ "$reconfig" != "s" ]] && echo "🚫 Instalación cancelada." && exit 0
|
||||
fi
|
||||
|
||||
# 🔐 Solicitar credenciales
|
||||
read -p "👤 Usuario de Gitea: " GITEA_USER
|
||||
read -s -p "🔑 Token o contraseña: " GITEA_TOKEN
|
||||
# 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 (ej: https://gitea.midominio.org): " GITEA_API_URL
|
||||
read -p "🌐 URL base de la API de Gitea: " API_BASE
|
||||
|
||||
# 🔎 Verificar credenciales
|
||||
response=$(curl -s -u "$GITEA_USER:$GITEA_TOKEN" "$GITEA_API_URL/api/v1/user")
|
||||
if ! echo "$response" | grep -q '"login":"'"$GITEA_USER"'"'; then
|
||||
echo "❌ Credenciales inválidas. Abortando instalación."
|
||||
exit 1
|
||||
# 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
|
||||
|
||||
# 🔐 Cifrar si desea
|
||||
read -p "¿Querés guardar las credenciales cifradas con openssl? (s/n): " cifrar
|
||||
if [[ "$cifrar" == "s" ]]; then
|
||||
echo "🔐 Ingresá una contraseña para cifrar:"
|
||||
read -s passphrase
|
||||
echo -e "GITEA_USER=$GITEA_USER\nGITEA_TOKEN=$GITEA_TOKEN\nGITEA_API_URL=$GITEA_API_URL" | \
|
||||
openssl enc -aes-256-cbc -pbkdf2 -salt -out "$HOME/.scripts/.env.enc" -pass pass:"$passphrase"
|
||||
rm -f "$HOME/.scripts/.env"
|
||||
echo "✅ Credenciales cifradas guardadas."
|
||||
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 -e "GITEA_USER=$GITEA_USER\nGITEA_TOKEN=$GITEA_TOKEN\nGITEA_API_URL=$GITEA_API_URL" > "$HOME/.scripts/.env"
|
||||
chmod 600 "$HOME/.scripts/.env"
|
||||
rm -f "$HOME/.scripts/.env.enc"
|
||||
echo "⚠️ Credenciales guardadas sin cifrar."
|
||||
echo "$USUARIO:$TOKEN:$API_BASE" > "$CONFIG_DIR/credenciales.txt"
|
||||
fi
|
||||
|
||||
# 📜 Script principal
|
||||
cat <<'EOF' > "$HOME/.scripts/git-subir.sh"
|
||||
# Crear script git-subir
|
||||
cat > "$CONFIG_DIR/git-subir" <<'EOF'
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
CONFIG_DIR="$HOME/.config/git-subir"
|
||||
|
||||
# 🔐 Cargar credenciales
|
||||
if [[ -f "$HOME/.scripts/.env.enc" ]]; then
|
||||
echo "🔐 Ingresá la contraseña para descifrar tus credenciales:"
|
||||
read -s passphrase
|
||||
openssl enc -aes-256-cbc -pbkdf2 -d -in "$HOME/.scripts/.env.enc" -pass pass:"$passphrase" > /tmp/.env.dec
|
||||
source /tmp/.env.dec
|
||||
rm /tmp/.env.dec
|
||||
elif [[ -f "$HOME/.scripts/.env" ]]; then
|
||||
source "$HOME/.scripts/.env"
|
||||
else
|
||||
echo "❌ No se encontraron credenciales. Ejecutá instalar-subir.sh primero."
|
||||
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
|
||||
|
||||
# 📍 Verificar si estamos en un repositorio Git
|
||||
if ! git rev-parse --is-inside-work-tree &> /dev/null; then
|
||||
echo "📁 Este directorio no es un repositorio Git."
|
||||
read -p "¿Querés inicializarlo ahora? (s/n): " init_repo
|
||||
if [[ "$init_repo" == "s" ]]; then
|
||||
git init
|
||||
read -p "📦 Ingresá el nombre del repositorio remoto (ej: git-operaciones.git): " repo_nombre
|
||||
remote_url="$GITEA_API_URL/$GITEA_USER/$repo_nombre"
|
||||
git remote add origin "$remote_url"
|
||||
echo "✅ Repositorio inicializado y remoto configurado: $remote_url"
|
||||
else
|
||||
echo "🚫 Operación cancelada. Ejecutá desde un repositorio Git válido."
|
||||
exit 1
|
||||
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
|
||||
|
||||
remote_url=$(git remote get-url origin | sed 's|https://||')
|
||||
auth_url="https://$GITEA_USER:$GITEA_TOKEN@$remote_url"
|
||||
# 📂 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
|
||||
}
|
||||
|
||||
function marco() {
|
||||
echo -e "╔════════════════════════════════╗"
|
||||
echo -e "║ $1"
|
||||
echo -e "╚════════════════════════════════╝"
|
||||
# 🧼 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 "📦 Se detectaron cambios sin commitear:"
|
||||
git status -s
|
||||
read -p "¿Querés hacer commit automático de estos cambios? (s/n): " auto_commit
|
||||
if [[ "$auto_commit" == "s" ]]; then
|
||||
read -p "📝 Ingresá el mensaje de commit: " mensaje
|
||||
git add .
|
||||
git commit -m "$mensaje"
|
||||
echo "✅ Commit realizado."
|
||||
fi
|
||||
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 "🚀 Subiendo archivos al repositorio remoto..."
|
||||
if ! git push "$auth_url"; then
|
||||
echo "⚠️ Push fallido. Intentando pull con --allow-unrelated-histories..."
|
||||
git pull "$auth_url" --allow-unrelated-histories || true
|
||||
|
||||
conflictos=$(git diff --name-only --diff-filter=U)
|
||||
if [[ -n "$conflictos" ]]; then
|
||||
echo "🚨 Conflictos detectados:"
|
||||
echo "$conflictos"
|
||||
read -p "¿Hacer merge automático? (s/n): " merge_auto
|
||||
if [[ "$merge_auto" == "s" ]]; then
|
||||
git add .
|
||||
git commit -m "🔀 Merge automático"
|
||||
else
|
||||
echo "❌ Merge cancelado. Resolvé los conflictos manualmente."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
git push "$auth_url"
|
||||
echo "🚀 Ejecutando git push..."
|
||||
git push
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "⚠️ Push falló. Intentando git pull y reintento..."
|
||||
git pull --rebase
|
||||
git push
|
||||
fi
|
||||
|
||||
echo "🎉 Push completado."
|
||||
EOF
|
||||
|
||||
chmod +x "$HOME/.scripts/git-subir.sh"
|
||||
chmod +x "$CONFIG_DIR/git-subir"
|
||||
|
||||
# 🔗 Alias global
|
||||
echo "Creando alias git-subir..."
|
||||
echo -e "#!/bin/bash\nexec \"$HOME/.scripts/git-subir.sh\" \"\$@\"" | sudo tee /usr/local/bin/git-subir > /dev/null
|
||||
sudo chmod +x /usr/local/bin/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"
|
||||
|
||||
echo "✅ Instalación completa. Usá el comando: 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."
|
||||
|
Reference in New Issue
Block a user