File size: 1,298 Bytes
f764cdf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | """Read-only adapter for scanning a repository checkout."""
from __future__ import annotations
from pathlib import Path
from .repo_model import Repo, RepoFile
SKIP_DIRS = {".git", ".hg", ".svn", "node_modules", ".venv", "venv", "__pycache__"}
def repo_from_path(root: str | Path, *, repo_id: str | None = None,
revision_id: str = "working-tree", max_file_bytes: int = 2_000_000) -> Repo:
root = Path(root).resolve()
files: list[RepoFile] = []
for path in sorted(root.rglob("*")):
if not path.is_file() or any(part in SKIP_DIRS for part in path.relative_to(root).parts):
continue
try:
size = path.stat().st_size
if size > max_file_bytes:
continue
content = path.read_bytes()
except OSError:
continue
if b"\x00" in content[:8192]:
continue
relative = path.relative_to(root).as_posix()
language = None
try:
from tree_sitter_language_pack import detect_language_from_path
language = detect_language_from_path(relative)
except Exception:
pass
files.append(RepoFile(relative, content, language=language))
return Repo(repo_id or root.name, revision_id, tuple(files))
|