Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion dvc/repo/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dvc.exceptions import DvcException
from dvc.repo import locked
from dvc.scm.git import Git
from dvc.scm.tree import is_working_tree


@locked
Expand Down Expand Up @@ -36,10 +37,23 @@ def _to_path(output):
else os.path.join(str(output), "")
)

on_working_tree = is_working_tree(self.tree)

def _to_checksum(output):
if on_working_tree:
return self.cache.local.get_checksum(output.path_info)
return output.checksum

def _exists(output):
if on_working_tree:
return output.exists
return True

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

working_tree = self.tree
Expand Down
42 changes: 36 additions & 6 deletions tests/func/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,51 @@ 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"
)
tmp_dir.dvc_gen({"dir": {"file": "file content"}}, commit="first")
scm.tag("v1")

tmp_dir.dvc_gen(
{"dir": {"file": "modified file content"}}, commit="second"
)
scm.tag("v2")

remove(first(stage.outs).cache_path)
remove("dir")
remove(dvc.cache.local.cache_dir)

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

diff = dvc.diff("v1")
diff = dvc.diff("v1", "v2")
Comment on lines -171 to +175
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change in this test is in order to actually test diff when there is no dir cache, and not intermix it with dirty repo diff.

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


def test_diff_dirty(tmp_dir, scm, dvc):
tmp_dir.dvc_gen(
{"file": "file_content", "dir": {"dir_file1": "dir file content"}},
commit="initial",
)

(tmp_dir / "file").unlink()
tmp_dir.gen({"dir": {"dir_file2": "dir file 2 content"}})
tmp_dir.dvc_gen("new_file", "new_file_content")

result = dvc.diff()

assert result == {
"added": [
{"hash": "86d049de17c76ac44cdcac146042ec9b", "path": "new_file"}
],
"deleted": [
{"hash": "7f0b6bb0b7e951b7fd2b2a4a326297e1", "path": "file"}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a problem with deleted, because user will not be able to commit stage file when there is no output. But I presumed that its better to just log deleted instead some ambiguous warning saying "Output is deleted but stage file still exists, so you will not be able to commit current state of file".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleted seems ok to me, since it's consistent with what dvc status reports for this case

file.dvc:
        changed outs:
                deleted:            file

],
"modified": [
{
"hash": {
"new": "38175ad60f0e58ac94e0e2b7688afd81.dir",
"old": "92daf39af116ca2fb245acaeb2ae65f7.dir",
},
"path": os.path.join("dir", ""),
}
],
}