From 61e4bd4e85a8a395901f66ef7eb0c2bb062f0b63 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 8 Aug 2024 23:42:47 +0900 Subject: [PATCH] tree: fill in valid id to null tree, rename function to empty() If a null tree were written to the store, GitBackend would crash because of invalid hash length. --- lib/src/local_working_copy.rs | 2 +- lib/src/merge.rs | 2 +- lib/src/merged_tree.rs | 8 ++++---- lib/src/tree.rs | 5 +++-- lib/src/tree_builder.rs | 2 +- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/src/local_working_copy.rs b/lib/src/local_working_copy.rs index 7a52af951dc..0b3ea2a60c8 100644 --- a/lib/src/local_working_copy.rs +++ b/lib/src/local_working_copy.rs @@ -1315,7 +1315,7 @@ impl TreeState { let new_matcher = PrefixMatcher::new(&sparse_patterns); let added_matcher = DifferenceMatcher::new(&new_matcher, &old_matcher); let removed_matcher = DifferenceMatcher::new(&old_matcher, &new_matcher); - let empty_tree = MergedTree::resolved(Tree::null(self.store.clone(), RepoPathBuf::root())); + let empty_tree = MergedTree::resolved(Tree::empty(self.store.clone(), RepoPathBuf::root())); let added_stats = self.update(&empty_tree, &tree, &added_matcher).block_on()?; let removed_stats = self .update(&tree, &empty_tree, &removed_matcher) diff --git a/lib/src/merge.rs b/lib/src/merge.rs index e16786ee36c..6569f842a7f 100644 --- a/lib/src/merge.rs +++ b/lib/src/merge.rs @@ -625,7 +625,7 @@ where if let Some(id) = id { store.get_tree(dir, id) } else { - Ok(Tree::null(store.clone(), dir.to_owned())) + Ok(Tree::empty(store.clone(), dir.to_owned())) } }; Ok(Some(tree_id_merge.try_map(get_tree)?)) diff --git a/lib/src/merged_tree.rs b/lib/src/merged_tree.rs index 621465495d6..f83acf77ad9 100644 --- a/lib/src/merged_tree.rs +++ b/lib/src/merged_tree.rs @@ -209,7 +209,7 @@ impl MergedTree { } _ => { let subdir = self.dir().join(name); - Ok(Tree::null(self.store().clone(), subdir.clone())) + Ok(Tree::empty(self.store().clone(), subdir.clone())) } })?; Ok(Some(MergedTree { trees })) @@ -642,7 +642,7 @@ impl<'matcher> TreeDiffIterator<'matcher> { if let Some(trees) = values.to_tree_merge(store, dir)? { Ok(trees) } else { - Ok(Merge::resolved(Tree::null(store.clone(), dir.to_owned()))) + Ok(Merge::resolved(Tree::empty(store.clone(), dir.to_owned()))) } } } @@ -846,7 +846,7 @@ impl<'matcher> TreeDiffStreamImpl<'matcher> { ) -> BackendResult { match value { Some(TreeValue::Tree(tree_id)) => store.get_tree_async(dir, tree_id).await, - _ => Ok(Tree::null(store.clone(), dir.to_owned())), + _ => Ok(Tree::empty(store.clone(), dir.to_owned())), } } @@ -863,7 +863,7 @@ impl<'matcher> TreeDiffStreamImpl<'matcher> { .await?; builder.build() } else { - Merge::resolved(Tree::null(store, dir.clone())) + Merge::resolved(Tree::empty(store, dir.clone())) }; Ok(MergedTree { trees }) } diff --git a/lib/src/tree.rs b/lib/src/tree.rs index 2d4a4d86b0c..75066144ca4 100644 --- a/lib/src/tree.rs +++ b/lib/src/tree.rs @@ -76,11 +76,12 @@ impl Tree { } } - pub fn null(store: Arc, dir: RepoPathBuf) -> Self { + pub fn empty(store: Arc, dir: RepoPathBuf) -> Self { + let id = store.empty_tree_id().clone(); Tree { store, dir, - id: TreeId::new(vec![]), + id, data: Arc::new(backend::Tree::default()), } } diff --git a/lib/src/tree_builder.rs b/lib/src/tree_builder.rs index 88fc0d33516..0b3bf4457ea 100644 --- a/lib/src/tree_builder.rs +++ b/lib/src/tree_builder.rs @@ -136,7 +136,7 @@ impl TreeBuilder { let (parent, basename) = dir.split().expect("root must be populated"); let tree = populate_trees(tree_cache, store, parent)? .sub_tree(basename)? - .unwrap_or_else(|| Tree::null(store.clone(), dir.to_owned())); + .unwrap_or_else(|| Tree::empty(store.clone(), dir.to_owned())); Ok(tree_cache.entry(dir.to_owned()).or_insert(tree)) }