Files
gitea-sync-ai/app/git_manager.py
T
2026-08-03 16:00:06 +02:00

87 lines
2.8 KiB
Python

import subprocess
import os
import shutil
from pathlib import Path
def set_permissions(path: str):
"""Setzt korrekte POSIX-Berechtigungen für ein Verzeichnis."""
try:
# Ordner = 755 (rwxr-xr-x)
os.chmod(str(path), 0o755)
# Rekursiv alle Teildateien bearbeiten
for root, dirs, files in os.walk(str(path)):
for d in dirs:
full_path = os.path.join(root, d)
try:
os.chmod(full_path, 0o755)
except Exception:
pass
for f in files:
full_path = os.path.join(root, f)
try:
os.chmod(full_path, 0o644)
except Exception:
pass
except Exception as e:
print(f"Warnung bei Berechtigungen: {e}")
def clone_repo(repo_url: str, target_dir: Path, token: str = None):
"""Klont ein Repository in den Zielordner."""
if not target_dir.exists():
target_dir.mkdir(parents=True)
# URL mit Token (falls nötig)
if token and "@" not in repo_url:
repo_url = repo_url.replace("https://", f"https://{token}@")
result = subprocess.run(
["git", "clone", repo_url, str(target_dir)],
capture_output=True,
text=True
)
# ✅ BERECHTIGUNGEN NACH DEM KLOONEN SETZEN!
if result.returncode == 0:
set_permissions(str(target_dir))
return {"success": result.returncode == 0, "output": result.stdout, "error": result.stderr}
def unsubscribe_repo(project_name: str, projects_dir: Path):
"""Loeschen eines abonnierten Projekts inklusive Berechtigungen."""
target_dir = projects_dir / project_name
if not target_dir.exists():
return {"success": False, "error": "Projekt nicht gefunden"}
try:
shutil.rmtree(str(target_dir))
return {"success": True, "message": f"{project_name} erfolgreich geloescht"}
except Exception as e:
return {"success": False, "error": str(e)}
def push_changes(repo_path: Path, message: str = "Update 🧠"):
"""Pusht lokale Änderungen ins Repository."""
result = subprocess.run(
["git", "add", "."], cwd=repo_path, capture_output=True, text=True
)
if result.returncode != 0:
return {"success": False, "error": f"git add fehlgeschlagen: {result.stderr}"}
result = subprocess.run(
["git", "commit", "-m", message], cwd=repo_path, capture_output=True, text=True
)
if result.returncode != 0 and "nothing to commit" not in result.stderr.lower():
return {"success": False, "error": f"Commit fehlgeschlagen: {result.stderr}"}
result = subprocess.run(
["git", "push"], cwd=repo_path, capture_output=True, text=True
)
return {"success": result.returncode == 0, "output": result.stdout}