Skip to content

Commit 75c5d17

Browse files
authored
fix(data status): handle missing DVC repo at Git HEAD (#10862)
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 75c5d17

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

dvc/repo/data.py

Lines changed: 6 additions & 0 deletions
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

@@ -338,6 +339,11 @@ def _diff_head_to_index(
338339
except RevError:
339340
logger.debug("failed to switch to '%s'", head)
340341
head_view = DataIndex()
342+
except NotDvcRepoError as exc:
343+
# NOTE: this only gets raised on subdir repos at the moment,
344+
# which looks like a bug in `repo.switch`.
345+
logger.warning(exc)
346+
head_view = DataIndex()
341347

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

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)