merged_tree: make id() return a MergedTreeId

We will rarely want to use the tree id without knowing whether it can
contain `TreeValue::Conflict` values, so let's make the callers check.
This commit is contained in:
Martin von Zweigbergk 2023-08-25 15:57:55 -07:00 committed by Martin von Zweigbergk
parent a0ae09f461
commit 36674e8f7e
4 changed files with 22 additions and 15 deletions

View file

@ -186,6 +186,13 @@ impl MergedTreeId {
MergedTreeId::Merge(_) => panic!(),
}
}
/// If this is a legacy tree, gets its tree id
// TODO(#1624): delete when all callers have been updated to support tree-level
// conflicts
pub fn to_legacy_tree_id(&self) -> TreeId {
self.as_legacy_tree_id().clone()
}
}
content_hash! {

View file

@ -22,7 +22,7 @@ use std::{iter, vec};
use itertools::Itertools;
use crate::backend;
use crate::backend::{BackendResult, ConflictId, MergedTreeId, TreeId, TreeValue};
use crate::backend::{BackendResult, ConflictId, MergedTreeId, TreeValue};
use crate::matchers::Matcher;
use crate::merge::Merge;
use crate::repo_path::{RepoPath, RepoPathComponent, RepoPathJoin};
@ -258,17 +258,20 @@ impl MergedTree {
None => Merge::absent(),
Some(tree) => tree.value(basename).to_merge(),
},
None => self
.id()
.map(|tree_id| Some(TreeValue::Tree((*tree_id).clone()))),
None => match self {
MergedTree::Legacy(tree) => Merge::normal(TreeValue::Tree(tree.id().clone())),
MergedTree::Merge(trees) => {
trees.map(|tree| Some(TreeValue::Tree(tree.id().clone())))
}
},
}
}
/// The tree's id(s). May be a legacy tree's id.
pub fn id(&self) -> Merge<&TreeId> {
/// The tree's id
pub fn id(&self) -> MergedTreeId {
match self {
MergedTree::Legacy(tree) => Merge::resolved(tree.id()),
MergedTree::Merge(merge) => merge.map(|tree| tree.id()),
MergedTree::Legacy(tree) => MergedTreeId::Legacy(tree.id().clone()),
MergedTree::Merge(merge) => MergedTreeId::Merge(merge.map(|tree| tree.id().clone())),
}
}

View file

@ -1177,7 +1177,7 @@ impl TreeState {
other => CheckoutError::InternalBackendError(other),
})?;
let stats = self.update(&old_tree, new_tree, self.sparse_matcher().as_ref(), Err)?;
self.tree_id = new_tree.id().into_resolved().unwrap().clone();
self.tree_id = new_tree.id().to_legacy_tree_id();
Ok(stats)
}
@ -1333,7 +1333,7 @@ impl TreeState {
self.file_states.insert(path.clone(), file_state);
}
}
self.tree_id = new_tree.id().into_resolved().unwrap().clone();
self.tree_id = new_tree.id().to_legacy_tree_id();
Ok(())
}
}

View file

@ -372,10 +372,7 @@ fn test_reset() {
assert!(ignored_path.to_fs_path(&workspace_root).is_file());
assert!(!wc.file_states().unwrap().contains_key(&ignored_path));
let new_tree = test_workspace.snapshot().unwrap();
assert_eq!(
new_tree.id(),
tree_without_file.id().into_resolved().unwrap()
);
assert_eq!(new_tree.id(), tree_without_file.id().as_legacy_tree_id());
// Now test the opposite direction: resetting to a commit where the file is
// tracked. The file should become tracked (even though it's ignored).
@ -386,7 +383,7 @@ fn test_reset() {
assert!(ignored_path.to_fs_path(&workspace_root).is_file());
assert!(wc.file_states().unwrap().contains_key(&ignored_path));
let new_tree = test_workspace.snapshot().unwrap();
assert_eq!(new_tree.id(), tree_with_file.id().into_resolved().unwrap());
assert_eq!(new_tree.id(), tree_with_file.id().as_legacy_tree_id());
}
#[test]