From 613f5da50643a8eb78167f85745d7644437f9bec Mon Sep 17 00:00:00 2001 From: Alexander Weiss Date: Fri, 29 Jul 2022 22:01:13 +0200 Subject: [PATCH] Tree: Add function subtree_id and use in cat --- src/blob/tree.rs | 20 +++++++++++++++++++- src/commands/cat.rs | 20 ++------------------ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/blob/tree.rs b/src/blob/tree.rs index 1ba438b..308c3e1 100644 --- a/src/blob/tree.rs +++ b/src/blob/tree.rs @@ -1,6 +1,6 @@ use std::collections::{HashSet, VecDeque}; use std::mem; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::pin::Pin; use anyhow::{anyhow, Result}; @@ -56,6 +56,24 @@ impl Tree { Ok(serde_json::from_slice(&data)?) } + + pub async fn subtree_id(be: &impl IndexedBackend, mut id: Id, path: &Path) -> Result { + for p in path.iter() { + let p = p.to_str().unwrap(); + // TODO: check for root instead + if p == "/" { + continue; + } + let tree = Tree::from_backend(be, id).await?; + let node = tree + .nodes() + .iter() + .find(|node| node.name() == p) + .ok_or_else(|| anyhow!("{} not found", p))?; + id = node.subtree().ok_or_else(|| anyhow!("{} is no dir", p))?; + } + Ok(id) + } } /// NodeStreamer recursively streams all nodes of a given tree including all subtrees in-order diff --git a/src/commands/cat.rs b/src/commands/cat.rs index a151bfd..ca8121f 100644 --- a/src/commands/cat.rs +++ b/src/commands/cat.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use anyhow::{anyhow, Result}; +use anyhow::Result; use clap::{Parser, Subcommand}; use indicatif::ProgressBar; @@ -78,23 +78,7 @@ async fn cat_blob(be: &impl DecryptReadBackend, tpe: BlobType, opt: IdOpt) -> Re async fn cat_tree(be: &impl DecryptReadBackend, opts: TreeOpts) -> Result<()> { let snap = SnapshotFile::from_str(be, &opts.id, |_| true, progress_counter()).await?; let index = IndexBackend::new(be, progress_counter()).await?; - let mut id = snap.tree; - - for p in opts.path.iter() { - let p = p.to_str().unwrap(); - // TODO: check for root instead - if p == "/" { - continue; - } - let tree = Tree::from_backend(&index, id).await?; - let node = tree - .nodes() - .iter() - .find(|node| node.name() == p) - .ok_or_else(|| anyhow!("{} not found", p))?; - id = node.subtree().ok_or_else(|| anyhow!("{} is no dir", p))?; - } - + let id = Tree::subtree_id(&index, snap.tree, &opts.path).await?; let data = index.blob_from_backend(&BlobType::Tree, &id).await?; println!("{}", String::from_utf8(data)?);