Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions scmrepo/fs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import errno
import os
import posixpath
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -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__(
Expand All @@ -53,20 +53,12 @@ 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)
relparts = path.split(self.sep)
if relparts == ["."]:
return ()
return tuple(relparts)
Comment on lines 60 to 64
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will introduce _strip_protocol later.

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record: Will switch to fs.path once it is introduced to fsspec.

{f: self.info(posixpath.join(root, f)) for f in files},
)
else:
yield root, dirs, files
Expand Down
26 changes: 11 additions & 15 deletions tests/test_fs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

import pytest
from pytest_test_utils import TmpDir

Expand Down Expand Up @@ -34,15 +32,15 @@ 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")

fs = scm.get_fs("master")
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")


Expand Down Expand Up @@ -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):
Expand All @@ -86,22 +84,20 @@ def convert_to_sets(walk_results):

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"],
)
Expand All @@ -124,21 +120,21 @@ def test_ls(tmp_dir: TmpDir, scm: Git):
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",
Expand Down