merge: add non-optional Merge accessor to the zeroth value

We have a few callers which just need to obtain an object common among all
the merge values. Let's add a non-failing accessor for that purpose.
This commit is contained in:
Yuya Nishihara 2023-11-05 10:09:05 +09:00
parent b12c688ea0
commit f6d85c51cd
2 changed files with 8 additions and 3 deletions

View file

@ -170,6 +170,11 @@ impl<T> Merge<T> {
&self.adds
}
/// Returns the zeroth added value, which is guaranteed to exist.
pub fn first(&self) -> &T {
&self.adds[0]
}
/// Returns the `index`-th removed value, which is considered belonging to
/// the `index`-th diff pair.
pub fn get_remove(&self, index: usize) -> Option<&T> {

View file

@ -156,7 +156,7 @@ impl MergedTree {
pub fn dir(&self) -> &RepoPath {
match self {
MergedTree::Legacy(tree) => tree.dir(),
MergedTree::Merge(conflict) => conflict.adds()[0].dir(),
MergedTree::Merge(conflict) => conflict.first().dir(),
}
}
@ -164,7 +164,7 @@ impl MergedTree {
pub fn store(&self) -> &Arc<Store> {
match self {
MergedTree::Legacy(tree) => tree.store(),
MergedTree::Merge(trees) => trees.adds()[0].store(),
MergedTree::Merge(trees) => trees.first().store(),
}
}
@ -448,7 +448,7 @@ fn merge_trees(merge: &Merge<Tree>) -> Result<Merge<Tree>, TreeMergeError> {
return Ok(Merge::resolved(tree.clone()));
}
let base_tree = &merge.adds()[0];
let base_tree = merge.first();
let store = base_tree.store();
let dir = base_tree.dir();
// Keep resolved entries in `new_tree` and conflicted entries in `conflicts` to