diff --git a/dvc/scm/git/__init__.py b/dvc/scm/git/__init__.py index 6b5a3e2279..bc230d8dda 100644 --- a/dvc/scm/git/__init__.py +++ b/dvc/scm/git/__init__.py @@ -271,11 +271,7 @@ def untracked_files(self): return [os.path.join(self.repo.working_dir, fname) for fname in files] def is_tracked(self, path): - # it is equivalent to `bool(self.repo.git.ls_files(path))` by - # functionality, but ls_files fails on unicode filenames - path = relpath(path, self.root_dir) - # There are 4 stages, see BaseIndexEntry.stage - return any((path, i) in self.repo.index.entries for i in (0, 1, 2, 3)) + return bool(self.repo.git.ls_files(path)) def is_dirty(self): return self.repo.is_dirty() diff --git a/tests/unit/scm/test_git.py b/tests/unit/scm/test_git.py index 737c4e52ef..2e7763f31b 100644 --- a/tests/unit/scm/test_git.py +++ b/tests/unit/scm/test_git.py @@ -37,3 +37,37 @@ def test_walk_with_submodules(tmp_dir, scm, git_dir): # currently we don't walk through submodules assert not dirs assert set(files) == {".gitmodules", "submodule"} + + +def test_is_tracked(tmp_dir, scm): + tmp_dir.scm_gen( + { + "tracked": "tracked", + "dir": {"data": "data", "subdir": {"subdata": "subdata"}}, + }, + commit="add dirs and files", + ) + tmp_dir.gen({"untracked": "untracked", "dir": {"untracked": "untracked"}}) + + # sanity check + assert (tmp_dir / "untracked").exists() + assert (tmp_dir / "tracked").exists() + assert (tmp_dir / "dir" / "untracked").exists() + assert (tmp_dir / "dir" / "data").exists() + assert (tmp_dir / "dir" / "subdir" / "subdata").exists() + + assert not scm.is_tracked("untracked") + assert not scm.is_tracked(os.path.join("dir", "untracked")) + + assert scm.is_tracked("tracked") + assert scm.is_tracked("dir") + assert scm.is_tracked(os.path.join("dir", "data")) + assert scm.is_tracked(os.path.join("dir", "subdir")) + assert scm.is_tracked(os.path.join("dir", "subdir", "subdata")) + + +def test_is_tracked_unicode(tmp_dir, scm): + tmp_dir.scm_gen("ṭṝḁḉḵḗḋ", "tracked", commit="add unicode") + tmp_dir.gen("ṳṋṭṝḁḉḵḗḋ", "untracked") + assert scm.is_tracked("ṭṝḁḉḵḗḋ") + assert not scm.is_tracked("ṳṋṭṝḁḉḵḗḋ")