fix(logging): add detailed error logging to git repository endpoints and utilities

This commit is contained in:
Fusion
2026-05-21 10:47:30 +02:00
parent fba01ddfb2
commit ecc0acc8dd
2 changed files with 27 additions and 2 deletions
+17
View File
@@ -1,3 +1,4 @@
import logging
import os
import shutil
import subprocess
@@ -36,6 +37,8 @@ from src.utils.git_url_parser import parse_git_url
router = APIRouter(prefix="/projects", tags=["git-repositories"])
logger = logging.getLogger(__name__)
async def _get_user(session: AsyncSession, user_id: uuid.UUID) -> User:
"""Fetch a user by ID or raise 401 if not found."""
@@ -494,6 +497,14 @@ async def list_repository_files(
],
)
except RuntimeError as e:
logger.error(
"Failed to list files for repo %s (path=%s, branch=%s): %s",
repo_id,
path,
branch,
str(e),
exc_info=True,
)
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
@@ -599,6 +610,12 @@ async def get_repository_branches(
default_branch=default_branch,
)
except RuntimeError as e:
logger.error(
"Failed to list branches for repo %s: %s",
repo_id,
str(e),
exc_info=True,
)
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
+10 -2
View File
@@ -54,6 +54,8 @@ def _run_git_command(repo_path: str, *args: str) -> str:
return result.stdout
logger = logging.getLogger(__name__)
def list_tree(repo_path: str, branch: str = "main", path: str = "") -> list[FileTreeEntry]:
"""List files and directories in a repository path.
@@ -69,12 +71,14 @@ def list_tree(repo_path: str, branch: str = "main", path: str = "") -> list[File
try:
output = _run_git_command(repo_path, "ls-tree", "-l", tree_path)
except RuntimeError:
except RuntimeError as e:
logger.warning("git ls-tree failed for %s with branch '%s': %s", repo_path, tree_path, str(e))
# Try with HEAD if branch doesn't exist
tree_path = f"HEAD:{path}" if path else "HEAD"
try:
output = _run_git_command(repo_path, "ls-tree", "-l", tree_path)
except RuntimeError as e:
logger.error("git ls-tree failed for %s with HEAD: %s", repo_path, str(e))
# Check if this is an empty repository (no commits yet)
error_msg = str(e).lower()
if "not a valid object name" in error_msg or "does not exist" in error_msg:
@@ -256,7 +260,11 @@ def list_branches(repo_path: str) -> tuple[list[BranchInfo], str]:
Tuple of (list of BranchInfo, default branch name)
"""
# Get all branches
output = _run_git_command(repo_path, "branch", "-a", "--format=%(refname:short)")
try:
output = _run_git_command(repo_path, "branch", "-a", "--format=%(refname:short)")
except RuntimeError as e:
logger.error("Failed to list branches for %s: %s", repo_path, str(e))
raise
branches: list[BranchInfo] = []
default_branch = "main"