Skip to content

Commit 1e71489

Browse files
authored
checkout: ensure subdirs are deleted before parent dir (#601)
* checkout: ensure subdirs are deleted before parent dir * Rename test function
1 parent 459d7a7 commit 1e71489

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/dvc_data/index/checkout.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ def _create_files( # noqa: C901, PLR0912, PLR0913
160160

161161

162162
def _delete_dirs(entries, path, fs):
163-
for entry in entries:
163+
reversed_by_key = sorted(entries, key=lambda entry: entry.key, reverse=True)
164+
for entry in reversed_by_key:
164165
try:
165166
fs.rmdir(fs.join(path, *entry.key))
166167
except OSError:

tests/index/test_checkout.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,32 @@ def test_checkout_broken_dir(tmp_upath, odb, as_filesystem):
103103
(tmp_upath / "data" / "baz"),
104104
}
105105
assert not (tmp_upath / "broken").exists()
106+
107+
108+
def test_checkout_delete_nested_dir(tmp_upath, odb, as_filesystem):
109+
old = DataIndex(
110+
{
111+
("dir1",): DataIndexEntry(
112+
key=("dir1",),
113+
meta=Meta(isdir=True),
114+
),
115+
("dir1", "subdir1"): DataIndexEntry(
116+
key=("dir1", "subdir1"),
117+
meta=Meta(isdir=True),
118+
),
119+
}
120+
)
121+
diff = compare(None, old)
122+
apply(diff, str(tmp_upath), as_filesystem(tmp_upath.fs))
123+
124+
assert (tmp_upath / "dir1").exists()
125+
assert (tmp_upath / "dir1").is_dir()
126+
assert (tmp_upath / "dir1" / "subdir1").exists()
127+
assert (tmp_upath / "dir1" / "subdir1").is_dir()
128+
129+
new = DataIndex({})
130+
diff = compare(old, new, delete=True)
131+
apply(diff, str(tmp_upath), as_filesystem(tmp_upath.fs))
132+
133+
assert not (tmp_upath / "dir1" / "subdir1").exists()
134+
assert not (tmp_upath / "dir1").exists()

0 commit comments

Comments
 (0)