Repository: psf/black
File: src/black/files.py (Python)

Code before:
    try:
        return PathSpec.from_lines("gitwildmatch", lines)
    except GitWildMatchPatternError as e:
        err(f"Could not parse {gitignore}: {e}")
        raise


def resolves_outside_root_or_cannot_stat(
    path: Path,
    root: Path,
    report: Optional[Report] = None,
) -> bool:
    """
    Returns whether the path is a symbolic link that points outside the
    root directory. Also returns True if we failed to resolve the path.
    """
    try:
        if sys.version_info < (3, 8, 6):
            path = path.absolute()  # https://bugs.python.org/issue33660
        resolved_path = _cached_resolve(path)
    except OSError as e:
        if report:
            report.path_ignored(path, f"cannot be read because {e}")
        return True
    try:
        resolved_path.relative_to(root).as_posix()
    except ValueError:
        if report:
            report.path_ignored(path, f"is a symbolic link that points outside {root}")
        return True
    return False


def best_effort_relative_path(path: Path, root: Path) -> Path:
    # Precondition: resolves_outside_root_or_cannot_stat(path, root) is False
    try:
        return path.absolute().relative_to(root)
    except ValueError:
        pass
    root_parent = next((p for p in path.parents if _cached_resolve(p) == root), None)
    if root_parent is not None:
        return path.relative_to(root_parent)
    # something adversarial, fallback to path guaranteed by precondition
    return _cached_resolve(path).relative_to(root)


def _path_is_ignored(
    root_relative_path: str,
    root: Path,
    gitignore_dict: Dict[Path, PathSpec],
) -> bool:

Review comment (suggestion):
```suggestion
        resolved_path.relative_to(root)
```
Doesn't look like `.as_posix()` should ever throw.

Code after:
    try:
        return PathSpec.from_lines("gitwildmatch", lines)
    except GitWildMatchPatternError as e:
        err(f"Could not parse {gitignore}: {e}")
        raise


def resolves_outside_root_or_cannot_stat(
    path: Path,
    root: Path,
    report: Optional[Report] = None,
) -> bool:
    """
    Returns whether the path is a symbolic link that points outside the
    root directory. Also returns True if we failed to resolve the path.
    """
    try:
        if sys.version_info < (3, 8, 6):
            path = path.absolute()  # https://bugs.python.org/issue33660
        resolved_path = _cached_resolve(path)
    except OSError as e:
        if report:
            report.path_ignored(path, f"cannot be read because {e}")
        return True
    try:
        resolved_path.relative_to(root)
    except ValueError:
        if report:
            report.path_ignored(path, f"is a symbolic link that points outside {root}")
        return True
    return False


def best_effort_relative_path(path: Path, root: Path) -> Path:
    # Precondition: resolves_outside_root_or_cannot_stat(path, root) is False
    try:
        return path.absolute().relative_to(root)
    except ValueError:
        pass
    root_parent = next((p for p in path.parents if _cached_resolve(p) == root), None)
    if root_parent is not None:
        return path.relative_to(root_parent)
    # something adversarial, fallback to path guaranteed by precondition
    return _cached_resolve(path).relative_to(root)


def _path_is_ignored(
    root_relative_path: str,
    root: Path,
    gitignore_dict: Dict[Path, PathSpec],
) -> bool: