"""Compare the course's resolve_abs_path (copied verbatim, L28-32) with a repo-bounded version."""
import os, tempfile
from pathlib import Path

def resolve_abs_path(path_str: str) -> Path:          # course L28-32, verbatim
    path = Path(path_str).expanduser()
    if not path.is_absolute():
        path = (Path.cwd() / path).resolve()
    return path

class OutsideRepoError(ValueError):
    pass

def resolve_in_repo(path_str: str, root: Path) -> Path:
    root = root.resolve()
    candidate = Path(path_str).expanduser()
    if not candidate.is_absolute():
        candidate = root / candidate
    resolved = candidate.resolve()                     # follows symlinks and collapses ".."
    if not resolved.is_relative_to(root):
        raise OutsideRepoError(f"{path_str!r} resolves outside {root}")
    return resolved

with tempfile.TemporaryDirectory() as tmp:
    root = Path(tmp, "repo"); root.mkdir()
    (root / "notes.md").write_text("x")
    outside = Path(tmp, "secret.txt"); outside.write_text("s")
    (root / "link").symlink_to(outside)
    os.chdir(root)
    for p in ["notes.md", "../secret.txt", "/etc/hosts", "~/.ssh/config", "link", "sub/../notes.md"]:
        orig = resolve_abs_path(p)
        orig_inside = orig.resolve().is_relative_to(root.resolve())
        try:
            resolve_in_repo(p, root); bounded = "允许"
        except OutsideRepoError:
            bounded = "拒绝"
        print(f"{p:18s} 原函数落在仓库内={orig_inside!s:5s} 加边界后={bounded}")
