Improve info.
This commit is contained in:
@@ -29,7 +29,7 @@ Basic features. Naïve CAS implementation that store everything in a single big
|
||||
- [x] Simple CAS implementation (it's OK if it's naïve).
|
||||
- [x] Content-based chunking to store files.
|
||||
- [x] `bsv init` command to initialize bsv.
|
||||
- [ ] `bsv info` print useful information bsv configuration.
|
||||
- [x] `bsv info` print useful information bsv configuration.
|
||||
- [ ] `bsv log` show the history of snapshots.
|
||||
- [ ] `bsv show <digest>` show the object `digest`.
|
||||
- [ ] `bsv ls <bsv-path>` list files in a bsv directory.
|
||||
|
||||
@@ -19,6 +19,7 @@ from argparse import ArgumentParser
|
||||
from pathlib import Path
|
||||
|
||||
from bsv import __version__
|
||||
from bsv.cli import get_console
|
||||
from bsv.command import command
|
||||
from bsv.repository import Repository
|
||||
|
||||
@@ -33,27 +34,29 @@ def init_parser(parser: ArgumentParser):
|
||||
)
|
||||
|
||||
@command(init_parser)
|
||||
def info(repository_path: Path | None, verbosity: int=0) -> int:
|
||||
def info(config_path: Path, verbosity: int=0) -> int:
|
||||
"""Print informations about bsv: config file used, known repository, file mapping...
|
||||
"""
|
||||
|
||||
print(f"bsv v{__version__}")
|
||||
print = get_console().print
|
||||
|
||||
if repository_path is None:
|
||||
print("Repository path not found. Bsv is likely not setup on this device.")
|
||||
print(f"bsv [green]v{__version__}")
|
||||
|
||||
if not config_path.exists():
|
||||
print("bsv configuration not found. Bsv is likely not setup on this device.", style="red")
|
||||
return 0
|
||||
else:
|
||||
print(f"Repository path: {repository_path}")
|
||||
|
||||
repo = Repository(repository_path)
|
||||
repo = Repository(config_path)
|
||||
|
||||
print(f"Repository name: {repo.name}")
|
||||
print(f"[blue]Config path: [bold yellow]{repo.config_path}")
|
||||
print(f"[blue]Device name: [bold yellow]{repo.device_name}")
|
||||
print(f"[blue]Local repository: [bold yellow]{repo._local_repository_path}")
|
||||
|
||||
if repo.path_map:
|
||||
print("Path map: (bsv path <-> filesystem path)")
|
||||
for pair in sorted(repo.path_map):
|
||||
print("[blue]Path map:[/blue] (bsv path <-> filesystem path)")
|
||||
if repo.path_map.pairs:
|
||||
for pair in sorted(repo.path_map.pairs):
|
||||
print(f" {pair.bsv} <-> {pair.fs}")
|
||||
else:
|
||||
print("Path map is empty.")
|
||||
print(" [bold yellow]No path mapped.")
|
||||
|
||||
return 0
|
||||
|
||||
@@ -58,7 +58,7 @@ class Repository:
|
||||
_context_depth: int = 0
|
||||
|
||||
def __init__(self, config_path: Path):
|
||||
self._config_path = config_path
|
||||
self._config_path = config_path.resolve()
|
||||
|
||||
with self._config_path.open("rb") as stream:
|
||||
config = tomllib.load(stream)
|
||||
@@ -209,6 +209,15 @@ class Repository:
|
||||
# return self.add_snapshot(snapshot, dry_run=dry_run)
|
||||
|
||||
|
||||
def get_ref(self, key: str) -> Digest | None:
|
||||
return self._cas.get_ref(key)
|
||||
|
||||
def set_ref(self, key: str, digest: Digest):
|
||||
self._cas.set_ref(key, digest)
|
||||
|
||||
def get_head_snapshot(self) -> Digest | None:
|
||||
return self.get_ref("HEAD")
|
||||
|
||||
|
||||
def _read(self, digest: Digest, object_type: bytes) -> tuple[ObjectInfo, Blob]:
|
||||
obj = self._cas.read(digest, object_type=object_type)
|
||||
@@ -528,6 +537,21 @@ class Tree:
|
||||
self.write(stream)
|
||||
return stream.getvalue()
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self.items)
|
||||
|
||||
def get(self, key: str) -> TreeItem | None:
|
||||
for item in self.items:
|
||||
if item.name == key:
|
||||
return item
|
||||
return None
|
||||
|
||||
def __getitem__(self, key: str) -> TreeItem:
|
||||
item = self.get(key)
|
||||
if item is None:
|
||||
raise KeyError(f"{key} not found")
|
||||
return item
|
||||
|
||||
@dataclass(frozen=True, order=True, slots=True)
|
||||
class TreeObject(ObjectInfo):
|
||||
tree: Tree
|
||||
|
||||
@@ -102,9 +102,7 @@ class SimpleCas:
|
||||
|
||||
hash = self._hash_factory()
|
||||
hash.update(object_type)
|
||||
hash.update(b"\0")
|
||||
hash.update(len(data).to_bytes(4))
|
||||
hash.update(b"\0")
|
||||
hash.update(data)
|
||||
digest = Digest(hash.digest())
|
||||
|
||||
@@ -153,7 +151,7 @@ class SimpleCas:
|
||||
return self._root_dir / "refs" / key_path
|
||||
|
||||
|
||||
@dataclass
|
||||
@dataclass(frozen=True)
|
||||
class IndexItem:
|
||||
object_type: bytes
|
||||
offset: int
|
||||
|
||||
Reference in New Issue
Block a user