From db7facb78e97d24444fec555e0a76629952c6d91 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Wed, 14 Jun 2023 18:00:30 +0900 Subject: [PATCH] tree: rewrite sub_tree_recursive() to not use known_sub_tree() This is the only place where the caller of known_sub_tree() doesn't know the full repo path. --- lib/src/tree.rs | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/lib/src/tree.rs b/lib/src/tree.rs index 0329fd4ee..b63efeef5 100644 --- a/lib/src/tree.rs +++ b/lib/src/tree.rs @@ -159,27 +159,14 @@ impl Tree { } fn sub_tree_recursive(&self, components: &[RepoPathComponent]) -> Option { - if components.is_empty() { + if let Some((first, tail)) = components.split_first() { + tail.iter() + .try_fold(self.sub_tree(first)?, |tree, name| tree.sub_tree(name)) + } else { // TODO: It would be nice to be able to return a reference here, but // then we would have to figure out how to share Tree instances // across threads. - Some(Tree { - store: self.store.clone(), - dir: self.dir.clone(), - id: self.id.clone(), - data: self.data.clone(), - }) - } else { - match self.data.entry(&components[0]) { - None => None, - Some(entry) => match entry.value() { - TreeValue::Tree(sub_tree_id) => { - let sub_tree = self.known_sub_tree(entry.name(), sub_tree_id); - sub_tree.sub_tree_recursive(&components[1..]) - } - _ => None, - }, - } + Some(self.clone()) } }