def test_commonpath() -> None: path = Path("/foo/bar/baz/path") subpath = path / "sampledir" assert commonpath(path, subpath) == path assert commonpath(subpath, path) == path assert commonpath(Path(str(path) + "suffix"), path) == path.parent assert commonpath(path, path.parent.parent) == path.parent.parent
def _check_initialpaths_for_relpath(session: "Session", path: Path) -> Optional[str]: for initial_path in session._initialpaths: if commonpath(path, initial_path) == initial_path: rel = str(path.relative_to(initial_path)) return "" if rel == "." else rel return None
def get_common_ancestor(paths: Iterable[Path]) -> Path: common_ancestor: Optional[Path] = None for path in paths: if not path.exists(): continue if common_ancestor is None: common_ancestor = path else: if common_ancestor in path.parents or path == common_ancestor: continue elif path in common_ancestor.parents: common_ancestor = path else: shared = commonpath(path, common_ancestor) if shared is not None: common_ancestor = shared if common_ancestor is None: common_ancestor = Path.cwd() elif common_ancestor.is_file(): common_ancestor = common_ancestor.parent return common_ancestor