From 9a85dca8ae2995f06e6d859a8b0567bbaf16b71a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Saugat=20Pachhai=20=28=E0=A4=B8=E0=A5=8C=E0=A4=97=E0=A4=BE?= =?UTF-8?q?=E0=A4=A4=29?= Date: Mon, 7 Mar 2022 09:33:13 +0545 Subject: [PATCH 1/2] Revert "fs: support root_marker (#25)" This reverts commit 42db9e4754d45f21ff93d5d8ed96fc4eb7f42801. --- scmrepo/fs.py | 2 +- tests/test_fs.py | 6 +++--- tests/test_git.py | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/scmrepo/fs.py b/scmrepo/fs.py index c01da0e4..dfebae84 100644 --- a/scmrepo/fs.py +++ b/scmrepo/fs.py @@ -59,7 +59,7 @@ def __init__( def _get_key(self, path: str) -> Tuple[str, ...]: relparts = path.split(self.sep) - if relparts == [self.root_marker]: + if relparts == ["."]: return () return tuple(relparts) diff --git a/tests/test_fs.py b/tests/test_fs.py index c6e8113a..37bc677f 100644 --- a/tests/test_fs.py +++ b/tests/test_fs.py @@ -82,7 +82,7 @@ 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"], []), @@ -116,8 +116,8 @@ 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", 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) From b4a4154415c133aa01ec34d7d30671a674179acb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Saugat=20Pachhai=20=28=E0=A4=B8=E0=A5=8C=E0=A4=97=E0=A4=BE?= =?UTF-8?q?=E0=A4=A4=29?= Date: Mon, 7 Mar 2022 09:33:24 +0545 Subject: [PATCH 2/2] Revert "fs: detach from local filesystem (#24)" This reverts commit 29d834163ca6c5aa60be6f0f68785e3c2f6f2f87. --- scmrepo/fs.py | 22 ++++++++++++++++------ tests/test_fs.py | 26 +++++++++++++++----------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/scmrepo/fs.py b/scmrepo/fs.py index dfebae84..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,12 +53,20 @@ 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) + 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 37bc677f..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): @@ -84,20 +86,22 @@ def convert_to_sets(walk_results): 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"], ) @@ -120,21 +124,21 @@ def test_ls(tmp_dir: TmpDir, scm: Git): 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",