From e74eaf0408c0ac922bf470795cc17c98c4a41d4e Mon Sep 17 00:00:00 2001 From: Draklaw Date: Sun, 10 Nov 2024 10:58:54 +0100 Subject: [PATCH] Improve info. --- README.md | 2 +- src/bsv/command/info.py | 27 +++++++++++++++------------ src/bsv/repository.py | 26 +++++++++++++++++++++++++- src/bsv/simple_cas/cas.py | 4 +--- 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 3370553..4566870 100644 --- a/README.md +++ b/README.md @@ -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 ` show the object `digest`. - [ ] `bsv ls ` list files in a bsv directory. diff --git a/src/bsv/command/info.py b/src/bsv/command/info.py index 312ac17..2fd30f2 100644 --- a/src/bsv/command/info.py +++ b/src/bsv/command/info.py @@ -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 diff --git a/src/bsv/repository.py b/src/bsv/repository.py index 59a6146..db99e91 100644 --- a/src/bsv/repository.py +++ b/src/bsv/repository.py @@ -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 diff --git a/src/bsv/simple_cas/cas.py b/src/bsv/simple_cas/cas.py index 3581a48..40ea785 100644 --- a/src/bsv/simple_cas/cas.py +++ b/src/bsv/simple_cas/cas.py @@ -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