Skip to content

Commit

Permalink
cmd: diff: don't load dir cache for diff (iterative#3468)
Browse files Browse the repository at this point in the history
  • Loading branch information
pared authored Mar 16, 2020
1 parent 109ded4 commit 87338c9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
28 changes: 12 additions & 16 deletions dvc/repo/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,25 @@ def _paths_checksums():
A dictionary of checksums addressed by relpaths collected from
the current tree outputs.
Unpack directories to include their entries
To help distinguish between a directory and a file output,
the former one will come with a trailing slash in the path:
directory: "data/"
file: "data"
"""
result = {}

for stage in self.stages:
for output in stage.outs:
if not output.is_dir_checksum:
result.update({str(output): output.checksum})
continue

result.update({os.path.join(str(output), ""): output.checksum})

for entry in output.dir_cache:
path = str(output.path_info / entry["relpath"])
result.update({path: entry["md5"]})

return result
def _to_path(output):
return (
str(output)
if not output.is_dir_checksum
else os.path.join(str(output), "")
)

return {
_to_path(output): output.checksum
for stage in self.stages
for output in stage.outs
}

working_tree = self.tree
a_tree = self.scm.get_tree(a_rev)
Expand Down
39 changes: 26 additions & 13 deletions tests/func/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import pytest
import shutil

from funcy import first

from dvc.compat import fspath
from dvc.exceptions import DvcException

Expand Down Expand Up @@ -40,11 +42,7 @@ def test_no_cache_entry(tmp_dir, scm, dvc):
dir_checksum = "5fb6b29836c388e093ca0715c872fe2a.dir"

assert dvc.diff() == {
"added": [
{"path": os.path.join("dir", ""), "hash": dir_checksum},
{"path": os.path.join("dir", "1"), "hash": digest("1")},
{"path": os.path.join("dir", "2"), "hash": digest("2")},
],
"added": [{"path": os.path.join("dir", ""), "hash": dir_checksum}],
"deleted": [],
"modified": [
{
Expand Down Expand Up @@ -126,15 +124,13 @@ def test_directories(tmp_dir, scm, dvc):
"path": os.path.join("dir", ""),
"hash": "5fb6b29836c388e093ca0715c872fe2a.dir",
},
{"path": os.path.join("dir", "1"), "hash": digest("1")},
{"path": os.path.join("dir", "2"), "hash": digest("2")},
],
"deleted": [],
"modified": [],
}

assert dvc.diff(":/directory", ":/modify") == {
"added": [{"path": os.path.join("dir", "3"), "hash": digest("3")}],
"added": [],
"deleted": [],
"modified": [
{
Expand All @@ -144,16 +140,12 @@ def test_directories(tmp_dir, scm, dvc):
"new": "9b5faf37366b3370fd98e3e60ca439c1.dir",
},
},
{
"path": os.path.join("dir", "2"),
"hash": {"old": digest("2"), "new": digest("two")},
},
],
}

assert dvc.diff(":/modify", ":/delete") == {
"added": [],
"deleted": [{"path": os.path.join("dir", "2"), "hash": digest("two")}],
"deleted": [],
"modified": [
{
"path": os.path.join("dir", ""),
Expand All @@ -164,3 +156,24 @@ def test_directories(tmp_dir, scm, dvc):
}
],
}


def test_diff_no_cache(tmp_dir, scm, dvc):
(stage,) = tmp_dir.dvc_gen(
{"dir": {"file": "file content"}}, commit="first"
)
scm.tag("v1")
tmp_dir.dvc_gen(
{"dir": {"file": "modified file content"}}, commit="second"
)

os.remove(first(stage.outs).cache_path)
shutil.rmtree("dir")

# invalidate_dir_info to force cache loading
dvc.cache.local._dir_info = {}

diff = dvc.diff("v1")
assert diff["added"] == []
assert diff["deleted"] == []
assert first(diff["modified"])["path"] == os.path.join("dir", "")

0 comments on commit 87338c9

Please sign in to comment.