Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path changes from #24 and #25 #40

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 7 additions & 17 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,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)

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},
{f: self.info(posixpath.join(root, f)) for f in files},
)
else:
yield root, dirs, files
Expand Down
32 changes: 14 additions & 18 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 @@ -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"],
)
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down