diff --git a/scmrepo/fs.py b/scmrepo/fs.py index cb8c5046..c01da0e4 100644 --- a/scmrepo/fs.py +++ b/scmrepo/fs.py @@ -1,5 +1,6 @@ import errno import os +import posixpath from typing import ( TYPE_CHECKING, Any, @@ -31,7 +32,6 @@ def bytesio_len(obj: "BytesIO") -> Optional[int]: class GitFileSystem(AbstractFileSystem): # pylint: disable=abstract-method - sep = os.sep cachable = False def __init__( @@ -53,21 +53,13 @@ def __init__( resolved = resolver(scm, rev or "HEAD") tree_obj = scm.pygit2.get_tree_obj(rev=resolved) trie = GitTrie(tree_obj, resolved) - path = scm.root_dir - else: - assert path self.trie = trie - self.root_dir = path self.rev = self.trie.rev def _get_key(self, path: str) -> Tuple[str, ...]: - from scmrepo.utils import relpath - - if os.path.isabs(path): - path = relpath(path, self.root_dir) - relparts = path.split(os.sep) - if relparts == ["."]: + relparts = path.split(self.sep) + if relparts == [self.root_marker]: return () return tuple(relparts) @@ -101,7 +93,7 @@ def info(self, path: str, **kwargs: Any) -> Dict[str, Any]: try: return { **self.trie.info(key), - "name": os.path.join(self.root_dir, self.sep.join(key)), + "name": path, } except KeyError: raise FileNotFoundError( @@ -145,15 +137,13 @@ def walk( # pylint: disable=arguments-differ key = self._get_key(top) for prefix, dirs, files in self.trie.walk(key, topdown=topdown): - root = self.root_dir + root = self.sep.join(prefix) if prefix else "" - if prefix: - root = os.path.join(root, os.sep.join(prefix)) if detail: yield ( root, - {d: self.info(os.path.join(root, d)) for d in dirs}, - {f: self.info(os.path.join(root, f)) for f in files}, + {d: self.info(posixpath.join(root, d)) for d in dirs}, + {f: self.info(posixpath.join(root, f)) for f in files}, ) else: yield root, dirs, files diff --git a/tests/test_fs.py b/tests/test_fs.py index 90d3f921..c6e8113a 100644 --- a/tests/test_fs.py +++ b/tests/test_fs.py @@ -1,5 +1,3 @@ -import os - import pytest from pytest_test_utils import TmpDir @@ -34,7 +32,7 @@ def test_exists(tmp_dir: TmpDir, scm: Git): assert not fs.exists("foo") assert not fs.exists("тест") assert not fs.exists("data") - assert not fs.exists(os.path.join("data", "lorem")) + assert not fs.exists("data/lorem") scm.add_commit(files, message="add") @@ -42,7 +40,7 @@ def test_exists(tmp_dir: TmpDir, scm: Git): assert fs.exists("foo") assert fs.exists("тест") assert fs.exists("data") - assert fs.exists(os.path.join("data", "lorem")) + assert fs.exists("data/lorem") assert not fs.exists("non-existing-file") @@ -75,7 +73,7 @@ def test_walk(tmp_dir: TmpDir, scm: Git): "data": {"lorem": "ipsum", "subdir": {"sub": "sub"}}, } ) - scm.add_commit(os.path.join("data", "subdir"), message="add") + scm.add_commit("data/subdir", message="add") fs = scm.get_fs("master") def convert_to_sets(walk_results): @@ -84,24 +82,22 @@ def convert_to_sets(walk_results): for root, dirs, nondirs in walk_results ] - assert convert_to_sets(fs.walk(".")) == convert_to_sets( + assert convert_to_sets(fs.walk("")) == convert_to_sets( [ - (scm.root_dir, ["data"], []), - (os.path.join(scm.root_dir, "data"), ["subdir"], []), + ("", ["data"], []), + ("data", ["subdir"], []), ( - os.path.join(scm.root_dir, "data", "subdir"), + "data/subdir", [], ["sub"], ), ] ) - assert convert_to_sets( - fs.walk(os.path.join("data", "subdir")) - ) == convert_to_sets( + assert convert_to_sets(fs.walk("data/subdir")) == convert_to_sets( [ ( - os.path.join(scm.root_dir, "data", "subdir"), + "data/subdir", [], ["sub"], ) @@ -120,25 +116,25 @@ def test_ls(tmp_dir: TmpDir, scm: Git): scm.add_commit(files, message="add") fs = scm.get_fs("master") - assert fs.ls(".", detail=False) == ["foo", "тест", "data"] - assert fs.ls(".") == { + assert fs.ls("", detail=False) == ["foo", "тест", "data"] + assert fs.ls("") == { "data": { "mode": 16384, - "name": str(tmp_dir / "data"), + "name": "data", "sha": "f5d6ac1955c85410b71bb6e35e4c57c54e2ad524", "size": 66, "type": "directory", }, "foo": { "mode": 33188, - "name": str(tmp_dir / "foo"), + "name": "foo", "sha": "19102815663d23f8b75a47e7a01965dcdc96468c", "size": 3, "type": "file", }, "тест": { "mode": 33188, - "name": str(tmp_dir / "тест"), + "name": "тест", "sha": "eeeba1738f4c12844163b89112070c6e57eb764e", "size": 16, "type": "file", diff --git a/tests/test_git.py b/tests/test_git.py index bf51bb03..d0eb1919 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -126,7 +126,7 @@ def test_walk_with_submodules( files = [] dirs = [] fs = scm.get_fs("HEAD") - for _, dnames, fnames in fs.walk("."): + for _, dnames, fnames in fs.walk(""): dirs.extend(dnames) files.extend(fnames)