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