Girishug's picture
Create tools/git_tool.py
4254386 verified
Raw
History Blame Contribute Delete
1.37 kB
# tools/git_tool.py
from pathlib import Path
def scan_repository_structure(repo_path: str, max_depth: int = 2) -> str:
"""
Recursively scan and summarize the directory structure of a repo up to a max depth.
"""
repo_path = Path(repo_path)
if not repo_path.exists():
return f"❌ Error: Path {repo_path} does not exist."
def _walk(path: Path, depth: int = 0):
if depth > max_depth:
return []
lines = []
for child in sorted(path.iterdir()):
prefix = " " * depth + ("β”œβ”€β”€ " if child.is_file() else "πŸ“‚ ")
lines.append(f"{prefix}{child.name}")
if child.is_dir():
lines.extend(_walk(child, depth + 1))
return lines
structure_lines = _walk(repo_path)
return f"\nπŸ“ Repository Structure ({repo_path.name}):\n" + "\n".join(structure_lines)
def read_code_file(file_path: str) -> str:
try:
with open(file_path, "r", encoding="utf-8") as f:
return f.read()
except Exception as e:
return f"Error reading file: {e}"
# Optional: for LangChain Tool usage
from langchain.tools import Tool
git_scan_tool = Tool(
name="ScanGitRepoStructure",
func=scan_repository_structure,
description="Scans a local Git repository and returns a summarized directory tree. Provide the full path."
)