+34
-14
@@ -40,21 +40,41 @@ def _normalize_path(path: str) -> str:
|
||||
return (path or "").replace("\\", "/").rstrip("/")
|
||||
|
||||
|
||||
def is_path_under_branches(path: str, project_root: str | None = None) -> bool:
|
||||
"""True when *path* resolves inside ``<project_root>/branches/``."""
|
||||
normalized = _normalize_path(path)
|
||||
if not normalized:
|
||||
return False
|
||||
if "/branches/" in f"{normalized}/":
|
||||
return True
|
||||
if normalized.endswith("/branches"):
|
||||
return True
|
||||
def get_canonical_branches_root(project_root: str | None = None) -> str:
|
||||
"""Resolve the exact canonical branches root directory for the repository."""
|
||||
if project_root:
|
||||
root = _normalize_path(os.path.realpath(project_root))
|
||||
real = _normalize_path(os.path.realpath(path))
|
||||
if real.startswith(f"{root}/"):
|
||||
rel = real[len(root) + 1 :]
|
||||
return rel == "branches" or rel.startswith("branches/")
|
||||
root = os.path.realpath(project_root)
|
||||
else:
|
||||
root = os.path.realpath(os.getcwd())
|
||||
|
||||
norm = root.replace("\\", "/")
|
||||
if "/branches/" in norm:
|
||||
base_part = norm.split("/branches/")[0]
|
||||
return os.path.realpath(os.path.join(base_part, "branches"))
|
||||
elif norm.endswith("/branches"):
|
||||
return os.path.realpath(norm)
|
||||
|
||||
return os.path.realpath(os.path.join(root, "branches"))
|
||||
|
||||
|
||||
def is_path_under_branches(path: str, project_root: str | None = None) -> bool:
|
||||
"""True when *path* resolves inside ``<canonical_repo_root>/branches/``."""
|
||||
if not path or not str(path).strip():
|
||||
return False
|
||||
try:
|
||||
real_path = os.path.realpath(os.path.abspath(str(path).strip()))
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
branches_root = get_canonical_branches_root(project_root)
|
||||
|
||||
if real_path == branches_root:
|
||||
return True
|
||||
|
||||
prefix = branches_root + os.sep
|
||||
if real_path.startswith(prefix):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user