Skip to content

Commit e278c9c

Browse files
fix(data status): show committed changes in a new Git repository (#10790)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent aac32e1 commit e278c9c

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

dvc/repo/data.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from typing import TYPE_CHECKING, Optional, TypedDict, Union
55

66
from dvc.fs.callbacks import DEFAULT_CALLBACK
7+
from dvc.log import logger
8+
from dvc.scm import RevError
79
from dvc.ui import ui
810
from dvc_data.index.view import DataIndexView
911

@@ -14,6 +16,8 @@
1416
from dvc_data.index import BaseDataIndex, DataIndex, DataIndexKey
1517
from dvc_data.index.diff import Change
1618

19+
logger = logger.getChild(__name__)
20+
1721

1822
def posixpath_to_os_path(path: str) -> str:
1923
return path.replace(posixpath.sep, os.path.sep)
@@ -220,12 +224,18 @@ def _diff_head_to_index(
220224
filter_keys: Optional[list["DataIndexKey"]] = None,
221225
granular: bool = False,
222226
) -> dict[str, list[str]]:
227+
from dvc_data.index import DataIndex
228+
223229
index = repo.index.data["repo"]
224230
index_view = filter_index(index, filter_keys=filter_keys)
225231

226-
with repo.switch(head):
227-
head_index = repo.index.data["repo"]
228-
head_view = filter_index(head_index, filter_keys=filter_keys)
232+
try:
233+
with repo.switch(head):
234+
head_index = repo.index.data["repo"]
235+
head_view = filter_index(head_index, filter_keys=filter_keys)
236+
except RevError:
237+
logger.debug("failed to switch to '%s'", head)
238+
head_view = DataIndex()
229239

230240
with ui.progress(desc="Calculating diff between head/index", unit="entry") as pb:
231241
return _diff(

tests/func/test_data_status.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,19 @@ def test_new_empty_git_repo(M, tmp_dir, scm):
144144
"git": M.dict(is_empty=True, is_dirty=True),
145145
}
146146

147+
tmp_dir.gen("foo", "foo")
148+
dvc.add(["foo"])
149+
assert dvc.data_status() == {
150+
**EMPTY_STATUS,
151+
"git": M.dict(is_empty=True, is_dirty=True),
152+
"committed": {"added": ["foo"]},
153+
}
154+
147155

148-
def test_noscm_repo(dvc):
156+
def test_noscm_repo(tmp_dir, dvc):
149157
assert dvc.data_status() == EMPTY_STATUS
158+
tmp_dir.dvc_gen("foo", "foo")
159+
assert dvc.data_status() == {**EMPTY_STATUS, "unchanged": ["foo"]}
150160

151161

152162
def test_unchanged(M, tmp_dir, dvc, scm):

0 commit comments

Comments
 (0)