Files
gitea-sync-ai/app/git_manager.py
T
2026-08-03 15:51:44 +02:00

75 lines
2.5 KiB
Python

import subprocess
import os
from pathlib import Path
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 SETZEN NACH DEM KLOONEN!
if result.returncode == 0:
try:
os.chmod(str(target_dir), 0o755)
for root, dirs, files in os.walk(str(target_dir)):
for d in dirs:
os.chmod(os.path.join(root, d), 0o755)
for f in files:
os.chmod(os.path.join(root, f), 0o644)
except Exception as e:
print(f"Warnung bei Berechtigungen: {e}")
return {"success": result.returncode == 0, "output": result.stdout, "error": result.stderr}
def unsubscribe_repo(target_dir: Path):
"""Loescht ein abonniertes Projekt."""
import shutil
if not target_dir.exists():
return {"success": False, "error": "Projekt nicht gefunden"}
try:
# ✅ ALLE CHANGES MARKIERUNG LÖSCHEN
changes_marker = target_dir / ".gitea-changes.marker"
if changes_marker.exists():
changes_marker.unlink()
shutil.rmtree(str(target_dir))
return {"success": True, "message": f"{target_dir.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}