Skip to content

Commit

Permalink
data status: fix path for committed changes in Windows (#8220)
Browse files Browse the repository at this point in the history
The path we get from GitFileSystem is posixpath, even on Windows. We need to
convert that posixpath to os.path here.
  • Loading branch information
skshetry authored Sep 1, 2022
1 parent b6e8ba6 commit 8968405
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dvc/repo/data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import posixpath
from collections import defaultdict
from typing import (
TYPE_CHECKING,
Expand All @@ -11,6 +12,7 @@
cast,
)

from dvc.fs.git import GitFileSystem
from dvc.ui import ui

if TYPE_CHECKING:
Expand All @@ -23,6 +25,10 @@
from dvc_data.hashfile.obj import HashFile


def posixpath_to_os_path(path: str) -> str:
return path.replace(posixpath.sep, os.path.sep)


def _in_cache(obj: "HashInfo", cache: "HashFileDB") -> bool:
from dvc_objects.errors import ObjectFormatError

Expand Down Expand Up @@ -213,6 +219,8 @@ def _diff_head_to_index(
continue

root = str(out)
if isinstance(out.fs, GitFileSystem):
root = posixpath_to_os_path(root)
typ = "index" if rev == "workspace" else head
objs[root][typ] = (out.get_obj(), out.hash_info)

Expand Down
32 changes: 32 additions & 0 deletions tests/func/test_data_status.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from os import fspath
from os.path import join

import pytest
Expand Down Expand Up @@ -99,6 +100,37 @@ def test_directory(M, tmp_dir, dvc, scm):
}


def test_tracked_directory_deep(M, tmp_dir, dvc, scm):
"""Test for a directory not in cwd, but nested inside other directories."""
(tmp_dir / "sub").gen({"dir": {"foo": "foo"}})
dvc.add(fspath(tmp_dir / "sub" / "dir"))
scm.add_commit(["sub/dir.dvc", "sub/.gitignore"], message="add sub/dir")

(tmp_dir / "sub" / "dir").gen("bar", "bar")
dvc.commit(None, force=True)
(tmp_dir / "sub" / "dir").gen("foobar", "foobar")

assert dvc.data_status() == {
**EMPTY_STATUS,
"committed": {"modified": [join("sub", "dir", "")]},
"uncommitted": {"modified": [join("sub", "dir", "")]},
"git": M.dict(),
}
assert dvc.data_status(granular=True, untracked_files="all") == {
**EMPTY_STATUS,
"committed": {
"added": [join("sub", "dir", "bar")],
"modified": [join("sub", "dir", "")],
},
"uncommitted": {
"added": [join("sub", "dir", "foobar")],
"modified": [join("sub", "dir", "")],
},
"git": M.dict(),
"unchanged": [join("sub", "dir", "foo")],
}


def test_new_empty_git_repo(M, tmp_dir, scm):
dvc = Repo.init()
assert dvc.data_status() == {
Expand Down

0 comments on commit 8968405

Please sign in to comment.