Skip to content

Commit 558f51c

Browse files
authored
fix: restore support for preserve_symlinks: false for directories (#1820)
1 parent c53b928 commit 558f51c

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

copier/main.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,8 @@ def match_skip(self) -> Callable[[Path], bool]:
602602

603603
def _render_template(self) -> None:
604604
"""Render the template in the subproject root."""
605-
for src in scantree(str(self.template_copy_root)):
605+
follow_symlinks = not self.template.preserve_symlinks
606+
for src in scantree(str(self.template_copy_root), follow_symlinks):
606607
src_abspath = Path(src.path)
607608
src_relpath = Path(src_abspath).relative_to(self.template.local_abspath)
608609
dst_relpath = self._render_path(
@@ -612,7 +613,7 @@ def _render_template(self) -> None:
612613
continue
613614
if src.is_symlink() and self.template.preserve_symlinks:
614615
self._render_symlink(src_relpath, dst_relpath)
615-
elif src.is_dir(follow_symlinks=False):
616+
elif src.is_dir(follow_symlinks=follow_symlinks):
616617
self._render_folder(dst_relpath)
617618
else:
618619
self._render_file(src_relpath, dst_relpath)

copier/tools.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ def set_git_alternates(*repos: Path, path: Path = Path(".")) -> None:
258258
alternates_file.write_bytes(b"\n".join(map(bytes, map(get_git_objects_dir, repos))))
259259

260260

261-
def scantree(path: str) -> Iterator[os.DirEntry[str]]:
261+
def scantree(path: str, follow_symlinks: bool) -> Iterator[os.DirEntry[str]]:
262262
"""A recursive extension of `os.scandir`."""
263263
for entry in os.scandir(path):
264264
yield entry
265-
if entry.is_dir(follow_symlinks=False):
266-
yield from scantree(entry.path)
265+
if entry.is_dir(follow_symlinks=follow_symlinks):
266+
yield from scantree(entry.path, follow_symlinks)

tests/test_symlinks.py

+15
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,18 @@ def test_recursive_symlink(tmp_path_factory: pytest.TempPathFactory) -> None:
464464
run_copy(str(src), dst, defaults=True, overwrite=True)
465465
assert (dst / "one" / "two" / "three" / "root").is_symlink()
466466
assert (dst / "one" / "two" / "three" / "root").readlink() == Path("../../../")
467+
468+
469+
def test_symlinked_dir_expanded(tmp_path_factory: pytest.TempPathFactory) -> None:
470+
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
471+
build_file_tree(
472+
{
473+
src / "a_dir" / "a_file.txt": "some content",
474+
src / "a_symlinked_dir": Path("a_dir"),
475+
src / "a_nested" / "symlink": Path("../a_dir"),
476+
}
477+
)
478+
run_copy(str(src), dst, defaults=True, overwrite=True)
479+
assert (dst / "a_dir" / "a_file.txt").read_text() == "some content"
480+
assert (dst / "a_symlinked_dir" / "a_file.txt").read_text() == "some content"
481+
assert (dst / "a_nested" / "symlink" / "a_file.txt").read_text() == "some content"

0 commit comments

Comments
 (0)