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
+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"