Skip to content

Commit dabf250

Browse files
committed
fix(data status): handle missing DVC repo at Git HEAD
Avoid raising errors in `data_status()` when Git HEAD does not contain a DVC repository. Instead, fall back to an empty index. Tests now cover both unborn and committed Git repos, with and without subdirs.
1 parent ea9dd5f commit dabf250

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

dvc/repo/data.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ def _diff_head_to_index(
325325
granular: bool = False,
326326
with_renames: bool = False,
327327
) -> DiffResult:
328+
from dvc.exceptions import NotDvcRepoError
328329
from dvc.scm import RevError
329330
from dvc_data.index import DataIndex
330331

@@ -335,7 +336,7 @@ def _diff_head_to_index(
335336
with repo.switch(head):
336337
head_index = repo.index.data["repo"]
337338
head_view = filter_index(head_index, filter_keys=filter_keys)
338-
except RevError:
339+
except (RevError, NotDvcRepoError):
339340
logger.debug("failed to switch to '%s'", head)
340341
head_view = DataIndex()
341342

tests/func/test_data_status.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,25 @@ def test_tracked_directory_deep(M, tmp_dir, dvc, scm):
138138
}
139139

140140

141-
def test_new_empty_git_repo(M, tmp_dir, scm):
142-
dvc = Repo.init()
141+
@pytest.mark.parametrize("git_repo_state", ["unborn", "committed"])
142+
@pytest.mark.parametrize("subdir", [True, False])
143+
def test_new_dvc_repo(M, tmp_dir, scm, subdir, git_repo_state):
144+
if git_repo_state == "committed":
145+
tmp_dir.scm_gen("test", "test", commit="init")
146+
147+
is_empty = git_repo_state == "unborn"
148+
dir_ = tmp_dir / "sub" if subdir else tmp_dir
149+
dvc = Repo.init(dir_, subdir=subdir)
143150
assert dvc.data_status() == {
144151
**EMPTY_STATUS,
145-
"git": M.dict(is_empty=True, is_dirty=True),
152+
"git": M.dict(is_dirty=True, is_empty=is_empty),
146153
}
147154

148-
tmp_dir.gen("foo", "foo")
149-
dvc.add(["foo"])
155+
dir_.gen("foo", "foo")
156+
dvc.add([dir_ / "foo"])
150157
assert dvc.data_status() == {
151158
**EMPTY_STATUS,
152-
"git": M.dict(is_empty=True, is_dirty=True),
159+
"git": M.dict(is_empty=is_empty, is_dirty=True),
153160
"committed": {"added": ["foo"]},
154161
}
155162

0 commit comments

Comments
 (0)