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.
This commit is contained in:
Yuya Nishihara 2024-08-08 23:42:47 +09:00
parent bf2cb55ea7
commit ed1c07e73e
5 changed files with 10 additions and 9 deletions

View file

@ -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)

View file

@ -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)?))

View file

@ -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<Tree> {
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 })
}

View file

@ -76,11 +76,12 @@ impl Tree {
}
}
pub fn null(store: Arc<Store>, dir: RepoPathBuf) -> Self {
pub fn empty(store: Arc<Store>, dir: RepoPathBuf) -> Self {
let id = store.empty_tree_id().clone();
Tree {
store,
dir,
id: TreeId::new(vec![]),
id,
data: Arc::new(backend::Tree::default()),
}
}

View file

@ -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))
}